Skip to content

Commit

Permalink
Split container test
Browse files Browse the repository at this point in the history
  • Loading branch information
Stratadox committed Feb 6, 2018
1 parent f6077d1 commit 41a1dd7
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 96 deletions.
96 changes: 0 additions & 96 deletions tests/ContainerTest.php
Expand Up @@ -6,11 +6,7 @@

use Exception;
use PHPUnit\Framework\TestCase;
use Psr\Container\NotFoundExceptionInterface;
use Stratadox\Di\Container;
use Stratadox\Di\Exception\InvalidFactory;
use Stratadox\Di\Exception\InvalidServiceType;
use Stratadox\Di\Exception\ServiceNotFound;
use Stratadox\Di\Test\Stub\Bar;
use Stratadox\Di\Test\Stub\BarInterface;
use Stratadox\Di\Test\Stub\Baz;
Expand All @@ -19,10 +15,6 @@

/**
* @covers \Stratadox\Di\Container
* @covers \Stratadox\Di\Exception\DependenciesCannotBeCircular
* @covers \Stratadox\Di\Exception\InvalidFactory
* @covers \Stratadox\Di\Exception\InvalidServiceType
* @covers \Stratadox\Di\Exception\ServiceNotFound
*/
class ContainerTest extends TestCase
{
Expand Down Expand Up @@ -124,40 +116,6 @@ function caching_composite_services()
$this->assertSame($baz1, $baz2);
}

/** @scenario */
function throwing_an_exception_when_a_factory_is_invalid()
{
$di = new Container();
$di->set('baz', function () use ($di) {
return new Baz($di->get('foo', Foo::class));
});
$di->set('foo', function () {
return new Bar();
});

// Invalid because service 'foo' has a Bar, not a Foo
$this->expectException(InvalidFactory::class);
$di->get('baz');
}

/** @scenario */
function throwing_an_exception_when_a_service_does_not_exist()
{
$di = new Container();

$this->expectException(ServiceNotFound::class);
$di->get('foo');
}

/** @scenario */
function using_the_psr_interface_when_a_service_does_not_exist()
{
$di = new Container();

$this->expectException(NotFoundExceptionInterface::class);
$di->get('foo');
}

/** @scenario */
function caching_the_instances_for_future_use()
{
Expand Down Expand Up @@ -199,18 +157,6 @@ function looking_up_a_service_with_an_interface_constraint()
$this->assertInstanceOf(BarInterface::class, $bar);
}

/** @scenario */
function throwing_an_exception_when_an_interface_constraint_is_not_met()
{
$di = new Container();
$di->set('bar', function () {
return new Bar();
});

$this->expectException(InvalidServiceType::class);
$di->get('bar', Foo::class);
}

/** @scenario */
function looking_up_a_service_with_a_scalar_constraint()
{
Expand All @@ -222,18 +168,6 @@ function looking_up_a_service_with_a_scalar_constraint()
$this->assertSame('Hello world!', $di->get('string', 'string'));
}

/** @scenario */
function throwing_an_exception_when_a_scalar_constraint_is_not_met()
{
$di = new Container();
$di->set('string', function () {
return 'Hello world!';
});

$this->expectException(InvalidServiceType::class);
$di->get('string', 'double');
}

/** @scenario */
function indicating_that_a_forgotten_service_does_not_exist_anymore()
{
Expand All @@ -249,36 +183,6 @@ function indicating_that_a_forgotten_service_does_not_exist_anymore()
$this->assertFalse($di->has('foo'));
}

/** @scenario */
function throwing_an_exception_when_a_factory_tries_to_infinitely_copy_itself()
{
$di = new Container();
$di->set('foo', function () use ($di) {
return $di->get('foo');
});

$this->expectException(InvalidFactory::class);
$di->get('foo');
}

/** @scenario */
function throwing_an_exception_when_factories_try_to_infinitely_copy_each_other()
{
$di = new Container();
$di->set('foo', function () use ($di) {
return $di->get('bar');
});
$di->set('bar', function () use ($di) {
return $di->get('baz');
});
$di->set('baz', function () use ($di) {
return $di->get('foo');
});

$this->expectException(InvalidFactory::class);
$di->get('foo');
}

