Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
143 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} | ||
} |