Skip to content

Commit 9d2d63c

Browse files
author
woxxy
committedOct 6, 2012
adding unit testing
1 parent 862357d commit 9d2d63c

File tree

13 files changed

+268
-14
lines changed

13 files changed

+268
-14
lines changed
 

‎.travis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
language: php
2+
3+
php:
4+
- 5.3
5+
- 5.4
6+
7+
notifications:
8+
email:
9+
- woxxy@foolrulez.org
10+
11+
before_script:
12+
- cd tests
13+
14+
script: phpunit --configuration phpunit.xml --coverage-text

‎LICENSE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2012 FoolRulez <support@foolz.us>
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

‎bootstrap.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
/**
4+
* Bootstrap for FuelPHP use only
5+
*/
6+
7+
\Autoloader::add_classes(array(
8+
'Foolz\\Plugin\\Hook' => __DIR__.'/classes/Foolz/Plugin/Hook.php',
9+
'Foolz\\Plugin\\Event' => __DIR__.'/classes/Foolz/Plugin/Event.php',
10+
'Foolz\\Plugin\\Result' => __DIR__.'/classes/Foolz/Plugin/Result.php',
11+
));

‎classes/Foolz/Plugin/Event.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ public function __construct($key)
4343
static::$events[$key][] = $this;
4444
}
4545

46+
/**
47+
* Shorthand for the construct for PHP 5.3 to allow chaining
48+
*
49+
* @param string $key
50+
* @return \Foolz\Plugin\Event
51+
*/
52+
public static function forge($key)
53+
{
54+
return new static($key);
55+
}
56+
4657
/**
4758
* Returns all the events ordered by ascending priority number
4859
*
@@ -64,6 +75,16 @@ public static function getByKey($key)
6475
return $events;
6576
}
6677

78+
/**
79+
* Returns the priority
80+
*
81+
* @return int
82+
*/
83+
public function getPriority()
84+
{
85+
return $this->priority;
86+
}
87+
6788
/**
6889
* Sets the priority
6990
*

‎classes/Foolz/Plugin/Hook.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct($key)
4444
* @param type $key
4545
* @return \Foolz\Plugin\Hook
4646
*/
47-
public function forge($key)
47+
public static function forge($key)
4848
{
4949
return new static($key);
5050
}
@@ -103,9 +103,13 @@ public function execute()
103103

104104
foreach ($events as $event)
105105
{
106-
$closure = $event->getCall();
106+
$call = $event->getCall();
107107

108-
$closure($result);
108+
// users may not set the call, and that would be troubles
109+
if ($call !== null)
110+
{
111+
call_user_func($call, $result);
112+
}
109113
}
110114

111115
return $result;

‎classes/Foolz/Plugin/Loader.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

‎classes/Foolz/Plugin/Result.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,20 @@ class Result
2828
*/
2929
protected $object = null;
3030

31+
/**
32+
* The result
33+
*
34+
* @var mixed
35+
*/
36+
protected $result = null;
37+
3138
/**
3239
* Sets the parameters and in case it's available the object
3340
*
3441
* @param array $params
3542
* @param null|object $object
3643
*/
37-
public function __construct(array $params, $object = null)
44+
public function __construct(array $params = array(), $object = null)
3845
{
3946
$this->params = $this->params_original = $params;
4047
$this->object = $object;
@@ -111,15 +118,15 @@ public function getParam($key, $orig = false)
111118
throw new \OutOfBoundsException;
112119
}
113120

114-
return $this->params_original;
121+
return $this->params_original[$key];
115122
}
116123

117124
if ( ! isset($this->params_original[$key]))
118125
{
119126
throw new \OutOfBoundsException;
120127
}
121128

122-
return $this->params;
129+
return $this->params[$key];
123130
}
124131

