Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

Commit

Permalink
Tests execute, composer.json setup, start on rule class
Browse files Browse the repository at this point in the history
  • Loading branch information
machuga committed Nov 2, 2012
1 parent 1255c37 commit 2123561
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
vendor/
26 changes: 26 additions & 0 deletions composer.json
@@ -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/"
}
}
}
18 changes: 18 additions & 0 deletions phpunit.xml
@@ -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>
47 changes: 47 additions & 0 deletions src/Authority/Rule.php
@@ -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;
}

}
45 changes: 45 additions & 0 deletions tests/RuleTest.php
@@ -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);
}
}

0 comments on commit 2123561

Please sign in to comment.