Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding the unit tests for ChdbLoader.

  • Loading branch information...
commit bd15366038933bfce9aa9deb8843aa7615a1b732 1 parent 69c806a
Tomasz Jędrzejewski zyxist authored
Showing with 226 additions and 0 deletions.
  1. +1 −0  tests/TestSuite/AllTests.php
  2. +225 −0 tests/TestSuite/ChdbLoaderTest.php
1  tests/TestSuite/AllTests.php
View
@@ -15,6 +15,7 @@ public static function suite()
$suite = new AllTests('TestSuite');
$suite->addTestSuite('TestSuite\\ApcLoaderTest');
+ $suite->addTestSuite('TestSuite\\ChdbLoaderTest');
$suite->addTestSuite('TestSuite\\ExceptionTest');
$suite->addTestSuite('TestSuite\\GenericLoaderTest');
$suite->addTestSuite('TestSuite\\ClassMapLoaderTest');
225 tests/TestSuite/ChdbLoaderTest.php
View
@@ -0,0 +1,225 @@
+<?php
+/**
+ * Unit tests for Open Power Autoloader
+ *
+ * @author Tomasz "Zyx" Jędrzejewski
+ * @copyright Copyright (c) 2009-2011 Invenzzia Group
+ * @license http://www.invenzzia.org/license/new-bsd New BSD License
+ */
+namespace TestSuite;
+use Opl\Autoloader\ChdbLoader;
+
+/**
+ * @covers \Opl\Autoloader\ChdbLoader
+ * @runTestsInSeparateProcesses
+ */
+class ChdbLoaderTest extends \PHPUnit_Framework_TestCase
+{
+ public function setUp()
+ {
+ if(!extension_loaded('chdb'))
+ {
+ $this->markTestSkipped('chdb extension is not installed.');
+ }
+ } // end setUp();
+
+ public function testLoaderInitialization()
+ {
+ $loader = new ChdbLoader('./data/', './data/classMap.chdb');
+ $this->assertEquals('./data/', $loader->getDefaultPath());
+ } // end testLoaderInitialization();
+
+ /**
+ * @expectedException RuntimeException
+ */
+ public function testConstructorThrowsExceptionIfFileDoesNotExist()
+ {
+ $loader = new ChdbLoader('./data/', './data/not_exist.chdb');
+ } // end testLoaderInitialization();
+
+ /**
+ * @expectedException RuntimeException
+ */
+ public function testConstructorThrowsExceptionIfMapIsInvalid()
+ {
+ $loader = new ChdbLoader('./data/', './data/invalid_map.chdb');
+ } // end testLoaderInitialization();
+
+ public function testGetClassMapLocationReturnsTheRequestedData()
+ {
+ $loader = new ChdbLoader('./data/', './data/classMap.chdb');
+ $this->assertEquals('./data/classMap.chdb', $loader->getClassMapLocation());
+ } // end testGetClassMapLocationReturnsTheRequestedData();
+
+ /**
+ * @depends testLoaderInitialization
+ */
+ public function testConstructorAppendsSlash()
+ {
+ $loader = new ChdbLoader('./foo/bar', './data/classMap.chdb');
+ $this->assertEquals('./foo/bar/', $loader->getDefaultPath());
+ } // end testConstructorAppendsSlash();
+
+ /**
+ * @depends testLoaderInitialization
+ */
+ public function testConstructorAppendsSlashToEmptyString()
+ {
+ $loader = new ChdbLoader('', './data/classMap.chdb');
+ $this->assertEquals('/', $loader->getDefaultPath());
+ } // end testConstructorAppendsSlash();
+
+ public function testSetDefaultPath()
+ {
+ $loader = new ChdbLoader('./foo/bar/', './data/classMap.chdb');
+ $this->assertEquals('./foo/bar/', $loader->getDefaultPath());
+
+ $loader->setDefaultPath('./bar/joe/');
+ $this->assertEquals('./bar/joe/', $loader->getDefaultPath());
+ } // end testSetDefaultPath();
+
+ /**
+ * @depends testSetDefaultPath
+ */
+ public function testSetDefaultPathAppendsSlash()
+ {
+ $loader = new ChdbLoader('./foo/bar/', './data/classMap.chdb');
+ $loader->setDefaultPath('./bar/joe');
+ $this->assertEquals('./bar/joe/', $loader->getDefaultPath());
+ } // end testSetDefaultPathAppendsSlash();
+
+ public function testAddingNamespace()
+ {
+ $loader = new ChdbLoader('./foo/bar/', './data/classMap.chdb');
+
+ $this->assertFalse($loader->hasNamespace('Foo'));
+ $this->assertFalse($loader->hasNamespace('Bar'));
+
+ $loader->addNamespace('Foo');
+
+ $this->assertTrue($loader->hasNamespace('Foo'));
+ $this->assertFalse($loader->hasNamespace('Bar'));
+ } // end testAddingNamespace();
+
+ public function testAddNamespaceSetsDefaultPath()
+ {
+ $loader = new ChdbLoader('./foo/bar/', './data/classMap.chdb');
+ $loader->addNamespace('Foo');
+
+ $reflection = new \ReflectionObject($loader);
+ $namespacesProperty = $reflection->getProperty('namespaces');
+ $namespacesProperty->setAccessible(true);
+
+ $this->assertEquals(array('Foo' => './foo/bar/'), $namespacesProperty->getValue($loader));
+ } // end testAddNamespaceSetsDefaultPath();
+
+ public function testAddNamespaceSetsCustomPath()
+ {
+ $loader = new ChdbLoader('./foo/bar/', './data/classMap.chdb');
+ $loader->addNamespace('Foo', './bar/joe/');
+
+ $reflection = new \ReflectionObject($loader);
+ $namespacesProperty = $reflection->getProperty('namespaces');
+ $namespacesProperty->setAccessible(true);
+
+ $this->assertEquals(array('Foo' => './bar/joe/'), $namespacesProperty->getValue($loader));
+ } // end testAddNamespaceSetsCustomPath();
+
+ public function testAddNamespaceAddsTrailingSlash()
+ {
+ $loader = new ChdbLoader('./foo/bar/', './data/classMap.chdb');
+ $loader->addNamespace('Foo', './bar/joe');
+
+ $reflection = new \ReflectionObject($loader);
+ $namespacesProperty = $reflection->getProperty('namespaces');
+ $namespacesProperty->setAccessible(true);
+
+ $this->assertEquals(array('Foo' => './bar/joe/'), $namespacesProperty->getValue($loader));
+ } // end testAddNamespaceAddsTrailingSlash();
+
+ /**
+ * @expectedException DomainException
+ */
+ public function testAddNamespaceThrowsExceptionWhenNamespaceExists()
+ {
+ $loader = new ChdbLoader('./foo/bar/', './data/classMap.chdb');
+ $loader->addNamespace('Foo');
+ $this->assertTrue($loader->hasNamespace('Foo'));
+ $loader->addNamespace('Foo');
+ } // end testAddNamespaceThrowsExceptionWhenNamespaceExists();
+
+ public function testRemoveNamespace()
+ {
+ $loader = new ChdbLoader('./foo/bar/', './data/classMap.chdb');
+ $loader->addNamespace('Foo');
+ $this->assertTrue($loader->hasNamespace('Foo'));
+
+ $reflection = new \ReflectionObject($loader);
+ $namespacesProperty = $reflection->getProperty('namespaces');
+ $namespacesProperty->setAccessible(true);
+
+ $this->assertEquals(array('Foo' => './foo/bar/'), $namespacesProperty->getValue($loader));
+
+ $loader->removeNamespace('Foo');
+ $this->assertFalse($loader->hasNamespace('Foo'));
+
+ $this->assertEquals(array(), $namespacesProperty->getValue($loader));
+ } // end testRemoveNamespace();
+
+ /**
+ * @depends testRemoveNamespace
+ * @expectedException DomainException
+ */
+ public function testRemoveNamespaceThrowsExceptionWhenNamespaceDoesNotExist()
+ {
+ $loader = new ChdbLoader('./foo/bar/', './data/classMap.chdb');
+ $this->assertFalse($loader->hasNamespace('Moo'));
+ $loader->removeNamespace('Moo');
+ } // end testRemoveNamespaceThrowsExceptionWhenNamespaceDoesNotExist();
+
+ public function testRegisterWorks()
+ {
+ $loader = new ChdbLoader('./foo/bar/', './data/classMap.chdb');
+ $loader->register();
+
+ $functions = spl_autoload_functions();
+ $this->assertContains(array($loader, 'loadClass'), $functions);
+ } // end testRegisterWorks();
+
+ public function testUnregisterWorks()
+ {
+ $loader = new ChdbLoader('./foo/bar/', './data/classMap.chdb');
+ $loader->register();
+
+ $functions = spl_autoload_functions();
+ $this->assertContains(array($loader, 'loadClass'), $functions);
+
+ $loader->unregister();
+
+ $functions = spl_autoload_functions();
+ $this->assertThat($functions, $this->logicalNot($this->contains(array($loader, 'loadClass'))));
+ } // end testUnregisterWorks();
+
+ public function testLoadingClasses()
+ {
+ $loader = new ChdbLoader('./data/', './data/classMap.chdb');
+ $loader->addNamespace('Dummy');
+ $loader->register();
+
+ // No error should happen here.
+ $object = new \Dummy\ShortFile();
+ } // end testLoadingClasses();
+
+ public function testSkippingUnknownClasses()
+ {
+ $loader = new ChdbLoader('./data/', './data/classMap.chdb');
+ $loader->addNamespace('Dummy');
+ $loader->register();
+
+ spl_autoload_register(function($name){ echo 'yey'; return true; });
+
+ ob_start();
+ spl_autoload_call('Foo\\Bar');
+ $this->assertEquals('yey', ob_get_clean());
+ } // end testSkippingUnknownClasses();
+} // end ChdbLoaderTest;
Please sign in to comment.
Something went wrong with that request. Please try again.