From 88bf93b43c272cc7ef32b4542b54f064a9337ca7 Mon Sep 17 00:00:00 2001 From: Adrian Mejias Date: Fri, 31 Dec 2021 22:51:13 -0600 Subject: [PATCH] wip --- README.md | 8 +++--- src/Exceptions/AutoloadRegisterException.php | 26 ++++++++++++++++++++ src/Veil.php | 15 +++++++---- tests/MockVeilsTest.php | 10 ++++---- tests/VeilsTest.php | 16 +++++------- 5 files changed, 52 insertions(+), 23 deletions(-) create mode 100644 src/Exceptions/AutoloadRegisterException.php diff --git a/README.md b/README.md index 3fe6540..cb93dab 100644 --- a/README.md +++ b/README.md @@ -13,18 +13,20 @@ This version supports PHP 8.0. You can install the package via composer: ## Usage ### Example + ```php register(); +// Register the autoloader +$veil = (new Veil)->register(); // An example when setting up a flolder and composer psr-4 // autoload is setup as: "Veils\\": "src/Veils/" +// FooVeil class extends VeilAbstract with instance of loadable class $veil->add([ 'Foo' => Veils\FooVeil::class, // The alias name and abstract class to alias against. ]); diff --git a/src/Exceptions/AutoloadRegisterException.php b/src/Exceptions/AutoloadRegisterException.php new file mode 100644 index 0000000..f42c2b5 --- /dev/null +++ b/src/Exceptions/AutoloadRegisterException.php @@ -0,0 +1,26 @@ + $this->autoload($class); - return spl_autoload_register($callback, true, $prepend); + if (spl_autoload_register($callback, true, $prepend)) { + return $this; + } + + throw new AutoloadRegisterException(); } /** diff --git a/tests/MockVeilsTest.php b/tests/MockVeilsTest.php index a871bc8..b5cd5f4 100644 --- a/tests/MockVeilsTest.php +++ b/tests/MockVeilsTest.php @@ -65,7 +65,7 @@ public function it_can_get_a_list_of_all_veils() public function it_can_get_a_list_of_registered_veils_as_array() { $mock = mock(Veil::class); - $mock->shouldReceive('register')->once()->andReturn(true); + $mock->shouldReceive('register')->once()->andReturn(new Veil); $mock->shouldReceive('add')->once()->with([ 'Foo' => FooVeil::class, ]); @@ -86,7 +86,7 @@ public function it_can_get_a_list_of_registered_veils_as_array() public function it_can_get_a_list_of_registered_veils_as_key_value() { $mock = mock(Veil::class); - $mock->shouldReceive('register')->once()->andReturn(true); + $mock->shouldReceive('register')->once()->andReturn(new Veil); $mock->shouldReceive('add')->once()->with('Foo', FooVeil::class); $mock->shouldReceive('registered')->once()->andReturn([ 'Foo' => FooVeil::class, @@ -113,7 +113,7 @@ public static function getVeilInstance() } }; $mock = Mockery::mock(Veil::class); - $mock->shouldReceive('register')->once()->andReturn(true); + $mock->shouldReceive('register')->once()->andReturn(new Veil); $mock->shouldReceive('add')->once()->with('MockFoo', $mockClass::class); $mock->shouldReceive('registered')->once()->andReturn([ 'MockFoo' => $mockClass::class, @@ -143,7 +143,7 @@ public static function getVeilAccessor() } }; $mock = Mockery::mock(Veil::class); - $mock->shouldReceive('register')->once()->andReturn(true); + $mock->shouldReceive('register')->once()->andReturn(new Veil); $mock->shouldReceive('add')->once()->with('MockFoo', $mockClass::class); $mock->shouldReceive('registered')->once()->andReturn([ 'MockFoo' => $mockClass::class, @@ -166,7 +166,7 @@ public static function getVeilAccessor() public function it_can_throw_exception_for_instance_method_not_found() { $mock = Mockery::mock(Veil::class); - $mock->shouldReceive('register')->once()->andReturn(true); + $mock->shouldReceive('register')->once()->andReturn(new Veil); $mock->shouldReceive('add')->once()->with('Foo', FooVeil::class); $mock->shouldReceive('registered')->once()->andReturn([ 'Foo' => FooVeil::class, diff --git a/tests/VeilsTest.php b/tests/VeilsTest.php index 981c8b8..fef7a3c 100644 --- a/tests/VeilsTest.php +++ b/tests/VeilsTest.php @@ -42,11 +42,11 @@ public function it_can_get_a_list_of_all_veils() */ public function it_can_get_a_list_of_registered_veils_as_array() { - $veil = new Veil; - $this->assertTrue($veil->register()); + $veil = (new Veil)->register(); $veil->add(['Foo' => FooVeil::class]); $registered = $veil->registered(); + $this->assertInstanceOf($veil::class, new Veil); $this->assertSame($registered, ['Foo' => FooVeil::class]); } @@ -57,11 +57,11 @@ public function it_can_get_a_list_of_registered_veils_as_array() */ public function it_can_get_a_list_of_registered_veils_as_key_value() { - $veil = new Veil; - $veil->register(); + $veil = (new Veil)->register(); $veil->add('Foo', FooVeil::class); $registered = $veil->registered(); + $this->assertInstanceOf($veil::class, new Veil); $this->assertSame($registered, ['Foo' => FooVeil::class]); } @@ -72,9 +72,7 @@ public function it_can_get_a_list_of_registered_veils_as_key_value() */ public function it_can_call_method() { - $veil = new Veil; - $veil->register(); - $veil->add('Foo', FooVeil::class); + (new Veil)->register()->add('Foo', FooVeil::class); $this->assertSame(\Foo::bar(), FooVeil::getVeilInstance()->bar()); } @@ -87,9 +85,7 @@ public function it_can_call_method() */ public function it_can_throw_exception_for_method_not_found() { - $veil = new Veil; - $veil->register(); - $veil->add('Foo', FooVeil::class); + (new Veil)->register()->add('Foo', FooVeil::class); $this->expectException(NoInstanceMethodFoundException::class); \Foo::noExist();