ExplainAdminCommandTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. //php 5.3 BC
  106. $admin = $this->admin;
  107. $validatorMetadata = $this->validatorFactory;
  108. $container->expects($this->any())
  109. ->method('get')
  110. ->will($this->returnCallback(function($id) use ($container, $admin, $validatorMetadata) {
  111. switch ($id) {
  112. case 'sonata.admin.pool':
  113. $pool = new Pool($container, '', '');
  114. $pool->setAdminServiceIds(array('acme.admin.foo', 'acme.admin.bar'));
  115. return $pool;
  116. break;
  117. case 'validator.mapping.class_metadata_factory':
  118. return $validatorMetadata;
  119. break;
  120. case 'acme.admin.foo':
  121. return $admin;
  122. break;
  123. }
  124. return null;
  125. }));
  126. $command->setContainer($container);
  127. $this->application->add($command);
  128. }
  129. public function testExecute()
  130. {
  131. $metadata = $this->getMock('Symfony\Component\Validator\MetadataInterface');
  132. $this->validatorFactory->expects($this->once())
  133. ->method('getMetadataFor')
  134. ->with($this->equalTo('Acme\Entity\Foo'))
  135. ->will($this->returnValue($metadata));
  136. $propertyMetadata = $this->getMockForAbstractClass('Symfony\Component\Validator\Mapping\ElementMetadata');
  137. $propertyMetadata->constraints = array(new NotNull(), new Length(array('min' => 2, 'max' => 50, 'groups' => array('create', 'edit'),)));
  138. $metadata->properties = array('firstName' => $propertyMetadata);
  139. $getterMetadata = $this->getMockForAbstractClass('Symfony\Component\Validator\Mapping\ElementMetadata');
  140. $getterMetadata->constraints = array(new NotNull(), new Email(array('groups' => array('registration', 'edit'),)));
  141. $metadata->getters = array('email' => $getterMetadata);
  142. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  143. $this->admin->expects($this->any())
  144. ->method('getModelManager')
  145. ->will($this->returnValue($modelManager));
  146. //@todo Mock of \Traversable is available since Phpunit 3.8. This should be completed after stable release of Phpunit 3.8.
  147. //@see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/103
  148. // $formBuilder = $this->getMock('Symfony\Component\Form\FormBuilderInterface');
  149. //
  150. // $this->admin->expects($this->any())
  151. // ->method('getFormBuilder')
  152. // ->will($this->returnValue($formBuilder));
  153. $datagridBuilder = $this->getMock('\Sonata\AdminBundle\Builder\DatagridBuilderInterface');
  154. $this->admin->expects($this->any())
  155. ->method('getDatagridBuilder')
  156. ->will($this->returnValue($datagridBuilder));
  157. $listBuilder = $this->getMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
  158. $this->admin->expects($this->any())
  159. ->method('getListBuilder')
  160. ->will($this->returnValue($listBuilder));
  161. $command = $this->application->find('sonata:admin:explain');
  162. $commandTester = new CommandTester($command);
  163. $commandTester->execute(array('command' => $command->getName(), 'admin'=>'acme.admin.foo'));
  164. $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());
  165. }
  166. public function testExecuteEmptyValidator()
  167. {
  168. $metadata = $this->getMock('Symfony\Component\Validator\MetadataInterface');
  169. $this->validatorFactory->expects($this->once())
  170. ->method('getMetadataFor')
  171. ->with($this->equalTo('Acme\Entity\Foo'))
  172. ->will($this->returnValue($metadata));
  173. $metadata->properties = array();
  174. $metadata->getters = array();
  175. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  176. $this->admin->expects($this->any())
  177. ->method('getModelManager')
  178. ->will($this->returnValue($modelManager));
  179. //@todo Mock of \Traversable is available since Phpunit 3.8. This should be completed after stable release of Phpunit 3.8.
  180. //@see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/103
  181. // $formBuilder = $this->getMock('Symfony\Component\Form\FormBuilderInterface');
  182. //
  183. // $this->admin->expects($this->any())
  184. // ->method('getFormBuilder')
  185. // ->will($this->returnValue($formBuilder));
  186. $datagridBuilder = $this->getMock('\Sonata\AdminBundle\Builder\DatagridBuilderInterface');
  187. $this->admin->expects($this->any())
  188. ->method('getDatagridBuilder')
  189. ->will($this->returnValue($datagridBuilder));
  190. $listBuilder = $this->getMock('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->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());
  198. }
  199. public function testExecuteNonAdminService()
  200. {
  201. try {
  202. $command = $this->application->find('sonata:admin:explain');
  203. $commandTester = new CommandTester($command);
  204. $commandTester->execute(array('command' => $command->getName(), 'admin'=>'nonexistent.service'));
  205. } catch (\RuntimeException $e) {
  206. $this->assertEquals('Service "nonexistent.service" is not an admin class', $e->getMessage());
  207. return;
  208. }
  209. $this->fail('An expected exception has not been raised.');
  210. }
  211. }