125132
/**

‎composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "foolz/plugin",
3+
"type": "library",
4+
"description": "A plugin system for PHP, with cascading events.",
5+
"keywords": ["plugin", "event", "callback"],
6+
"homepage": "http://www.foolz.us",
7+
"license": "Apache-2.0",
8+
"authors": [{"name": "foolz", "email": "support@foolz.us"}],
9+
"support": {
10+
"email": "support@foolz.us",
11+
"irc": "irc://irc.irchighway.net/fooldriver"
12+
},
13+
"require": {
14+
"php": ">=5.3.0"
15+
},
16+
"suggest": {
17+
"fuel/core": "Integrates perfectly as a FuelPHP 1.x package"
18+
},
19+
"autoload": {
20+
"psr-0": {
21+
"Foolz\\Plugin": "classes/"
22+
}
23+
}
24+
}

‎tests/bootstrap.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
ini_set('display_errors', '1');
4+
5+
require_once '../classes/Foolz/Plugin/Event.php';
6+
require_once '../classes/Foolz/Plugin/Hook.php';
7+
require_once '../classes/Foolz/Plugin/Result.php';

‎tests/classes/EventTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Foolz\Plugin\Event as Event;
4+
5+
class EventTest extends PHPUnit_Framework_TestCase
6+
{
7+
public function testEvent()
8+
{
9+
$this->assertEmpty(Event::getByKey('testing'));
10+
11+
$new = new Event('testing');
12+
$retrieved = Event::getByKey('testing');
13+
$this->assertSame($new, $retrieved[0]);
14+
}
15+
16+
public function testSetGetPriority()
17+
{
18+
$new = Event::forge('testing');
19+
$this->assertSame(5, $new->getPriority());
20+
$new->setPriority(8);
21+
$this->assertSame(8, $new->getPriority());
22+
}
23+
24+
public function testSetGetCall()
25+
{
26+
$new = new Event('testing');
27+
$this->assertNull($new->getCall());
28+
29+
$new->setCall(function(){ return 123; });
30+
$call = $new->getCall();
31+
$this->assertSame(123, $call());
32+
}
33+
}

‎tests/classes/HookTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
use Foolz\Plugin\Event as Event;
4+
use Foolz\Plugin\Hook as Hook;
5+
6+
class HookTest extends PHPUnit_Framework_TestCase
7+
{
8+
public function testHook()
9+
{
10+
$ev = new Event('testing');
11+
$ev->setCall(function($result) {
12+
$obj = $result->getObject();
13+
$obj->assertSame(1, 1);
14+
$result->set('success '.$result->getParam('test'));
15+
});
16+
17+
$new = new Hook('testing');
18+
$result = $new->setObject($this)
19+
->setParam('test', 'tester')
20+
->execute();
21+
22+
$this->assertSame('success tester', $result->get());
23+
24+
// poor forge
25+
$result = Hook::forge('testing')
26+
->setObject($this)
27+
->setParams(array('test' => 'tester'))
28+
->execute();
29+
30+
$this->assertSame('success tester', $result->get());
31+
32+
$ev = new Event('hardcore');
33+
$ev->setCall(function($result){
34+
$num = $result->getParam('num');
35+
$num++;
36+
$result->setParam('num', $num);
37+
$result->set($num);
38+
});
39+
40+
$ev = new Event('hardcore');
41+
$ev->setCall(function($result){
42+
$num = $result->getParam('num');
43+
$num++;
44+
$result->setParam('num', $num);
45+
$result->set($num);
46+
});
47+
48+
$result = Hook::forge('hardcore')
49+
->setParam('num', 0)
50+
->execute();
51+
52+
$this->assertSame(2, $result->getParam('num'));
53+
$this->assertSame(2, $result->get());
54+
}
55+
}

‎tests/classes/ResultTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
use Foolz\Plugin\Result as Result;
4+
5+
class ResultTest extends PHPUnit_Framework_TestCase
6+
{
7+
public function testResult()
8+
{
9+
$std = new stdClass();
10+
$new = new Result(array('param1' => 'test'), $std);
11+
12+
$this->assertSame($std, $new->getObject());
13+
}
14+
15+
public function testSetGet()
16+
{
17+
$new = new Result();
18+
$new->set('bla');
19+
$this->assertSame('bla', $new->get());
20+
}
21+
22+
/**
23+
* @expectedException \OutOfBoundsException
24+
*/
25+
public function testSetGetThrowsOutOfBounds()
26+
{
27+
$new = new Result();
28+
$new->getObject();
29+
}
30+
31+
public function testSetGetParam()
32+
{
33+
$arr = array('param1' => 'test', 'param2' => 'testtest');
34+
$new = new Result($arr);
35+
36+
$this->assertSame($arr, $new->getParams());
37+
38+
$this->assertSame('test', $new->getParam('param1'));
39+
40+
$new->setParam('param1', 'test1');
41+
42+
$this->assertSame($arr, $new->getParams(true));
43+
$this->assertSame('test', $new->getParam('param1', true));
44+
$this->assertSame('test1', $new->getParam('param1'));
45+
}
46+
47+
/**
48+
* @expectedException \OutOfBoundsException
49+
*/
50+
public function testGetParamThrowsOutOfBounds()
51+
{
52+
$new = new Result();
53+
$new->getParam('herp');
54+
}
55+
56+
/**
57+
* @expectedException \OutOfBoundsException
58+
*/
59+
public function testGetParamOrigThrowsOutOfBounds()
60+
{
61+
$new = new Result();
62+
$new->getParam('herp', true);
63+
}
64+
}

‎tests/phpunit.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit colors="true" stopOnFailure="false" bootstrap="bootstrap.php">
4+
<testsuites>
5+
<testsuite name="sphinxql">
6+
<directory suffix=".php">classes</directory>
7+
</testsuite>
8+
</testsuites>
9+
</phpunit>

0 commit comments

Comments
 (0)
Please sign in to comment.