DoctrineAclCacheTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Symfony\Tests\Component\Security\Acl\Domain;
  3. use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
  4. use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
  5. use Symfony\Component\Security\Acl\Domain\PermissionGrantingStrategy;
  6. use Symfony\Component\Security\Acl\Domain\Acl;
  7. use Symfony\Component\Security\Acl\Domain\DoctrineAclCache;
  8. use Doctrine\Common\Cache\ArrayCache;
  9. class DoctrineAclCacheTest extends \PHPUnit_Framework_TestCase
  10. {
  11. protected $permissionGrantingStrategy;
  12. /**
  13. * @expectedException \InvalidArgumentException
  14. * @dataProvider getEmptyValue
  15. */
  16. public function testConstructorDoesNotAcceptEmptyPrefix($empty)
  17. {
  18. new DoctrineAclCache(new ArrayCache(), $this->getPermissionGrantingStrategy(), $empty);
  19. }
  20. public function getEmptyValue()
  21. {
  22. return array(
  23. array(null),
  24. array(false),
  25. array(''),
  26. );
  27. }
  28. public function test()
  29. {
  30. $cache = $this->getCache();
  31. $aclWithParent = $this->getAcl(1);
  32. $acl = $this->getAcl();
  33. $cache->putInCache($aclWithParent);
  34. $cache->putInCache($acl);
  35. $cachedAcl = $cache->getFromCacheByIdentity($acl->getObjectIdentity());
  36. $this->assertEquals($acl->getId(), $cachedAcl->getId());
  37. $this->assertNull($acl->getParentAcl());
  38. $cachedAclWithParent = $cache->getFromCacheByIdentity($aclWithParent->getObjectIdentity());
  39. $this->assertEquals($aclWithParent->getId(), $cachedAclWithParent->getId());
  40. $this->assertNotNull($cachedParentAcl = $cachedAclWithParent->getParentAcl());
  41. $this->assertEquals($aclWithParent->getParentAcl()->getId(), $cachedParentAcl->getId());
  42. }
  43. protected function getAcl($depth = 0)
  44. {
  45. static $id = 1;
  46. $acl = new Acl($id, new ObjectIdentity($id, 'foo'), $this->getPermissionGrantingStrategy(), array(), $depth > 0);
  47. // insert some ACEs
  48. $sid = new UserSecurityIdentity('johannes', 'Foo');
  49. $acl->insertClassAce($sid, 1);
  50. $acl->insertClassFieldAce('foo', $sid, 1);
  51. $acl->insertObjectAce($sid, 1);
  52. $acl->insertObjectFieldAce('foo', $sid, 1);
  53. $id++;
  54. if ($depth > 0) {
  55. $acl->setParentAcl($this->getAcl($depth - 1));
  56. }
  57. return $acl;
  58. }
  59. protected function getPermissionGrantingStrategy()
  60. {
  61. if (null === $this->permissionGrantingStrategy) {
  62. $this->permissionGrantingStrategy = new PermissionGrantingStrategy();
  63. }
  64. return $this->permissionGrantingStrategy;
  65. }
  66. protected function getCache($cacheDriver = null, $prefix = DoctrineAclCache::PREFIX)
  67. {
  68. if (null === $cacheDriver) {
  69. $cacheDriver = new ArrayCache();
  70. }
  71. return new DoctrineAclCache($cacheDriver, $this->getPermissionGrantingStrategy(), $prefix);
  72. }
  73. public function setUp()
  74. {
  75. if (!class_exists('Doctrine\DBAL\DriverManager')) {
  76. $this->markTestSkipped('The Doctrine2 DBAL is required for this test');
  77. }
  78. }
  79. }