|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GacelaTest\Integration\Framework\ClassResolver\InstanceCreator; |
|
6
|
|
|
|
|
7
|
|
|
use Gacela\Framework\ClassResolver\InstanceCreator\InstanceCreator; |
|
8
|
|
|
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigFileInterface; |
|
9
|
|
|
use GacelaTest\Fixtures\CustomClass; |
|
10
|
|
|
use GacelaTest\Fixtures\CustomClassWithDependencies; |
|
11
|
|
|
use GacelaTest\Fixtures\StringValue; |
|
12
|
|
|
use GacelaTest\Fixtures\StringValueInterface; |
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
|
14
|
|
|
|
|
15
|
|
|
final class InstanceCreatorTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
public function test_create_class_does_not_exists(): void |
|
18
|
|
|
{ |
|
19
|
|
|
$gacelaConfigFile = $this->createStub(GacelaConfigFileInterface::class); |
|
20
|
|
|
$instanceCreator = new InstanceCreator($gacelaConfigFile); |
|
21
|
|
|
|
|
22
|
|
|
self::assertNull($instanceCreator->createByClassName('non-existing-class')); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function test_create_class_without_dependencies(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$gacelaConfigFile = $this->createStub(GacelaConfigFileInterface::class); |
|
28
|
|
|
$instanceCreator = new InstanceCreator($gacelaConfigFile); |
|
29
|
|
|
|
|
30
|
|
|
self::assertEquals( |
|
31
|
|
|
new CustomClass(), |
|
32
|
|
|
$instanceCreator->createByClassName(CustomClass::class) |
|
33
|
|
|
); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function test_create_class_with_dependencies(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$gacelaConfigFile = $this->createStub(GacelaConfigFileInterface::class); |
|
39
|
|
|
$gacelaConfigFile->method('getMappingInterface')->willReturnMap([ |
|
40
|
|
|
[StringValueInterface::class, new StringValue('custom-string')], |
|
41
|
|
|
]); |
|
42
|
|
|
|
|
43
|
|
|
$instanceCreator = new InstanceCreator($gacelaConfigFile); |
|
44
|
|
|
|
|
45
|
|
|
self::assertEquals( |
|
46
|
|
|
new CustomClassWithDependencies(new StringValue('custom-string')), |
|
47
|
|
|
$instanceCreator->createByClassName(CustomClassWithDependencies::class) |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function test_caching_dependencies(): void |
|
52
|
|
|
{ |
|
53
|
|
|
$gacelaConfigFile = $this->createMock(GacelaConfigFileInterface::class); |
|
54
|
|
|
$gacelaConfigFile |
|
55
|
|
|
->expects(self::once()) |
|
56
|
|
|
->method('getMappingInterface') |
|
57
|
|
|
->with(StringValueInterface::class) |
|
58
|
|
|
->willReturn(new StringValue('custom-string')); |
|
59
|
|
|
|
|
60
|
|
|
$instanceCreator = new InstanceCreator($gacelaConfigFile); |
|
61
|
|
|
$actual1 = $instanceCreator->createByClassName(CustomClassWithDependencies::class); |
|
62
|
|
|
$actual2 = $instanceCreator->createByClassName(CustomClassWithDependencies::class); |
|
63
|
|
|
|
|
64
|
|
|
self::assertEquals($actual1, $actual2); |
|
65
|
|
|
self::assertNotSame($actual1, $actual2); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|