Passed
Push — main ( c13965...4bb0c8 )
by Chema
19:59 queued 06:11
created

DocBlockResolverTest::serviceResolutionProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 28
rs 9.6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GacelaTest\Integration\Framework\DocBlockResolver;
6
7
use Gacela\Framework\Bootstrap\GacelaConfig;
8
use Gacela\Framework\ClassResolver\DocBlockService\DocBlockServiceNotFoundException;
9
use Gacela\Framework\ClassResolver\DocBlockService\DocBlockServiceResolver;
10
use Gacela\Framework\ClassResolver\DocBlockService\MissingClassDefinitionException;
11
use Gacela\Framework\DocBlockResolver\DocBlockResolvable;
12
use Gacela\Framework\DocBlockResolver\DocBlockResolver;
13
use Gacela\Framework\Gacela;
14
use GacelaTest\Integration\Framework\DocBlockResolver\Module\FakeCommand;
15
use GacelaTest\Integration\Framework\DocBlockResolver\Module\FakeConfig;
16
use GacelaTest\Integration\Framework\DocBlockResolver\Module\FakeFacade;
17
use GacelaTest\Integration\Framework\DocBlockResolver\Module\FakeFactory;
18
use GacelaTest\Integration\Framework\DocBlockResolver\Module\FakeRandomService;
19
use PHPUnit\Framework\TestCase;
20
21
use function sprintf;
22
23
final class DocBlockResolverTest extends TestCase
24
{
25
    protected function setUp(): void
26
    {
27
        Gacela::bootstrap(__DIR__, static function (GacelaConfig $config): void {
28
            $config->resetInMemoryCache();
29
        });
30
    }
31
32
    /**
33
     * Verifies that attempting to resolve a non-existent class from a @method annotation
34
     * throws MissingClassDefinitionException.
35
     */
36
    public function test_throws_exception_when_class_definition_is_missing(): void
37
    {
38
        $this->expectException(MissingClassDefinitionException::class);
39
40
        (new FakeCommand())->getUnknown();
41
    }
42
43
    /**
44
     * Verifies that DocBlockServiceResolver throws an exception when given an empty
45
     * service name, as there's no valid service to resolve.
46
     */
47
    public function test_throws_exception_when_service_name_is_empty(): void
48
    {
49
        $this->expectException(DocBlockServiceNotFoundException::class);
50
51
        $resolver = new DocBlockServiceResolver('');
52
        $command = new FakeCommand();
53
        $resolver->resolve($command);
54
    }
55
56
    /**
57
     * Tests that DocBlockResolver correctly resolves service types from @method annotations.
58
     *
59
     * @dataProvider serviceResolutionProvider
60
     */
61
    public function test_resolves_service_type_from_docblock(
62
        object $caller,
63
        string $methodName,
64
        string $expectedClass,
65
        string $expectedSuffix,
66
    ): void {
67
        $resolver = DocBlockResolver::fromCaller($caller);
68
        $actual = $resolver->getDocBlockResolvable($methodName);
69
        $expected = new DocBlockResolvable($expectedClass, $expectedSuffix);
70
71
        self::assertEquals(
72
            $expected,
73
            $actual,
74
            sprintf(
75
                'Failed to resolve %s() from %s to %s',
76
                $methodName,
77
                $caller::class,
78
                $expectedClass,
79
            ),
80
        );
81
    }
82
83
    /**
84
     * Provides test cases for service resolution from different callers.
85
     *
86
     * @return iterable<string, array{object, string, class-string, string}>
87
     */
88
    public static function serviceResolutionProvider(): iterable
89
    {
90
        yield 'Facade resolution from Command' => [
91
            new FakeCommand(),
92
            'getFacade',
93
            FakeFacade::class,
94
            'Facade',
95
        ];
96
97
        yield 'Factory resolution from Facade' => [
98
            new FakeFacade(),
99
            'getFactory',
100
            FakeFactory::class,
101
            'Factory',
102
        ];
103
104
        yield 'Config resolution from Factory' => [
105
            new FakeFactory(),
106
            'getConfig',
107
            FakeConfig::class,
108
            'Config',
109
        ];
110
111
        yield 'Custom service resolution from Command' => [
112
            new FakeCommand(),
113
            'getRandom',
114
            FakeRandomService::class,
115
            'FakeRandomService',
116
        ];
117
    }
118
}
119