ModelToIdPropertyTransformerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. ->will($this->returnCallback(function ($class, $id) use ($entity) {
  31. if ($class === 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo' && $id === 123) {
  32. return $entity;
  33. }
  34. return null;
  35. }));
  36. $this->assertNull($transformer->reverseTransform(null));
  37. $this->assertNull($transformer->reverseTransform(false));
  38. $this->assertNull($transformer->reverseTransform(''));
  39. $this->assertNull($transformer->reverseTransform(12));
  40. $this->assertNull($transformer->reverseTransform(array(123)));
  41. $this->assertNull($transformer->reverseTransform(array(123, 456, 789)));
  42. $this->assertEquals($entity, $transformer->reverseTransform(123));
  43. }
  44. /**
  45. * @dataProvider getReverseTransformMultipleTests
  46. */
  47. public function testReverseTransformMultiple($expected, $params, $entity1, $entity2, $entity3)
  48. {
  49. $transformer = new ModelToIdPropertyTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo', 'bar', true);
  50. $this->modelManager
  51. ->expects($this->any())
  52. ->method('find')
  53. ->will($this->returnCallback(function ($className, $value) use ($entity1, $entity2, $entity3) {
  54. if ($className != 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo') {
  55. return null;
  56. }
  57. if ($value == 123) {
  58. return $entity1;
  59. }
  60. if ($value == 456) {
  61. return $entity2;
  62. }
  63. if ($value == 789) {
  64. return $entity3;
  65. }
  66. return null;
  67. }));
  68. $collection = new ArrayCollection();
  69. $this->modelManager
  70. ->expects($this->any())
  71. ->method('getModelCollectionInstance')
  72. ->with($this->equalTo('Sonata\AdminBundle\Tests\Fixtures\Entity\Foo'))
  73. ->will($this->returnValue($collection));
  74. $result = $transformer->reverseTransform($params);
  75. $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $result);
  76. $this->assertEquals($expected, $result->getValues());
  77. }
  78. public function getReverseTransformMultipleTests()
  79. {
  80. $entity1 = new Foo();
  81. $entity1->setBaz(123);
  82. $entity1->setBar('example');
  83. $entity2 = new Foo();
  84. $entity2->setBaz(456);
  85. $entity2->setBar('example2');
  86. $entity3 = new Foo();
  87. $entity3->setBaz(789);
  88. $entity3->setBar('example3');
  89. return array(
  90. array(array(), null, $entity1, $entity2, $entity3),
  91. array(array(), false, $entity1, $entity2, $entity3),
  92. array(array($entity1), array(123, '_labels' => array('example')), $entity1, $entity2, $entity3),
  93. array(array($entity1, $entity2, $entity3), array(123, 456, 789, '_labels' => array('example', 'example2', 'example3')), $entity1, $entity2, $entity3),
  94. );
  95. }
  96. /**
  97. * @dataProvider getReverseTransformMultipleInvalidTypeTests
  98. */
  99. public function testReverseTransformMultipleInvalidTypeTests($expected, $params, $type)
  100. {
  101. $this->setExpectedException(
  102. 'UnexpectedValueException', sprintf('Value should be array, %s given.', $type)
  103. );
  104. $transformer = new ModelToIdPropertyTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo', 'bar', true);
  105. $collection = new ArrayCollection();
  106. $this->modelManager
  107. ->expects($this->any())
  108. ->method('getModelCollectionInstance')
  109. ->with($this->equalTo('Sonata\AdminBundle\Tests\Fixtures\Entity\Foo'))
  110. ->will($this->returnValue($collection));
  111. $result = $transformer->reverseTransform($params);
  112. $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $result);
  113. $this->assertEquals($expected, $result->getValues());
  114. }
  115. public function getReverseTransformMultipleInvalidTypeTests()
  116. {
  117. return array(
  118. array(array(), true, 'boolean'),
  119. array(array(), 12, 'integer'),
  120. array(array(), 12.9, 'double'),
  121. array(array(), '_labels', 'string'),
  122. array(array(), new \stdClass(), 'object'),
  123. );
  124. }
  125. public function testTransform()
  126. {
  127. $entity = new Foo();
  128. $entity->setBar('example');
  129. $this->modelManager->expects($this->once())
  130. ->method('getIdentifierValues')
  131. ->will($this->returnValue(array(123)));
  132. $transformer = new ModelToIdPropertyTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo', 'bar', false);
  133. $this->assertEquals(array(), $transformer->transform(null));
  134. $this->assertEquals(array(), $transformer->transform(false));
  135. $this->assertEquals(array(), $transformer->transform(''));
  136. $this->assertEquals(array(), $transformer->transform(0));
  137. $this->assertEquals(array(), $transformer->transform('0'));
  138. $this->assertEquals(array(123, '_labels' => array('example')), $transformer->transform($entity));
  139. }
  140. public function testTransformWorksWithArrayAccessEntity()
  141. {
  142. $entity = new FooArrayAccess();
  143. $entity->setBar('example');
  144. $this->modelManager->expects($this->once())
  145. ->method('getIdentifierValues')
  146. ->will($this->returnValue(array(123)));
  147. $transformer = new ModelToIdPropertyTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\FooArrayAccess', 'bar', false);
  148. $this->assertEquals(array(123, '_labels' => array('example')), $transformer->transform($entity));
  149. }
  150. public function testTransformToStringCallback()
  151. {
  152. $entity = new Foo();
  153. $entity->setBar('example');
  154. $entity->setBaz('bazz');
  155. $this->modelManager->expects($this->once())
  156. ->method('getIdentifierValues')
  157. ->will($this->returnValue(array(123)));
  158. $transformer = new ModelToIdPropertyTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo', 'bar', false, function ($entity) {
  159. return $entity->getBaz();
  160. });
  161. $this->assertEquals(array(123, '_labels' => array('bazz')), $transformer->transform($entity));
  162. }
  163. /**
  164. * @expectedException \RuntimeException
  165. * @expectedExceptionMessage Callback in "to_string_callback" option doesn`t contain callable function.
  166. */
  167. public function testTransformToStringCallbackException()
  168. {
  169. $entity = new Foo();
  170. $entity->setBar('example');
  171. $entity->setBaz('bazz');
  172. $this->modelManager->expects($this->once())
  173. ->method('getIdentifierValues')
  174. ->will($this->returnValue(array(123)));
  175. $transformer = new ModelToIdPropertyTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo', 'bar', false, '987654');
  176. $transformer->transform($entity);
  177. }
  178. public function testTransformMultiple()
  179. {
  180. $entity1 = new Foo();
  181. $entity1->setBar('foo');
  182. $entity2 = new Foo();
  183. $entity2->setBar('bar');
  184. $entity3 = new Foo();
  185. $entity3->setBar('baz');
  186. $collection = new ArrayCollection();
  187. $collection[] = $entity1;
  188. $collection[] = $entity2;
  189. $collection[] = $entity3;
  190. $this->modelManager->expects($this->exactly(3))
  191. ->method('getIdentifierValues')
  192. ->will($this->returnCallback(function ($value) use ($entity1, $entity2, $entity3) {
  193. if ($value == $entity1) {
  194. return array(123);
  195. }
  196. if ($value == $entity2) {
  197. return array(456);
  198. }
  199. if ($value == $entity3) {
  200. return array(789);
  201. }
  202. return array(999);
  203. }));
  204. $transformer = new ModelToIdPropertyTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo', 'bar', true);
  205. $this->assertEquals(array(), $transformer->transform(null));
  206. $this->assertEquals(array(), $transformer->transform(false));
  207. $this->assertEquals(array(), $transformer->transform(''));
  208. $this->assertEquals(array(), $transformer->transform(0));
  209. $this->assertEquals(array(), $transformer->transform('0'));
  210. $this->assertEquals(array(123, 456, 789, '_labels' => array('foo', 'bar', 'baz')), $transformer->transform($collection));
  211. }
  212. /**
  213. * @expectedException \InvalidArgumentException
  214. * @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.
  215. */
  216. public function testTransformCollectionException()
  217. {
  218. $entity = new Foo();
  219. $transformer = new ModelToIdPropertyTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo', 'bar', true);
  220. $transformer->transform($entity);
  221. }
  222. /**
  223. * @expectedException \InvalidArgumentException
  224. * @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.
  225. */
  226. public function testTransformArrayAccessException()
  227. {
  228. $entity = new FooArrayAccess();
  229. $entity->setBar('example');
  230. $transformer = new ModelToIdPropertyTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\FooArrayAccess', 'bar', true);
  231. $transformer->transform($entity);
  232. }
  233. /**
  234. * @expectedException \InvalidArgumentException
  235. * @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.
  236. */
  237. public function testTransformEntityException()
  238. {
  239. $entity1 = new Foo();
  240. $entity1->setBar('foo');
  241. $entity2 = new Foo();
  242. $entity2->setBar('bar');
  243. $entity3 = new Foo();
  244. $entity3->setBar('baz');
  245. $collection = new ArrayCollection();
  246. $collection[] = $entity1;
  247. $collection[] = $entity2;
  248. $collection[] = $entity3;
  249. $transformer = new ModelToIdPropertyTransformer($this->modelManager, 'Sonata\AdminBundle\Tests\Fixtures\Entity\Foo', 'bar', false);
  250. $transformer->transform($collection);
  251. }
  252. }