ExplainAdminCommandTest.php 10 KB

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