exceptionIsThrownIfAnyStepCannotBeResolved()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 12
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioTest\Shlink\Config\Factory;
6
7
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
8
use Laminas\ServiceManager\ServiceManager;
9
use PHPUnit\Framework\TestCase;
10
use Shlinkio\Shlink\Config\Exception\InvalidArgumentException;
11
use Shlinkio\Shlink\Config\Factory\DottedAccessConfigAbstractFactory;
12
13
class DottedAccessConfigAbstractFactoryTest extends TestCase
14
{
15
    private DottedAccessConfigAbstractFactory $factory;
16
17
    public function setUp(): void
18
    {
19
        $this->factory = new DottedAccessConfigAbstractFactory();
20
    }
21
22
    /**
23
     * @test
24
     * @dataProvider provideDotNames
25
     */
26
    public function canCreateOnlyServicesWithDot(string $serviceName, bool $canCreate): void
27
    {
28
        $this->assertEquals($canCreate, $this->factory->canCreate(new ServiceManager(), $serviceName));
29
    }
30
31
    public function provideDotNames(): iterable
32
    {
33
        yield 'with a valid service' => ['foo.bar', true];
34
        yield 'with another valid service' => ['config.something', true];
35
        yield 'with an invalid service' => ['config_something', false];
36
        yield 'with another invalid service' => ['foo', false];
37
    }
38
39
    /** @test */
40
    public function throwsExceptionWhenFirstPartOfTheServiceIsNotRegistered(): void
41
    {
42
        $this->expectException(ServiceNotCreatedException::class);
43
        $this->expectExceptionMessage(
44
            'Defined service "foo" could not be found in container after resolving dotted expression "foo.bar"',
45
        );
46
47
        $this->factory->__invoke(new ServiceManager(), 'foo.bar');
48
    }
49
50
    /** @test */
51
    public function dottedNotationIsRecursivelyResolvedUntilLastValueIsFoundAndReturned(): void
52
    {
53
        $expected = 'this is the result';
54
55
        $result = $this->factory->__invoke(new ServiceManager(['services' => [
56
            'foo' => [
57
                'bar' => ['baz' => $expected],
58
            ],
59
        ]]), 'foo.bar.baz');
60
61
        $this->assertEquals($expected, $result);
62
    }
63
64
    /** @test */
65
    public function exceptionIsThrownIfAnyStepCannotBeResolved(): void
66
    {
67
        $this->expectException(InvalidArgumentException::class);
68
        $this->expectExceptionMessage(
69
            'The key "baz" provided in the dotted notation could not be found in the array service',
70
        );
71
72
        $this->factory->__invoke(new ServiceManager(['services' => [
73
            'foo' => [
74
                'bar' => ['something' => 123],
75
            ],
76
        ]]), 'foo.bar.baz');
77
    }
78
}
79