Passed
Push — master ( 2c1787...c36eb2 )
by Jesús
04:16 queued 13s
created

InstanceCreatorTest::test_caching_dependencies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 15
rs 9.9
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