|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bridge\Doctrine\Tests\Form\DataTransformer; |
| 13 | + |
| 14 | +use Doctrine\Common\Collections\ArrayCollection; |
| 15 | +use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Bernhard Schussek <bschussek@gmail.com> |
| 19 | + */ |
| 20 | +class CollectionToArrayTransformerTest extends \PHPUnit_Framework_TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var CollectionToArrayTransformer |
| 24 | + */ |
| 25 | + private $transformer; |
| 26 | + |
| 27 | + protected function setUp() |
| 28 | + { |
| 29 | + $this->transformer = new CollectionToArrayTransformer(); |
| 30 | + } |
| 31 | + |
| 32 | + public function testTransform() |
| 33 | + { |
| 34 | + $array = array( |
| 35 | + 2 => 'foo', |
| 36 | + 3 => 'bar', |
| 37 | + ); |
| 38 | + |
| 39 | + $this->assertSame($array, $this->transformer->transform(new ArrayCollection($array))); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * This test is needed for cases when getXxxs() in the entity returns the |
| 44 | + * result of $collection->toArray(), in order to prevent modifications of |
| 45 | + * the inner collection. |
| 46 | + * |
| 47 | + * See https://github.com/symfony/symfony/pull/9308 |
| 48 | + */ |
| 49 | + public function testTransformArray() |
| 50 | + { |
| 51 | + $array = array( |
| 52 | + 2 => 'foo', |
| 53 | + 3 => 'bar', |
| 54 | + ); |
| 55 | + |
| 56 | + $this->assertSame($array, $this->transformer->transform($array)); |
| 57 | + } |
| 58 | + |
| 59 | + public function testTransformNull() |
| 60 | + { |
| 61 | + $this->assertSame(array(), $this->transformer->transform(null)); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException |
| 66 | + */ |
| 67 | + public function testTransformExpectsArrayOrCollection() |
| 68 | + { |
| 69 | + $this->transformer->transform('Foo'); |
| 70 | + } |
| 71 | + |
| 72 | + public function testReverseTransform() |
| 73 | + { |
| 74 | + $array = array( |
| 75 | + 2 => 'foo', |
| 76 | + 3 => 'bar', |
| 77 | + ); |
| 78 | + |
| 79 | + $this->assertEquals(new ArrayCollection($array), $this->transformer->reverseTransform($array)); |
| 80 | + } |
| 81 | + |
| 82 | + public function testReverseTransformEmpty() |
| 83 | + { |
| 84 | + $this->assertEquals(new ArrayCollection(), $this->transformer->reverseTransform('')); |
| 85 | + } |
| 86 | + |
| 87 | + public function testReverseTransformNull() |
| 88 | + { |
| 89 | + $this->assertEquals(new ArrayCollection(), $this->transformer->reverseTransform(null)); |
| 90 | + } |
| 91 | +} |
0 commit comments