1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ArpTest\LaminasFactory; |
6
|
|
|
|
7
|
|
|
use Arp\LaminasFactory\AbstractFactory; |
8
|
|
|
use Arp\LaminasFactory\FactoryInterface; |
9
|
|
|
use ArpTest\LaminasFactory\Stub\StdClassFactory; |
10
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
use Psr\Container\ContainerInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @covers \Arp\LaminasFactory\AbstractFactory |
16
|
|
|
* |
17
|
|
|
* @author Alex Patterson <[email protected]> |
18
|
|
|
* @package ArpTest\LaminasFactory |
19
|
|
|
*/ |
20
|
|
|
final class AbstractFactoryTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var ContainerInterface&MockObject |
24
|
|
|
*/ |
25
|
|
|
private $container; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private string $serviceName = \stdClass::class; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array<mixed>|null |
34
|
|
|
*/ |
35
|
|
|
private ?array $options = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Prepare the test case dependencies |
39
|
|
|
*/ |
40
|
|
|
public function setUp(): void |
41
|
|
|
{ |
42
|
|
|
$this->container = $this->createMock(ContainerInterface::class); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Assert that the factory implements FactoryInterface |
47
|
|
|
*/ |
48
|
|
|
public function testImplementsFactoryInterface(): void |
49
|
|
|
{ |
50
|
|
|
$factory = new class() extends AbstractFactory { |
51
|
|
|
/** |
52
|
|
|
* @param ContainerInterface $container |
53
|
|
|
* @param string $requestedName |
54
|
|
|
* @param array<mixed>|null $options |
55
|
|
|
* |
56
|
|
|
* @return \stdClass |
57
|
|
|
*/ |
58
|
|
|
public function __invoke( |
59
|
|
|
ContainerInterface $container, |
60
|
|
|
string $requestedName, |
61
|
|
|
array $options = null |
62
|
|
|
): \stdClass { |
63
|
|
|
return new \stdClass(); |
64
|
|
|
} |
65
|
|
|
}; |
66
|
|
|
|
67
|
|
|
$this->assertInstanceOf(FactoryInterface::class, $factory); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Assert the stdClass can be correctly configured and returned from __invoke() |
72
|
|
|
*/ |
73
|
|
|
public function testInvokeWillReturnConfiguredStdClass(): void |
74
|
|
|
{ |
75
|
|
|
$applicationOptionsService = 'config'; |
76
|
|
|
$applicationOptions = [ |
77
|
|
|
'arp' => [ |
78
|
|
|
'services' => [ |
79
|
|
|
\stdClass::class => [ |
80
|
|
|
'foo' => 'bar', |
81
|
|
|
123 => true, |
82
|
|
|
'hello' => [ |
83
|
|
|
'clown' => 'world', |
84
|
|
|
], |
85
|
|
|
] |
86
|
|
|
] |
87
|
|
|
], |
88
|
|
|
]; |
89
|
|
|
|
90
|
|
|
$factory = new StdClassFactory(); |
91
|
|
|
|
92
|
|
|
$this->container->expects($this->once()) |
93
|
|
|
->method('has') |
94
|
|
|
->with($applicationOptionsService) |
95
|
|
|
->willReturn(true); |
96
|
|
|
|
97
|
|
|
$this->container->expects($this->once()) |
98
|
|
|
->method('get') |
99
|
|
|
->with($applicationOptionsService) |
100
|
|
|
->willReturn($applicationOptions); |
101
|
|
|
|
102
|
|
|
$stdObject = $factory($this->container, $this->serviceName, $this->options); |
103
|
|
|
|
104
|
|
|
$this->assertSame($applicationOptions['arp']['services'][\stdClass::class], $stdObject->options); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|