forked from laminas/laminas-di
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutowireFactoryTest.php
118 lines (95 loc) · 3.74 KB
/
AutowireFactoryTest.php
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
<?php
declare(strict_types=1);
namespace LaminasTest\Di\Container;
use Laminas\Di\Container\AutowireFactory;
use Laminas\Di\Exception;
use Laminas\Di\InjectorInterface;
use LaminasTest\Di\TestAsset;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use stdClass;
/**
* AutowireFactory test case.
*
* @coversDefaultClass Laminas\Di\Container\AutowireFactory
*/
class AutowireFactoryTest extends TestCase
{
private AutowireFactory $instance;
/**
* Prepares the environment before running a test.
*/
protected function setUp(): void
{
parent::setUp();
$this->instance = new AutowireFactory();
}
private function createContainerMock(InjectorInterface $injector): ContainerInterface
{
$container = $this->getMockBuilder(ContainerInterface::class)->getMockForAbstractClass();
$container->method('has')->with(InjectorInterface::class)->willReturn(true);
$container->method('get')
->with(InjectorInterface::class)
->willReturn($injector);
return $container;
}
public function testCanCreateUsesInjector()
{
$injector = $this->getMockBuilder(InjectorInterface::class)->getMockForAbstractClass();
$injector->expects($this->atLeastOnce())
->method('canCreate')
->willReturn(true);
$container = $this->createContainerMock($injector);
$result = $this->instance->canCreate($container, 'AnyClass');
$this->assertTrue($result);
}
private function createContainerForCreateTest(string $className, object $expected): ContainerInterface
{
$injector = $this->getMockBuilder(InjectorInterface::class)->getMockForAbstractClass();
$injector->method('canCreate')
->willReturn(true);
$injector->expects($this->atLeastOnce())
->method('create')
->with($className)
->willReturn($expected);
return $this->createContainerMock($injector);
}
public function testCreateUsesInjector()
{
$expected = new stdClass();
$className = 'SomeClassName';
$container = $this->createContainerForCreateTest($className, $expected);
$result = $this->instance->create($container, $className);
$this->assertSame($expected, $result);
}
public function testInstanceIsInvokable()
{
$expected = new stdClass();
$className = 'SomeOtherClassName';
$container = $this->createContainerForCreateTest($className, $expected);
$factory = $this->instance;
$result = $factory($container, $className);
$this->assertSame($expected, $result);
}
public function testCanCreateReturnsFalseWithoutInjector()
{
$container = $this->getMockBuilder(ContainerInterface::class)->getMockForAbstractClass();
$container->method('has')->willReturn(false);
$this->assertFalse($this->instance->canCreate($container, TestAsset\A::class));
}
public function testCreateWithoutInjectorThrowsException()
{
$container = $this->getMockBuilder(ContainerInterface::class)->getMockForAbstractClass();
$container->method('has')->willReturn(false);
$this->expectException(Exception\RuntimeException::class);
$this->instance->create($container, TestAsset\A::class);
}
public function testCreateWithInvalidInjectorThrowsException()
{
$container = $this->getMockBuilder(ContainerInterface::class)->getMockForAbstractClass();
$container->method('has')->willReturn(true);
$container->method('get')->willReturn(new stdClass());
$this->expectException(Exception\RuntimeException::class);
$this->instance->create($container, TestAsset\A::class);
}
}