100% Coverage for core Classes

pull/501/head
n0nag0n 1 year ago
parent e36e9024c6
commit 9a007c5216

@ -141,6 +141,7 @@ class Loader
return new $class(); return new $class();
case 1: case 1:
return new $class($params[0]); return new $class($params[0]);
// @codeCoverageIgnoreStart
case 2: case 2:
return new $class($params[0], $params[1]); return new $class($params[0], $params[1]);
case 3: case 3:
@ -149,6 +150,7 @@ class Loader
return new $class($params[0], $params[1], $params[2], $params[3]); return new $class($params[0], $params[1], $params[2], $params[3]);
case 5: case 5:
return new $class($params[0], $params[1], $params[2], $params[3], $params[4]); return new $class($params[0], $params[1], $params[2], $params[3], $params[4]);
// @codeCoverageIgnoreEnd
default: default:
try { try {
$refClass = new ReflectionClass($class); $refClass = new ReflectionClass($class);
@ -192,7 +194,7 @@ class Loader
if ($enabled) { if ($enabled) {
spl_autoload_register([__CLASS__, 'loadClass']); spl_autoload_register([__CLASS__, 'loadClass']);
} else { } else {
spl_autoload_unregister([__CLASS__, 'loadClass']); spl_autoload_unregister([__CLASS__, 'loadClass']); // @codeCoverageIgnore
} }
if (!empty($dirs)) { if (!empty($dirs)) {

@ -10,6 +10,7 @@ use flight\core\Loader;
require_once __DIR__ . '/classes/User.php'; require_once __DIR__ . '/classes/User.php';
require_once __DIR__ . '/classes/Factory.php'; require_once __DIR__ . '/classes/Factory.php';
require_once __DIR__ . '/classes/TesterClass.php';
class LoaderTest extends PHPUnit\Framework\TestCase class LoaderTest extends PHPUnit\Framework\TestCase
{ {
@ -117,4 +118,42 @@ class LoaderTest extends PHPUnit\Framework\TestCase
self::assertIsObject($obj); self::assertIsObject($obj);
self::assertInstanceOf(Factory::class, $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());
}
} }

@ -0,0 +1,17 @@
<?php
class TesterClass {
public $param1;
public $param2;
public $param3;
public $param4;
public $param5;
public $param6;
public function __construct($param1, $param2, $param3, $param4, $param5, $param6) {
$this->param1 = $param1;
$this->param2 = $param2;
$this->param3 = $param3;
$this->param4 = $param4;
$this->param5 = $param5;
$this->param6 = $param6;
}
};
Loading…
Cancel
Save