Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

* Specs

  • Loading branch information...
commit 078f8855c1d574447f6265141ae916c05486691a 1 parent 3ffd552
@cpojer authored
View
73 Specs/Loader.php
@@ -0,0 +1,73 @@
+<?php
+
+namespace Tests\Loader;
+
+$prefix = __DIR__ . '/../../';
+$cache = $prefix . (file_exists($prefix . 'Cache') ? 'Cache' : 'php-cache') . '/Source/';
+
+require_once $cache . 'Cache.php';
+require_once $cache . 'ICacheBackend.php';
+require_once $cache . 'Backend/File.php';
+
+use Loader\Loader;
+use Cache\Cache;
+
+class LoaderTest extends \PHPUnit_Framework_TestCase {
+
+ public function setUp(){
+ static $id;
+
+ $this->cache = new Cache(__DIR__ . '/LoaderCacheTest/', array(
+ 'prefix' => 'Tests'
+ ));
+
+ $this->loader = Loader::create(($id ? $id++ : ($id = 1)), $this->cache);
+ }
+
+ public function tearDown(){
+ $this->loader->unregister();
+ $this->cache->flush();
+ }
+
+ public function testLoader(){
+ $this->loader->register()->add(__DIR__ . '/loader-test', 'LoaderTest');
+
+ $a = new \LoaderTest\A;
+ $this->assertEquals($a->who(), 'LoaderTest\\A');
+
+ $this->assertFalse($this->loader->classExists('LoaderTest\\B'));
+ $this->assertTrue($this->loader->classExists('LoaderTest\\Subspace\\B'));
+
+ $d = new \LoaderTest\Subspace\D;
+ $this->assertEquals($d->who(), 'LoaderTest\\Subspace\\D');
+ }
+
+ public function testUnregister(){
+ $this->loader->add(__DIR__ . '/loader-test', 'LoaderTest')->unregister();
+
+ // class_exists will attempt to load the class
+ $this->assertFalse(class_exists('LoaderTest\\Subspace\\E'));
+
+ $this->loader->register();
+ $this->assertTrue(class_exists('LoaderTest\\Subspace\\E'));
+
+ $e = new \LoaderTest\Subspace\E;
+ $this->assertEquals($e->who(), 'LoaderTest\\Subspace\\E');
+ }
+
+ public function testAutoNamespace(){
+ $this->loader->add(__DIR__ . '/test2')->unregister();
+ $this->assertFalse($this->loader->classExists('A'));
+ $this->assertTrue($this->loader->classExists('Test2\\A'));
+
+ // class_exists will attempt to load the class
+ $this->assertFalse(class_exists('Test2\\A'));
+
+ $this->loader->register();
+ $this->assertTrue(class_exists('Test2\\A'));
+
+ $b = new \Test2\A;
+ $this->assertEquals($b->who(), 'Test2\\A');
+ }
+
+}
View
57 Specs/RunAll.php
@@ -0,0 +1,57 @@
+<?php
+
+error_reporting(E_ALL | E_STRICT);
+
+require_once 'PHPUnit/Framework.php';
+require_once 'PHPUnit/TextUI/TestRunner.php';
+
+$namespace = trim(file_get_contents(__DIR__ . '/../namespace'));
+
+spl_autoload_register(function($className) use ($namespace){
+ $className = explode('\\', $className);
+ if ($className[0] == $namespace) array_shift($className);
+
+ $file = __DIR__ . '/../Source/' . implode('/', $className) . '.php';
+ if (file_exists($file)) require_once $file;
+});
+
+$$namespace = function() use ($namespace){
+ $tests = array();
+ foreach (glob(__DIR__ . '/*.php') as $file){
+ $name = basename($file, '.php');
+ if ($name == 'RunAll') continue;
+
+ $tests[] = $name;
+ require_once $file;
+ }
+
+ return $tests;
+};
+
+if (!class_exists('RunAll')){
+ class RunAll {
+
+ public static function run(){
+ \PHPUnit_TextUI_TestRunner::run(self::suite());
+ }
+
+ public static function suite(){
+ $namespace = trim(file_get_contents(__DIR__ . '/../namespace'));
+
+ $suite = new \PHPUnit_Framework_TestSuite($namespace . ' Tests');
+
+ global $$namespace;
+ foreach ($$namespace() as $testCase)
+ $suite->addTestSuite('Tests\\' . $namespace . '\\' . $testCase . 'Test');
+
+ return $suite;
+ }
+
+ }
+}
+
+if (!defined('PHPUnit_MAIN_METHOD'))
+ define('PHPUnit_MAIN_METHOD', $namespace);
+
+if (PHPUnit_MAIN_METHOD == $namespace)
+ RunAll::run();
View
9 Specs/Test2/A.php
@@ -0,0 +1,9 @@
+<?php
+
+namespace Test2;
+
+class A {
+
+ public function who(){ return __CLASS__; }
+
+}
View
9 Specs/loader-test/A.php
@@ -0,0 +1,9 @@
+<?php
+
+namespace LoaderTest;
+
+class A {
+
+ public function who(){ return __CLASS__; }
+
+}
View
9 Specs/loader-test/Subspace/B.php
@@ -0,0 +1,9 @@
+<?php
+
+namespace LoaderTest\Subspace;
+
+class B {
+
+ public function who(){ return __CLASS__; }
+
+}
View
9 Specs/loader-test/Subspace/C.php
@@ -0,0 +1,9 @@
+<?php
+
+namespace LoaderTest\Subspace;
+
+class C {
+
+ public function who(){ return __CLASS__; }
+
+}
View
9 Specs/loader-test/Subspace/D.php
@@ -0,0 +1,9 @@
+<?php
+
+namespace LoaderTest\Subspace;
+
+class D extends C {
+
+ public function who(){ return __CLASS__; }
+
+}
View
9 Specs/loader-test/Subspace/E.php
@@ -0,0 +1,9 @@
+<?php
+
+namespace LoaderTest\Subspace;
+
+class E {
+
+ public function who(){ return __CLASS__; }
+
+}
View
2  Specs/run
@@ -0,0 +1,2 @@
+DIRECTORY=$(cd `dirname $0` && pwd)
+phpunit --no-globals-backup "$DIRECTORY/RunAll.php"
View
1  namespace
@@ -0,0 +1 @@
+Loader
Please sign in to comment.
Something went wrong with that request. Please try again.