diff --git a/src/ZfcRbac/Collector/RbacCollector.php b/src/ZfcRbac/Collector/RbacCollector.php index a8ff2c7..ba8f728 100644 --- a/src/ZfcRbac/Collector/RbacCollector.php +++ b/src/ZfcRbac/Collector/RbacCollector.php @@ -29,6 +29,7 @@ use ZendDeveloperTools\Collector\CollectorInterface; use ZfcRbac\Options\ModuleOptions; use ZfcRbac\Service\RoleService; +use ZfcRbac\Exception\InvalidArgumentException; /** * RbacCollector @@ -43,11 +44,6 @@ class RbacCollector implements CollectorInterface, Serializable */ const PRIORITY = -100; - /** - * @var array - */ - protected $collection = []; - /** * @var array */ @@ -184,7 +180,6 @@ private function collectPermissions(RoleInterface $role) if (method_exists($role, 'getPermissions')) { $permissions = $role->getPermissions(); } else { - // Gather the permissions for the given role. We have to use reflection as // the RoleInterface does not have "getPermissions" method $reflectionProperty = new ReflectionProperty($role, 'permissions'); @@ -209,20 +204,19 @@ private function collectPermissions(RoleInterface $role) */ public function getCollection() { - return $this->collection; + return [ + 'guards' => $this->collectedGuards, + 'roles' => $this->collectedRoles, + 'permissions' => $this->collectedPermissions, + 'options' => $this->collectedOptions + ]; } - /** * {@inheritDoc} */ public function serialize() { - return serialize([ - 'guards' => $this->collectedGuards, - 'roles' => $this->collectedRoles, - 'permissions' => $this->collectedPermissions, - 'options' => $this->collectedOptions - ]); + return serialize($this->getCollection()); } /** @@ -230,6 +224,14 @@ public function serialize() */ public function unserialize($serialized) { - $this->collection = unserialize($serialized); + $collection = unserialize($serialized); + if (!is_array($collection)) { + throw new InvalidArgumentException(__METHOD__ . ": Unserialized data should be an array."); + } + $this->collectedGuards = $collection['guards']; + $this->collectedRoles = $collection['roles']; + $this->collectedPermissions = $collection['permissions']; + $this->collectedOptions = $collection['options']; + } } diff --git a/tests/ZfcRbacTest/Collector/RbacCollectorTest.php b/tests/ZfcRbacTest/Collector/RbacCollectorTest.php index ae0c99c..47aa5b2 100644 --- a/tests/ZfcRbacTest/Collector/RbacCollectorTest.php +++ b/tests/ZfcRbacTest/Collector/RbacCollectorTest.php @@ -65,6 +65,7 @@ public function testUnserialize() $unserialized = [ 'guards' => ['foo' => 'bar'], 'roles' => ['foo' => 'bar'], + 'permissions' => ['foo' => 'bar'], 'options' => ['foo' => 'bar'] ]; $serialized = serialize($unserialized); @@ -77,8 +78,21 @@ public function testUnserialize() $this->assertSame(['foo' => 'bar'], $collection['guards']); $this->assertSame(['foo' => 'bar'], $collection['roles']); $this->assertSame(['foo' => 'bar'], $collection['options']); + $this->assertSame(['foo' => 'bar'], $collection['permissions']); } + public function testUnserializeThrowsInvalidArgumentException() + { + $this->setExpectedException('ZfcRbac\Exception\InvalidArgumentException'); + $collector = new RbacCollector(); + $unserialized = 'not_an_array'; + $serialized = serialize($unserialized); + + $collector->unserialize($serialized); + + } + + public function testCollectNothingIfNoApplicationIsSet() { $mvcEvent = new MvcEvent(); @@ -250,9 +264,11 @@ private function collectPermissionsPropertyTestBase(RoleInterface $role) $roleProvider = $this->getMock('ZfcRbac\Role\RoleProviderInterface'); - $roleService = new RoleService($identityProvider, + $roleService = new RoleService( + $identityProvider, $roleProvider, - new RecursiveRoleIteratorStrategy()); + new RecursiveRoleIteratorStrategy() + ); $serviceManager->expects($this->at(0)) ->method('get') @@ -270,5 +286,4 @@ private function collectPermissionsPropertyTestBase(RoleInterface $role) $collector->unserialize($collector->serialize()); return $collector->getCollection(); } - -} \ No newline at end of file +}