ExplainAdminCommandTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /*
  3. * This file is part of the Sonata 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\Command\ExplainAdminCommand;
  12. use Symfony\Component\Console\Tester\CommandTester;
  13. use Symfony\Component\Console\Application;
  14. use Sonata\AdminBundle\Admin\Pool;
  15. use Sonata\AdminBundle\Route\RouteCollection;
  16. use Symfony\Component\Validator\MetadataFactoryInterface;
  17. use Sonata\AdminBundle\Model\ModelManagerInterface;
  18. use Symfony\Component\Form\FormBuilderInterface;
  19. use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
  20. use Sonata\AdminBundle\Builder\ListBuilderInterface;
  21. use Symfony\Component\Form\FormBuilder;
  22. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  23. use Symfony\Component\Validator\Mapping\ElementMetadata;
  24. use Symfony\Component\Validator\Constraints\NotNull;
  25. use Symfony\Component\Validator\Constraints\Length;
  26. use Symfony\Component\Validator\Constraints\Email;
  27. /**
  28. * @author Andrej Hudec <pulzarraider@gmail.com>
  29. */
  30. class ExplainAdminCommandTest extends \PHPUnit_Framework_TestCase
  31. {
  32. /**
  33. * @var Application
  34. */
  35. private $application;
  36. /**
  37. * @var AdminInterface
  38. */
  39. private $admin;
  40. /**
  41. * @var Symfony\Component\Validator\MetadataFactoryInterface
  42. */
  43. private $validatorFactory;
  44. protected function setUp()
  45. {
  46. $this->application = new Application();
  47. $command = new ExplainAdminCommand();
  48. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  49. $this->admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  50. $this->admin->expects($this->any())
  51. ->method('getCode')
  52. ->will($this->returnValue('foo'));
  53. $this->admin->expects($this->any())
  54. ->method('getClass')
  55. ->will($this->returnValue('Acme\Entity\Foo'));
  56. $this->admin->expects($this->any())
  57. ->method('getBaseControllerName')
  58. ->will($this->returnValue('SonataAdminBundle:CRUD'));
  59. $routeCollection = new RouteCollection('foo', 'fooBar', 'foo-bar', 'SonataAdminBundle:CRUD');
  60. $routeCollection->add('list');
  61. $routeCollection->add('edit');
  62. $this->admin->expects($this->any())
  63. ->method('getRoutes')
  64. ->will($this->returnValue($routeCollection));
  65. $fieldDescription1 = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  66. $fieldDescription1->expects($this->any())
  67. ->method('getType')
  68. ->will($this->returnValue('text'));
  69. $fieldDescription1->expects($this->any())
  70. ->method('getTemplate')
  71. ->will($this->returnValue('SonataAdminBundle:CRUD:foo_text.html.twig'));
  72. $fieldDescription2 = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
  73. $fieldDescription2->expects($this->any())
  74. ->method('getType')
  75. ->will($this->returnValue('datetime'));
  76. $fieldDescription2->expects($this->any())
  77. ->method('getTemplate')
  78. ->will($this->returnValue('SonataAdminBundle:CRUD:bar_datetime.html.twig'));
  79. $this->admin->expects($this->any())
  80. ->method('getListFieldDescriptions')
  81. ->will($this->returnValue(array('fooTextField'=>$fieldDescription1, 'barDateTimeField'=>$fieldDescription2)));
  82. $this->admin->expects($this->any())
  83. ->method('getFilterFieldDescriptions')
  84. ->will($this->returnValue(array('fooTextField'=>$fieldDescription1, 'barDateTimeField'=>$fieldDescription2)));
  85. $this->admin->expects($this->any())
  86. ->method('getFormTheme')
  87. ->will($this->returnValue(array('FooBundle::bar.html.twig')));
  88. $this->admin->expects($this->any())
  89. ->method('getFormFieldDescriptions')
  90. ->will($this->returnValue(array('fooTextField'=>$fieldDescription1, 'barDateTimeField'=>$fieldDescription2)));
  91. $this->admin->expects($this->any())
  92. ->method('isChild')
  93. ->will($this->returnValue(true));
  94. // php 5.3 BC
  95. $adminParent = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  96. $adminParent->expects($this->any())
  97. ->method('getCode')
  98. ->will($this->returnValue('foo_child'));
  99. $this->admin->expects($this->any())
  100. ->method('getParent')
  101. ->will($this->returnCallback(function() use ($adminParent) {
  102. return $adminParent;
  103. }));
  104. $this->validatorFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface');
  105. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  106. $validator->expects($this->any())->method('getMetadataFactory')->will($this->returnValue($this->validatorFactory));
  107. // php 5.3 BC
  108. $admin = $this->admin;
  109. $container->expects($this->any())
  110. ->method('get')
  111. ->will($this->returnCallback(function($id) use ($container, $admin, $validator) {
  112. switch ($id) {
  113. case 'sonata.admin.pool':
  114. $pool = new Pool($container, '', '');
  115. $pool->setAdminServiceIds(array('acme.admin.foo', 'acme.admin.bar'));
  116. return $pool;
  117. case 'validator':
  118. return $validator;
  119. case 'acme.admin.foo':
  120. return $admin;
  121. }
  122. return null;
  123. }));
  124. $command->setContainer($container);
  125. $this->application->add($command);
  126. }
  127. public function testExecute()
  128. {
  129. $metadata = $this->getMock('Symfony\Component\Validator\MetadataInterface');
  130. $this->validatorFactory->expects($this->once())
  131. ->method('getMetadataFor')
  132. ->with($this->equalTo('Acme\Entity\Foo'))
  133. ->will($this->returnValue($metadata));
  134. $propertyMetadata = $this->getMockForAbstractClass('Symfony\Component\Validator\Mapping\ElementMetadata');
  135. $propertyMetadata->constraints = array(new NotNull(), new Length(array('min' => 2, 'max' => 50, 'groups' => array('create', 'edit'),)));
  136. $metadata->properties = array('firstName' => $propertyMetadata);
  137. $getterMetadata = $this->getMockForAbstractClass('Symfony\Component\Validator\Mapping\ElementMetadata');
  138. $getterMetadata->constraints = array(new NotNull(), new Email(array('groups' => array('registration', 'edit'),)));
  139. $metadata->getters = array('email' => $getterMetadata);
  140. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  141. $this->admin->expects($this->any())
  142. ->method('getModelManager')
  143. ->will($this->returnValue($modelManager));
  144. // @todo Mock of \Traversable is available since Phpunit 3.8. This should be completed after stable release of Phpunit 3.8.
  145. // @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/103
  146. // $formBuilder = $this->getMock('Symfony\Component\Form\FormBuilderInterface');
  147. //
  148. // $this->admin->expects($this->any())
  149. // ->method('getFormBuilder')
  150. // ->will($this->returnValue($formBuilder));
  151. $datagridBuilder = $this->getMock('\Sonata\AdminBundle\Builder\DatagridBuilderInterface');
  152. $this->admin->expects($this->any())
  153. ->method('getDatagridBuilder')
  154. ->will($this->returnValue($datagridBuilder));
  155. $listBuilder = $this->getMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
  156. $this->admin->expects($this->any())
  157. ->method('getListBuilder')
  158. ->will($this->returnValue($listBuilder));
  159. $command = $this->application->find('sonata:admin:explain');
  160. $commandTester = new CommandTester($command);
  161. $commandTester->execute(array('command' => $command->getName(), 'admin'=>'acme.admin.foo'));
  162. $this->assertEquals(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());
  163. }
  164. public function testExecuteEmptyValidator()
  165. {
  166. $metadata = $this->getMock('Symfony\Component\Validator\MetadataInterface');
  167. $this->validatorFactory->expects($this->once())
  168. ->method('getMetadataFor')
  169. ->with($this->equalTo('Acme\Entity\Foo'))
  170. ->will($this->returnValue($metadata));
  171. $metadata->properties = array();
  172. $metadata->getters = array();
  173. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  174. $this->admin->expects($this->any())
  175. ->method('getModelManager')
  176. ->will($this->returnValue($modelManager));
  177. // @todo Mock of \Traversable is available since Phpunit 3.8. This should be completed after stable release of Phpunit 3.8.
  178. // @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/103
  179. // $formBuilder = $this->getMock('Symfony\Component\Form\FormBuilderInterface');
  180. //
  181. // $this->admin->expects($this->any())
  182. // ->method('getFormBuilder')
  183. // ->will($this->returnValue($formBuilder));
  184. $datagridBuilder = $this->getMock('\Sonata\AdminBundle\Builder\DatagridBuilderInterface');
  185. $this->admin->expects($this->any())
  186. ->method('getDatagridBuilder')
  187. ->will($this->returnValue($datagridBuilder));
  188. $listBuilder = $this->getMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
  189. $this->admin->expects($this->any())
  190. ->method('getListBuilder')
  191. ->will($this->returnValue($listBuilder));
  192. $command = $this->application->find('sonata:admin:explain');
  193. $commandTester = new CommandTester($command);
  194. $commandTester->execute(array('command' => $command->getName(), 'admin'=>'acme.admin.foo'));
  195. $this->assertEquals(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());
  196. }
  197. public function testExecuteNonAdminService()
  198. {
  199. try {
  200. $command = $this->application->find('sonata:admin:explain');
  201. $commandTester = new CommandTester($command);
  202. $commandTester->execute(array('command' => $command->getName(), 'admin'=>'nonexistent.service'));
  203. } catch (\RuntimeException $e) {
  204. $this->assertEquals('Service "nonexistent.service" is not an admin class', $e->getMessage());
  205. return;
  206. }
  207. $this->fail('An expected exception has not been raised.');
  208. }
  209. }