Skip to content

Commit

Permalink
Moved Service Provider test to own class.
Browse files Browse the repository at this point in the history
  • Loading branch information
nomadicjosh committed Nov 21, 2021
1 parent 9745fc8 commit d6310ac
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 56 deletions.
56 changes: 0 additions & 56 deletions Tests/InjectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
use Qubus\Injector\InjectionException;
use Qubus\Injector\Injector;
use Qubus\Injector\InjectorException;
use Qubus\Injector\Psr11\Container;
use Qubus\Injector\ServiceContainer;
use Qubus\Injector\ServiceProvider\BaseServiceProvider;
use stdClass;
use TypeError;

Expand Down Expand Up @@ -1407,21 +1404,6 @@ public function testInjectionChainValue()
Assert::assertEquals($object->icv->value, 'unknown value');
Assert::assertEquals($object->dependency->icv->value, 'unknown value');
}

public function testServiceProvider()
{
$injector = new Container(Factory::create([]));

$service = new FakeServiceProvider();
$service->register($injector);

$name = new Person('Joseph Smith');

$injected = $injector->make('user.model');

Assert::assertEquals($name, $injected->userName());
Assert::assertInstanceOf(Person::class, $injected->userName());
}
}

interface SharedAliasedInterface
Expand Down Expand Up @@ -2210,41 +2192,3 @@ public function __construct(
$this->icv = $icv;
}
}

interface Model
{
public function userName(): Name;
}

interface Name
{
}

class Person implements Name
{
public function __construct(protected ?string $userName = null)
{
}
}

class UserModel implements Model
{
public function __construct(
protected ?Name $userName = null
) {
}

public function userName(): Name
{
return $this->userName;
}
}

class FakeServiceProvider extends BaseServiceProvider
{
public function register(ServiceContainer $container): void
{
$container->alias('user.model', UserModel::class)
->define('user.model', [':userName' => new Person('Joseph Smith')]);
}
}
27 changes: 27 additions & 0 deletions Tests/ServiceProvider/FakeServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* Qubus\Injector
*
* @link https://github.com/QubusPHP/injector
* @copyright 2021 Joshua Parker <[email protected]>
* @license https://opensource.org/licenses/mit-license.php MIT License
*
* @since 2.0.0
*/

declare(strict_types=1);

namespace Qubus\Injector\Tests\ServiceProvider;

use Qubus\Injector\ServiceContainer;
use Qubus\Injector\ServiceProvider\BaseServiceProvider;

class FakeServiceProvider extends BaseServiceProvider
{
public function register(ServiceContainer $container): void
{
$container->alias('user.model', UserModel::class)
->define('user.model', [':userName' => new Person('Joseph Smith')]);
}
}
19 changes: 19 additions & 0 deletions Tests/ServiceProvider/Identity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* Qubus\Injector
*
* @link https://github.com/QubusPHP/injector
* @copyright 2021 Joshua Parker <[email protected]>
* @license https://opensource.org/licenses/mit-license.php MIT License
*
* @since 2.0.0
*/

declare(strict_types=1);

namespace Qubus\Injector\Tests\ServiceProvider;

interface Identity
{
}
20 changes: 20 additions & 0 deletions Tests/ServiceProvider/Model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/**
* Qubus\Injector
*
* @link https://github.com/QubusPHP/injector
* @copyright 2021 Joshua Parker <[email protected]>
* @license https://opensource.org/licenses/mit-license.php MIT License
*
* @since 2.0.0
*/

declare(strict_types=1);

namespace Qubus\Injector\Tests\ServiceProvider;

interface Model
{
public function userName(): Identity;
}
22 changes: 22 additions & 0 deletions Tests/ServiceProvider/Person.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* Qubus\Injector
*
* @link https://github.com/QubusPHP/injector
* @copyright 2021 Joshua Parker <[email protected]>
* @license https://opensource.org/licenses/mit-license.php MIT License
*
* @since 2.0.0
*/

declare(strict_types=1);

namespace Qubus\Injector\Tests\ServiceProvider;

class Person implements Identity
{
public function __construct(protected ?string $userName = null)
{
}
}
28 changes: 28 additions & 0 deletions Tests/ServiceProvider/UserModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* Qubus\Injector
*
* @link https://github.com/QubusPHP/injector
* @copyright 2021 Joshua Parker <[email protected]>
* @license https://opensource.org/licenses/mit-license.php MIT License
*
* @since 2.0.0
*/

declare(strict_types=1);

namespace Qubus\Injector\Tests\ServiceProvider;

class UserModel implements Model
{
public function __construct(
protected ?Identity $userName = null
) {
}

public function userName(): Identity
{
return $this->userName;
}
}
38 changes: 38 additions & 0 deletions Tests/ServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* Qubus\Injector
*
* @link https://github.com/QubusPHP/injector
* @copyright 2021 Joshua Parker <[email protected]>
* @license https://opensource.org/licenses/mit-license.php MIT License
*
* @since 2.0.0
*/

namespace Qubus\Tests\Injector;

use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Qubus\Injector\Config\Factory;
use Qubus\Injector\Psr11\Container;
use Qubus\Injector\Tests\ServiceProvider\FakeServiceProvider;
use Qubus\Injector\Tests\ServiceProvider\Person;

class ServiceProviderTest extends TestCase
{
public function testFakeServiceProvider()
{
$injector = new Container(Factory::create([]));

$service = new FakeServiceProvider();
$service->register($injector);

$name = new Person('Joseph Smith');

$injected = $injector->make('user.model');

Assert::assertEquals($name, $injected->userName());
Assert::assertInstanceOf(Person::class, $injected->userName());
}
}

0 comments on commit d6310ac

Please sign in to comment.