Passed
Push — feature/add-cache-created-even... ( 7e365d...949ace )
by Chema
08:30 queued 04:40
created

AnonymousClassesTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GacelaTest\Integration\Framework\GlobalServices;
6
7
use Gacela\Framework\AbstractConfig;
8
use Gacela\Framework\AbstractDependencyProvider;
9
use Gacela\Framework\AbstractFacade;
10
use Gacela\Framework\AbstractFactory;
11
use Gacela\Framework\ClassResolver\GlobalInstance\AnonymousGlobal;
12
use Gacela\Framework\Container\Container;
13
use Gacela\Framework\Gacela;
14
use PHPUnit\Framework\TestCase;
15
16
final class AnonymousClassesTest extends TestCase
17
{
18
    protected function setUp(): void
19
    {
20
        Gacela::bootstrap(__DIR__);
21
    }
22
23
    public function test_class_resolver_using_anonymous_classes(): void
24
    {
25
        $this->registerConfig();
26
        $this->registerFactory();
27
        $this->registerDependencyProvider();
28
29
        $facade = new class() extends AbstractFacade {
30
            public function getSomething(): array
31
            {
32
                return $this->getFactory()
33
                    ->createDomainClass()
0 ignored issues
show
Bug introduced by
The method createDomainClass() does not exist on Gacela\Framework\AbstractFactory. It seems like you code against a sub-type of Gacela\Framework\AbstractFactory such as GacelaTest\Benchmark\Fra...eCache\ModuleD\FactoryD or GacelaTest\Benchmark\Fra...leCache\ModuleF\Factory or GacelaTest\Feature\Frame...ileCache\Module\Factory or GacelaTest\Benchmark\Fra...\ModuleG\ModuleGFactory or GacelaTest\Benchmark\Fra...eCache\ModuleC\FactoryC or GacelaTest\Benchmark\Fra...eCache\ModuleE\FactoryE or anonymous//tests/Benchma...ymousGlobalsBench.php$3 or GacelaTest\Benchmark\Fra...ule\NormalModuleFactory or GacelaTest\Benchmark\Fra...eCache\ModuleA\FactoryA or anonymous//tests/Integra...nymousClassesTest.php$3 or GacelaTest\Benchmark\Fra...eCache\ModuleB\FactoryB. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
                    ->/** @scrutinizer ignore-call */ createDomainClass()
Loading history...
34
                    ->getConfigValues();
35
            }
36
37
            public function greet(string $name): string
38
            {
39
                return $this->getFactory()
40
                    ->createDomainClass()
41
                    ->greet($name);
42
            }
43
        };
44
45
        self::assertSame([1, 2, 3, 4, 5], $facade->getSomething());
46
        self::assertSame('Hello, Chema!', $facade->greet('Chema'));
47
    }
48
49
    /**
50
     * Using $this object as context.
51
     */
52
    private function registerConfig(): void
53
    {
54
        AnonymousGlobal::addGlobal(
55
            $this,
56
            new class() extends AbstractConfig {
57
                /**
58
                 * @return int[]
59
                 */
60
                public function getValues(): array
61
                {
62
                    return [1, 2, 3, 4, 5];
63
                }
64
            }
65
        );
66
    }
67
68
    /**
69
     * Using a string (class name) as context.
70
     */
71
    private function registerFactory(): void
72
    {
73
        AnonymousGlobal::addGlobal(
74
            'AnonymousClassesTest',
75
            new class() extends AbstractFactory {
76
                public function createDomainClass(): object
77
                {
78
                    /** @var object $myService */
79
                    $myService = $this->getProvidedDependency('my-greeter');
80
81
                    /** @var int[] $configValues */
82
                    $configValues = $this->getConfig()->getValues();
83
84
                    return new class($myService, ...$configValues) {
85
                        private object $myService;
86
                        /** @var int[] */
87
                        private array $configValues;
88
89
                        public function __construct(
90
                            object $myService,
91
                            int ...$configValues
92
                        ) {
93
                            $this->myService = $myService;
94
                            $this->configValues = $configValues;
95
                        }
96
97
                        public function getConfigValues(): array
98
                        {
99
                            return $this->configValues;
100
                        }
101
102
                        public function greet(string $name): string
103
                        {
104
                            return $this->myService->greet($name);
105
                        }
106
                    };
107
                }
108
            }
109
        );
110
    }
111
112
    private function registerDependencyProvider(): void
113
    {
114
        AnonymousGlobal::addGlobal(
115
            $this,
116
            new class() extends AbstractDependencyProvider {
117
                public function provideModuleDependencies(Container $container): void
118
                {
119
                    $container->set('my-greeter', new class() {
120
                        public function greet(string $name): string
121
                        {
122
                            return "Hello, {$name}!";
123
                        }
124
                    });
125
                }
126
            }
127
        );
128
    }
129
}
130