/** @scenario */
function factories_that_forget_themselves_produce_once_and_quit_forever()
{
Expand Down
66 changes: 66 additions & 0 deletions tests/InvalidFactoryTest.php
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Stratadox\Di\Test;

use PHPUnit\Framework\TestCase;
use Stratadox\Di\Container;
use Stratadox\Di\Exception\InvalidFactory;
use Stratadox\Di\Test\Stub\Bar;
use Stratadox\Di\Test\Stub\Baz;
use Stratadox\Di\Test\Stub\Foo;

/**
* @covers \Stratadox\Di\Container
* @covers \Stratadox\Di\Exception\InvalidFactory
* @covers \Stratadox\Di\Exception\DependenciesCannotBeCircular
*/
class InvalidFactoryTest extends TestCase
{
/** @scenario */
function throwing_an_exception_when_a_factory_is_invalid()
{
$di = new Container();
$di->set('baz', function () use ($di) {
return new Baz($di->get('foo', Foo::class));
});
$di->set('foo', function () {
return new Bar();
});

// Invalid because service 'foo' has a Bar, not a Foo
$this->expectException(InvalidFactory::class);
$di->get('baz');
}

/** @scenario */
function throwing_an_exception_when_a_factory_tries_to_infinitely_copy_itself()
{
$di = new Container();
$di->set('foo', function () use ($di) {
return $di->get('foo');
});

$this->expectException(InvalidFactory::class);
$di->get('foo');
}

/** @scenario */
function throwing_an_exception_when_factories_try_to_infinitely_copy_each_other()
{
$di = new Container();
$di->set('foo', function () use ($di) {
return $di->get('bar');
});
$di->set('bar', function () use ($di) {
return $di->get('baz');
});
$di->set('baz', function () use ($di) {
return $di->get('foo');
});

$this->expectException(InvalidFactory::class);
$di->get('foo');
}
}
42 changes: 42 additions & 0 deletions tests/InvalidServiceTypeTest.php
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Stratadox\Di\Test;

use PHPUnit\Framework\TestCase;
use Stratadox\Di\Container;
use Stratadox\Di\Exception\InvalidServiceType;
use Stratadox\Di\Test\Stub\Bar;
use Stratadox\Di\Test\Stub\Foo;

/**
* @covers \Stratadox\Di\Container
* @covers \Stratadox\Di\Exception\InvalidServiceType
*/
class InvalidServiceTypeTest extends TestCase
{
/** @scenario */
function throwing_an_exception_when_a_scalar_constraint_is_not_met()
{
$di = new Container();
$di->set('string', function () {
return 'Hello world!';
});

$this->expectException(InvalidServiceType::class);
$di->get('string', 'double');
}

/** @scenario */
function throwing_an_exception_when_an_interface_constraint_is_not_met()
{
$di = new Container();
$di->set('bar', function () {
return new Bar();
});

$this->expectException(InvalidServiceType::class);
$di->get('bar', Foo::class);
}
}
35 changes: 35 additions & 0 deletions tests/ServiceNotFoundTest.php
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Stratadox\Di\Test;

use PHPUnit\Framework\TestCase;
use Psr\Container\NotFoundExceptionInterface;
use Stratadox\Di\Container;
use Stratadox\Di\Exception\ServiceNotFound;

/**
* @covers \Stratadox\Di\Container
* @covers \Stratadox\Di\Exception\ServiceNotFound
*/
class ServiceNotFoundTest extends TestCase
{
/** @scenario */
function throwing_an_exception_when_a_service_does_not_exist()
{
$di = new Container();

$this->expectException(ServiceNotFound::class);
$di->get('foo');
}

/** @scenario */
function using_the_psr_interface_when_a_service_does_not_exist()
{
$di = new Container();

$this->expectException(NotFoundExceptionInterface::class);
$di->get('foo');
}
}

0 comments on commit 41a1dd7

Please sign in to comment.