Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianmejias committed Jan 1, 2022
1 parent 6445429 commit 88bf93b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 23 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ This version supports PHP 8.0. You can install the package via composer:
## Usage

### Example

```php
<?php

require __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/vendor/autoload.php';

use AdrianMejias\Veil\Veil;

$veil = new Veil();
$veil->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.
]);
Expand Down
26 changes: 26 additions & 0 deletions src/Exceptions/AutoloadRegisterException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace AdrianMejias\Veil\Exceptions;

use Exception;

/**
* Veil autoload register exception.
*
* @package Veil
* @category Support
*/
class AutoloadRegisterException extends Exception
{
/**
* String representation of the exception.
*
* @return string
*/
public function __toString(): string
{
return 'Could not register autoload.';
}
}
15 changes: 10 additions & 5 deletions src/Veil.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace AdrianMejias\Veil;

use TypeError;
use AdrianMejias\Veil\Exceptions\AutoloadRegisterException;

/**
* Veil.
Expand Down Expand Up @@ -92,14 +92,19 @@ public function add($veils, $class = null): void
* @param bool $prepend If true, spl_autoload_register()
* will prepend the autoloader on the autoload stack
* instead of appending it.
* @return bool true on success or false on failure.
* @throws TypeError
* @return \AdrianMejias\Veil\Veil
* @throws \TypeError
* @throws \AdrianMejias\Veil\Exceptions\AutoloadRegisterException
*/
public function register(bool $prepend = true): bool
public function register(bool $prepend = true): Veil
{
$callback = fn (string $class) => $this->autoload($class);

return spl_autoload_register($callback, true, $prepend);
if (spl_autoload_register($callback, true, $prepend)) {
return $this;
}

throw new AutoloadRegisterException();
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/MockVeilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
16 changes: 6 additions & 10 deletions tests/VeilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

Expand All @@ -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]);
}

Expand All @@ -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());
}
Expand All @@ -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();
Expand Down

0 comments on commit 88bf93b

Please sign in to comment.