ModelsToArrayTransformerTest.php 6.6 KB

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