ModelToIdPropertyTransformerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. /**
  129. * @expectedException \RuntimeException
  130. * @expectedExceptionMessage Callback in "to_string_callback" option doesn`t contain callable function.
  131. */
  132. public function testTransformToStringCallbackException()
  133. {
  134. $entity = new Foo();
  135. $entity->setBar('example');
  136. $entity->setBaz('bazz');
  137. $this->modelManager->expects($this->once())
  138. ->method('getIdentifierValues')
  139. ->will($this->returnValue(array(123)));
  140. $transformer = new ModelToIdPropertyTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo', 'bar', false, '987654');
  141. $transformer->transform($entity);
  142. }
  143. public function testTransformMultiple()
  144. {
  145. $entity1 = new Foo();
  146. $entity1->setBar('foo');
  147. $entity2 = new Foo();
  148. $entity2->setBar('bar');
  149. $entity3 = new Foo();
  150. $entity3->setBar('baz');
  151. $collection = new ArrayCollection();
  152. $collection[] = $entity1;
  153. $collection[] = $entity2;
  154. $collection[] = $entity3;
  155. $this->modelManager->expects($this->exactly(3))
  156. ->method('getIdentifierValues')
  157. ->will($this->returnCallback(function ($value) use ($entity1, $entity2, $entity3) {
  158. if ($value == $entity1) {
  159. return array(123);
  160. }
  161. if ($value == $entity2) {
  162. return array(456);
  163. }
  164. if ($value == $entity3) {
  165. return array(789);
  166. }
  167. return array(999);
  168. }));
  169. $transformer = new ModelToIdPropertyTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo', 'bar', true);
  170. $this->assertEquals(array('identifiers' => array(), 'labels' => array()), $transformer->transform(null));
  171. $this->assertEquals(array('identifiers' => array(), 'labels' => array()), $transformer->transform(false));
  172. $this->assertEquals(array('identifiers' => array(), 'labels' => array()), $transformer->transform(0));
  173. $this->assertEquals(array('identifiers' => array(), 'labels' => array()), $transformer->transform('0'));
  174. $this->assertEquals(array('identifiers' => array(123, 456, 789), 'labels' => array('foo', 'bar', 'baz')), $transformer->transform($collection));
  175. }
  176. /**
  177. * @expectedException \InvalidArgumentException
  178. * @expectedExceptionMessage A multiple selection must be passed a collection not a single value. Make sure that form option "multiple=false" is set for many-to-one relation and "multiple=true" is set for many-to-many or one-to-many relations.
  179. */
  180. public function testTransformCollectionException()
  181. {
  182. $entity = new Foo();
  183. $transformer = new ModelToIdPropertyTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo', 'bar', true);
  184. $transformer->transform($entity);
  185. }
  186. /**
  187. * @expectedException \InvalidArgumentException
  188. * @expectedExceptionMessage A multiple selection must be passed a collection not a single value. Make sure that form option "multiple=false" is set for many-to-one relation and "multiple=true" is set for many-to-many or one-to-many relations.
  189. */
  190. public function testTransformArrayAccessException()
  191. {
  192. $entity = new FooArrayAccess();
  193. $entity->setBar('example');
  194. $transformer = new ModelToIdPropertyTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\FooArrayAccess', 'bar', true);
  195. $transformer->transform($entity);
  196. }
  197. /**
  198. * @expectedException \InvalidArgumentException
  199. * @expectedExceptionMessage A single selection must be passed a single value not a collection. Make sure that form option "multiple=false" is set for many-to-one relation and "multiple=true" is set for many-to-many or one-to-many relations.
  200. */
  201. public function testTransformEntityException()
  202. {
  203. $entity1 = new Foo();
  204. $entity1->setBar('foo');
  205. $entity2 = new Foo();
  206. $entity2->setBar('bar');
  207. $entity3 = new Foo();
  208. $entity3->setBar('baz');
  209. $collection = new ArrayCollection();
  210. $collection[] = $entity1;
  211. $collection[] = $entity2;
  212. $collection[] = $entity3;
  213. $transformer = new ModelToIdPropertyTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo', 'bar', false);
  214. $transformer->transform($collection);
  215. }
  216. }