1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Http\Factory\Discovery; |
5
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
8
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
9
|
|
|
use Psr\Http\Message\ServerRequestFactoryInterface; |
10
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
11
|
|
|
use Psr\Http\Message\UriFactoryInterface; |
12
|
|
|
use Psr\Http\Message\UploadedFileFactoryInterface; |
13
|
|
|
|
14
|
|
|
class FactoryLocatorTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
public function dataFactoryInterfaces(): array |
17
|
|
|
{ |
18
|
|
|
return [ |
19
|
|
|
RequestFactoryInterface::class => [RequestFactoryInterface::class], |
20
|
|
|
ResponseFactoryInterface::class => [ResponseFactoryInterface::class], |
21
|
|
|
ServerRequestFactoryInterface::class => [ServerRequestFactoryInterface::class], |
22
|
|
|
StreamFactoryInterface::class => [StreamFactoryInterface::class], |
23
|
|
|
UriFactoryInterface::class => [UriFactoryInterface::class], |
24
|
|
|
UploadedFileFactoryInterface::class => [UploadedFileFactoryInterface::class], |
25
|
|
|
]; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @dataProvider dataFactoryInterfaces |
30
|
|
|
*/ |
31
|
|
|
public function testCanRegisterAdditionalFactoriesForLocation(string $interface): void |
32
|
|
|
{ |
33
|
|
|
$factory = $this->getMockBuilder($interface)->getMock(); |
34
|
|
|
$factoryClass = get_class($factory); |
35
|
|
|
|
36
|
|
|
FactoryLocator::register($interface, $factoryClass); |
37
|
|
|
|
38
|
|
|
$this->assertSame($factoryClass, FactoryLocator::locate($interface)); |
39
|
|
|
|
40
|
|
|
FactoryLocator::unregister($interface, $factoryClass); |
41
|
|
|
|
42
|
|
|
$this->assertNotSame($factoryClass, FactoryLocator::locate($interface)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testCannotRegisterInvalidFactory(): void |
46
|
|
|
{ |
47
|
|
|
$this->expectException(\InvalidArgumentException::class); |
48
|
|
|
$this->expectExceptionMessage('foo'); |
49
|
|
|
|
50
|
|
|
FactoryLocator::register('foo', self::class); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testCannotRegisterInvalidFactoryImlementation(): void |
54
|
|
|
{ |
55
|
|
|
$this->expectException(\InvalidArgumentException::class); |
56
|
|
|
$this->expectExceptionMessage(self::class); |
57
|
|
|
|
58
|
|
|
FactoryLocator::register(RequestFactoryInterface::class, self::class); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testCannotUnregisterInvalidFactory(): void |
62
|
|
|
{ |
63
|
|
|
$this->expectException(\InvalidArgumentException::class); |
64
|
|
|
$this->expectExceptionMessage('foo'); |
65
|
|
|
|
66
|
|
|
FactoryLocator::unregister('foo', self::class); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testCannotUnregisterInvalidFactoryImplementation(): void |
70
|
|
|
{ |
71
|
|
|
$this->expectException(\InvalidArgumentException::class); |
72
|
|
|
$this->expectExceptionMessage(self::class); |
73
|
|
|
|
74
|
|
|
FactoryLocator::unregister(RequestFactoryInterface::class, self::class); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testCannotLocateInvalidFactory(): void |
78
|
|
|
{ |
79
|
|
|
$this->expectException(\RuntimeException::class); |
80
|
|
|
|
81
|
|
|
FactoryLocator::locate('foo'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|