SetupAclCommandTest.php 4.8 KB

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