PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Form/Type/AdminTypeTest.php

http://github.com/sonata-project/SonataAdminBundle
PHP | 256 lines | 194 code | 48 blank | 14 comment | 0 complexity | dc4e7b877e542154c2bb8d79f7995f9d MD5 | raw file
Possible License(s): JSON, Apache-2.0, MIT
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the Sonata Project package.
  5. *
  6. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Sonata\AdminBundle\Tests\Form\Type;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Prophecy\Argument;
  14. use Prophecy\Argument\Token\AnyValueToken;
  15. use Prophecy\Prophecy\ObjectProphecy;
  16. use Sonata\AdminBundle\Admin\AbstractAdmin;
  17. use Sonata\AdminBundle\Admin\AdminHelper;
  18. use Sonata\AdminBundle\Admin\AdminInterface;
  19. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  20. use Sonata\AdminBundle\Form\Extension\Field\Type\FormTypeFieldExtension;
  21. use Sonata\AdminBundle\Form\Type\AdminType;
  22. use Sonata\AdminBundle\Model\ModelManagerInterface;
  23. use Sonata\AdminBundle\Tests\Fixtures\Entity\Foo;
  24. use Sonata\AdminBundle\Tests\Fixtures\TestExtension;
  25. use Symfony\Component\Form\FormTypeGuesserInterface;
  26. use Symfony\Component\Form\PreloadedExtension;
  27. use Symfony\Component\Form\Test\TypeTestCase;
  28. use Symfony\Component\OptionsResolver\OptionsResolver;
  29. use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
  30. class AdminTypeTest extends TypeTestCase
  31. {
  32. /**
  33. * @var AdminHelper|ObjectProphecy
  34. */
  35. private $adminHelper;
  36. /**
  37. * @var AdminType
  38. */
  39. private $adminType;
  40. protected function setUp(): void
  41. {
  42. $this->adminHelper = $this->prophesize(AdminHelper::class);
  43. $this->adminType = new AdminType($this->adminHelper->reveal());
  44. parent::setUp();
  45. }
  46. public function testGetDefaultOptions(): void
  47. {
  48. $optionResolver = new OptionsResolver();
  49. $this->adminType->configureOptions($optionResolver);
  50. $options = $optionResolver->resolve();
  51. $this->assertTrue($options['delete']);
  52. $this->assertFalse($options['auto_initialize']);
  53. $this->assertSame('link_add', $options['btn_add']);
  54. $this->assertSame('link_list', $options['btn_list']);
  55. $this->assertSame('link_delete', $options['btn_delete']);
  56. $this->assertSame('SonataAdminBundle', $options['btn_catalogue']);
  57. }
  58. public function testSubmitValidData(): void
  59. {
  60. $parentAdmin = $this->prophesize(AdminInterface::class);
  61. $parentAdmin->hasSubject()->shouldBeCalled()->willReturn(false);
  62. $parentField = $this->prophesize(FieldDescriptionInterface::class);
  63. $parentField->setAssociationAdmin(Argument::type(AdminInterface::class))->shouldBeCalled();
  64. $parentField->getAdmin()->shouldBeCalled()->willReturn($parentAdmin->reveal());
  65. $modelManager = $this->prophesize(ModelManagerInterface::class);
  66. $foo = new Foo();
  67. $admin = $this->prophesize(AbstractAdmin::class);
  68. $admin->hasParentFieldDescription()->shouldBeCalled()->willReturn(true);
  69. $admin->getParentFieldDescription()->shouldBeCalled()->willReturn($parentField->reveal());
  70. $admin->hasAccess('delete')->shouldBeCalled()->willReturn(false);
  71. $admin->defineFormBuilder(new AnyValueToken())->shouldBeCalled();
  72. $admin->getModelManager()->shouldBeCalled()->willReturn($modelManager);
  73. $admin->getClass()->shouldBeCalled()->willReturn(Foo::class);
  74. $admin->getNewInstance()->shouldBeCalled()->willReturn($foo);
  75. $admin->setSubject($foo)->shouldBeCalled();
  76. $field = $this->prophesize(FieldDescriptionInterface::class);
  77. $field->getAssociationAdmin()->shouldBeCalled()->willReturn($admin->reveal());
  78. $field->getAdmin()->shouldBeCalled();
  79. $field->getName()->shouldBeCalled();
  80. $field->getOption('edit', 'standard')->shouldBeCalled();
  81. $field->getOption('inline', 'natural')->shouldBeCalled();
  82. $field->getOption('block_name', false)->shouldBeCalled();
  83. $formData = [];
  84. $form = $this->factory->create(
  85. AdminType::class,
  86. null,
  87. [
  88. 'sonata_field_description' => $field->reveal(),
  89. ]
  90. );
  91. $form->submit($formData);
  92. $this->assertTrue($form->isSynchronized());
  93. }
  94. public function testDotFields(): void
  95. {
  96. $foo = new \stdClass();
  97. $foo->bar = 1;
  98. $parentSubject = new \stdClass();
  99. $parentSubject->foo = $foo;
  100. $parentAdmin = $this->prophesize(AdminInterface::class);
  101. $parentAdmin->getSubject()->shouldBeCalled()->willReturn($parentSubject);
  102. $parentAdmin->hasSubject()->shouldBeCalled()->willReturn(true);
  103. $parentField = $this->prophesize(FieldDescriptionInterface::class);
  104. $parentField->setAssociationAdmin(Argument::type(AdminInterface::class))->shouldBeCalled();
  105. $parentField->getAdmin()->shouldBeCalled()->willReturn($parentAdmin->reveal());
  106. $modelManager = $this->prophesize(ModelManagerInterface::class);
  107. $admin = $this->prophesize(AbstractAdmin::class);
  108. $admin->hasParentFieldDescription()->shouldBeCalled()->willReturn(true);
  109. $admin->getParentFieldDescription()->shouldBeCalled()->willReturn($parentField->reveal());
  110. $admin->setSubject(1)->shouldBeCalled();
  111. $admin->defineFormBuilder(new AnyValueToken())->shouldBeCalled();
  112. $admin->getModelManager()->shouldBeCalled()->willReturn($modelManager);
  113. $admin->getClass()->shouldBeCalled()->willReturn(Foo::class);
  114. $field = $this->prophesize(FieldDescriptionInterface::class);
  115. $field->getAssociationAdmin()->shouldBeCalled()->willReturn($admin->reveal());
  116. $field->getFieldName()->shouldBeCalled()->willReturn('bar');
  117. $field->getParentAssociationMappings()->shouldBeCalled()->willReturn([['fieldName' => 'foo']]);
  118. $this->builder->add('foo.bar');
  119. try {
  120. $this->adminType->buildForm($this->builder, [
  121. 'sonata_field_description' => $field->reveal(),
  122. 'delete' => false, // not needed
  123. 'property_path' => 'bar', // actual test case
  124. ]);
  125. } catch (NoSuchPropertyException $exception) {
  126. $this->fail($exception->getMessage());
  127. }
  128. }
  129. public function testArrayCollection(): void
  130. {
  131. $foo = new Foo();
  132. $parentSubject = new \stdClass();
  133. $parentSubject->foo = new ArrayCollection([$foo]);
  134. $parentAdmin = $this->prophesize(AdminInterface::class);
  135. $parentAdmin->getSubject()->shouldBeCalled()->willReturn($parentSubject);
  136. $parentAdmin->hasSubject()->shouldBeCalled()->willReturn(true);
  137. $parentField = $this->prophesize(FieldDescriptionInterface::class);
  138. $parentField->setAssociationAdmin(Argument::type(AdminInterface::class))->shouldBeCalled();
  139. $parentField->getAdmin()->shouldBeCalled()->willReturn($parentAdmin->reveal());
  140. $modelManager = $this->prophesize(ModelManagerInterface::class);
  141. $admin = $this->prophesize(AbstractAdmin::class);
  142. $admin->hasParentFieldDescription()->shouldBeCalled()->willReturn(true);
  143. $admin->getParentFieldDescription()->shouldBeCalled()->willReturn($parentField->reveal());
  144. $admin->defineFormBuilder(new AnyValueToken())->shouldBeCalled();
  145. $admin->getModelManager()->shouldBeCalled()->willReturn($modelManager);
  146. $admin->getClass()->shouldBeCalled()->willReturn(Foo::class);
  147. $admin->setSubject($foo)->shouldBeCalled();
  148. $field = $this->prophesize(FieldDescriptionInterface::class);
  149. $field->getAssociationAdmin()->shouldBeCalled()->willReturn($admin->reveal());
  150. $field->getFieldName()->shouldBeCalled()->willReturn('foo');
  151. $field->getParentAssociationMappings()->shouldBeCalled()->willReturn([]);
  152. $this->builder->add('foo');
  153. try {
  154. $this->adminType->buildForm($this->builder, [
  155. 'sonata_field_description' => $field->reveal(),
  156. 'delete' => false, // not needed
  157. 'property_path' => '[0]', // actual test case
  158. ]);
  159. } catch (NoSuchPropertyException $exception) {
  160. $this->fail($exception->getMessage());
  161. }
  162. }
  163. public function testArrayCollectionNotFound(): void
  164. {
  165. $parentSubject = new \stdClass();
  166. $parentSubject->foo = new ArrayCollection();
  167. $parentAdmin = $this->prophesize(AdminInterface::class);
  168. $parentAdmin->getSubject()->shouldBeCalled()->willReturn($parentSubject);
  169. $parentAdmin->hasSubject()->shouldBeCalled()->willReturn(true);
  170. $parentField = $this->prophesize(FieldDescriptionInterface::class);
  171. $parentField->setAssociationAdmin(Argument::type(AdminInterface::class))->shouldBeCalled();
  172. $parentField->getAdmin()->shouldBeCalled()->willReturn($parentAdmin->reveal());
  173. $modelManager = $this->prophesize(ModelManagerInterface::class);
  174. $foo = new Foo();
  175. $admin = $this->prophesize(AbstractAdmin::class);
  176. $admin->hasParentFieldDescription()->shouldBeCalled()->willReturn(true);
  177. $admin->getParentFieldDescription()->shouldBeCalled()->willReturn($parentField->reveal());
  178. $admin->defineFormBuilder(new AnyValueToken())->shouldBeCalled();
  179. $admin->getModelManager()->shouldBeCalled()->willReturn($modelManager);
  180. $admin->getClass()->shouldBeCalled()->willReturn(Foo::class);
  181. $admin->setSubject($foo)->shouldBeCalled();
  182. $this->adminHelper->addNewInstance($parentSubject, $parentField->reveal())->shouldBeCalled()->willReturn($foo);
  183. $field = $this->prophesize(FieldDescriptionInterface::class);
  184. $field->getAssociationAdmin()->shouldBeCalled()->willReturn($admin->reveal());
  185. $field->getFieldName()->shouldBeCalled()->willReturn('foo');
  186. $field->getParentAssociationMappings()->shouldBeCalled()->willReturn([]);
  187. $this->builder->add('foo');
  188. try {
  189. $this->adminType->buildForm($this->builder, [
  190. 'sonata_field_description' => $field->reveal(),
  191. 'delete' => false, // not needed
  192. 'property_path' => '[0]', // actual test case
  193. ]);
  194. } catch (NoSuchPropertyException $exception) {
  195. $this->fail($exception->getMessage());
  196. }
  197. }
  198. protected function getExtensions()
  199. {
  200. $extensions = parent::getExtensions();
  201. $guesser = $this->prophesize(FormTypeGuesserInterface::class)->reveal();
  202. $extension = new TestExtension($guesser);
  203. $extension->addTypeExtension(new FormTypeFieldExtension([], []));
  204. $extensions[] = $extension;
  205. $extensions[] = new PreloadedExtension([$this->adminType], []);
  206. return $extensions;
  207. }
  208. }