ExplainAdminCommandTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 Sonata\AdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
  15. use Symfony\Component\Console\Application;
  16. use Symfony\Component\Console\Tester\CommandTester;
  17. use Symfony\Component\Validator\Constraints\Email;
  18. use Symfony\Component\Validator\Constraints\Length;
  19. use Symfony\Component\Validator\Constraints\NotNull;
  20. /**
  21. * @author Andrej Hudec <pulzarraider@gmail.com>
  22. */
  23. class ExplainAdminCommandTest extends PHPUnit_Framework_TestCase
  24. {
  25. /**
  26. * @var Application
  27. */
  28. private $application;
  29. /**
  30. * @var AdminInterface
  31. */
  32. private $admin;
  33. /**
  34. * @var Symfony\Component\Validator\MetadataFactoryInterface
  35. */
  36. private $validatorFactory;
  37. protected function setUp()
  38. {
  39. $this->application = new Application();
  40. $command = new ExplainAdminCommand();
  41. $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
  42. $this->admin = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
  43. $this->admin->expects($this->any())
  44. ->method('getCode')
  45. ->will($this->returnValue('foo'));
  46. $this->admin->expects($this->any())
  47. ->method('getClass')
  48. ->will($this->returnValue('Acme\Entity\Foo'));
  49. $this->admin->expects($this->any())
  50. ->method('getBaseControllerName')
  51. ->will($this->returnValue('SonataAdminBundle:CRUD'));
  52. $routeCollection = new RouteCollection('foo', 'fooBar', 'foo-bar', 'SonataAdminBundle:CRUD');
  53. $routeCollection->add('list');
  54. $routeCollection->add('edit');
  55. $this->admin->expects($this->any())
  56. ->method('getRoutes')
  57. ->will($this->returnValue($routeCollection));
  58. $fieldDescription1 = $this->createMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  59. $fieldDescription1->expects($this->any())
  60. ->method('getType')
  61. ->will($this->returnValue('text'));
  62. $fieldDescription1->expects($this->any())
  63. ->method('getTemplate')
  64. ->will($this->returnValue('SonataAdminBundle:CRUD:foo_text.html.twig'));
  65. $fieldDescription2 = $this->createMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  66. $fieldDescription2->expects($this->any())
  67. ->method('getType')
  68. ->will($this->returnValue('datetime'));
  69. $fieldDescription2->expects($this->any())
  70. ->method('getTemplate')
  71. ->will($this->returnValue('SonataAdminBundle:CRUD:bar_datetime.html.twig'));
  72. $this->admin->expects($this->any())
  73. ->method('getListFieldDescriptions')
  74. ->will($this->returnValue(array(
  75. 'fooTextField' => $fieldDescription1,
  76. 'barDateTimeField' => $fieldDescription2,
  77. )));
  78. $this->admin->expects($this->any())
  79. ->method('getFilterFieldDescriptions')
  80. ->will($this->returnValue(array(
  81. 'fooTextField' => $fieldDescription1,
  82. 'barDateTimeField' => $fieldDescription2,
  83. )));
  84. $this->admin->expects($this->any())
  85. ->method('getFormTheme')
  86. ->will($this->returnValue(array('FooBundle::bar.html.twig')));
  87. $this->admin->expects($this->any())
  88. ->method('getFormFieldDescriptions')
  89. ->will($this->returnValue(array(
  90. 'fooTextField' => $fieldDescription1,
  91. 'barDateTimeField' => $fieldDescription2,
  92. )));
  93. $this->admin->expects($this->any())
  94. ->method('isChild')
  95. ->will($this->returnValue(true));
  96. // php 5.3 BC
  97. $adminParent = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
  98. $adminParent->expects($this->any())
  99. ->method('getCode')
  100. ->will($this->returnValue('foo_child'));
  101. $this->admin->expects($this->any())
  102. ->method('getParent')
  103. ->will($this->returnCallback(function () use ($adminParent) {
  104. return $adminParent;
  105. }));
  106. if (interface_exists(
  107. 'Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface'
  108. )) { // Prefer Symfony 2.5+ interfaces
  109. $this->validatorFactory = $this->createMock(
  110. 'Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface'
  111. );
  112. $validator = $this->createMock('Symfony\Component\Validator\Validator\ValidatorInterface');
  113. $validator->expects($this->any())->method('getMetadataFor')->will(
  114. $this->returnValue($this->validatorFactory)
  115. );
  116. } else {
  117. $this->validatorFactory = $this->createMock('Symfony\Component\Validator\MetadataFactoryInterface');
  118. $validator = $this->createMock('Symfony\Component\Validator\ValidatorInterface');
  119. $validator->expects($this->any())->method('getMetadataFactory')->will(
  120. $this->returnValue($this->validatorFactory)
  121. );
  122. }
  123. // php 5.3 BC
  124. $admin = $this->admin;
  125. $validatorFactory = $this->validatorFactory;
  126. $container->expects($this->any())
  127. ->method('get')
  128. ->will($this->returnCallback(function ($id) use ($container, $admin, $validator, $validatorFactory) {
  129. switch ($id) {
  130. case 'sonata.admin.pool':
  131. $pool = new Pool($container, '', '');
  132. $pool->setAdminServiceIds(array('acme.admin.foo', 'acme.admin.bar'));
  133. return $pool;
  134. case 'validator.validator_factory':
  135. return $validatorFactory;
  136. case 'validator':
  137. return $validator;
  138. case 'acme.admin.foo':
  139. return $admin;
  140. }
  141. return;
  142. }));
  143. $container->expects($this->any())->method('has')->will($this->returnValue(true));
  144. $command->setContainer($container);
  145. $this->application->add($command);
  146. }
  147. public function testExecute()
  148. {
  149. // NEXT_MAJOR: Remove check, when bumping requirements to SF 2.5+
  150. if (interface_exists('Symfony\Component\Validator\Mapping\MetadataInterface')) { //sf2.5+
  151. $metadata = $this->createMock('Symfony\Component\Validator\Mapping\MetadataInterface');
  152. } else {
  153. $metadata = $this->createMock('Symfony\Component\Validator\MetadataInterface');
  154. }
  155. $this->validatorFactory->expects($this->once())
  156. ->method('getMetadataFor')
  157. ->with($this->equalTo('Acme\Entity\Foo'))
  158. ->will($this->returnValue($metadata));
  159. // NEXT_MAJOR: Remove check, when bumping requirements to SF 2.5+
  160. if (class_exists('Symfony\Component\Validator\Mapping\GenericMetadata')) {
  161. $class = 'GenericMetadata';
  162. } else {
  163. // Symfony <2.5 compatibility
  164. $class = 'ElementMetadata';
  165. }
  166. $propertyMetadata = $this->getMockForAbstractClass('Symfony\Component\Validator\Mapping\\'.$class);
  167. $propertyMetadata->constraints = array(
  168. new NotNull(),
  169. new Length(array('min' => 2, 'max' => 50, 'groups' => array('create', 'edit'))),
  170. );
  171. $metadata->properties = array('firstName' => $propertyMetadata);
  172. $getterMetadata = $this->getMockForAbstractClass('Symfony\Component\Validator\Mapping\\'.$class);
  173. $getterMetadata->constraints = array(
  174. new NotNull(),
  175. new Email(array('groups' => array('registration', 'edit'))),
  176. );
  177. $metadata->getters = array('email' => $getterMetadata);
  178. $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  179. $this->admin->expects($this->any())
  180. ->method('getModelManager')
  181. ->will($this->returnValue($modelManager));
  182. $formBuilder = $this->createMock('Symfony\Component\Form\FormBuilderInterface');
  183. $this->admin->expects($this->any())
  184. ->method('getFormBuilder')
  185. ->will($this->returnValue($formBuilder));
  186. $datagridBuilder = $this->createMock('\Sonata\AdminBundle\Builder\DatagridBuilderInterface');
  187. $this->admin->expects($this->any())
  188. ->method('getDatagridBuilder')
  189. ->will($this->returnValue($datagridBuilder));
  190. $listBuilder = $this->createMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
  191. $this->admin->expects($this->any())
  192. ->method('getListBuilder')
  193. ->will($this->returnValue($listBuilder));
  194. $command = $this->application->find('sonata:admin:explain');
  195. $commandTester = new CommandTester($command);
  196. $commandTester->execute(array('command' => $command->getName(), 'admin' => 'acme.admin.foo'));
  197. $this->assertSame(sprintf(
  198. str_replace("\n", PHP_EOL, file_get_contents(__DIR__.'/../Fixtures/Command/explain_admin.txt')),
  199. get_class($this->admin),
  200. get_class($modelManager),
  201. get_class($formBuilder),
  202. get_class($datagridBuilder),
  203. get_class($listBuilder)
  204. ), $commandTester->getDisplay());
  205. }
  206. public function testExecuteEmptyValidator()
  207. {
  208. if (interface_exists('Symfony\Component\Validator\Mapping\MetadataInterface')) { //sf2.5+
  209. $metadata = $this->createMock('Symfony\Component\Validator\Mapping\MetadataInterface');
  210. } else {
  211. $metadata = $this->createMock('Symfony\Component\Validator\MetadataInterface');
  212. }
  213. $this->validatorFactory->expects($this->once())
  214. ->method('getMetadataFor')
  215. ->with($this->equalTo('Acme\Entity\Foo'))
  216. ->will($this->returnValue($metadata));
  217. $metadata->properties = array();
  218. $metadata->getters = array();
  219. $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  220. $this->admin->expects($this->any())
  221. ->method('getModelManager')
  222. ->will($this->returnValue($modelManager));
  223. $formBuilder = $this->createMock('Symfony\Component\Form\FormBuilderInterface');
  224. $this->admin->expects($this->any())
  225. ->method('getFormBuilder')
  226. ->will($this->returnValue($formBuilder));
  227. $datagridBuilder = $this->createMock('\Sonata\AdminBundle\Builder\DatagridBuilderInterface');
  228. $this->admin->expects($this->any())
  229. ->method('getDatagridBuilder')
  230. ->will($this->returnValue($datagridBuilder));
  231. $listBuilder = $this->createMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
  232. $this->admin->expects($this->any())
  233. ->method('getListBuilder')
  234. ->will($this->returnValue($listBuilder));
  235. $command = $this->application->find('sonata:admin:explain');
  236. $commandTester = new CommandTester($command);
  237. $commandTester->execute(array('command' => $command->getName(), 'admin' => 'acme.admin.foo'));
  238. $this->assertSame(sprintf(
  239. str_replace(
  240. "\n",
  241. PHP_EOL,
  242. file_get_contents(__DIR__.'/../Fixtures/Command/explain_admin_empty_validator.txt')
  243. ),
  244. get_class($this->admin),
  245. get_class($modelManager),
  246. get_class($formBuilder),
  247. get_class($datagridBuilder),
  248. get_class($listBuilder)
  249. ), $commandTester->getDisplay());
  250. }
  251. public function testExecuteNonAdminService()
  252. {
  253. try {
  254. $command = $this->application->find('sonata:admin:explain');
  255. $commandTester = new CommandTester($command);
  256. $commandTester->execute(array('command' => $command->getName(), 'admin' => 'nonexistent.service'));
  257. } catch (\RuntimeException $e) {
  258. $this->assertSame('Service "nonexistent.service" is not an admin class', $e->getMessage());
  259. return;
  260. }
  261. $this->fail('An expected exception has not been raised.');
  262. }
  263. }