Permalink
Browse files

Updated test suite to utilize PHP 5.3 and namespaces

  • Loading branch information...
1 parent bceac39 commit f2ed57ceb6db3442f13b94df3471264d816e9f84 @weierophinney committed Nov 12, 2009
View
@@ -9,11 +9,11 @@ as well as publish notifications to any topic from any location. As an
example:
// Create logger and subscribe to 'log' topic
- $log = new Zend_Log(new Zend_Log_Writer_Stream('/tmp/app.log'));
- Phly_PubSub::subscribe('log', $log, 'info');
+ $log = new \Zend_Log(new \Zend_Log_Writer_Stream('/tmp/app.log'));
+ \phly\PubSub::subscribe('log', $log, 'info');
// Within some application code:
- Phly_PubSub::publish('log', 'Log message');
+ \phly\PubSub::publish('log', 'Log message');
Phly_PubSub can therefore be used:
@@ -27,7 +27,3 @@ TODO/Further Questions
* Exception handling. Should exceptions raised by subscribers prevent
execution of other subscribers and/or the calling code?
* If not, how should errors be handled/aggregated/reported?
-* Should return values from subscribers be allowed? If so, how would they be
- handled?
- * Use case would be to allow early escape from a method based on the return
- value of a subscriber
@@ -1,15 +1,24 @@
<?php
-function testAutoloader($class)
-{
- $file = str_replace(array('\\', '_'), '/', $class) . '.php';
- return include_once $file;
-}
-
$libDir = dirname(__FILE__) . '/../library';
if (file_exists($libDir) && is_dir($libDir)) {
- set_include_path($libDir . PATH_SEPARATOR . get_include_path());
+ $incPath = get_include_path();
+ if (!strstr($incPath, $libDir)) {
+ set_include_path($libDir . PATH_SEPARATOR . $incPath);
+ }
+ unset($incPath);
}
+unset($libDir);
-spl_autoload_register('testAutoloader');
+spl_autoload_register(function($classname)
+{
+ $namespace = '';
+ $filename = '';
+ if (false !== ($lastNsPos = strripos($classname, '\\'))) {
+ $namespace = substr($classname, 0, $lastNsPos);
+ $classname = substr($classname, $lastNsPos + 1);
+ $filename = str_replace('\\', '/', $namespace) . '/';
+ }
+ $filename .= str_replace('_', '/', $classname) . '.php';
+ return require_once($filename);
+});
-unset($libDir);
@@ -10,18 +10,20 @@
* @license New BSD {@link http://www.opensource.org/licenses/bsd-license.php}
*/
+namespace phlytest;
+
if (!defined('PHPUnit_MAIN_METHOD')) {
- define('PHPUnit_MAIN_METHOD', 'Phly_AllTests::main');
+ define('PHPUnit_MAIN_METHOD', '\phlytest\AllTests::main');
}
/**
* Test helper
*/
require_once dirname(__FILE__) . '/../TestHelper.php';
-require_once 'Phly/PubSubTest.php';
-require_once 'Phly/PubSub/HandleTest.php';
-require_once 'Phly/PubSub/ProviderTest.php';
+require_once 'phlytest/PubSubTest.php';
+require_once 'phlytest/pubsub/HandleTest.php';
+require_once 'phlytest/pubsub/ProviderTest.php';
/**
* @category Phly
@@ -30,25 +32,25 @@
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
* @license New BSD {@link http://www.opensource.org/licenses/bsd-license.php}
*/
-class Phly_AllTests
+class AllTests
{
public static function main()
{
- PHPUnit_TextUI_TestRunner::run(self::suite());
+ \PHPUnit_TextUI_TestRunner::run(self::suite());
}
public static function suite()
{
- $suite = new PHPUnit_Framework_TestSuite('Phly - All Tests');
+ $suite = new \PHPUnit_Framework_TestSuite('Phly - All Tests');
- $suite->addTestSuite('Phly_PubSubTest');
- $suite->addTestSuite('Phly_PubSub_ProviderTest');
- $suite->addTestSuite('Phly_PubSub_HandleTest');
+ $suite->addTestSuite('\phlytest\PubSubTest');
+ $suite->addTestSuite('\phlytest\pubsub\ProviderTest');
+ $suite->addTestSuite('\phlytest\pubsub\HandleTest');
return $suite;
}
}
-if (PHPUnit_MAIN_METHOD == 'Phly_AllTests::main') {
- Phly_AllTests::main();
+if (PHPUnit_MAIN_METHOD == '\phlytest\AllTests::main') {
+ AllTests::main();
}
@@ -10,20 +10,21 @@
* @license New BSD {@link http://www.opensource.org/licenses/bsd-license.php}
*/
-namespace phly\test;
-use \phly\PubSub as PubSub;
-use \phly\pubsub\Handle as Handle;
+namespace phlytest;
-// Call Phly_PubSubTest::main() if this source file is executed directly.
+// Call \phlytest\PubSubTest::main() if this source file is executed directly.
if (!defined("PHPUnit_MAIN_METHOD")) {
- define("PHPUnit_MAIN_METHOD", "PubSubTest::main");
+ define("PHPUnit_MAIN_METHOD", "\phlytest\PubSubTest::main");
}
/**
* Test helper
*/
require_once dirname(__FILE__) . '/../TestHelper.php';
+use phly\PubSub as PubSub;
+use phly\pubsub\Handle as Handle;
+
/**
* @category Phly
* @package Phly_PubSub
@@ -35,7 +36,7 @@ class PubSubTest extends \PHPUnit_Framework_TestCase
{
public static function main()
{
- $suite = new \PHPUnit_Framework_TestSuite("PubSubTest");
+ $suite = new \PHPUnit_Framework_TestSuite(__CLASS__);
$result = \PHPUnit_TextUI_TestRunner::run($suite);
}
@@ -128,7 +129,7 @@ public function handleTestTopic($message)
}
}
-// Call Phly_PubSubTest::main() if this source file is executed directly.
-if (\PHPUnit_MAIN_METHOD == "PubSubTest::main") {
+// Call \phlytest\PubSubTest::main() if this source file is executed directly.
+if (PHPUnit_MAIN_METHOD == "\phlytest\PubSubTest::main") {
PubSubTest::main();
}
@@ -10,20 +10,20 @@
* @license New BSD {@link http://www.opensource.org/licenses/bsd-license.php}
*/
-// Call Phly_PubSub_HandleTest::main() if this source file is executed directly.
+namespace phlytest\pubsub;
+
+// Call \phlytest\pubsub\HandleTest::main() if this source file is executed directly.
if (!defined("PHPUnit_MAIN_METHOD")) {
- define("PHPUnit_MAIN_METHOD", "Phly_PubSub_HandleTest::main");
+ define("PHPUnit_MAIN_METHOD", "\phlytest\pubsub\HandleTest::main");
}
/**
* Test helper
*/
require_once dirname(__FILE__) . '/../../TestHelper.php';
-/**
- * Phly_PubSub_Handle
- */
-require_once 'Phly/PubSub/Handle.php';
+use phly\pubsub\Handle as Handle;
+use \PHPUnit_Framework_TestCase;
/**
* @category Phly
@@ -32,12 +32,12 @@
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
* @license New BSD {@link http://www.opensource.org/licenses/bsd-license.php}
*/
-class Phly_PubSub_HandleTest extends PHPUnit_Framework_TestCase
+class HandleTest extends \PHPUnit_Framework_TestCase
{
public static function main()
{
- $suite = new PHPUnit_Framework_TestSuite("Phly_PubSub_HandleTest");
- $result = PHPUnit_TextUI_TestRunner::run($suite);
+ $suite = new \PHPUnit_Framework_TestSuite(__CLASS__);
+ $result = \PHPUnit_TextUI_TestRunner::run($suite);
}
public function setUp()
@@ -49,41 +49,41 @@ public function setUp()
public function testGetTopicShouldReturnTopic()
{
- $handle = new Phly_PubSub_Handle('foo', 'rand');
+ $handle = new Handle('foo', 'rand');
$this->assertEquals('foo', $handle->getTopic());
}
public function testCallbackShouldBeStringIfNoHandlerPassedToConstructor()
{
- $handle = new Phly_PubSub_Handle('foo', 'rand');
+ $handle = new Handle('foo', 'rand');
$this->assertSame('rand', $handle->getCallback());
}
public function testCallbackShouldBeArrayIfHandlerPassedToConstructor()
{
- $handle = new Phly_PubSub_Handle('foo', 'Phly_PubSub_HandleTest_ObjectCallback', 'test');
- $this->assertSame(array('Phly_PubSub_HandleTest_ObjectCallback', 'test'), $handle->getCallback());
+ $handle = new Handle('foo', '\phlytest\pubsub\HandleTest_ObjectCallback', 'test');
+ $this->assertSame(array('\phlytest\pubsub\HandleTest_ObjectCallback', 'test'), $handle->getCallback());
}
public function testCallShouldInvokeCallbackWithSuppliedArguments()
{
- $handle = new Phly_PubSub_Handle('foo', $this, 'handleCall');
+ $handle = new Handle('foo', $this, 'handleCall');
$args = array('foo', 'bar', 'baz');
$handle->call($args);
$this->assertSame($args, $this->args);
}
/**
- * @expectedException Phly_PubSub_InvalidCallbackException
+ * @expectedException \phly\pubsub\InvalidCallbackException
*/
public function testPassingInvalidCallbackShouldRaiseInvalidCallbackException()
{
- $handle = new Phly_PubSub_Handle('foo', 'boguscallback');
+ $handle = new Handle('foo', 'boguscallback');
}
public function testCallShouldReturnTheReturnValueOfTheCallback()
{
- $handle = new Phly_PubSub_Handle('foo', 'Phly_PubSub_HandleTest_ObjectCallback', 'test');
+ $handle = new Handle('foo', '\phlytest\pubsub\HandleTest_ObjectCallback', 'test');
$this->assertEquals('bar', $handle->call(array()));
}
@@ -93,15 +93,15 @@ public function handleCall()
}
}
-class Phly_PubSub_HandleTest_ObjectCallback
+class HandleTest_ObjectCallback
{
public static function test()
{
return 'bar';
}
}
-// Call Phly_PubSub_HandleTest::main() if this source file is executed directly.
-if (PHPUnit_MAIN_METHOD == "Phly_PubSub_HandleTest::main") {
- Phly_PubSub_HandleTest::main();
+// Call \phlytest\pubsub\HandleTest::main() if this source file is executed directly.
+if (PHPUnit_MAIN_METHOD == "\phlytest\pubsub\HandleTest::main") {
+ HandleTest::main();
}
@@ -10,20 +10,21 @@
* @license New BSD {@link http://www.opensource.org/licenses/bsd-license.php}
*/
-// Call Phly_PubSub_ProviderTest::main() if this source file is executed directly.
+namespace phlytest\pubsub;
+
+// Call \phlytest\pubsub\ProviderTest::main() if this source file is executed directly.
if (!defined("PHPUnit_MAIN_METHOD")) {
- define("PHPUnit_MAIN_METHOD", "Phly_PubSub_ProviderTest::main");
+ define("PHPUnit_MAIN_METHOD", "\phlytest\pubsub\ProviderTest::main");
}
/**
* Test helper
*/
require_once dirname(__FILE__) . '/../../TestHelper.php';
-/**
- * Phly_PubSub
- */
-require_once 'Phly/PubSub/Provider.php';
+use phly\pubsub\Provider as Provider;
+use phly\pubsub\Handle as Handle;
+use \PHPUnit_Framework_TestCase;
/**
* @category Phly
@@ -32,11 +33,11 @@
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
* @license New BSD {@link http://www.opensource.org/licenses/bsd-license.php}
*/
-class Phly_PubSub_ProviderTest extends PHPUnit_Framework_TestCase
+class ProviderTest extends \PHPUnit_Framework_TestCase
{
public static function main()
{
- $suite = new PHPUnit_Framework_TestSuite("Phly_PubSub_ProviderTest");
+ $suite = new \PHPUnit_Framework_TestSuite(__CLASS__);
$result = PHPUnit_TextUI_TestRunner::run($suite);
}
@@ -45,7 +46,7 @@ public function setUp()
if (isset($this->message)) {
unset($this->message);
}
- $this->provider = new Phly_PubSub_Provider;
+ $this->provider = new Provider;
}
public function tearDown()
@@ -55,7 +56,7 @@ public function tearDown()
public function testSubscribeShouldReturnHandle()
{
$handle = $this->provider->subscribe('test', $this, __METHOD__);
- $this->assertTrue($handle instanceof Phly_PubSub_Handle);
+ $this->assertTrue($handle instanceof Handle);
}
public function testSubscribeShouldAddHandleToTopic()
@@ -119,7 +120,7 @@ public function testPublishShouldReturnTheReturnValueOfTheLastInvokedSubscriber(
$this->provider->subscribe('string.transform', 'trim');
$this->provider->subscribe('string.transform', 'str_rot13');
$value = $this->provider->publish('string.transform', ' foo ');
- $this->assertEquals(str_rot13(' foo '), $value);
+ $this->assertEquals(\str_rot13(' foo '), $value);
}
public function testPublishUntilShouldReturnAsSoonAsCallbackReturnsTrue()
@@ -139,7 +140,7 @@ public function testFilterShouldPassReturnValueOfEachSubscriberToNextSubscriber(
$this->provider->subscribe('string.transform', 'trim');
$this->provider->subscribe('string.transform', 'str_rot13');
$value = $this->provider->filter('string.transform', ' foo ');
- $this->assertEquals(str_rot13('foo'), $value);
+ $this->assertEquals(\str_rot13('foo'), $value);
}
public function handleTestTopic($message)
@@ -153,7 +154,7 @@ public function evaluateStringCallback($value)
}
}
-// Call Phly_PubSub_ProviderTest::main() if this source file is executed directly.
-if (PHPUnit_MAIN_METHOD == "Phly_PubSub_ProviderTest::main") {
- Phly_PubSub_ProviderTest::main();
+// Call ProviderTest::main() if this source file is executed directly.
+if (PHPUnit_MAIN_METHOD == "\phlytest\pubsub\ProviderTest::main") {
+ ProviderTest::main();
}

0 comments on commit f2ed57c

Please sign in to comment.