ModelsToArrayTransformerTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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\Form\DataTransformer;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList;
  13. use Sonata\AdminBundle\Tests\Fixtures\Entity\Form\FooEntity;
  14. use Symfony\Component\Form\ChoiceList\LegacyChoiceListAdapter;
  15. /**
  16. * @author Andrej Hudec <pulzarraider@gmail.com>
  17. */
  18. class ModelsToArrayTransformerTest extends \PHPUnit_Framework_TestCase
  19. {
  20. private $choiceList;
  21. private $modelChoiceList;
  22. private $modelManager;
  23. public function setUp()
  24. {
  25. $this->modelChoiceList = $this->getMockBuilder('Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList')
  26. ->disableOriginalConstructor()
  27. ->getMock();
  28. // Symfony < 2.7 BC
  29. if (class_exists('Symfony\Component\Form\ChoiceList\LegacyChoiceListAdapter')) {
  30. $this->choiceList = new LegacyChoiceListAdapter($this->modelChoiceList);
  31. } else {
  32. $this->choiceList = $this->modelChoiceList;
  33. }
  34. $this->modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  35. // php 5.3 BC
  36. $modelManager = $this->modelManager;
  37. $this->modelChoiceList->expects($this->any())
  38. ->method('getModelManager')
  39. ->will($this->returnCallback(function () use ($modelManager) {
  40. return $modelManager;
  41. }));
  42. }
  43. /**
  44. * @dataProvider getTransformTests
  45. */
  46. public function testTransform($expected, $collection, $identifiers)
  47. {
  48. $transformer = new ModelsToArrayTransformer($this->choiceList);
  49. $this->modelChoiceList->expects($this->any())
  50. ->method('getIdentifierValues')
  51. ->will($this->returnCallback(function ($entity) use ($identifiers) {
  52. if ($entity instanceof FooEntity) {
  53. return $identifiers;
  54. }
  55. return array();
  56. }));
  57. $this->modelChoiceList->expects($this->any())
  58. ->method('getIdentifier')
  59. ->will($this->returnCallback(function () use ($identifiers) {
  60. return $identifiers;
  61. }));
  62. $this->modelChoiceList->expects($this->any())
  63. ->method('getEntities')
  64. ->will($this->returnCallback(function () {
  65. return array('bcd' => new FooEntity(array('bcd')), 'efg' => new FooEntity(array('efg')), 'abc' => new FooEntity(array('abc')));
  66. }));
  67. $this->assertEquals($expected, $transformer->transform($collection));
  68. }
  69. public function getTransformTests()
  70. {
  71. return array(
  72. array(array(), null, array()),
  73. array(array(), array(), array()),
  74. array(array('id'), array(new FooEntity()), array('id')),
  75. array(array('id', 'id'), array(new FooEntity(), new FooEntity()), array('id')),
  76. array(array('abc', 'bcd', 'efg'), array(new FooEntity(array('abc')), new FooEntity(array('bcd')), new FooEntity(array('efg'))), array('id1', 'id2')),
  77. );
  78. }
  79. public function testReverseTransformWithException1()
  80. {
  81. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException', 'Expected argument of type "\ArrayAccess", "NULL" given');
  82. $transformer = new ModelsToArrayTransformer($this->choiceList);
  83. $this->modelManager->expects($this->any())
  84. ->method('getModelCollectionInstance')
  85. ->will($this->returnValue(null));
  86. $transformer->reverseTransform(array());
  87. }
  88. public function testReverseTransformWithException2()
  89. {
  90. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException', 'Expected argument of type "array", "integer" given');
  91. $transformer = new ModelsToArrayTransformer($this->choiceList);
  92. $this->modelManager->expects($this->any())
  93. ->method('getModelCollectionInstance')
  94. ->will($this->returnValue(new ArrayCollection()));
  95. $transformer->reverseTransform(123);
  96. }
  97. /**
  98. * @dataProvider getReverseTransformEmptyTests
  99. */
  100. public function testReverseTransformEmpty($keys)
  101. {
  102. $transformer = new ModelsToArrayTransformer($this->choiceList);
  103. $this->modelManager->expects($this->any())
  104. ->method('getModelCollectionInstance')
  105. ->will($this->returnValue(new ArrayCollection()));
  106. $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $transformer->reverseTransform($keys));
  107. }
  108. public function getReverseTransformEmptyTests()
  109. {
  110. return array(
  111. array(null),
  112. array(''),
  113. );
  114. }
  115. public function testReverseTransform()
  116. {
  117. $transformer = new ModelsToArrayTransformer($this->choiceList);
  118. $this->modelManager->expects($this->any())
  119. ->method('getModelCollectionInstance')
  120. ->will($this->returnValue(new ArrayCollection()));
  121. $entity1 = new FooEntity(array('foo'));
  122. $entity2 = new FooEntity(array('bar'));
  123. $entity3 = new FooEntity(array('baz'));
  124. $this->modelChoiceList->expects($this->any())
  125. ->method('getEntity')
  126. ->will($this->returnCallback(function ($key) use ($entity1, $entity2, $entity3) {
  127. switch ($key) {
  128. case 'foo':
  129. return $entity1;
  130. break;
  131. case 'bar':
  132. return $entity2;
  133. break;
  134. case 'baz':
  135. return $entity3;
  136. break;
  137. }
  138. return;
  139. }));
  140. $collection = $transformer->reverseTransform(array('foo', 'bar'));
  141. $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $collection);
  142. $this->assertEquals(array($entity1, $entity2), $collection->getValues());
  143. $this->assertCount(2, $collection);
  144. }
  145. public function testReverseTransformWithNonexistentEntityKey()
  146. {
  147. $this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException', 'The entities with keys "nonexistent" could not be found');
  148. $transformer = new ModelsToArrayTransformer($this->choiceList);
  149. $this->modelManager->expects($this->any())
  150. ->method('getModelCollectionInstance')
  151. ->will($this->returnValue(new ArrayCollection()));
  152. $this->modelChoiceList->expects($this->any())
  153. ->method('getEntity')
  154. ->will($this->returnValue(false));
  155. $transformer->reverseTransform(array('nonexistent'));
  156. }
  157. }