From d9ce9825b6b4c3ba5f6e3ab35baaa8f0b1cca5fe Mon Sep 17 00:00:00 2001 From: William DURAND Date: Tue, 14 Feb 2012 00:40:32 +0100 Subject: [PATCH] [Propel] Added tests for CollectionToArrayTransformer --- .../CollectionToArrayTransformerTest.php | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 tests/Symfony/Tests/Bridge/Propel1/Form/DataTransformer/CollectionToArrayTransformerTest.php diff --git a/tests/Symfony/Tests/Bridge/Propel1/Form/DataTransformer/CollectionToArrayTransformerTest.php b/tests/Symfony/Tests/Bridge/Propel1/Form/DataTransformer/CollectionToArrayTransformerTest.php new file mode 100644 index 0000000000..ea47662d3e --- /dev/null +++ b/tests/Symfony/Tests/Bridge/Propel1/Form/DataTransformer/CollectionToArrayTransformerTest.php @@ -0,0 +1,105 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Bridge\Propel1\Form\DataTransformer; + +use \PropelCollection; +use Symfony\Bridge\Propel1\Form\DataTransformer\CollectionToArrayTransformer; +use Symfony\Tests\Bridge\Propel1\Propel1TestCase; + +class CollectionToArrayTransformerTest extends Propel1TestCase +{ + private $transformer; + + public function setUp() + { + $this->transformer = new CollectionToArrayTransformer(); + } + + public function testTransform() + { + $result = $this->transformer->transform(new PropelCollection()); + + $this->assertTrue(is_array($result)); + $this->assertEquals(0, count($result)); + } + + public function testTransformWithNull() + { + $result = $this->transformer->transform(null); + + $this->assertTrue(is_array($result)); + $this->assertEquals(0, count($result)); + } + + /** + * @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException + */ + public function testTransformThrowsExceptionIfNotPropelCollection() + { + $this->transformer->transform(new DummyObject()); + } + + public function testTransformWithData() + { + $coll = new PropelCollection(); + $coll->setData(array('foo', 'bar')); + + $result = $this->transformer->transform($coll); + + $this->assertTrue(is_array($result)); + $this->assertEquals(2, count($result)); + $this->assertEquals('foo', $result[0]); + $this->assertEquals('bar', $result[1]); + } + + public function testReverseTransformWithNull() + { + $result = $this->transformer->reverseTransform(null); + + $this->assertInstanceOf('\PropelCollection', $result); + $this->assertEquals(0, count($result->getData())); + } + + public function testReverseTransformWithEmptyString() + { + $result = $this->transformer->reverseTransform(''); + + $this->assertInstanceOf('\PropelCollection', $result); + $this->assertEquals(0, count($result->getData())); + } + + /** + * @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException + */ + public function testReverseTransformThrowsExceptionIfNotArray() + { + $this->transformer->reverseTransform(new DummyObject()); + } + + public function testReverseTransformWithData() + { + $inputData = array('foo', 'bar'); + + $result = $this->transformer->reverseTransform($inputData); + $data = $result->getData(); + + $this->assertInstanceOf('\PropelCollection', $result); + + $this->assertTrue(is_array($data)); + $this->assertEquals(2, count($data)); + $this->assertEquals('foo', $data[0]); + $this->assertEquals('bar', $data[1]); + $this->assertsame($inputData, $data); + } +} + +class DummyObject {}