HummingBirdAnimeClient/tests/Ion/Di/ContainerTest.php

217 lines
5.5 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
/**
2020-03-12 11:45:11 -04:00
* Hummingbird Anime List Client
*
2020-03-12 11:45:11 -04:00
* An API client for Kitsu to manage anime and manga watch lists
*
2021-02-04 11:57:01 -05:00
* PHP version 8
*
2022-03-04 15:50:35 -05:00
* @copyright 2015 - 2022 Timothy J. Warren <tim@timshome.page>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2020-12-10 17:06:50 -05:00
* @version 5.2
2022-03-04 15:50:35 -05:00
* @link https://git.timshome.page/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\Ion\Tests\Di;
2022-03-04 12:19:47 -05:00
use Aviat\Ion\Di\ContainerInterface;
use Aviat\Ion\Di\Exception\{ContainerException, NotFoundException};
use Aviat\Ion\Di\{Container, ContainerAware};
2020-03-12 09:52:45 -04:00
use Aviat\Ion\Tests\IonTestCase;
2022-03-04 12:19:47 -05:00
use Monolog\Handler\{NullHandler, TestHandler};
use Monolog\Logger;
2023-05-09 12:46:52 -04:00
use PHPUnit\Framework\Attributes\DataProvider;
2021-02-26 14:42:07 -05:00
use Throwable;
use TypeError;
2022-03-04 12:19:47 -05:00
/**
* @internal
*/
final class FooTest
{
public $item;
2022-03-04 12:19:47 -05:00
public function __construct($item)
{
$this->item = $item;
}
}
2022-03-04 12:19:47 -05:00
class FooTest2
{
use ContainerAware;
}
2022-03-04 12:19:47 -05:00
/**
* @internal
*/
final class ContainerTest extends IonTestCase
{
protected function setUp(): void
{
$this->container = new Container();
}
2023-05-09 12:46:52 -04:00
public static function dataGetWithException(): array
{
return [
'Bad index type: number' => [
'id' => 42,
2021-02-26 14:42:07 -05:00
'exception' => TypeError::class,
],
'Bad index type: array' => [
'id' => [],
2021-02-26 14:42:07 -05:00
'exception' => TypeError::class,
],
'Non-existent id' => [
'id' => 'foo',
'exception' => NotFoundException::class,
2022-03-04 12:19:47 -05:00
'message' => "Item 'foo' does not exist in container.",
],
];
}
/**
2022-03-04 12:19:47 -05:00
* @param mixed $exception
*/
2023-05-09 12:46:52 -04:00
#[DataProvider('dataGetWithException')]
2021-02-26 14:42:07 -05:00
public function testGetWithException(mixed $id, $exception, ?string $message = NULL): void
{
try
{
$this->container->get($id);
}
2022-03-04 12:19:47 -05:00
catch (ContainerException $e)
{
$this->assertInstanceOf($exception, $e);
2022-03-04 12:19:47 -05:00
$this->assertSame($message, $e->getMessage());
}
2022-03-04 12:19:47 -05:00
catch (Throwable $e)
2021-02-26 14:42:07 -05:00
{
$this->assertInstanceOf($exception, $e);
}
}
/**
2022-03-04 12:19:47 -05:00
* @param mixed $exception
*/
2023-05-09 12:46:52 -04:00
#[DataProvider('dataGetWithException')]
2021-02-26 14:42:07 -05:00
public function testGetNewWithException(mixed $id, $exception, ?string $message = NULL): void
{
$this->expectException($exception);
2021-02-26 14:42:07 -05:00
if ($message !== NULL)
{
$this->expectExceptionMessage($message);
}
$this->container->getNew($id);
}
2023-05-09 12:46:52 -04:00
public static function dataSetInstanceWithException(): array
{
return [
'Non-existent id' => [
'id' => 'foo',
'exception' => NotFoundException::class,
'message' => "Factory 'foo' does not exist in container. Set that first.",
],
'Non-existent id 2' => [
'id' => 'foobarbaz',
'exception' => NotFoundException::class,
'message' => "Factory 'foobarbaz' does not exist in container. Set that first.",
],
];
}
/**
2022-03-04 12:19:47 -05:00
* @param mixed $id
* @param mixed $exception
* @param mixed $message
*/
2023-05-09 12:46:52 -04:00
#[DataProvider('dataSetInstanceWithException')]
public function testSetInstanceWithException($id, $exception, $message): void
{
try
{
$this->container->setInstance($id, NULL);
}
2022-03-04 12:19:47 -05:00
catch (ContainerException $e)
{
$this->assertInstanceOf($exception, $e);
2022-03-04 12:19:47 -05:00
$this->assertSame($message, $e->getMessage());
}
}
public function testGetNew(): void
{
2022-03-04 12:19:47 -05:00
$this->container->set('footest', static fn ($item) => new FooTest($item));
// Check that the item is the container, if called without arguments
$footest1 = $this->container->getNew('footest');
$this->assertInstanceOf(ContainerInterface::class, $footest1->item);
$footest2 = $this->container->getNew('footest', ['Test String']);
2022-03-04 12:19:47 -05:00
$this->assertSame('Test String', $footest2->item);
}
public function testSetContainerInInstance(): void
{
2022-03-04 12:19:47 -05:00
$this->container->set('footest2', static fn () => new FooTest2());
$footest2 = $this->container->get('footest2');
2022-03-04 12:19:47 -05:00
$this->assertSame($this->container, $footest2->getContainer());
}
public function testGetNewReturnCallable(): void
{
2022-03-04 12:19:47 -05:00
$this->container->set('footest', static fn ($item) => static fn () => $item);
// Check that the item is the container, if called without arguments
$footest1 = $this->container->getNew('footest');
$this->assertInstanceOf(ContainerInterface::class, $footest1());
$footest2 = $this->container->getNew('footest', ['Test String']);
2022-03-04 12:19:47 -05:00
$this->assertSame('Test String', $footest2());
}
public function testGetSet(): void
{
2022-03-04 12:19:47 -05:00
$container = $this->container->set('foo', static function () {
return static function () {};
});
$this->assertInstanceOf(Container::class, $container);
$this->assertInstanceOf(ContainerInterface::class, $container);
// The factory returns a callable
2022-03-04 12:19:47 -05:00
$this->assertIsCallable($container->get('foo'));
}
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');
$this->assertInstanceOf(ContainerInterface::class, $container);
$this->assertInstanceOf(Container::class, $container2);
2022-03-04 12:19:47 -05:00
$this->assertSame($logger1, $this->container->getLogger('default'));
$this->assertSame($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'));
}
2022-03-04 12:19:47 -05:00
}