ExplainAdminCommandTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. // NEXT_MAJOR: Remove check, when bumping requirements to SF 2.5+
  149. if (interface_exists('Symfony\Component\Validator\Mapping\MetadataInterface')) { //sf2.5+
  150. $metadata = $this->getMock('Symfony\Component\Validator\Mapping\MetadataInterface');
  151. } else {
  152. $metadata = $this->getMock('Symfony\Component\Validator\MetadataInterface');
  153. }
  154. $this->validatorFactory->expects($this->once())
  155. ->method('getMetadataFor')
  156. ->with($this->equalTo('Acme\Entity\Foo'))
  157. ->will($this->returnValue($metadata));
  158. // NEXT_MAJOR: Remove check, when bumping requirements to SF 2.5+
  159. if (class_exists('Symfony\Component\Validator\Mapping\GenericMetadata')) {
  160. $class = 'GenericMetadata';
  161. } else {
  162. // Symfony <2.5 compatibility
  163. $class = 'ElementMetadata';
  164. }
  165. $propertyMetadata = $this->getMockForAbstractClass('Symfony\Component\Validator\Mapping\\'.$class);
  166. $propertyMetadata->constraints = array(
  167. new NotNull(),
  168. new Length(array('min' => 2, 'max' => 50, 'groups' => array('create', 'edit'))),
  169. );
  170. $metadata->properties = array('firstName' => $propertyMetadata);
  171. $getterMetadata = $this->getMockForAbstractClass('Symfony\Component\Validator\Mapping\\'.$class);
  172. $getterMetadata->constraints = array(
  173. new NotNull(),
  174. new Email(array('groups' => array('registration', 'edit'))),
  175. );
  176. $metadata->getters = array('email' => $getterMetadata);
  177. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  178. $this->admin->expects($this->any())
  179. ->method('getModelManager')
  180. ->will($this->returnValue($modelManager));
  181. $formBuilder = $this->getMock('Symfony\Component\Form\FormBuilderInterface');
  182. $this->admin->expects($this->any())
  183. ->method('getFormBuilder')
  184. ->will($this->returnValue($formBuilder));
  185. $datagridBuilder = $this->getMock('\Sonata\AdminBundle\Builder\DatagridBuilderInterface');
  186. $this->admin->expects($this->any())
  187. ->method('getDatagridBuilder')
  188. ->will($this->returnValue($datagridBuilder));
  189. $listBuilder = $this->getMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
  190. $this->admin->expects($this->any())
  191. ->method('getListBuilder')
  192. ->will($this->returnValue($listBuilder));
  193. $command = $this->application->find('sonata:admin:explain');
  194. $commandTester = new CommandTester($command);
  195. $commandTester->execute(array('command' => $command->getName(), 'admin' => 'acme.admin.foo'));
  196. $this->assertSame(sprintf(
  197. str_replace("\n", PHP_EOL, file_get_contents(__DIR__.'/../Fixtures/Command/explain_admin.txt')),
  198. get_class($this->admin),
  199. get_class($modelManager),
  200. get_class($formBuilder),
  201. get_class($datagridBuilder),
  202. get_class($listBuilder)
  203. ), $commandTester->getDisplay());
  204. }
  205. public function testExecuteEmptyValidator()
  206. {
  207. if (interface_exists('Symfony\Component\Validator\Mapping\MetadataInterface')) { //sf2.5+
  208. $metadata = $this->getMock('Symfony\Component\Validator\Mapping\MetadataInterface');
  209. } else {
  210. $metadata = $this->getMock('Symfony\Component\Validator\MetadataInterface');
  211. }
  212. $this->validatorFactory->expects($this->once())
  213. ->method('getMetadataFor')
  214. ->with($this->equalTo('Acme\Entity\Foo'))
  215. ->will($this->returnValue($metadata));
  216. $metadata->properties = array();
  217. $metadata->getters = array();
  218. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  219. $this->admin->expects($this->any())
  220. ->method('getModelManager')
  221. ->will($this->returnValue($modelManager));
  222. $formBuilder = $this->getMock('Symfony\Component\Form\FormBuilderInterface');
  223. $this->admin->expects($this->any())
  224. ->method('getFormBuilder')
  225. ->will($this->returnValue($formBuilder));
  226. $datagridBuilder = $this->getMock('\Sonata\AdminBundle\Builder\DatagridBuilderInterface');
  227. $this->admin->expects($this->any())
  228. ->method('getDatagridBuilder')
  229. ->will($this->returnValue($datagridBuilder));
  230. $listBuilder = $this->getMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
  231. $this->admin->expects($this->any())
  232. ->method('getListBuilder')
  233. ->will($this->returnValue($listBuilder));
  234. $command = $this->application->find('sonata:admin:explain');
  235. $commandTester = new CommandTester($command);
  236. $commandTester->execute(array('command' => $command->getName(), 'admin' => 'acme.admin.foo'));
  237. $this->assertSame(sprintf(
  238. str_replace(
  239. "\n",
  240. PHP_EOL,
  241. file_get_contents(__DIR__.'/../Fixtures/Command/explain_admin_empty_validator.txt')
  242. ),
  243. get_class($this->admin),
  244. get_class($modelManager),
  245. get_class($formBuilder),
  246. get_class($datagridBuilder),
  247. get_class($listBuilder)
  248. ), $commandTester->getDisplay());
  249. }
  250. public function testExecuteNonAdminService()
  251. {
  252. try {
  253. $command = $this->application->find('sonata:admin:explain');
  254. $commandTester = new CommandTester($command);
  255. $commandTester->execute(array('command' => $command->getName(), 'admin' => 'nonexistent.service'));
  256. } catch (\RuntimeException $e) {
  257. $this->assertSame('Service "nonexistent.service" is not an admin class', $e->getMessage());
  258. return;
  259. }
  260. $this->fail('An expected exception has not been raised.');
  261. }
  262. }