ServiceAnalyzerTest.php 3.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace JMS\SecurityExtraBundle\Tests\Analysis;
  3. use Doctrine\Common\Annotations\AnnotationReader;
  4. use JMS\SecurityExtraBundle\Analysis\ServiceAnalyzer;
  5. class ServiceAnalyzerTest extends \PHPUnit_Framework_TestCase
  6. {
  7. /**
  8. * @expectedException \RuntimeException
  9. * @expectedMessage You have overridden a secured method "differentMethodSignature" in "SubService". Please copy over the applicable security metadata, and also add @SatisfiesParentSecurityPolicy.
  10. */
  11. public function testAnalyzeThrowsExceptionWhenSecureMethodIsOverridden()
  12. {
  13. $service = new ServiceAnalyzer('JMS\SecurityExtraBundle\Tests\Fixtures\SubService', new AnnotationReader());
  14. $service->analyze();
  15. }
  16. public function testAnalyzeThrowsNoExceptionWhenAbstractMethodIsNotOverridenInDirectChildClass()
  17. {
  18. $service = new ServiceAnalyzer('JMS\SecurityExtraBundle\Tests\Fixtures\AbstractMethodNotDirectlyOverwrittenInDirectChildService', new AnnotationReader());
  19. $service->analyze();
  20. $methods = $service->getMetadata()->methodMetadata;
  21. $this->assertTrue(isset($methods['abstractMethod']));
  22. $metadata = $methods['abstractMethod'];
  23. $this->assertEquals(array('VIEW'), $metadata->returnPermissions);
  24. }
  25. public function testAnalyzeThrowsNoExceptionWhenSatisfiesParentSecurityPolicyIsDefined()
  26. {
  27. $service = new ServiceAnalyzer('JMS\SecurityExtraBundle\Tests\Fixtures\CorrectSubService', new AnnotationReader());
  28. $service->analyze();
  29. $methods = $service->getMetadata()->methodMetadata;
  30. $this->assertTrue(isset($methods['differentMethodSignature']));
  31. $metadata = $methods['differentMethodSignature'];
  32. $this->assertEquals(array(), $metadata->roles);
  33. $this->assertEquals(array(), $metadata->paramPermissions);
  34. $this->assertEquals(array('VIEW'), $metadata->returnPermissions);
  35. }
  36. public function testAnalyzeWithComplexHierarchy()
  37. {
  38. $service = new ServiceAnalyzer('JMS\SecurityExtraBundle\Tests\Fixtures\ComplexService', new AnnotationReader());
  39. $service->analyze();
  40. $methods = $service->getMetadata()->methodMetadata;
  41. $this->assertTrue(isset($methods['delete'], $methods['retrieve'], $methods['abstractMethod']));
  42. $metadata = $methods['delete'];
  43. $this->assertEquals(array(0 => array('MASTER', 'EDIT'), 2 => array('OWNER')), $metadata->paramPermissions);
  44. $this->assertEquals(array(), $metadata->returnPermissions);
  45. $this->assertEquals(array(), $metadata->roles);
  46. $metadata = $methods['retrieve'];
  47. $this->assertEquals(array('VIEW', 'UNDELETE'), $metadata->returnPermissions);
  48. $this->assertEquals(array(), $metadata->paramPermissions);
  49. $this->assertEquals(array(), $metadata->roles);
  50. $metadata = $methods['abstractMethod'];
  51. $this->assertEquals(array('ROLE_FOO', 'IS_AUTHENTICATED_FULLY'), $metadata->roles);
  52. $this->assertEquals(array(1 => array('FOO')), $metadata->paramPermissions);
  53. $this->assertEquals(array('WOW'), $metadata->returnPermissions);
  54. }
  55. public function testAnalyze()
  56. {
  57. $service = new ServiceAnalyzer('JMS\SecurityExtraBundle\Tests\Fixtures\MainService', new AnnotationReader());
  58. $service->analyze();
  59. $methods = $service->getMetadata()->methodMetadata;
  60. $this->assertTrue(isset($methods['differentMethodSignature']));
  61. $metadata = $methods['differentMethodSignature'];
  62. $this->assertEquals(array(array('EDIT')), $metadata->paramPermissions);
  63. $this->assertEquals(array(), $metadata->returnPermissions);
  64. $this->assertEquals(array(), $metadata->roles);
  65. $this->assertFalse($metadata->isDeclaredOnInterface());
  66. }
  67. }