From 544a0d56d8bc9ccac1832d9d6eb93bb271d0f2ff Mon Sep 17 00:00:00 2001 From: Andrej Hudec Date: Mon, 16 Sep 2013 22:28:50 +0200 Subject: [PATCH] Added ArrayToModelTransformerTest --- Tests/Fixtures/Entity/Form/FooEntity.php | 14 +++ .../ArrayToModelTransformerTest.php | 85 +++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 Tests/Fixtures/Entity/Form/FooEntity.php create mode 100644 Tests/Form/DataTransformer/ArrayToModelTransformerTest.php diff --git a/Tests/Fixtures/Entity/Form/FooEntity.php b/Tests/Fixtures/Entity/Form/FooEntity.php new file mode 100644 index 0000000000..cbd0478d89 --- /dev/null +++ b/Tests/Fixtures/Entity/Form/FooEntity.php @@ -0,0 +1,14 @@ +values = $values; + } + +} diff --git a/Tests/Form/DataTransformer/ArrayToModelTransformerTest.php b/Tests/Form/DataTransformer/ArrayToModelTransformerTest.php new file mode 100644 index 0000000000..33cb802f16 --- /dev/null +++ b/Tests/Form/DataTransformer/ArrayToModelTransformerTest.php @@ -0,0 +1,85 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\AdminBundle\Tests\Form\DataTransformer; + +use Sonata\AdminBundle\Model\ModelManagerInterface; +use Sonata\AdminBundle\Form\DataTransformer\ArrayToModelTransformer; +use Sonata\AdminBundle\Tests\Fixtures\Entity\Form\FooEntity; + +/** + * @author Andrej Hudec + */ +class ArrayToModelTransformerTest extends \PHPUnit_Framework_TestCase +{ + private $modelManager = null; + + public function setUp() + { + $this->modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface'); + } + + public function testReverseTransformEntity() + { + $transformer = new ArrayToModelTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Form\FooEntity'); + + $entity = new FooEntity(); + $this->assertEquals($entity, $transformer->reverseTransform($entity)); + } + + /** + * @dataProvider getReverseTransformTests + */ + public function testReverseTransform($value) + { + $transformer = new ArrayToModelTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Form\FooEntity'); + + $this->modelManager->expects($this->any()) + ->method('modelReverseTransform') + ->will($this->returnValue(new FooEntity())); + + $this->assertInstanceOf('Sonata\AdminBundle\Tests\Fixtures\Entity\Form\FooEntity', $transformer->reverseTransform($value)); + } + + public function getReverseTransformTests() + { + return array( + array('Sonata\AdminBundle\Tests\Fixtures\Entity\Form\FooEntity'), + array(array()), + array(array('foo'=>'bar')), + array('foo'), + array(123), + array(null), + array(false), + ); + } + + /** + * @dataProvider getTransformTests + */ + public function testTransform($expected, $value) + { + $transformer = new ArrayToModelTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Form\FooEntity'); + + $this->assertEquals($expected, $transformer->transform($value)); + } + + public function getTransformTests() + { + return array( + array(123, 123), + array('foo', 'foo'), + array(false, false), + array(null, null), + array(0, 0), + ); + } +}