ion/tests/Di/ContainerTest.php

205 lines
5.3 KiB
PHP
Raw Normal View History

2016-10-13 13:11:22 -04:00
<?php declare(strict_types=1);
/**
* Ion
*
* Building blocks for web development
*
2019-12-05 15:39:02 -05:00
* PHP version 7.2
2016-10-13 13:11:22 -04:00
*
* @package Ion
* @author Timothy J. Warren <tim@timshomepage.net>
2019-12-05 15:39:02 -05:00
* @copyright 2015 - 2019 Timothy J. Warren
2016-10-13 13:11:22 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2019-12-05 15:39:02 -05:00
* @version 3.0.0
* @link https://git.timshomepage.net/aviat/ion
2016-10-13 13:11:22 -04:00
*/
2016-08-29 11:34:25 -04:00
namespace Aviat\Ion\Tests\Di;
use Aviat\Ion\Di\{Container, ContainerAware};
use Aviat\Ion\Di\Exception\ContainerException;
2016-10-19 13:24:08 -04:00
use Aviat\Ion\Tests\Ion_TestCase;
use Monolog\Logger;
2016-10-19 13:24:08 -04:00
use Monolog\Handler\{TestHandler, NullHandler};
2019-12-05 15:25:47 -05:00
use Aviat\Ion\Di\ContainerInterface;
use Aviat\Ion\Di\Exception\NotFoundException;
2016-08-29 12:51:40 -04:00
class FooTest {
public $item;
public function __construct($item) {
$this->item = $item;
}
}
class FooTest2 {
use ContainerAware;
2016-08-29 12:51:40 -04:00
}
2016-10-19 13:24:08 -04:00
class ContainerTest extends Ion_TestCase {
2019-12-05 15:25:47 -05:00
public function setUp(): void
{
$this->container = new Container();
}
2019-12-05 15:25:47 -05:00
public function dataGetWithException(): array
{
return [
'Bad index type: number' => [
'id' => 42,
2019-12-05 15:25:47 -05:00
'exception' => ContainerException::class,
'message' => 'Id must be a string'
],
'Bad index type: array' => [
'id' => [],
2019-12-05 15:25:47 -05:00
'exception' => ContainerException::class,
'message' => 'Id must be a string'
],
'Non-existent id' => [
'id' => 'foo',
2019-12-05 15:25:47 -05:00
'exception' => NotFoundException::class,
'message' => "Item 'foo' does not exist in container."
]
];
}
/**
* @dataProvider dataGetWithException
*/
2019-12-05 15:25:47 -05:00
public function testGetWithException($id, $exception, $message): void
{
try
{
$this->container->get($id);
}
catch(ContainerException $e)
{
$this->assertInstanceOf($exception, $e);
$this->assertEquals($message, $e->getMessage());
}
}
2016-08-29 12:51:40 -04:00
/**
* @dataProvider dataGetWithException
*/
2019-12-05 15:25:47 -05:00
public function testGetNewWithException($id, $exception, $message): void
2016-08-29 12:51:40 -04:00
{
$this->expectException($exception);
$this->expectExceptionMessage($message);
$this->container->getNew($id);
2016-08-29 12:51:40 -04:00
}
2019-12-05 15:25:47 -05:00
public function dataSetInstanceWithException(): array
2016-08-29 12:51:40 -04:00
{
return [
'Non-existent id' => [
'id' => 'foo',
2019-12-05 15:25:47 -05:00
'exception' => NotFoundException::class,
2016-08-29 12:51:40 -04:00
'message' => "Factory 'foo' does not exist in container. Set that first.",
],
'Non-existent id 2' => [
'id' => 'foobarbaz',
2019-12-05 15:25:47 -05:00
'exception' => NotFoundException::class,
2016-08-29 12:51:40 -04:00
'message' => "Factory 'foobarbaz' does not exist in container. Set that first.",
],
];
}
/**
* @dataProvider dataSetInstanceWithException
*/
2019-12-05 15:25:47 -05:00
public function testSetInstanceWithException($id, $exception, $message): void
2016-08-29 12:51:40 -04:00
{
try
{
$this->container->setInstance($id, NULL);
}
catch(ContainerException $e)
{
$this->assertInstanceOf($exception, $e);
$this->assertEquals($message, $e->getMessage());
}
}
2019-12-05 15:25:47 -05:00
public function testGetNew(): void
2016-08-29 12:51:40 -04:00
{
2019-12-05 15:25:47 -05:00
$this->container->set('footest', static function($item) {
2016-08-29 12:51:40 -04:00
return new FooTest($item);
});
// Check that the item is the container, if called without arguments
$footest1 = $this->container->getNew('footest');
2019-12-05 15:25:47 -05:00
$this->assertInstanceOf(ContainerInterface::class, $footest1->item);
2016-08-29 12:51:40 -04:00
$footest2 = $this->container->getNew('footest', ['Test String']);
$this->assertEquals('Test String', $footest2->item);
}
2019-12-05 15:25:47 -05:00
public function testSetContainerInInstance(): void
2016-08-29 12:51:40 -04:00
{
$this->container->set('footest2', function() {
return new FooTest2();
});
$footest2 = $this->container->get('footest2');
$this->assertEquals($this->container, $footest2->getContainer());
}
2019-12-05 15:25:47 -05:00
public function testGetNewReturnCallable(): void
2016-08-29 12:51:40 -04:00
{
2019-12-05 15:25:47 -05:00
$this->container->set('footest', static function($item) {
return static function() use ($item) {
2016-08-29 12:51:40 -04:00
return $item;
};
});
// Check that the item is the container, if called without arguments
$footest1 = $this->container->getNew('footest');
2019-12-05 15:25:47 -05:00
$this->assertInstanceOf(ContainerInterface::class, $footest1());
2016-08-29 12:51:40 -04:00
$footest2 = $this->container->getNew('footest', ['Test String']);
$this->assertEquals('Test String', $footest2());
}
2019-12-05 15:25:47 -05:00
public function testGetSet(): void
{
2019-12-05 15:25:47 -05:00
$container = $this->container->set('foo', static function() {
return static function() {};
});
2018-01-17 15:32:41 -05:00
$this->assertInstanceOf(Container::class, $container);
2019-12-05 15:25:47 -05:00
$this->assertInstanceOf(ContainerInterface::class, $container);
2016-08-29 12:51:40 -04:00
// The factory returns a callable
$this->assertTrue(is_callable($container->get('foo')));
}
2019-12-05 15:25:47 -05:00
public function testLoggerMethods(): void
{
// Does the container have the default logger?
$this->assertFalse($this->container->hasLogger());
$this->assertFalse($this->container->hasLogger('default'));
$logger1 = new Logger('default');
$logger2 = new Logger('testing');
$logger1->pushHandler(new NullHandler());
$logger2->pushHandler(new TestHandler());
// Set the logger channels
$container = $this->container->setLogger($logger1);
$container2 = $this->container->setLogger($logger2, 'test');
2019-12-05 15:25:47 -05:00
$this->assertInstanceOf(ContainerInterface::class, $container);
$this->assertInstanceOf(Container::class, $container2);
$this->assertEquals($logger1, $this->container->getLogger('default'));
$this->assertEquals($logger2, $this->container->getLogger('test'));
$this->assertNull($this->container->getLogger('foo'));
$this->assertTrue($this->container->hasLogger());
$this->assertTrue($this->container->hasLogger('default'));
$this->assertTrue($this->container->hasLogger('test'));
}
}