diff --git a/flight/core/Loader.php b/flight/core/Loader.php index 5ff66fe..c438445 100644 --- a/flight/core/Loader.php +++ b/flight/core/Loader.php @@ -141,6 +141,7 @@ class Loader return new $class(); case 1: return new $class($params[0]); + // @codeCoverageIgnoreStart case 2: return new $class($params[0], $params[1]); case 3: @@ -149,6 +150,7 @@ class Loader return new $class($params[0], $params[1], $params[2], $params[3]); case 5: return new $class($params[0], $params[1], $params[2], $params[3], $params[4]); + // @codeCoverageIgnoreEnd default: try { $refClass = new ReflectionClass($class); @@ -192,7 +194,7 @@ class Loader if ($enabled) { spl_autoload_register([__CLASS__, 'loadClass']); } else { - spl_autoload_unregister([__CLASS__, 'loadClass']); + spl_autoload_unregister([__CLASS__, 'loadClass']); // @codeCoverageIgnore } if (!empty($dirs)) { diff --git a/tests/LoaderTest.php b/tests/LoaderTest.php index 0fa9683..ecadd0a 100644 --- a/tests/LoaderTest.php +++ b/tests/LoaderTest.php @@ -10,6 +10,7 @@ use flight\core\Loader; require_once __DIR__ . '/classes/User.php'; require_once __DIR__ . '/classes/Factory.php'; +require_once __DIR__ . '/classes/TesterClass.php'; class LoaderTest extends PHPUnit\Framework\TestCase { @@ -117,4 +118,42 @@ class LoaderTest extends PHPUnit\Framework\TestCase self::assertIsObject($obj); self::assertInstanceOf(Factory::class, $obj); } + + public function testUnregisterClass() { + $this->loader->register('g', 'User'); + $current_class = $this->loader->get('g'); + $this->assertEquals([ 'User', [], null ], $current_class); + $this->loader->unregister('g'); + $unregistered_class_result = $this->loader->get('g'); + $this->assertNull($unregistered_class_result); + } + + public function testNewInstance6Params() { + $TesterClass = $this->loader->newInstance('TesterClass', ['Bob','Fred', 'Joe', 'Jane', 'Sally', 'Suzie']); + $this->assertEquals('Bob', $TesterClass->param1); + $this->assertEquals('Fred', $TesterClass->param2); + $this->assertEquals('Joe', $TesterClass->param3); + $this->assertEquals('Jane', $TesterClass->param4); + $this->assertEquals('Sally', $TesterClass->param5); + $this->assertEquals('Suzie', $TesterClass->param6); + } + + public function testNewInstance6ParamsBadClass() { + $this->expectException(Exception::class); + $this->expectExceptionMessage('Cannot instantiate BadClass'); + $TesterClass = $this->loader->newInstance('BadClass', ['Bob','Fred', 'Joe', 'Jane', 'Sally', 'Suzie']); + } + + public function testAddDirectoryAsArray() { + $loader = new class extends Loader { + public function getDirectories() { + return self::$dirs; + } + }; + $loader->addDirectory([__DIR__ . '/classes']); + self::assertEquals([ + dirname(__DIR__), + __DIR__ . '/classes' + ], $loader->getDirectories()); + } } diff --git a/tests/classes/TesterClass.php b/tests/classes/TesterClass.php new file mode 100644 index 0000000..1b063fd --- /dev/null +++ b/tests/classes/TesterClass.php @@ -0,0 +1,17 @@ +param1 = $param1; + $this->param2 = $param2; + $this->param3 = $param3; + $this->param4 = $param4; + $this->param5 = $param5; + $this->param6 = $param6; + } +}; \ No newline at end of file