ArrayToModelTransformerTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  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\Form\DataTransformer\ArrayToModelTransformer;
  12. use Sonata\AdminBundle\Tests\Fixtures\Entity\Form\FooEntity;
  13. /**
  14. * @author Andrej Hudec <pulzarraider@gmail.com>
  15. */
  16. class ArrayToModelTransformerTest extends \PHPUnit_Framework_TestCase
  17. {
  18. private $modelManager = null;
  19. public function setUp()
  20. {
  21. $this->modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  22. }
  23. public function testReverseTransformEntity()
  24. {
  25. $transformer = new ArrayToModelTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Form\FooEntity');
  26. $entity = new FooEntity();
  27. $this->assertSame($entity, $transformer->reverseTransform($entity));
  28. }
  29. /**
  30. * @dataProvider getReverseTransformTests
  31. */
  32. public function testReverseTransform($value)
  33. {
  34. $transformer = new ArrayToModelTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Form\FooEntity');
  35. $this->modelManager->expects($this->any())
  36. ->method('modelReverseTransform')
  37. ->will($this->returnValue(new FooEntity()));
  38. $this->assertInstanceOf('Sonata\AdminBundle\Tests\Fixtures\Entity\Form\FooEntity', $transformer->reverseTransform($value));
  39. }
  40. public function getReverseTransformTests()
  41. {
  42. return array(
  43. array('Sonata\AdminBundle\Tests\Fixtures\Entity\Form\FooEntity'),
  44. array(array()),
  45. array(array('foo' => 'bar')),
  46. array('foo'),
  47. array(123),
  48. array(null),
  49. array(false),
  50. );
  51. }
  52. /**
  53. * @dataProvider getTransformTests
  54. */
  55. public function testTransform($expected, $value)
  56. {
  57. $transformer = new ArrayToModelTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Form\FooEntity');
  58. $this->assertSame($expected, $transformer->transform($value));
  59. }
  60. public function getTransformTests()
  61. {
  62. return array(
  63. array(123, 123),
  64. array('foo', 'foo'),
  65. array(false, false),
  66. array(null, null),
  67. array(0, 0),
  68. );
  69. }
  70. }