LegacyModelsToArrayTransformerTest.php 6.6 KB

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