SetupAclCommandTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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\SetupAclCommand;
  13. use Sonata\AdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
  14. use Sonata\AdminBundle\Util\AdminAclManipulatorInterface;
  15. use Symfony\Component\Console\Application;
  16. use Symfony\Component\Console\Tester\CommandTester;
  17. /**
  18. * @author Andrej Hudec <pulzarraider@gmail.com>
  19. */
  20. class SetupAclCommandTest extends PHPUnit_Framework_TestCase
  21. {
  22. public function testExecute()
  23. {
  24. $application = new Application();
  25. $command = new SetupAclCommand();
  26. $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
  27. $admin = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
  28. $aclManipulator = $this->createMock('Sonata\AdminBundle\Util\AdminAclManipulatorInterface');
  29. $container->expects($this->any())
  30. ->method('get')
  31. ->will($this->returnCallback(function ($id) use ($container, $admin, $aclManipulator) {
  32. switch ($id) {
  33. case 'sonata.admin.pool':
  34. $pool = new Pool($container, '', '');
  35. $pool->setAdminServiceIds(array('acme.admin.foo'));
  36. return $pool;
  37. case 'sonata.admin.manipulator.acl.admin':
  38. return $aclManipulator;
  39. case 'acme.admin.foo':
  40. return $admin;
  41. }
  42. return;
  43. }));
  44. $command->setContainer($container);
  45. $application->add($command);
  46. $command = $application->find('sonata:admin:setup-acl');
  47. $commandTester = new CommandTester($command);
  48. $commandTester->execute(array('command' => $command->getName()));
  49. $this->assertRegExp('/Starting ACL AdminBundle configuration/', $commandTester->getDisplay());
  50. }
  51. public function testExecuteWithException1()
  52. {
  53. $application = new Application();
  54. $command = new SetupAclCommand();
  55. $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
  56. $container->expects($this->any())
  57. ->method('get')
  58. ->will($this->returnCallback(function ($id) use ($container) {
  59. if ($id == 'sonata.admin.pool') {
  60. $pool = new Pool($container, '', '');
  61. $pool->setAdminServiceIds(array('acme.admin.foo'));
  62. return $pool;
  63. }
  64. throw new \Exception('Foo Exception');
  65. }));
  66. $command->setContainer($container);
  67. $application->add($command);
  68. $command = $application->find('sonata:admin:setup-acl');
  69. $commandTester = new CommandTester($command);
  70. $commandTester->execute(array('command' => $command->getName()));
  71. $this->assertRegExp('@Starting ACL AdminBundle configuration\s+Warning : The admin class cannot be initiated from the command line\s+Foo Exception@', $commandTester->getDisplay());
  72. }
  73. public function testExecuteWithException2()
  74. {
  75. $application = new Application();
  76. $command = new SetupAclCommand();
  77. $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
  78. $admin = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
  79. $container->expects($this->any())
  80. ->method('get')
  81. ->will($this->returnCallback(function ($id) use ($container, $admin) {
  82. switch ($id) {
  83. case 'sonata.admin.pool':
  84. $pool = new Pool($container, '', '');
  85. $pool->setAdminServiceIds(array('acme.admin.foo'));
  86. return $pool;
  87. case 'sonata.admin.manipulator.acl.admin':
  88. return new \stdClass();
  89. case 'acme.admin.foo':
  90. return $admin;
  91. }
  92. return;
  93. }));
  94. $command->setContainer($container);
  95. $application->add($command);
  96. $command = $application->find('sonata:admin:setup-acl');
  97. $commandTester = new CommandTester($command);
  98. $commandTester->execute(array('command' => $command->getName()));
  99. $this->assertRegExp('@Starting ACL AdminBundle configuration\s+The interface "AdminAclManipulatorInterface" is not implemented for stdClass: ignoring@', $commandTester->getDisplay());
  100. }
  101. }