|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ArpTest\Container\Factory; |
|
6
|
|
|
|
|
7
|
|
|
use Arp\Container\ContainerInterface; |
|
8
|
|
|
use Arp\Container\Factory\Exception\ServiceFactoryException; |
|
9
|
|
|
use Arp\Container\Factory\ObjectFactory; |
|
10
|
|
|
use Arp\Container\Factory\ServiceFactoryInterface; |
|
11
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
12
|
|
|
use PHPUnit\Framework\TestCase; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @covers \Arp\Container\Factory\ObjectFactory |
|
16
|
|
|
* |
|
17
|
|
|
* @author Alex Patterson <[email protected]> |
|
18
|
|
|
* @package ArpTest\Container\Factory |
|
19
|
|
|
*/ |
|
20
|
|
|
final class ObjectFactoryTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Assert that the object factory implements ServiceFactoryInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
public function testImplementsServiceFactoryInterface(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$factory = new ObjectFactory(); |
|
28
|
|
|
|
|
29
|
|
|
$this->assertInstanceOf(ServiceFactoryInterface::class, $factory); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Assert that if the requested $name of the service does not map to a valid class name, a ServiceFactoryException |
|
34
|
|
|
* is thrown from __invoke() |
|
35
|
|
|
* |
|
36
|
|
|
* @throws ServiceFactoryException |
|
37
|
|
|
*/ |
|
38
|
|
|
public function testNameIsInvalidClassNameWillThrowServiceFactoryException(): void |
|
39
|
|
|
{ |
|
40
|
|
|
$factory = new ObjectFactory(); |
|
41
|
|
|
|
|
42
|
|
|
$name = 'Foo'; // non-existing class name |
|
43
|
|
|
|
|
44
|
|
|
/** @var ContainerInterface|MockObject $container */ |
|
45
|
|
|
$container = $this->createMock(ContainerInterface::class); |
|
46
|
|
|
|
|
47
|
|
|
$this->expectException(ServiceFactoryException::class); |
|
48
|
|
|
$this->expectExceptionMessage( |
|
49
|
|
|
sprintf( |
|
50
|
|
|
'Unable to create a new object from requested service \'%s\': ' |
|
51
|
|
|
. 'The service name does not resolve to a valid class name', |
|
52
|
|
|
$name |
|
53
|
|
|
) |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
$factory($container, $name); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Assert that a new object will be created from the provided service name |
|
61
|
|
|
* |
|
62
|
|
|
* @dataProvider getInvokeWillCreateObjectFromServiceNameIfValidClassNameData |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $className |
|
65
|
|
|
* |
|
66
|
|
|
* @throws ServiceFactoryException |
|
67
|
|
|
*/ |
|
68
|
|
|
public function testInvokeWillCreateObjectFromServiceNameIfValidClassName(string $className): void |
|
69
|
|
|
{ |
|
70
|
|
|
$factory = new ObjectFactory(); |
|
71
|
|
|
|
|
72
|
|
|
/** @var ContainerInterface|MockObject $container */ |
|
73
|
|
|
$container = $this->createMock(ContainerInterface::class); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertInstanceOf($className, $factory($container, $className)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getInvokeWillCreateObjectFromServiceNameIfValidClassNameData(): array |
|
82
|
|
|
{ |
|
83
|
|
|
return [ |
|
84
|
|
|
[ |
|
85
|
|
|
\stdClass::class, |
|
86
|
|
|
], |
|
87
|
|
|
[ |
|
88
|
|
|
\DateTime::class, |
|
89
|
|
|
] |
|
90
|
|
|
]; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|