ExplainAdminCommandTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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\Command;
  11. use Sonata\AdminBundle\Admin\Pool;
  12. use Sonata\AdminBundle\Command\ExplainAdminCommand;
  13. use Sonata\AdminBundle\Route\RouteCollection;
  14. use Symfony\Component\Console\Application;
  15. use Symfony\Component\Console\Tester\CommandTester;
  16. use Symfony\Component\Validator\Constraints\Email;
  17. use Symfony\Component\Validator\Constraints\Length;
  18. use Symfony\Component\Validator\Constraints\NotNull;
  19. /**
  20. * @author Andrej Hudec <pulzarraider@gmail.com>
  21. */
  22. class ExplainAdminCommandTest extends \PHPUnit_Framework_TestCase
  23. {
  24. /**
  25. * @var Application
  26. */
  27. private $application;
  28. /**
  29. * @var AdminInterface
  30. */
  31. private $admin;
  32. /**
  33. * @var Symfony\Component\Validator\MetadataFactoryInterface
  34. */
  35. private $validatorFactory;
  36. protected function setUp()
  37. {
  38. $this->application = new Application();
  39. $command = new ExplainAdminCommand();
  40. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  41. $this->admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  42. $this->admin->expects($this->any())
  43. ->method('getCode')
  44. ->will($this->returnValue('foo'));
  45. $this->admin->expects($this->any())
  46. ->method('getClass')
  47. ->will($this->returnValue('Acme\Entity\Foo'));
  48. $this->admin->expects($this->any())
  49. ->method('getBaseControllerName')
  50. ->will($this->returnValue('SonataAdminBundle:CRUD'));
  51. $routeCollection = new RouteCollection('foo', 'fooBar', 'foo-bar', 'SonataAdminBundle:CRUD');
  52. $routeCollection->add('list');
  53. $routeCollection->add('edit');
  54. $this->admin->expects($this->any())
  55. ->method('getRoutes')
  56. ->will($this->returnValue($routeCollection));
  57. $fieldDescription1 = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  58. $fieldDescription1->expects($this->any())
  59. ->method('getType')
  60. ->will($this->returnValue('text'));
  61. $fieldDescription1->expects($this->any())
  62. ->method('getTemplate')
  63. ->will($this->returnValue('SonataAdminBundle:CRUD:foo_text.html.twig'));
  64. $fieldDescription2 = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  65. $fieldDescription2->expects($this->any())
  66. ->method('getType')
  67. ->will($this->returnValue('datetime'));
  68. $fieldDescription2->expects($this->any())
  69. ->method('getTemplate')
  70. ->will($this->returnValue('SonataAdminBundle:CRUD:bar_datetime.html.twig'));
  71. $this->admin->expects($this->any())
  72. ->method('getListFieldDescriptions')
  73. ->will($this->returnValue(array('fooTextField' => $fieldDescription1, 'barDateTimeField' => $fieldDescription2)));
  74. $this->admin->expects($this->any())
  75. ->method('getFilterFieldDescriptions')
  76. ->will($this->returnValue(array('fooTextField' => $fieldDescription1, 'barDateTimeField' => $fieldDescription2)));
  77. $this->admin->expects($this->any())
  78. ->method('getFormTheme')
  79. ->will($this->returnValue(array('FooBundle::bar.html.twig')));
  80. $this->admin->expects($this->any())
  81. ->method('getFormFieldDescriptions')
  82. ->will($this->returnValue(array('fooTextField' => $fieldDescription1, 'barDateTimeField' => $fieldDescription2)));
  83. $this->admin->expects($this->any())
  84. ->method('isChild')
  85. ->will($this->returnValue(true));
  86. // php 5.3 BC
  87. $adminParent = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  88. $adminParent->expects($this->any())
  89. ->method('getCode')
  90. ->will($this->returnValue('foo_child'));
  91. $this->admin->expects($this->any())
  92. ->method('getParent')
  93. ->will($this->returnCallback(function () use ($adminParent) {
  94. return $adminParent;
  95. }));
  96. $this->validatorFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface');
  97. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  98. $validator->expects($this->any())->method('getMetadataFactory')->will($this->returnValue($this->validatorFactory));
  99. // php 5.3 BC
  100. $admin = $this->admin;
  101. $container->expects($this->any())
  102. ->method('get')
  103. ->will($this->returnCallback(function ($id) use ($container, $admin, $validator) {
  104. switch ($id) {
  105. case 'sonata.admin.pool':
  106. $pool = new Pool($container, '', '');
  107. $pool->setAdminServiceIds(array('acme.admin.foo', 'acme.admin.bar'));
  108. return $pool;
  109. case 'validator':
  110. return $validator;
  111. case 'acme.admin.foo':
  112. return $admin;
  113. }
  114. return;
  115. }));
  116. $command->setContainer($container);
  117. $this->application->add($command);
  118. }
  119. public function testExecute()
  120. {
  121. $metadata = $this->getMock('Symfony\Component\Validator\MetadataInterface');
  122. $this->validatorFactory->expects($this->once())
  123. ->method('getMetadataFor')
  124. ->with($this->equalTo('Acme\Entity\Foo'))
  125. ->will($this->returnValue($metadata));
  126. $propertyMetadata = $this->getMockForAbstractClass('Symfony\Component\Validator\Mapping\ElementMetadata');
  127. $propertyMetadata->constraints = array(new NotNull(), new Length(array('min' => 2, 'max' => 50, 'groups' => array('create', 'edit'))));
  128. $metadata->properties = array('firstName' => $propertyMetadata);
  129. $getterMetadata = $this->getMockForAbstractClass('Symfony\Component\Validator\Mapping\ElementMetadata');
  130. $getterMetadata->constraints = array(new NotNull(), new Email(array('groups' => array('registration', 'edit'))));
  131. $metadata->getters = array('email' => $getterMetadata);
  132. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  133. $this->admin->expects($this->any())
  134. ->method('getModelManager')
  135. ->will($this->returnValue($modelManager));
  136. // @todo Mock of \Traversable is available since Phpunit 3.8. This should be completed after stable release of Phpunit 3.8.
  137. // @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/103
  138. // $formBuilder = $this->getMock('Symfony\Component\Form\FormBuilderInterface');
  139. //
  140. // $this->admin->expects($this->any())
  141. // ->method('getFormBuilder')
  142. // ->will($this->returnValue($formBuilder));
  143. $datagridBuilder = $this->getMock('\Sonata\AdminBundle\Builder\DatagridBuilderInterface');
  144. $this->admin->expects($this->any())
  145. ->method('getDatagridBuilder')
  146. ->will($this->returnValue($datagridBuilder));
  147. $listBuilder = $this->getMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
  148. $this->admin->expects($this->any())
  149. ->method('getListBuilder')
  150. ->will($this->returnValue($listBuilder));
  151. $command = $this->application->find('sonata:admin:explain');
  152. $commandTester = new CommandTester($command);
  153. $commandTester->execute(array('command' => $command->getName(), 'admin' => 'acme.admin.foo'));
  154. $this->assertSame(sprintf(str_replace("\n", PHP_EOL, file_get_contents(__DIR__.'/../Fixtures/Command/explain_admin.txt')), get_class($this->admin), get_class($modelManager), get_class($datagridBuilder), get_class($listBuilder)), $commandTester->getDisplay());
  155. }
  156. public function testExecuteEmptyValidator()
  157. {
  158. $metadata = $this->getMock('Symfony\Component\Validator\MetadataInterface');
  159. $this->validatorFactory->expects($this->once())
  160. ->method('getMetadataFor')
  161. ->with($this->equalTo('Acme\Entity\Foo'))
  162. ->will($this->returnValue($metadata));
  163. $metadata->properties = array();
  164. $metadata->getters = array();
  165. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  166. $this->admin->expects($this->any())
  167. ->method('getModelManager')
  168. ->will($this->returnValue($modelManager));
  169. // @todo Mock of \Traversable is available since Phpunit 3.8. This should be completed after stable release of Phpunit 3.8.
  170. // @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/103
  171. // $formBuilder = $this->getMock('Symfony\Component\Form\FormBuilderInterface');
  172. //
  173. // $this->admin->expects($this->any())
  174. // ->method('getFormBuilder')
  175. // ->will($this->returnValue($formBuilder));
  176. $datagridBuilder = $this->getMock('\Sonata\AdminBundle\Builder\DatagridBuilderInterface');
  177. $this->admin->expects($this->any())
  178. ->method('getDatagridBuilder')
  179. ->will($this->returnValue($datagridBuilder));
  180. $listBuilder = $this->getMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
  181. $this->admin->expects($this->any())
  182. ->method('getListBuilder')
  183. ->will($this->returnValue($listBuilder));
  184. $command = $this->application->find('sonata:admin:explain');
  185. $commandTester = new CommandTester($command);
  186. $commandTester->execute(array('command' => $command->getName(), 'admin' => 'acme.admin.foo'));
  187. $this->assertSame(sprintf(str_replace("\n", PHP_EOL, file_get_contents(__DIR__.'/../Fixtures/Command/explain_admin_empty_validator.txt')), get_class($this->admin), get_class($modelManager), get_class($datagridBuilder), get_class($listBuilder)), $commandTester->getDisplay());
  188. }
  189. public function testExecuteNonAdminService()
  190. {
  191. try {
  192. $command = $this->application->find('sonata:admin:explain');
  193. $commandTester = new CommandTester($command);
  194. $commandTester->execute(array('command' => $command->getName(), 'admin' => 'nonexistent.service'));
  195. } catch (\RuntimeException $e) {
  196. $this->assertSame('Service "nonexistent.service" is not an admin class', $e->getMessage());
  197. return;
  198. }
  199. $this->fail('An expected exception has not been raised.');
  200. }
  201. }