Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Tests for Assertion

  • Loading branch information...
commit 709a94bb6ec44fd599db734734b22f30371b1443 1 parent 766c1e0
@Bukarinov Bukarinov authored
View
19 library/GeometriaLab/Permissions/Assertion/Assertion.php
@@ -2,8 +2,6 @@
namespace GeometriaLab\Permissions\Assertion;
-use GeometriaLab\Model\AbstractModel;
-
class Assertion
{
const DYNAMIC_ASSERT_PREFIX = 'can';
@@ -99,15 +97,6 @@ public function getResource($resource)
*/
public function removeResource($resource)
{
- if (!$this->hasResource($resource)) {
- if ($resource instanceof ResourceInterface) {
- $resourceName = $resource->getName();
- } else {
- $resourceName = $resource;
- }
- throw new Exception\InvalidArgumentException("Resource '$resourceName' not found");
- }
-
$resourceName = $this->getResource($resource)->getName();
unset($this->resources[$resourceName]);
@@ -124,14 +113,10 @@ public function removeResource($resource)
* @param mixed $arg2 [optional]
* @param mixed $argN [optional]
* @return bool
- * @throws Exception\InvalidArgumentException
+ * @throws Exception\RuntimeException
*/
public function assert($resource, $privilege, $arg1 = null, $arg2 = null, $argN = null)
{
- if (!$this->hasResource($resource)) {
- return false;
- }
-
$resource = $this->getResource($resource);
if (in_array($privilege, $resource->getAllowedPrivileges())) {
@@ -140,7 +125,7 @@ public function assert($resource, $privilege, $arg1 = null, $arg2 = null, $argN
$methodName = self::DYNAMIC_ASSERT_PREFIX . ucfirst($privilege);
if (!method_exists($resource, $methodName)) {
- throw new Exception\InvalidArgumentException("No rules for privilege '{$privilege}'");
+ throw new Exception\RuntimeException("No rules for privilege '{$privilege}'");
}
$funcArgs = func_get_args();
View
2  library/GeometriaLab/Permissions/Assertion/Service.php
@@ -76,7 +76,7 @@ public function setConfig($config)
}
/**
- * Get Acl object
+ * Get Assertion object
*
* @return Assertion
*/
View
154 tests/GeometriaLabTest/Permissions/Assertion/AssertionTest.php
@@ -0,0 +1,154 @@
+<?php
+
+namespace GeometriaLabTest\Permissions\Assertion;
+
+use GeometriaLab\Permissions\Assertion\Assertion;
+
+class AssertionTest extends \PHPUnit_Framework_TestCase
+{
+ public function testAddResource()
+ {
+ $assertion = new Assertion();
+ $fooResource = new Sample\Foo('Foo');
+ $assertion->addResource($fooResource);
+
+ $this->assertInstanceOf('\\GeometriaLab\\Permissions\\Assertion\\ResourceInterface', $assertion->getResource('Foo'));
+ $this->assertEquals($fooResource, $assertion->getResource('Foo'));
+ }
+
+ public function testAddResources()
+ {
+ $assertion = new Assertion();
+ $fooResource = new Sample\Foo('Foo');
+ $assertion->addResource($fooResource);
+
+ $barResource = new Sample\Bar('Bar');
+ $assertion->addResource($barResource);
+
+ $expected = array(
+ 'Foo' => $fooResource,
+ 'Bar' => $barResource,
+ );
+
+ $this->assertEquals($expected, $assertion->getResources());
+ }
+
+ public function testAddExistingResource()
+ {
+ $this->setExpectedException('\\GeometriaLab\\Permissions\\Assertion\\Exception\\InvalidArgumentException');
+ $assertion = new Assertion();
+ $fooResource = new Sample\Foo('Foo');
+ $assertion->addResource($fooResource);
+ $assertion->addResource($fooResource);
+ }
+
+ public function testHasResourceByName()
+ {
+ $assertion = new Assertion();
+ $fooResource = new Sample\Foo('Foo');
+ $assertion->addResource($fooResource);
+
+ $this->assertTrue($assertion->hasResource('Foo'));
+ }
+
+ public function testHasResourceByObject()
+ {
+ $assertion = new Assertion();
+ $fooResource = new Sample\Foo('Foo');
+ $assertion->addResource($fooResource);
+
+ $this->assertTrue($assertion->hasResource($fooResource));
+ }
+
+ public function testHasNotExistingResource()
+ {
+ $assertion = new Assertion();
+ $fooResource = new Sample\Foo('Foo');
+ $assertion->addResource($fooResource);
+
+ $this->assertFalse($assertion->hasResource('Bar'));
+ }
+
+ public function testGetResourceByName()
+ {
+ $assertion = new Assertion();
+ $fooResource = new Sample\Foo('Foo');
+ $assertion->addResource($fooResource);
+
+ $this->assertEquals($fooResource, $assertion->getResource('Foo'));
+ }
+
+ public function testGetResourceByObject()
+ {
+ $assertion = new Assertion();
+ $fooResource = new Sample\Foo('Foo');
+ $assertion->addResource($fooResource);
+
+ $this->assertEquals($fooResource, $assertion->getResource($fooResource));
+ }
+
+ public function testGetNotExistingResource()
+ {
+ $this->setExpectedException('\\GeometriaLab\\Permissions\\Assertion\\Exception\\InvalidArgumentException');
+
+ $assertion = new Assertion();
+ $fooResource = new Sample\Foo('Foo');
+ $assertion->addResource($fooResource);
+
+ $assertion->getResource('Bar');
+ }
+
+ public function testRemoveResourceByName()
+ {
+ $assertion = new Assertion();
+ $fooResource = new Sample\Foo('Foo');
+ $assertion->addResource($fooResource);
+
+ $this->assertEquals($fooResource, $assertion->getResource('Foo'));
+
+ $assertion->removeResource('Foo');
+
+ $this->assertFalse($assertion->hasResource('Foo'));
+ $this->assertEmpty($assertion->getResources());
+ }
+
+ public function testAssertWithoutResource()
+ {
+ $this->setExpectedException('\\GeometriaLab\\Permissions\\Assertion\\Exception\\InvalidArgumentException');
+
+ $assertion = new Assertion();
+ $assertion->assert('Bar', 'privilege');
+ }
+
+ public function testAssertWithoutPrivilege()
+ {
+ $this->setExpectedException('\\GeometriaLab\\Permissions\\Assertion\\Exception\\RuntimeException');
+
+ $assertion = new Assertion();
+ $barResource = new Sample\Bar('Bar');
+ $assertion->addResource($barResource);
+
+ $assertion->assert('Bar', 'privilege');
+ }
+
+ public function testAssertWithAllowedPrivilege()
+ {
+ $assertion = new Assertion();
+ $fooResource = new Sample\Foo('Foo');
+ $assertion->addResource($fooResource);
+
+ $this->assertTrue($assertion->assert('Foo', 'allowedForAll'));
+ }
+
+ public function testWithDynamicAssert()
+ {
+ $assertion = new Assertion();
+ $fooResource = new Sample\Foo('Foo');
+ $assertion->addResource($fooResource);
+
+ $obj = new \stdClass();
+ $obj->bar = 'bar';
+
+ $this->assertTrue($assertion->assert('Foo', 'dynamicAssert', $obj, array('bar')));
+ }
+}
View
16 tests/GeometriaLabTest/Permissions/Assertion/ResourceTest.php
@@ -0,0 +1,16 @@
+<?php
+
+namespace GeometriaLabTest\Permissions\Assertion;
+
+use GeometriaLab\Permissions\Assertion\Assertion;
+
+class ResourceTest extends \PHPUnit_Framework_TestCase
+{
+ public function testCreate()
+ {
+ $resource = new Sample\Foo('Foo');
+
+ $this->assertEquals('Foo', $resource->getName());
+ $this->assertEquals(array('allowedForAll'), $resource->getAllowedPrivileges());
+ }
+}
View
10 tests/GeometriaLabTest/Permissions/Assertion/Sample/Bar.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace GeometriaLabTest\Permissions\Assertion\Sample;
+
+use GeometriaLab\Permissions\Assertion\AbstractResource;
+
+class Bar extends AbstractResource
+{
+
+}
View
18 tests/GeometriaLabTest/Permissions/Assertion/Sample/Foo.php
@@ -0,0 +1,18 @@
+<?php
+
+namespace GeometriaLabTest\Permissions\Assertion\Sample;
+
+use GeometriaLab\Permissions\Assertion\Assertion,
+ GeometriaLab\Permissions\Assertion\AbstractResource;
+
+class Foo extends AbstractResource
+{
+ protected $allowedPrivileges = array(
+ 'allowedForAll',
+ );
+
+ public function canDynamicAssert(Assertion $assertion, \stdClass $obj, array $array)
+ {
+ return !empty($obj) && !empty($array);
+ }
+}
View
80 tests/GeometriaLabTest/Permissions/Assertion/ServiceTest.php
@@ -0,0 +1,80 @@
+<?php
+
+namespace GeometriaLabTest\Permissions\Assertion;
+
+use GeometriaLab\Permissions\Assertion\Assertion,
+ GeometriaLab\Permissions\Assertion\Service;
+
+use Zend\ServiceManager\ServiceManager as ZendServiceManager,
+ Zend\Mvc\Service\ServiceManagerConfig as ZendServiceManagerConfig,
+ Zend\Mvc\Router\Http\RouteMatch as ZendRouteMatch;
+
+class ServiceTest extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * @var ZendServiceManager
+ */
+ static private $serviceManager;
+
+ /**
+ * @var Assertion
+ */
+ static private $assertion;
+
+ static public function setUpBeforeClass()
+ {
+ static::$serviceManager = new ZendServiceManager(
+ new ZendServiceManagerConfig(array(
+ 'invokables' => array(
+ 'DispatchListener' => 'Zend\Mvc\DispatchListener',
+ 'Request' => 'Zend\Http\PhpEnvironment\Request',
+ 'Response' => 'Zend\Http\PhpEnvironment\Response',
+ 'RouteListener' => 'Zend\Mvc\RouteListener',
+ 'ViewManager' => 'ZendTest\Mvc\TestAsset\MockViewManager'
+ ),
+ 'factories' => array(
+ 'ServiceListener' => 'Zend\Mvc\Service\ServiceListenerFactory',
+ 'ControllerLoader' => 'Zend\Mvc\Service\ControllerLoaderFactory',
+ 'ControllerPluginManager' => 'Zend\Mvc\Service\ControllerPluginManagerFactory',
+ 'Application' => 'Zend\Mvc\Service\ApplicationFactory',
+ 'HttpRouter' => 'Zend\Mvc\Service\RouterFactory',
+ 'Config' => function($e){
+ return array(
+ 'assertion' => array(
+ 'base_dir' => __DIR__ . '/Sample',
+ '__NAMESPACE__' => 'GeometriaLabTest\Permissions\Assertion\Sample',
+ ),
+ );
+ },
+ ),
+ 'aliases' => array(
+ 'Router' => 'HttpRouter',
+ 'Configuration' => 'Config',
+ ),
+ ))
+ );
+
+ static::$serviceManager->get('Application')->bootstrap();
+ static::$serviceManager->get('Application')->getMvcEvent()->setRouteMatch(new ZendRouteMatch(array(
+ '__NAMESPACE__' => 'Sample',
+ )));
+
+ $serviceFactory = new Service();
+ static::$assertion = $serviceFactory->createService(static::$serviceManager);
+ }
+
+ public function testCreateService()
+ {
+ $this->assertInstanceOf('\\GeometriaLab\\Permissions\\Assertion\\Assertion', static::$assertion);
+ }
+
+ public function testAddResources()
+ {
+ $expected = array(
+ 'Foo' => new Sample\Foo('Foo'),
+ 'Bar' => new Sample\Bar('Bar'),
+ );
+
+ $this->assertEquals($expected, static::$assertion->getResources());
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.