Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Tests for Assertion

  • Loading branch information...
commit 709a94bb6ec44fd599db734734b22f30371b1443 1 parent 766c1e0
Nikolay authored December 03, 2012
19  library/GeometriaLab/Permissions/Assertion/Assertion.php
@@ -2,8 +2,6 @@
2 2
 
3 3
 namespace GeometriaLab\Permissions\Assertion;
4 4
 
5  
-use GeometriaLab\Model\AbstractModel;
6  
-
7 5
 class Assertion
8 6
 {
9 7
     const DYNAMIC_ASSERT_PREFIX = 'can';
@@ -99,15 +97,6 @@ public function getResource($resource)
99 97
      */
100 98
     public function removeResource($resource)
101 99
     {
102  
-        if (!$this->hasResource($resource)) {
103  
-            if ($resource instanceof ResourceInterface) {
104  
-                $resourceName = $resource->getName();
105  
-            } else {
106  
-                $resourceName = $resource;
107  
-            }
108  
-            throw new Exception\InvalidArgumentException("Resource '$resourceName' not found");
109  
-        }
110  
-
111 100
         $resourceName = $this->getResource($resource)->getName();
112 101
 
113 102
         unset($this->resources[$resourceName]);
@@ -124,14 +113,10 @@ public function removeResource($resource)
124 113
      * @param mixed $arg2 [optional]
125 114
      * @param mixed $argN [optional]
126 115
      * @return bool
127  
-     * @throws Exception\InvalidArgumentException
  116
+     * @throws Exception\RuntimeException
128 117
      */
129 118
     public function assert($resource, $privilege, $arg1 = null, $arg2 = null, $argN = null)
130 119
     {
131  
-        if (!$this->hasResource($resource)) {
132  
-            return false;
133  
-        }
134  
-
135 120
         $resource = $this->getResource($resource);
136 121
 
137 122
         if (in_array($privilege, $resource->getAllowedPrivileges())) {
@@ -140,7 +125,7 @@ public function assert($resource, $privilege, $arg1 = null, $arg2 = null, $argN
140 125
 
141 126
         $methodName = self::DYNAMIC_ASSERT_PREFIX . ucfirst($privilege);
142 127
         if (!method_exists($resource, $methodName)) {
143  
-            throw new Exception\InvalidArgumentException("No rules for privilege '{$privilege}'");
  128
+            throw new Exception\RuntimeException("No rules for privilege '{$privilege}'");
144 129
         }
145 130
 
146 131
         $funcArgs = func_get_args();
2  library/GeometriaLab/Permissions/Assertion/Service.php
@@ -76,7 +76,7 @@ public function setConfig($config)
76 76
     }
77 77
 
78 78
     /**
79  
-     * Get Acl object
  79
+     * Get Assertion object
80 80
      *
81 81
      * @return Assertion
82 82
      */
154  tests/GeometriaLabTest/Permissions/Assertion/AssertionTest.php
... ...
@@ -0,0 +1,154 @@
  1
+<?php
  2
+
  3
+namespace GeometriaLabTest\Permissions\Assertion;
  4
+
  5
+use GeometriaLab\Permissions\Assertion\Assertion;
  6
+
  7
+class AssertionTest extends \PHPUnit_Framework_TestCase
  8
+{
  9
+    public function testAddResource()
  10
+    {
  11
+        $assertion = new Assertion();
  12
+        $fooResource = new Sample\Foo('Foo');
  13
+        $assertion->addResource($fooResource);
  14
+
  15
+        $this->assertInstanceOf('\\GeometriaLab\\Permissions\\Assertion\\ResourceInterface', $assertion->getResource('Foo'));
  16
+        $this->assertEquals($fooResource, $assertion->getResource('Foo'));
  17
+    }
  18
+
  19
+    public function testAddResources()
  20
+    {
  21
+        $assertion = new Assertion();
  22
+        $fooResource = new Sample\Foo('Foo');
  23
+        $assertion->addResource($fooResource);
  24
+
  25
+        $barResource = new Sample\Bar('Bar');
  26
+        $assertion->addResource($barResource);
  27
+
  28
+        $expected = array(
  29
+            'Foo' => $fooResource,
  30
+            'Bar' => $barResource,
  31
+        );
  32
+
  33
+        $this->assertEquals($expected, $assertion->getResources());
  34
+    }
  35
+
  36
+    public function testAddExistingResource()
  37
+    {
  38
+        $this->setExpectedException('\\GeometriaLab\\Permissions\\Assertion\\Exception\\InvalidArgumentException');
  39
+        $assertion = new Assertion();
  40
+        $fooResource = new Sample\Foo('Foo');
  41
+        $assertion->addResource($fooResource);
  42
+        $assertion->addResource($fooResource);
  43
+    }
  44
+
  45
+    public function testHasResourceByName()
  46
+    {
  47
+        $assertion = new Assertion();
  48
+        $fooResource = new Sample\Foo('Foo');
  49
+        $assertion->addResource($fooResource);
  50
+
  51
+        $this->assertTrue($assertion->hasResource('Foo'));
  52
+    }
  53
+
  54
+    public function testHasResourceByObject()
  55
+    {
  56
+        $assertion = new Assertion();
  57
+        $fooResource = new Sample\Foo('Foo');
  58
+        $assertion->addResource($fooResource);
  59
+
  60
+        $this->assertTrue($assertion->hasResource($fooResource));
  61
+    }
  62
+
  63
+    public function testHasNotExistingResource()
  64
+    {
  65
+        $assertion = new Assertion();
  66
+        $fooResource = new Sample\Foo('Foo');
  67
+        $assertion->addResource($fooResource);
  68
+
  69
+        $this->assertFalse($assertion->hasResource('Bar'));
  70
+    }
  71
+
  72
+    public function testGetResourceByName()
  73
+    {
  74
+        $assertion = new Assertion();
  75
+        $fooResource = new Sample\Foo('Foo');
  76
+        $assertion->addResource($fooResource);
  77
+
  78
+        $this->assertEquals($fooResource, $assertion->getResource('Foo'));
  79
+    }
  80
+
  81
+    public function testGetResourceByObject()
  82
+    {
  83
+        $assertion = new Assertion();
  84
+        $fooResource = new Sample\Foo('Foo');
  85
+        $assertion->addResource($fooResource);
  86
+
  87
+        $this->assertEquals($fooResource, $assertion->getResource($fooResource));
  88
+    }
  89
+
  90
+    public function testGetNotExistingResource()
  91
+    {
  92
+        $this->setExpectedException('\\GeometriaLab\\Permissions\\Assertion\\Exception\\InvalidArgumentException');
  93
+
  94
+        $assertion = new Assertion();
  95
+        $fooResource = new Sample\Foo('Foo');
  96
+        $assertion->addResource($fooResource);
  97
+
  98
+        $assertion->getResource('Bar');
  99
+    }
  100
+
  101
+    public function testRemoveResourceByName()
  102
+    {
  103
+        $assertion = new Assertion();
  104
+        $fooResource = new Sample\Foo('Foo');
  105
+        $assertion->addResource($fooResource);
  106
+
  107
+        $this->assertEquals($fooResource, $assertion->getResource('Foo'));
  108
+
  109
+        $assertion->removeResource('Foo');
  110
+
  111
+        $this->assertFalse($assertion->hasResource('Foo'));
  112
+        $this->assertEmpty($assertion->getResources());
  113
+    }
  114
+
  115
+    public function testAssertWithoutResource()
  116
+    {
  117
+        $this->setExpectedException('\\GeometriaLab\\Permissions\\Assertion\\Exception\\InvalidArgumentException');
  118
+
  119
+        $assertion = new Assertion();
  120
+        $assertion->assert('Bar', 'privilege');
  121
+    }
  122
+
  123
+    public function testAssertWithoutPrivilege()
  124
+    {
  125
+        $this->setExpectedException('\\GeometriaLab\\Permissions\\Assertion\\Exception\\RuntimeException');
  126
+
  127
+        $assertion = new Assertion();
  128
+        $barResource = new Sample\Bar('Bar');
  129
+        $assertion->addResource($barResource);
  130
+
  131
+        $assertion->assert('Bar', 'privilege');
  132
+    }
  133
+
  134
+    public function testAssertWithAllowedPrivilege()
  135
+    {
  136
+        $assertion = new Assertion();
  137
+        $fooResource = new Sample\Foo('Foo');
  138
+        $assertion->addResource($fooResource);
  139
+
  140
+        $this->assertTrue($assertion->assert('Foo', 'allowedForAll'));
  141
+    }
  142
+
  143
+    public function testWithDynamicAssert()
  144
+    {
  145
+        $assertion = new Assertion();
  146
+        $fooResource = new Sample\Foo('Foo');
  147
+        $assertion->addResource($fooResource);
  148
+
  149
+        $obj = new \stdClass();
  150
+        $obj->bar = 'bar';
  151
+
  152
+        $this->assertTrue($assertion->assert('Foo', 'dynamicAssert', $obj, array('bar')));
  153
+    }
  154
+}
16  tests/GeometriaLabTest/Permissions/Assertion/ResourceTest.php
... ...
@@ -0,0 +1,16 @@
  1
+<?php
  2
+
  3
+namespace GeometriaLabTest\Permissions\Assertion;
  4
+
  5
+use GeometriaLab\Permissions\Assertion\Assertion;
  6
+
  7
+class ResourceTest extends \PHPUnit_Framework_TestCase
  8
+{
  9
+    public function testCreate()
  10
+    {
  11
+        $resource = new Sample\Foo('Foo');
  12
+
  13
+        $this->assertEquals('Foo', $resource->getName());
  14
+        $this->assertEquals(array('allowedForAll'), $resource->getAllowedPrivileges());
  15
+    }
  16
+}
10  tests/GeometriaLabTest/Permissions/Assertion/Sample/Bar.php
... ...
@@ -0,0 +1,10 @@
  1
+<?php
  2
+
  3
+namespace GeometriaLabTest\Permissions\Assertion\Sample;
  4
+
  5
+use GeometriaLab\Permissions\Assertion\AbstractResource;
  6
+
  7
+class Bar extends AbstractResource
  8
+{
  9
+
  10
+}
18  tests/GeometriaLabTest/Permissions/Assertion/Sample/Foo.php
... ...
@@ -0,0 +1,18 @@
  1
+<?php
  2
+
  3
+namespace GeometriaLabTest\Permissions\Assertion\Sample;
  4
+
  5
+use GeometriaLab\Permissions\Assertion\Assertion,
  6
+    GeometriaLab\Permissions\Assertion\AbstractResource;
  7
+
  8
+class Foo extends AbstractResource
  9
+{
  10
+    protected $allowedPrivileges = array(
  11
+        'allowedForAll',
  12
+    );
  13
+
  14
+    public function canDynamicAssert(Assertion $assertion, \stdClass $obj, array $array)
  15
+    {
  16
+        return !empty($obj) && !empty($array);
  17
+    }
  18
+}
80  tests/GeometriaLabTest/Permissions/Assertion/ServiceTest.php
... ...
@@ -0,0 +1,80 @@
  1
+<?php
  2
+
  3
+namespace GeometriaLabTest\Permissions\Assertion;
  4
+
  5
+use GeometriaLab\Permissions\Assertion\Assertion,
  6
+    GeometriaLab\Permissions\Assertion\Service;
  7
+
  8
+use Zend\ServiceManager\ServiceManager as ZendServiceManager,
  9
+    Zend\Mvc\Service\ServiceManagerConfig as ZendServiceManagerConfig,
  10
+    Zend\Mvc\Router\Http\RouteMatch as ZendRouteMatch;
  11
+
  12
+class ServiceTest extends \PHPUnit_Framework_TestCase
  13
+{
  14
+    /**
  15
+     * @var ZendServiceManager
  16
+     */
  17
+    static private $serviceManager;
  18
+
  19
+    /**
  20
+     * @var Assertion
  21
+     */
  22
+    static private $assertion;
  23
+
  24
+    static public function setUpBeforeClass()
  25
+    {
  26
+        static::$serviceManager = new ZendServiceManager(
  27
+            new ZendServiceManagerConfig(array(
  28
+                'invokables' => array(
  29
+                    'DispatchListener' => 'Zend\Mvc\DispatchListener',
  30
+                    'Request'          => 'Zend\Http\PhpEnvironment\Request',
  31
+                    'Response'         => 'Zend\Http\PhpEnvironment\Response',
  32
+                    'RouteListener'    => 'Zend\Mvc\RouteListener',
  33
+                    'ViewManager'      => 'ZendTest\Mvc\TestAsset\MockViewManager'
  34
+                ),
  35
+                'factories' => array(
  36
+                    'ServiceListener'           => 'Zend\Mvc\Service\ServiceListenerFactory',
  37
+                    'ControllerLoader'          => 'Zend\Mvc\Service\ControllerLoaderFactory',
  38
+                    'ControllerPluginManager'   => 'Zend\Mvc\Service\ControllerPluginManagerFactory',
  39
+                    'Application'               => 'Zend\Mvc\Service\ApplicationFactory',
  40
+                    'HttpRouter'                => 'Zend\Mvc\Service\RouterFactory',
  41
+                    'Config'                    => function($e){
  42
+                        return array(
  43
+                            'assertion' => array(
  44
+                                'base_dir'      => __DIR__ . '/Sample',
  45
+                                '__NAMESPACE__' => 'GeometriaLabTest\Permissions\Assertion\Sample',
  46
+                            ),
  47
+                        );
  48
+                    },
  49
+                ),
  50
+                'aliases' => array(
  51
+                    'Router'        => 'HttpRouter',
  52
+                    'Configuration' => 'Config',
  53
+                ),
  54
+            ))
  55
+        );
  56
+
  57
+        static::$serviceManager->get('Application')->bootstrap();
  58
+        static::$serviceManager->get('Application')->getMvcEvent()->setRouteMatch(new ZendRouteMatch(array(
  59
+            '__NAMESPACE__' => 'Sample',
  60
+        )));
  61
+
  62
+        $serviceFactory = new Service();
  63
+        static::$assertion = $serviceFactory->createService(static::$serviceManager);
  64
+    }
  65
+
  66
+    public function testCreateService()
  67
+    {
  68
+        $this->assertInstanceOf('\\GeometriaLab\\Permissions\\Assertion\\Assertion', static::$assertion);
  69
+    }
  70
+
  71
+    public function testAddResources()
  72
+    {
  73
+        $expected = array(
  74
+            'Foo' => new Sample\Foo('Foo'),
  75
+            'Bar' => new Sample\Bar('Bar'),
  76
+        );
  77
+
  78
+        $this->assertEquals($expected, static::$assertion->getResources());
  79
+    }
  80
+}

0 notes on commit 709a94b

Please sign in to comment.
Something went wrong with that request. Please try again.