Skip to content

Commit 2781bc4

Browse files
committed
Unit tests implementation
1 parent a49aeb0 commit 2781bc4

File tree

7 files changed

+150
-2
lines changed

7 files changed

+150
-2
lines changed

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "phphacks/zend-mvc-acl",
3+
"require": {
4+
"zendframework/zend-mvc": "^3.1"
5+
},
6+
"require-dev": {
7+
"phpunit/phpunit": "^7.2"
8+
},
9+
"autoload": {
10+
"psr-4": {
11+
"Zend\\Mvc\\" : "src"
12+
}
13+
},
14+
"autoload-dev": {
15+
"psr-4": {
16+
"Tests\\" : "tests"
17+
}
18+
},
19+
"authors": [
20+
{
21+
"name": "Lucas A. de Araújo",
22+
"email": "[email protected]"
23+
}
24+
]
25+
}

src/Acl/Auth/Authorizator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ public function authorize(Request $request): bool
5959

6060
/** @var AuthorizatorInterface $authorizator */
6161
$authorizator = $this->serviceManager->get(ServiceEnum::AUTHORIZATOR);
62-
$authorizator->authorize($request);
62+
return $authorizator->authorize($request);
6363
}
6464
}

src/Acl/Custom/AuthorizatorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Zend\Db\Acl\Custom;
3+
namespace Zend\Mvc\Acl\Custom;
44

55
use Zend\Http\Request;
66

tests/Acl/Auth/AuthorizatorTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace Tests\Acl\Auth;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Tests\Shared\Module;
7+
use Tests\Shared\MyAuthorizer;
8+
use Zend\Http\Request;
9+
use Zend\Http\Response;
10+
use Zend\Mvc\Acl\Enum\ServiceEnum;
11+
use Zend\Mvc\Acl\Exceptions\AuthException;
12+
use Zend\Mvc\Application;
13+
use Zend\Mvc\MvcEvent;
14+
use Zend\Mvc\Service\EventManagerFactory;
15+
use Zend\ServiceManager\ServiceManager;
16+
17+
class AuthorizatorTest extends TestCase
18+
{
19+
/**
20+
* @var Request
21+
*/
22+
private $request;
23+
24+
/**
25+
* @var MvcEvent
26+
*/
27+
private $mvcEvent;
28+
29+
/**
30+
* setup
31+
*/
32+
public function setUp(): void
33+
{
34+
$this->request = new Request();
35+
36+
$serviceManager = new ServiceManager(include __DIR__ . '/../../Shared/module.config.php');
37+
$serviceManager->setFactory('EventManager', new EventManagerFactory());
38+
$serviceManager->setService('Request', $this->request);
39+
$serviceManager->setService('Response', new Response());
40+
$serviceManager->setService(ServiceEnum::AUTHORIZATOR, new MyAuthorizer());
41+
42+
$this->mvcEvent = new MvcEvent();
43+
$this->mvcEvent->setApplication(new Application($serviceManager));
44+
}
45+
46+
public function testWhenAnAuthorizedRequestIsMade(): void
47+
{
48+
$success = true;
49+
50+
try
51+
{
52+
$this->request->setUri('/login');
53+
$this->mvcEvent->setRequest($this->request);
54+
55+
$module = new Module();
56+
$module->onDispatch($this->mvcEvent);
57+
}
58+
catch (AuthException $ex)
59+
{
60+
$success = false;
61+
}
62+
63+
$this->assertTrue($success);
64+
}
65+
66+
/**
67+
* @expectedException \Zend\Mvc\Acl\Exceptions\AuthException
68+
*/
69+
public function testWhenAnUnauthorizedRequestIsMade(): void
70+
{
71+
$this->request->setUri('/notlogin');
72+
$this->mvcEvent->setRequest($this->request);
73+
74+
$module = new Module();
75+
$module->onDispatch($this->mvcEvent);
76+
}
77+
}

tests/Shared/Module.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Tests\Shared;
4+
5+
use Zend\Mvc\Acl\Module\AbstractModule;
6+
7+
/**
8+
* Module
9+
*
10+
* @package Tests\Shared
11+
*/
12+
class Module extends AbstractModule
13+
{
14+
/**
15+
* @return array
16+
*/
17+
public function getConfig(): array
18+
{
19+
return include __DIR__ . '/module.config.php';
20+
}
21+
22+
}

tests/Shared/MyAuthorizer.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Tests\Shared;
4+
5+
use Zend\Mvc\Acl\Custom\AuthorizatorInterface;
6+
use Zend\Http\Request;
7+
use Zend\Mvc\Acl\Exceptions\AuthException;
8+
9+
class MyAuthorizer implements AuthorizatorInterface
10+
{
11+
public function authorize(Request $request): bool
12+
{
13+
throw new AuthException('I deny everything, fuck you.');
14+
}
15+
}

tests/Shared/module.config.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
return [
4+
'router' => [
5+
'whitelist' => [
6+
'/login'
7+
]
8+
]
9+
];

0 commit comments

Comments
 (0)