ExplainAdminCommandTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. // Prefer Symfony 2.x interfaces
  97. if (interface_exists('Symfony\Component\Validator\MetadataFactoryInterface')) {
  98. $this->validatorFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface');
  99. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  100. $validator->expects($this->any())->method('getMetadataFactory')->will($this->returnValue($this->validatorFactory));
  101. } else {
  102. $this->validatorFactory = $this->getMock('Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface');
  103. $validator = $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface');
  104. $validator->expects($this->any())->method('getMetadataFor')->will($this->returnValue($this->validatorFactory));
  105. }
  106. // php 5.3 BC
  107. $admin = $this->admin;
  108. $container->expects($this->any())
  109. ->method('get')
  110. ->will($this->returnCallback(function ($id) use ($container, $admin, $validator) {
  111. switch ($id) {
  112. case 'sonata.admin.pool':
  113. $pool = new Pool($container, '', '');
  114. $pool->setAdminServiceIds(array('acme.admin.foo', 'acme.admin.bar'));
  115. return $pool;
  116. case 'validator':
  117. return $validator;
  118. case 'acme.admin.foo':
  119. return $admin;
  120. }
  121. return;
  122. }));
  123. $command->setContainer($container);
  124. $this->application->add($command);
  125. }
  126. public function testExecute()
  127. {
  128. $metadata = $this->getMock('Symfony\Component\Validator\MetadataInterface');
  129. $this->validatorFactory->expects($this->once())
  130. ->method('getMetadataFor')
  131. ->with($this->equalTo('Acme\Entity\Foo'))
  132. ->will($this->returnValue($metadata));
  133. if (class_exists('Symfony\Component\Validator\Mapping\GenericMetadata')) {
  134. $class = 'GenericMetadata';
  135. } else {
  136. // Symfony <2.5 compatibility
  137. $class = 'ElementMetadata';
  138. }
  139. $propertyMetadata = $this->getMockForAbstractClass('Symfony\Component\Validator\Mapping\\'.$class);
  140. $propertyMetadata->constraints = array(new NotNull(), new Length(array('min' => 2, 'max' => 50, 'groups' => array('create', 'edit'))));
  141. $metadata->properties = array('firstName' => $propertyMetadata);
  142. $getterMetadata = $this->getMockForAbstractClass('Symfony\Component\Validator\Mapping\\'.$class);
  143. $getterMetadata->constraints = array(new NotNull(), new Email(array('groups' => array('registration', 'edit'))));
  144. $metadata->getters = array('email' => $getterMetadata);
  145. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  146. $this->admin->expects($this->any())
  147. ->method('getModelManager')
  148. ->will($this->returnValue($modelManager));
  149. // @todo Mock of \Traversable is available since Phpunit 3.8. This should be completed after stable release of Phpunit 3.8.
  150. // @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/103
  151. // $formBuilder = $this->getMock('Symfony\Component\Form\FormBuilderInterface');
  152. //
  153. // $this->admin->expects($this->any())
  154. // ->method('getFormBuilder')
  155. // ->will($this->returnValue($formBuilder));
  156. $datagridBuilder = $this->getMock('\Sonata\AdminBundle\Builder\DatagridBuilderInterface');
  157. $this->admin->expects($this->any())
  158. ->method('getDatagridBuilder')
  159. ->will($this->returnValue($datagridBuilder));
  160. $listBuilder = $this->getMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
  161. $this->admin->expects($this->any())
  162. ->method('getListBuilder')
  163. ->will($this->returnValue($listBuilder));
  164. $command = $this->application->find('sonata:admin:explain');
  165. $commandTester = new CommandTester($command);
  166. $commandTester->execute(array('command' => $command->getName(), 'admin' => 'acme.admin.foo'));
  167. $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());
  168. }
  169. public function testExecuteEmptyValidator()
  170. {
  171. $metadata = $this->getMock('Symfony\Component\Validator\MetadataInterface');
  172. $this->validatorFactory->expects($this->once())
  173. ->method('getMetadataFor')
  174. ->with($this->equalTo('Acme\Entity\Foo'))
  175. ->will($this->returnValue($metadata));
  176. $metadata->properties = array();
  177. $metadata->getters = array();
  178. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  179. $this->admin->expects($this->any())
  180. ->method('getModelManager')
  181. ->will($this->returnValue($modelManager));
  182. // @todo Mock of \Traversable is available since Phpunit 3.8. This should be completed after stable release of Phpunit 3.8.
  183. // @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/103
  184. // $formBuilder = $this->getMock('Symfony\Component\Form\FormBuilderInterface');
  185. //
  186. // $this->admin->expects($this->any())
  187. // ->method('getFormBuilder')
  188. // ->will($this->returnValue($formBuilder));
  189. $datagridBuilder = $this->getMock('\Sonata\AdminBundle\Builder\DatagridBuilderInterface');
  190. $this->admin->expects($this->any())
  191. ->method('getDatagridBuilder')
  192. ->will($this->returnValue($datagridBuilder));
  193. $listBuilder = $this->getMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
  194. $this->admin->expects($this->any())
  195. ->method('getListBuilder')
  196. ->will($this->returnValue($listBuilder));
  197. $command = $this->application->find('sonata:admin:explain');
  198. $commandTester = new CommandTester($command);
  199. $commandTester->execute(array('command' => $command->getName(), 'admin' => 'acme.admin.foo'));
  200. $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());
  201. }
  202. public function testExecuteNonAdminService()
  203. {
  204. try {
  205. $command = $this->application->find('sonata:admin:explain');
  206. $commandTester = new CommandTester($command);
  207. $commandTester->execute(array('command' => $command->getName(), 'admin' => 'nonexistent.service'));
  208. } catch (\RuntimeException $e) {
  209. $this->assertSame('Service "nonexistent.service" is not an admin class', $e->getMessage());
  210. return;
  211. }
  212. $this->fail('An expected exception has not been raised.');
  213. }
  214. }