Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 KB | None | 0 0
  1. <?php
  2. declare(strict_types = 1);
  3.  
  4. namespace Tests\CommissionsBundle\Form\Type;
  5.  
  6. use CommissionsBundle\DTO\CatalogItemGroupDTO;
  7. use CommissionsBundle\Entity\CatalogItem;
  8. use CommissionsBundle\Entity\CatalogItemGroup;
  9. use CommissionsBundle\Form\Type\CatalogItemGroupType;
  10. use Doctrine\Bundle\DoctrineBundle\Registry;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\ORM\EntityManager;
  13. use Doctrine\ORM\EntityRepository;
  14. use Doctrine\ORM\Mapping\ClassMetadata;
  15. use Doctrine\ORM\QueryBuilder;
  16. use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface;
  17. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  18. use Symfony\Component\Form\Extension\Core\CoreExtension;
  19. use Symfony\Component\Form\FormExtensionInterface;
  20. use Symfony\Component\Form\PreloadedExtension;
  21. use Symfony\Component\Form\Test\TypeTestCase;
  22.  
  23. /**
  24. * Class CatalogItemGroupTypeTest
  25. */
  26. class CatalogItemGroupTypeTest extends TypeTestCase
  27. {
  28. private $itemRepositoryMock;
  29.  
  30. private $groupRepositoryMock;
  31.  
  32. private $managerMock;
  33.  
  34. /**
  35. * Tests if form submit works
  36. */
  37. public function testSubmitValidData()
  38. {
  39. $formData = [
  40. 'title' => 'test',
  41. 'parent' => null,
  42. 'items' => new ArrayCollection(),
  43. ];
  44. $form = $this->factory->create(CatalogItemGroupType::class);
  45.  
  46. $object = new CatalogItemGroupDTO();
  47. $object->setTitle($formData['title']);
  48. $object->setParent($formData['parent']);
  49.  
  50. $form->submit($formData);
  51.  
  52. $this->assertTrue($form->isSynchronized());
  53. $this->assertEquals($object, $form->getData());
  54.  
  55. $view = $form->createView();
  56. $children = $view->children;
  57.  
  58. foreach (array_keys($formData) as $key) {
  59. $this->assertArrayHasKey($key, $children);
  60. }
  61. }
  62.  
  63. protected function getExtensions()
  64. {
  65. $mockEntityManager = $this->createMock(EntityManager::class);
  66. $mockEntityManager->method('getClassMetadata')
  67. ->willReturn(new ClassMetadata(CatalogItemGroup::class))
  68. ;
  69.  
  70. $entityRepository = $this->createMock(EntityRepository::class);
  71. $entityRepository->method('createQueryBuilder')
  72. ->willReturn(new QueryBuilder($mockEntityManager))
  73. ;
  74. $mockEntityManager->method('getRepository')->willReturn($entityRepository);
  75.  
  76. $mockRegistry = $this->getMockBuilder(Registry::class)
  77. ->disableOriginalConstructor()
  78. ->setMethods(['getManagerForClass'])
  79. ->getMock()
  80. ;
  81.  
  82. $mockRegistry->method('getManagerForClass')
  83. ->willReturn($mockEntityManager)
  84. ;
  85.  
  86. /** @var EntityType|\PHPUnit_Framework_MockObject_MockObject $mockEntityType */
  87. $mockEntityType = $this->getMockBuilder(EntityType::class)
  88. ->setConstructorArgs([$mockRegistry])
  89. ->setMethodsExcept(['configureOptions', 'getParent'])
  90. ->getMock()
  91. ;
  92.  
  93. $mockEntityType->method('getLoader')->willReturnCallback(function ($a, $b, $class) {
  94. return new class($class) implements EntityLoaderInterface
  95. {
  96. /**
  97. * @var
  98. */
  99. private $class;
  100.  
  101. /**
  102. * constructor.
  103. *
  104. * @param $class
  105. */
  106. public function __construct($class)
  107. {
  108. $this->class = $class;
  109. }
  110.  
  111. /**
  112. * Returns an array of entities that are valid choices in the corresponding choice list.
  113. *
  114. * @return array The entities
  115. */
  116. public function getEntities()
  117. {
  118. switch ($this->class) {
  119. case CatalogItemGroup::class:
  120. return [new CatalogItemGroup('asd')];
  121. break;
  122. case CatalogItem::class:
  123. return [new CatalogItem('a', 'b', 'c')];
  124. break;
  125. }
  126. }
  127.  
  128. /**
  129. * Returns an array of entities matching the given identifiers.
  130. *
  131. * @param string $identifier The identifier field of the object. This method
  132. * is not applicable for fields with multiple
  133. * identifiers.
  134. * @param array $values The values of the identifiers
  135. *
  136. * @return array The entities
  137. */
  138. public function getEntitiesByIds($identifier, array $values)
  139. {
  140. // TODO: Implement getEntitiesByIds() method.
  141. }
  142. };
  143. })
  144. ;
  145.  
  146. return [
  147. new class($mockEntityType) implements FormExtensionInterface
  148. {
  149. private $type;
  150.  
  151. public function __construct($type)
  152. {
  153. $this->type = $type;
  154. }
  155.  
  156. public function getType($name)
  157. {
  158. return $this->type;
  159. }
  160.  
  161. public function hasType($name)
  162. {
  163. return $name === EntityType::class;
  164. }
  165.  
  166. public function getTypeExtensions($name)
  167. {
  168. return [];
  169. }
  170.  
  171. public function hasTypeExtensions($name)
  172. {
  173. }
  174.  
  175. public function getTypeGuesser()
  176. {
  177. }
  178. },
  179. ];
  180. }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement