PageRenderTime 92ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 2ms

/vendor/sonata-project/admin-bundle/Tests/Command/ExplainAdminCommandTest.php

https://gitlab.com/cuza/Clinic_Recods
PHP | 296 lines | 190 code | 69 blank | 37 comment | 9 complexity | ec40e8d1c8e9cb0008a5d7aae024e925 MD5 | raw file
  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('fooTextField' => $fieldDescription1, 'barDateTimeField' => $fieldDescription2)));
  74. $this->admin->expects($this->any())
  75. ->method('getFilterFieldDescriptions')
  76. ->will($this->returnValue(array('fooTextField' => $fieldDescription1, 'barDateTimeField' => $fieldDescription2)));
  77. $this->admin->expects($this->any())
  78. ->method('getFormTheme')
  79. ->will($this->returnValue(array('FooBundle::bar.html.twig')));
  80. $this->admin->expects($this->any())
  81. ->method('getFormFieldDescriptions')
  82. ->will($this->returnValue(array('fooTextField' => $fieldDescription1, 'barDateTimeField' => $fieldDescription2)));
  83. $this->admin->expects($this->any())
  84. ->method('isChild')
  85. ->will($this->returnValue(true));
  86. // php 5.3 BC
  87. $adminParent = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  88. $adminParent->expects($this->any())
  89. ->method('getCode')
  90. ->will($this->returnValue('foo_child'));
  91. $this->admin->expects($this->any())
  92. ->method('getParent')
  93. ->will($this->returnCallback(function () use ($adminParent) {
  94. return $adminParent;
  95. }));
  96. if (interface_exists('Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface')) { // Prefer Symfony 2.5+ interfaces
  97. $this->validatorFactory = $this->getMock('Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface');
  98. $validator = $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface');
  99. $validator->expects($this->any())->method('getMetadataFor')->will($this->returnValue($this->validatorFactory));
  100. } else {
  101. $this->validatorFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface');
  102. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  103. $validator->expects($this->any())->method('getMetadataFactory')->will($this->returnValue($this->validatorFactory));
  104. }
  105. // php 5.3 BC
  106. $admin = $this->admin;
  107. $validatorFactory = $this->validatorFactory;
  108. $container->expects($this->any())
  109. ->method('get')
  110. ->will($this->returnCallback(function ($id) use ($container, $admin, $validator, $validatorFactory) {
  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. case 'validator.validator_factory':
  117. return $validatorFactory;
  118. case 'validator':
  119. return $validator;
  120. case 'acme.admin.foo':
  121. return $admin;
  122. }
  123. return;
  124. }));
  125. $container->expects($this->any())->method('has')->will($this->returnValue(true));
  126. $command->setContainer($container);
  127. $this->application->add($command);
  128. }
  129. public function testExecute()
  130. {
  131. if (interface_exists('Symfony\Component\Validator\Mapping\MetadataInterface')) { //sf2.5+
  132. $metadata = $this->getMock('Symfony\Component\Validator\Mapping\MetadataInterface');
  133. } else {
  134. $metadata = $this->getMock('Symfony\Component\Validator\MetadataInterface');
  135. }
  136. $this->validatorFactory->expects($this->once())
  137. ->method('getMetadataFor')
  138. ->with($this->equalTo('Acme\Entity\Foo'))
  139. ->will($this->returnValue($metadata));
  140. if (class_exists('Symfony\Component\Validator\Mapping\GenericMetadata')) {
  141. $class = 'GenericMetadata';
  142. } else {
  143. // Symfony <2.5 compatibility
  144. $class = 'ElementMetadata';
  145. }
  146. $propertyMetadata = $this->getMockForAbstractClass('Symfony\Component\Validator\Mapping\\'.$class);
  147. $propertyMetadata->constraints = array(new NotNull(), new Length(array('min' => 2, 'max' => 50, 'groups' => array('create', 'edit'))));
  148. $metadata->properties = array('firstName' => $propertyMetadata);
  149. $getterMetadata = $this->getMockForAbstractClass('Symfony\Component\Validator\Mapping\\'.$class);
  150. $getterMetadata->constraints = array(new NotNull(), new Email(array('groups' => array('registration', 'edit'))));
  151. $metadata->getters = array('email' => $getterMetadata);
  152. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  153. $this->admin->expects($this->any())
  154. ->method('getModelManager')
  155. ->will($this->returnValue($modelManager));
  156. // @todo Mock of \Traversable is available since Phpunit 3.8. This should be completed after stable release of Phpunit 3.8.
  157. // @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/103
  158. // $formBuilder = $this->getMock('Symfony\Component\Form\FormBuilderInterface');
  159. //
  160. // $this->admin->expects($this->any())
  161. // ->method('getFormBuilder')
  162. // ->will($this->returnValue($formBuilder));
  163. $datagridBuilder = $this->getMock('\Sonata\AdminBundle\Builder\DatagridBuilderInterface');
  164. $this->admin->expects($this->any())
  165. ->method('getDatagridBuilder')
  166. ->will($this->returnValue($datagridBuilder));
  167. $listBuilder = $this->getMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
  168. $this->admin->expects($this->any())
  169. ->method('getListBuilder')
  170. ->will($this->returnValue($listBuilder));
  171. $command = $this->application->find('sonata:admin:explain');
  172. $commandTester = new CommandTester($command);
  173. $commandTester->execute(array('command' => $command->getName(), 'admin' => 'acme.admin.foo'));
  174. $this->assertSame(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());
  175. }
  176. public function testExecuteEmptyValidator()
  177. {
  178. if (interface_exists('Symfony\Component\Validator\Mapping\MetadataInterface')) { //sf2.5+
  179. $metadata = $this->getMock('Symfony\Component\Validator\Mapping\MetadataInterface');
  180. } else {
  181. $metadata = $this->getMock('Symfony\Component\Validator\MetadataInterface');
  182. }
  183. $this->validatorFactory->expects($this->once())
  184. ->method('getMetadataFor')
  185. ->with($this->equalTo('Acme\Entity\Foo'))
  186. ->will($this->returnValue($metadata));
  187. $metadata->properties = array();
  188. $metadata->getters = array();
  189. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  190. $this->admin->expects($this->any())
  191. ->method('getModelManager')
  192. ->will($this->returnValue($modelManager));
  193. // @todo Mock of \Traversable is available since Phpunit 3.8. This should be completed after stable release of Phpunit 3.8.
  194. // @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/103
  195. // $formBuilder = $this->getMock('Symfony\Component\Form\FormBuilderInterface');
  196. //
  197. // $this->admin->expects($this->any())
  198. // ->method('getFormBuilder')
  199. // ->will($this->returnValue($formBuilder));
  200. $datagridBuilder = $this->getMock('\Sonata\AdminBundle\Builder\DatagridBuilderInterface');
  201. $this->admin->expects($this->any())
  202. ->method('getDatagridBuilder')
  203. ->will($this->returnValue($datagridBuilder));
  204. $listBuilder = $this->getMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
  205. $this->admin->expects($this->any())
  206. ->method('getListBuilder')
  207. ->will($this->returnValue($listBuilder));
  208. $command = $this->application->find('sonata:admin:explain');
  209. $commandTester = new CommandTester($command);
  210. $commandTester->execute(array('command' => $command->getName(), 'admin' => 'acme.admin.foo'));
  211. $this->assertSame(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());
  212. }
  213. public function testExecuteNonAdminService()
  214. {
  215. try {
  216. $command = $this->application->find('sonata:admin:explain');
  217. $commandTester = new CommandTester($command);
  218. $commandTester->execute(array('command' => $command->getName(), 'admin' => 'nonexistent.service'));
  219. } catch (\RuntimeException $e) {
  220. $this->assertSame('Service "nonexistent.service" is not an admin class', $e->getMessage());
  221. return;
  222. }
  223. $this->fail('An expected exception has not been raised.');
  224. }
  225. }