This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests execute, composer.json setup, start on rule class
- Loading branch information
Showing
5 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "authority", | ||
"description": "An Authorization system for PHP", | ||
"authors": [ | ||
{ | ||
"name": "Matthew Machuga", | ||
"email": "machuga@gmail.com" | ||
}, | ||
{ | ||
"name": "Koen Schmeets", | ||
"email": "hello@koenschmeets.nl" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.3.0" | ||
}, | ||
"require-dev": { | ||
"mockery/mockery": "0.7.*" | ||
}, | ||
"minimum-stability": "dev", | ||
"autoload": { | ||
"psr-0": { | ||
"Authority": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false" | ||
> | ||
<testsuites> | ||
<testsuite name="tests"> | ||
<directory suffix=".php">./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
namespace Authority; | ||
|
||
class Rule { | ||
|
||
protected $action; | ||
protected $behavior; | ||
|
||
public function __construct($behavior, $action, $resource) | ||
{ | ||
$this->behavior = $behavior; | ||
$this->action = $action; | ||
$this->setResource($resource); | ||
} | ||
|
||
public function matchesAction($action) | ||
{ | ||
return $this->action === $action; | ||
} | ||
|
||
public function matchesResource($resource) | ||
{ | ||
$resource = is_object($resource) ? get_class($resource) : $resource; | ||
return $this->resource === $resource; | ||
} | ||
|
||
public function relevant($action, $resource) | ||
{ | ||
return $this->matchesAction($action) && $this->matchesResource($resource); | ||
} | ||
|
||
public function setResource($resource) | ||
{ | ||
$this->resource = is_object($resource) ? get_class($resource) : $resource; | ||
} | ||
|
||
public function getBehavior() | ||
{ | ||
return $this->behavior; | ||
} | ||
|
||
public function getResource() | ||
{ | ||
return $this->resource; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
use Mockery as m; | ||
use Authority\Rule; | ||
|
||
class RuleTest extends PHPUnit_Framework_Testcase | ||
{ | ||
public function setUp() | ||
{ | ||
$this->rule = new Rule(true, 'read', m::mock('Obj')); | ||
} | ||
|
||
public function testCanBeSetToAllowOrDeny() | ||
{ | ||
$allowed_rule = new Rule(true, 'read', m::mock('Obj')); | ||
$denied_rule = new Rule(false, 'write', m::mock('Obj')); | ||
$this->assertTrue($allowed_rule->getBehavior()); | ||
$this->assertFalse($denied_rule->getBehavior()); | ||
} | ||
|
||
public function testCanMatchAction() | ||
{ | ||
$this->assertTrue($this->rule->matchesAction('read')); | ||
$this->assertFalse($this->rule->matchesAction('write')); | ||
} | ||
|
||
public function testCanMatchResource() | ||
{ | ||
$this->assertTrue($this->rule->matchesResource(m::mock('Obj'))); | ||
$this->assertTrue($this->rule->matchesResource('Mockery\\Mock')); | ||
$this->assertFalse($this->rule->matchesResource('Duck')); | ||
} | ||
|
||
public function testCanDetermineRelevance() | ||
{ | ||
$this->assertTrue($this->rule->relevant('read', 'Mockery\\Mock')); | ||
$this->assertFalse($this->rule->relevant('write', 'Mockery\\Mock')); | ||
} | ||
|
||
|
||
public function testThis() | ||
{ | ||
$this->assertTrue(true); | ||
} | ||
} |