ModelToIdPropertyTransformerTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /*
  3. * This file is part of the Sonata 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\ModelToIdPropertyTransformer;
  12. use Sonata\AdminBundle\Tests\Fixtures\Entity\Foo;
  13. use Sonata\AdminBundle\Tests\Fixtures\Entity\FooArrayAccess;
  14. use Doctrine\Common\Collections\ArrayCollection;
  15. class ModelToIdPropertyTransformerTest extends \PHPUnit_Framework_TestCase
  16. {
  17. private $modelManager = null;
  18. public function setUp()
  19. {
  20. $this->modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  21. }
  22. public function testReverseTransform()
  23. {
  24. $transformer = new ModelToIdPropertyTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo', 'bar', false);
  25. $entity = new Foo();
  26. $entity->setBar('example');
  27. $this->modelManager
  28. ->expects($this->any())
  29. ->method('find')
  30. ->with($this->equalTo('Sonata\AdminBundle\Tests\Fixtures\Entity\Foo'), $this->equalTo(123))
  31. ->will($this->returnValue($entity));
  32. $this->assertNull($transformer->reverseTransform(null));
  33. $this->assertNull($transformer->reverseTransform(false));
  34. $this->assertNull($transformer->reverseTransform(12));
  35. $this->assertEquals($entity, $transformer->reverseTransform(array('identifiers' => array(123), 'titles' => array('example'))));
  36. }
  37. /**
  38. * @dataProvider getReverseTransformMultipleTests
  39. */
  40. public function testReverseTransformMultiple($expected, $params, $entity1, $entity2, $entity3)
  41. {
  42. $transformer = new ModelToIdPropertyTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo', 'bar', true);
  43. $this->modelManager
  44. ->expects($this->any())
  45. ->method('find')
  46. ->will($this->returnCallback(function ($className, $value) use ($entity1, $entity2, $entity3) {
  47. if ($className != 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo') {
  48. return null;
  49. }
  50. if ($value == 123) {
  51. return $entity1;
  52. }
  53. if ($value == 456) {
  54. return $entity2;
  55. }
  56. if ($value == 789) {
  57. return $entity3;
  58. }
  59. return null;
  60. }));
  61. $collection = new ArrayCollection();
  62. $this->modelManager
  63. ->expects($this->any())
  64. ->method('getModelCollectionInstance')
  65. ->with($this->equalTo('Sonata\AdminBundle\Tests\Fixtures\Entity\Foo'))
  66. ->will($this->returnValue($collection));
  67. $result = $transformer->reverseTransform($params);
  68. $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $result);
  69. $this->assertEquals($expected, $result->getValues());
  70. }
  71. public function getReverseTransformMultipleTests()
  72. {
  73. $entity1 = new Foo();
  74. $entity1->setBaz(123);
  75. $entity1->setBar('example');
  76. $entity2 = new Foo();
  77. $entity2->setBaz(456);
  78. $entity2->setBar('example2');
  79. $entity3 = new Foo();
  80. $entity3->setBaz(789);
  81. $entity3->setBar('example3');
  82. return array(
  83. array(array(), null, $entity1, $entity2, $entity3),
  84. array(array(), false, $entity1, $entity2, $entity3),
  85. array(array(), true, $entity1, $entity2, $entity3),
  86. array(array(), 12, $entity1, $entity2, $entity3),
  87. array(array($entity1), array('identifiers' => array(123), 'titles' => array('example')), $entity1, $entity2, $entity3),
  88. array(array($entity1, $entity2, $entity3), array('identifiers' => array(123, 456, 789), 'titles' => array('example', 'example2', 'example3')), $entity1, $entity2, $entity3),
  89. );
  90. }
  91. public function testTransform()
  92. {
  93. $entity = new Foo();
  94. $entity->setBar('example');
  95. $this->modelManager->expects($this->once())
  96. ->method('getIdentifierValues')
  97. ->will($this->returnValue(array(123)));
  98. $transformer = new ModelToIdPropertyTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo', 'bar', false);
  99. $this->assertEquals(array('identifiers' => array(), 'labels' => array()), $transformer->transform(null));
  100. $this->assertEquals(array('identifiers' => array(), 'labels' => array()), $transformer->transform(false));
  101. $this->assertEquals(array('identifiers' => array(), 'labels' => array()), $transformer->transform(0));
  102. $this->assertEquals(array('identifiers' => array(), 'labels' => array()), $transformer->transform('0'));
  103. $this->assertEquals(array('identifiers' => array(123), 'labels' => array('example')), $transformer->transform($entity));
  104. }
  105. public function testTransformWorksWithArrayAccessEntity()
  106. {
  107. $entity = new FooArrayAccess();
  108. $entity->setBar('example');
  109. $this->modelManager->expects($this->once())
  110. ->method('getIdentifierValues')
  111. ->will($this->returnValue(array(123)));
  112. $transformer = new ModelToIdPropertyTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\FooArrayAccess', 'bar', false);
  113. $this->assertEquals(array('identifiers' => array(123), 'labels' => array('example')), $transformer->transform($entity));
  114. }
  115. public function testTransformToStringCallback()
  116. {
  117. $entity = new Foo();
  118. $entity->setBar('example');
  119. $entity->setBaz('bazz');
  120. $this->modelManager->expects($this->once())
  121. ->method('getIdentifierValues')
  122. ->will($this->returnValue(array(123)));
  123. $transformer = new ModelToIdPropertyTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo', 'bar', false, function ($entity) {
  124. return $entity->getBaz();
  125. });
  126. $this->assertEquals(array('identifiers' => array(123), 'labels' => array('bazz')), $transformer->transform($entity));
  127. }
  128. public function testTransformMultiple()
  129. {
  130. $entity1 = new Foo();
  131. $entity1->setBar('foo');
  132. $entity2 = new Foo();
  133. $entity2->setBar('bar');
  134. $entity3 = new Foo();
  135. $entity3->setBar('baz');
  136. $collection = new ArrayCollection();
  137. $collection[] = $entity1;
  138. $collection[] = $entity2;
  139. $collection[] = $entity3;
  140. $this->modelManager->expects($this->exactly(3))
  141. ->method('getIdentifierValues')
  142. ->will($this->returnCallback(function ($value) use ($entity1, $entity2, $entity3) {
  143. if ($value == $entity1) {
  144. return array(123);
  145. }
  146. if ($value == $entity2) {
  147. return array(456);
  148. }
  149. if ($value == $entity3) {
  150. return array(789);
  151. }
  152. return array(999);
  153. }));
  154. $transformer = new ModelToIdPropertyTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo', 'bar', true);
  155. $this->assertEquals(array('identifiers' => array(), 'labels' => array()), $transformer->transform(null));
  156. $this->assertEquals(array('identifiers' => array(), 'labels' => array()), $transformer->transform(false));
  157. $this->assertEquals(array('identifiers' => array(), 'labels' => array()), $transformer->transform(0));
  158. $this->assertEquals(array('identifiers' => array(), 'labels' => array()), $transformer->transform('0'));
  159. $this->assertEquals(array('identifiers' => array(123, 456, 789), 'labels' => array('foo', 'bar', 'baz')), $transformer->transform($collection));
  160. }
  161. }