1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License)
*/
namespace Slim\Tests;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\RequiresPhpunit;
use PHPUnit;
use Psr\Container\ContainerInterface;
use Slim\Container;
class ContainerTest extends PHPUnit\Framework\TestCase
{
/**
* @var Container
*/
protected $container;
protected function setUp(): void
{
$this->container = new Container;
}
public function testGet()
{
$this->assertInstanceOf('\Slim\Http\Environment', $this->container->get('environment'));
}
public function testGetWithValueNotFoundError()
{
$this->expectException('\Slim\Exception\ContainerValueNotFoundException');
$this->container->get('foo');
}
/**
* Test `get()` throws something that is a ContainerException - typically a NotFoundException, when there is a DI
* config error
*/
public function testGetWithDiConfigErrorThrownAsContainerValueNotFoundException()
{
$this->expectException('\Slim\Exception\ContainerValueNotFoundException');
$container = new Container;
$container['foo'] =
function (ContainerInterface $container) {
return $container->get('doesnt-exist');
}
;
$container->get('foo');
}
/**
* Test `get()` recasts InvalidArgumentException as psr/container exceptions when an error is present
* in the DI config
*/
public function testGetWithDiConfigErrorThrownAsInvalidArgumentException()
{
$this->expectException('\Slim\Exception\ContainerException');
$container = new Container;
$container['foo'] =
function (ContainerInterface $container) {
return $container['doesnt-exist'];
}
;
$container->get('foo');
}
/**
* Test `get()` does not recast exceptions which are thrown in a factory closure
*/
#[RequiresPhpunit('< 12')]
public function testGetWithErrorThrownByFactoryClosure()
{
$this->expectException('InvalidArgumentException');
$invokable = $this->getMockBuilder('StdClass')->addMethods(['__invoke'])->getMock();
/** @var callable $invokable */
$invokable->expects($this->any())
->method('__invoke')
->will($this->throwException(new InvalidArgumentException()));
$container = new Container;
$container['foo'] =
function (ContainerInterface $container) use ($invokable) {
call_user_func($invokable);
}
;
$container->get('foo');
}
public function testGetRequest()
{
$this->assertInstanceOf('\Psr\Http\Message\RequestInterface', $this->container['request']);
}
public function testGetResponse()
{
$this->assertInstanceOf('\Psr\Http\Message\ResponseInterface', $this->container['response']);
}
public function testGetRouter()
{
$this->assertInstanceOf('\Slim\Router', $this->container['router']);
}
public function testGetErrorHandler()
{
$this->assertInstanceOf('\Slim\Handlers\Error', $this->container['errorHandler']);
}
public function testGetNotAllowedHandler()
{
$this->assertInstanceOf('\Slim\Handlers\NotAllowed', $this->container['notAllowedHandler']);
}
public function testSettingsCanBeEdited()
{
$this->assertSame('1.1', $this->container->get('settings')['httpVersion']);
$this->container->get('settings')['httpVersion'] = '1.2';
$this->assertSame('1.2', $this->container->get('settings')['httpVersion']);
}
public function testMagicIssetMethod()
{
$this->assertEquals(true, $this->container->__isset('settings'));
}
public function testMagicGetMethod()
{
$this->container->get('settings')['httpVersion'] = '1.2';
$this->assertSame('1.2', $this->container->__get('settings')['httpVersion']);
}
public function testRouteCacheDisabledByDefault()
{
$this->assertFalse($this->container->get('settings')['routerCacheFile']);
}
}
|