ArrayToModelTransformerTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /*
  3. * This file is part of the Sonata project.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Tests\Form\DataTransformer;
  11. use Sonata\AdminBundle\Model\ModelManagerInterface;
  12. use Sonata\AdminBundle\Form\DataTransformer\ArrayToModelTransformer;
  13. use Sonata\AdminBundle\Tests\Fixtures\Entity\Form\FooEntity;
  14. /**
  15. * @author Andrej Hudec <pulzarraider@gmail.com>
  16. */
  17. class ArrayToModelTransformerTest extends \PHPUnit_Framework_TestCase
  18. {
  19. private $modelManager = null;
  20. public function setUp()
  21. {
  22. $this->modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  23. }
  24. public function testReverseTransformEntity()
  25. {
  26. $transformer = new ArrayToModelTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Form\FooEntity');
  27. $entity = new FooEntity();
  28. $this->assertEquals($entity, $transformer->reverseTransform($entity));
  29. }
  30. /**
  31. * @dataProvider getReverseTransformTests
  32. */
  33. public function testReverseTransform($value)
  34. {
  35. $transformer = new ArrayToModelTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Form\FooEntity');
  36. $this->modelManager->expects($this->any())
  37. ->method('modelReverseTransform')
  38. ->will($this->returnValue(new FooEntity()));
  39. $this->assertInstanceOf('Sonata\AdminBundle\Tests\Fixtures\Entity\Form\FooEntity', $transformer->reverseTransform($value));
  40. }
  41. public function getReverseTransformTests()
  42. {
  43. return array(
  44. array('Sonata\AdminBundle\Tests\Fixtures\Entity\Form\FooEntity'),
  45. array(array()),
  46. array(array('foo'=>'bar')),
  47. array('foo'),
  48. array(123),
  49. array(null),
  50. array(false),
  51. );
  52. }
  53. /**
  54. * @dataProvider getTransformTests
  55. */
  56. public function testTransform($expected, $value)
  57. {
  58. $transformer = new ArrayToModelTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Form\FooEntity');
  59. $this->assertEquals($expected, $transformer->transform($value));
  60. }
  61. public function getTransformTests()
  62. {
  63. return array(
  64. array(123, 123),
  65. array('foo', 'foo'),
  66. array(false, false),
  67. array(null, null),
  68. array(0, 0),
  69. );
  70. }
  71. }