ExplainAdminCommandTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. if (class_exists('Symfony\Component\Validator\Mapping\GenericMetadata')) {
  127. $class = 'GenericMetadata';
  128. } else {
  129. // Symfony <2.5 compatibility
  130. $class = 'ElementMetadata';
  131. }
  132. $propertyMetadata = $this->getMockForAbstractClass('Symfony\Component\Validator\Mapping\\'.$class);
  133. $propertyMetadata->constraints = array(new NotNull(), new Length(array('min' => 2, 'max' => 50, 'groups' => array('create', 'edit'))));
  134. $metadata->properties = array('firstName' => $propertyMetadata);
  135. $getterMetadata = $this->getMockForAbstractClass('Symfony\Component\Validator\Mapping\\'.$class);
  136. $getterMetadata->constraints = array(new NotNull(), new Email(array('groups' => array('registration', 'edit'))));
  137. $metadata->getters = array('email' => $getterMetadata);
  138. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  139. $this->admin->expects($this->any())
  140. ->method('getModelManager')
  141. ->will($this->returnValue($modelManager));
  142. // @todo Mock of \Traversable is available since Phpunit 3.8. This should be completed after stable release of Phpunit 3.8.
  143. // @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/103
  144. // $formBuilder = $this->getMock('Symfony\Component\Form\FormBuilderInterface');
  145. //
  146. // $this->admin->expects($this->any())
  147. // ->method('getFormBuilder')
  148. // ->will($this->returnValue($formBuilder));
  149. $datagridBuilder = $this->getMock('\Sonata\AdminBundle\Builder\DatagridBuilderInterface');
  150. $this->admin->expects($this->any())
  151. ->method('getDatagridBuilder')
  152. ->will($this->returnValue($datagridBuilder));
  153. $listBuilder = $this->getMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
  154. $this->admin->expects($this->any())
  155. ->method('getListBuilder')
  156. ->will($this->returnValue($listBuilder));
  157. $command = $this->application->find('sonata:admin:explain');
  158. $commandTester = new CommandTester($command);
  159. $commandTester->execute(array('command' => $command->getName(), 'admin' => 'acme.admin.foo'));
  160. $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());
  161. }
  162. public function testExecuteEmptyValidator()
  163. {
  164. $metadata = $this->getMock('Symfony\Component\Validator\MetadataInterface');
  165. $this->validatorFactory->expects($this->once())
  166. ->method('getMetadataFor')
  167. ->with($this->equalTo('Acme\Entity\Foo'))
  168. ->will($this->returnValue($metadata));
  169. $metadata->properties = array();
  170. $metadata->getters = array();
  171. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  172. $this->admin->expects($this->any())
  173. ->method('getModelManager')
  174. ->will($this->returnValue($modelManager));
  175. // @todo Mock of \Traversable is available since Phpunit 3.8. This should be completed after stable release of Phpunit 3.8.
  176. // @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/103
  177. // $formBuilder = $this->getMock('Symfony\Component\Form\FormBuilderInterface');
  178. //
  179. // $this->admin->expects($this->any())
  180. // ->method('getFormBuilder')
  181. // ->will($this->returnValue($formBuilder));
  182. $datagridBuilder = $this->getMock('\Sonata\AdminBundle\Builder\DatagridBuilderInterface');
  183. $this->admin->expects($this->any())
  184. ->method('getDatagridBuilder')
  185. ->will($this->returnValue($datagridBuilder));
  186. $listBuilder = $this->getMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
  187. $this->admin->expects($this->any())
  188. ->method('getListBuilder')
  189. ->will($this->returnValue($listBuilder));
  190. $command = $this->application->find('sonata:admin:explain');
  191. $commandTester = new CommandTester($command);
  192. $commandTester->execute(array('command' => $command->getName(), 'admin' => 'acme.admin.foo'));
  193. $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());
  194. }
  195. public function testExecuteNonAdminService()
  196. {
  197. try {
  198. $command = $this->application->find('sonata:admin:explain');
  199. $commandTester = new CommandTester($command);
  200. $commandTester->execute(array('command' => $command->getName(), 'admin' => 'nonexistent.service'));
  201. } catch (\RuntimeException $e) {
  202. $this->assertSame('Service "nonexistent.service" is not an admin class', $e->getMessage());
  203. return;
  204. }
  205. $this->fail('An expected exception has not been raised.');
  206. }
  207. }