ExplainAdminCommandTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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(
  74. 'fooTextField' => $fieldDescription1,
  75. 'barDateTimeField' => $fieldDescription2,
  76. )));
  77. $this->admin->expects($this->any())
  78. ->method('getFilterFieldDescriptions')
  79. ->will($this->returnValue(array(
  80. 'fooTextField' => $fieldDescription1,
  81. 'barDateTimeField' => $fieldDescription2,
  82. )));
  83. $this->admin->expects($this->any())
  84. ->method('getFormTheme')
  85. ->will($this->returnValue(array('FooBundle::bar.html.twig')));
  86. $this->admin->expects($this->any())
  87. ->method('getFormFieldDescriptions')
  88. ->will($this->returnValue(array(
  89. 'fooTextField' => $fieldDescription1,
  90. 'barDateTimeField' => $fieldDescription2,
  91. )));
  92. $this->admin->expects($this->any())
  93. ->method('isChild')
  94. ->will($this->returnValue(true));
  95. // php 5.3 BC
  96. $adminParent = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  97. $adminParent->expects($this->any())
  98. ->method('getCode')
  99. ->will($this->returnValue('foo_child'));
  100. $this->admin->expects($this->any())
  101. ->method('getParent')
  102. ->will($this->returnCallback(function () use ($adminParent) {
  103. return $adminParent;
  104. }));
  105. if (interface_exists(
  106. 'Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface'
  107. )) { // Prefer Symfony 2.5+ interfaces
  108. $this->validatorFactory = $this->getMock(
  109. 'Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface'
  110. );
  111. $validator = $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface');
  112. $validator->expects($this->any())->method('getMetadataFor')->will(
  113. $this->returnValue($this->validatorFactory)
  114. );
  115. } else {
  116. $this->validatorFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface');
  117. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  118. $validator->expects($this->any())->method('getMetadataFactory')->will(
  119. $this->returnValue($this->validatorFactory)
  120. );
  121. }
  122. // php 5.3 BC
  123. $admin = $this->admin;
  124. $validatorFactory = $this->validatorFactory;
  125. $container->expects($this->any())
  126. ->method('get')
  127. ->will($this->returnCallback(function ($id) use ($container, $admin, $validator, $validatorFactory) {
  128. switch ($id) {
  129. case 'sonata.admin.pool':
  130. $pool = new Pool($container, '', '');
  131. $pool->setAdminServiceIds(array('acme.admin.foo', 'acme.admin.bar'));
  132. return $pool;
  133. case 'validator.validator_factory':
  134. return $validatorFactory;
  135. case 'validator':
  136. return $validator;
  137. case 'acme.admin.foo':
  138. return $admin;
  139. }
  140. return;
  141. }));
  142. $container->expects($this->any())->method('has')->will($this->returnValue(true));
  143. $command->setContainer($container);
  144. $this->application->add($command);
  145. }
  146. public function testExecute()
  147. {
  148. if (interface_exists('Symfony\Component\Validator\Mapping\MetadataInterface')) { //sf2.5+
  149. $metadata = $this->getMock('Symfony\Component\Validator\Mapping\MetadataInterface');
  150. } else {
  151. $metadata = $this->getMock('Symfony\Component\Validator\MetadataInterface');
  152. }
  153. $this->validatorFactory->expects($this->once())
  154. ->method('getMetadataFor')
  155. ->with($this->equalTo('Acme\Entity\Foo'))
  156. ->will($this->returnValue($metadata));
  157. if (class_exists('Symfony\Component\Validator\Mapping\GenericMetadata')) {
  158. $class = 'GenericMetadata';
  159. } else {
  160. // Symfony <2.5 compatibility
  161. $class = 'ElementMetadata';
  162. }
  163. $propertyMetadata = $this->getMockForAbstractClass('Symfony\Component\Validator\Mapping\\'.$class);
  164. $propertyMetadata->constraints = array(
  165. new NotNull(),
  166. new Length(array('min' => 2, 'max' => 50, 'groups' => array('create', 'edit'))),
  167. );
  168. $metadata->properties = array('firstName' => $propertyMetadata);
  169. $getterMetadata = $this->getMockForAbstractClass('Symfony\Component\Validator\Mapping\\'.$class);
  170. $getterMetadata->constraints = array(
  171. new NotNull(),
  172. new Email(array('groups' => array('registration', 'edit'))),
  173. );
  174. $metadata->getters = array('email' => $getterMetadata);
  175. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  176. $this->admin->expects($this->any())
  177. ->method('getModelManager')
  178. ->will($this->returnValue($modelManager));
  179. $formBuilder = $this->getMock('Symfony\Component\Form\FormBuilderInterface');
  180. $this->admin->expects($this->any())
  181. ->method('getFormBuilder')
  182. ->will($this->returnValue($formBuilder));
  183. $datagridBuilder = $this->getMock('\Sonata\AdminBundle\Builder\DatagridBuilderInterface');
  184. $this->admin->expects($this->any())
  185. ->method('getDatagridBuilder')
  186. ->will($this->returnValue($datagridBuilder));
  187. $listBuilder = $this->getMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
  188. $this->admin->expects($this->any())
  189. ->method('getListBuilder')
  190. ->will($this->returnValue($listBuilder));
  191. $command = $this->application->find('sonata:admin:explain');
  192. $commandTester = new CommandTester($command);
  193. $commandTester->execute(array('command' => $command->getName(), 'admin' => 'acme.admin.foo'));
  194. $this->assertSame(sprintf(
  195. str_replace("\n", PHP_EOL, file_get_contents(__DIR__.'/../Fixtures/Command/explain_admin.txt')),
  196. get_class($this->admin),
  197. get_class($modelManager),
  198. get_class($formBuilder),
  199. get_class($datagridBuilder),
  200. get_class($listBuilder)
  201. ), $commandTester->getDisplay());
  202. }
  203. public function testExecuteEmptyValidator()
  204. {
  205. if (interface_exists('Symfony\Component\Validator\Mapping\MetadataInterface')) { //sf2.5+
  206. $metadata = $this->getMock('Symfony\Component\Validator\Mapping\MetadataInterface');
  207. } else {
  208. $metadata = $this->getMock('Symfony\Component\Validator\MetadataInterface');
  209. }
  210. $this->validatorFactory->expects($this->once())
  211. ->method('getMetadataFor')
  212. ->with($this->equalTo('Acme\Entity\Foo'))
  213. ->will($this->returnValue($metadata));
  214. $metadata->properties = array();
  215. $metadata->getters = array();
  216. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  217. $this->admin->expects($this->any())
  218. ->method('getModelManager')
  219. ->will($this->returnValue($modelManager));
  220. $formBuilder = $this->getMock('Symfony\Component\Form\FormBuilderInterface');
  221. $this->admin->expects($this->any())
  222. ->method('getFormBuilder')
  223. ->will($this->returnValue($formBuilder));
  224. $datagridBuilder = $this->getMock('\Sonata\AdminBundle\Builder\DatagridBuilderInterface');
  225. $this->admin->expects($this->any())
  226. ->method('getDatagridBuilder')
  227. ->will($this->returnValue($datagridBuilder));
  228. $listBuilder = $this->getMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
  229. $this->admin->expects($this->any())
  230. ->method('getListBuilder')
  231. ->will($this->returnValue($listBuilder));
  232. $command = $this->application->find('sonata:admin:explain');
  233. $commandTester = new CommandTester($command);
  234. $commandTester->execute(array('command' => $command->getName(), 'admin' => 'acme.admin.foo'));
  235. $this->assertSame(sprintf(
  236. str_replace(
  237. "\n",
  238. PHP_EOL,
  239. file_get_contents(__DIR__.'/../Fixtures/Command/explain_admin_empty_validator.txt')
  240. ),
  241. get_class($this->admin),
  242. get_class($modelManager),
  243. get_class($formBuilder),
  244. get_class($datagridBuilder),
  245. get_class($listBuilder)
  246. ), $commandTester->getDisplay());
  247. }
  248. public function testExecuteNonAdminService()
  249. {
  250. try {
  251. $command = $this->application->find('sonata:admin:explain');
  252. $commandTester = new CommandTester($command);
  253. $commandTester->execute(array('command' => $command->getName(), 'admin' => 'nonexistent.service'));
  254. } catch (\RuntimeException $e) {
  255. $this->assertSame('Service "nonexistent.service" is not an admin class', $e->getMessage());
  256. return;
  257. }
  258. $this->fail('An expected exception has not been raised.');
  259. }
  260. }