|
|
|
@ -7,8 +7,13 @@
|
|
|
|
|
* @license MIT, http://flightphp.com/license
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace tests;
|
|
|
|
|
|
|
|
|
|
use flight\core\Loader;
|
|
|
|
|
use tests\classes\Factory;
|
|
|
|
|
use tests\classes\User;
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
use tests\classes\TesterClass;
|
|
|
|
|
|
|
|
|
|
class LoaderTest extends TestCase
|
|
|
|
|
{
|
|
|
|
@ -23,7 +28,7 @@ class LoaderTest extends TestCase
|
|
|
|
|
// Autoload a class
|
|
|
|
|
public function testAutoload()
|
|
|
|
|
{
|
|
|
|
|
$this->loader->register('tests', 'User');
|
|
|
|
|
$this->loader->register('tests', User::class);
|
|
|
|
|
|
|
|
|
|
$test = $this->loader->load('tests');
|
|
|
|
|
|
|
|
|
@ -34,7 +39,7 @@ class LoaderTest extends TestCase
|
|
|
|
|
// Register a class
|
|
|
|
|
public function testRegister()
|
|
|
|
|
{
|
|
|
|
|
$this->loader->register('a', 'User');
|
|
|
|
|
$this->loader->register('a', User::class);
|
|
|
|
|
|
|
|
|
|
$user = $this->loader->load('a');
|
|
|
|
|
|
|
|
|
@ -46,7 +51,7 @@ class LoaderTest extends TestCase
|
|
|
|
|
// Register a class with constructor parameters
|
|
|
|
|
public function testRegisterWithConstructor()
|
|
|
|
|
{
|
|
|
|
|
$this->loader->register('b', 'User', ['Bob']);
|
|
|
|
|
$this->loader->register('b', User::class, ['Bob']);
|
|
|
|
|
|
|
|
|
|
$user = $this->loader->load('b');
|
|
|
|
|
|
|
|
|
@ -58,7 +63,7 @@ class LoaderTest extends TestCase
|
|
|
|
|
// Register a class with initialization
|
|
|
|
|
public function testRegisterWithInitialization()
|
|
|
|
|
{
|
|
|
|
|
$this->loader->register('c', 'User', ['Bob'], function ($user) {
|
|
|
|
|
$this->loader->register('c', User::class, ['Bob'], function ($user) {
|
|
|
|
|
$user->name = 'Fred';
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
@ -72,7 +77,7 @@ class LoaderTest extends TestCase
|
|
|
|
|
// Get a non-shared instance of a class
|
|
|
|
|
public function testSharedInstance()
|
|
|
|
|
{
|
|
|
|
|
$this->loader->register('d', 'User');
|
|
|
|
|
$this->loader->register('d', User::class);
|
|
|
|
|
|
|
|
|
|
$user1 = $this->loader->load('d');
|
|
|
|
|
$user2 = $this->loader->load('d');
|
|
|
|
@ -85,7 +90,7 @@ class LoaderTest extends TestCase
|
|
|
|
|
// Gets an object from a factory method
|
|
|
|
|
public function testRegisterUsingCallable()
|
|
|
|
|
{
|
|
|
|
|
$this->loader->register('e', ['Factory', 'create']);
|
|
|
|
|
$this->loader->register('e', ['\tests\classes\Factory', 'create']);
|
|
|
|
|
|
|
|
|
|
$obj = $this->loader->load('e');
|
|
|
|
|
|
|
|
|
@ -119,9 +124,9 @@ class LoaderTest extends TestCase
|
|
|
|
|
|
|
|
|
|
public function testUnregisterClass()
|
|
|
|
|
{
|
|
|
|
|
$this->loader->register('g', 'User');
|
|
|
|
|
$this->loader->register('g', User::class);
|
|
|
|
|
$current_class = $this->loader->get('g');
|
|
|
|
|
$this->assertEquals([ 'User', [], null ], $current_class);
|
|
|
|
|
$this->assertEquals([ User::class, [], null ], $current_class);
|
|
|
|
|
$this->loader->unregister('g');
|
|
|
|
|
$unregistered_class_result = $this->loader->get('g');
|
|
|
|
|
$this->assertNull($unregistered_class_result);
|
|
|
|
@ -129,7 +134,7 @@ class LoaderTest extends TestCase
|
|
|
|
|
|
|
|
|
|
public function testNewInstance6Params()
|
|
|
|
|
{
|
|
|
|
|
$TesterClass = $this->loader->newInstance('TesterClass', ['Bob','Fred', 'Joe', 'Jane', 'Sally', 'Suzie']);
|
|
|
|
|
$TesterClass = $this->loader->newInstance(TesterClass::class, ['Bob','Fred', 'Joe', 'Jane', 'Sally', 'Suzie']);
|
|
|
|
|
$this->assertEquals('Bob', $TesterClass->param1);
|
|
|
|
|
$this->assertEquals('Fred', $TesterClass->param2);
|
|
|
|
|
$this->assertEquals('Joe', $TesterClass->param3);
|
|
|
|
|