Passed
Pull Request — master (#653)
by Aleksei
07:43
created

ReaderTestCase   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 221
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 31
eloc 75
dl 0
loc 221
rs 9.92
c 2
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Spiral Framework package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Attributes\Reader;
13
14
use Spiral\Attributes\Exception\SemanticAttributeException;
15
use Spiral\Tests\Attributes\Concerns\InteractWithMetadata;
16
use Spiral\Tests\Attributes\Reader\Fixture\AnnotatedClass;
17
use Spiral\Tests\Attributes\Reader\Fixture\Annotation\ClassAnnotation;
18
use Spiral\Tests\Attributes\Reader\Fixture\Annotation\ConstantAnnotation;
19
use Spiral\Tests\Attributes\Reader\Fixture\Annotation\FunctionAnnotation;
20
use Spiral\Tests\Attributes\Reader\Fixture\Annotation\FunctionParameterAnnotation;
21
use Spiral\Tests\Attributes\Reader\Fixture\Annotation\MethodAnnotation;
22
use Spiral\Tests\Attributes\Reader\Fixture\Annotation\MethodParameterAnnotation;
23
use Spiral\Tests\Attributes\Reader\Fixture\Annotation\PropertyAnnotation;
24
use Spiral\Tests\Attributes\Reader\Fixture\ClassWithAnnotatedTrait;
25
use Spiral\Tests\Attributes\Reader\Fixture\UndefinedMeta;
26
use Spiral\Tests\Attributes\TestCase;
27
28
/**
29
 * @group unit
30
 * @group reader
31
 */
32
abstract class ReaderTestCase extends TestCase
33
{
34
    use InteractWithMetadata;
35
36
    /**
37
     * @var string
38
     */
39
    private const FIXTURE_FUNCTION_FQN = '\\Spiral\\Tests\\Attributes\\Reader\\Fixture\\annotated_function';
40
41
    /**
42
     * @var int
43
     */
44
    protected $classMetadataCount = 1;
45
46
    /**
47
     * @var int
48
     */
49
    protected $constantMetadataCount = 1;
50
51
    /**
52
     * @var int
53
     */
54
    protected $propertyMetadataCount = 1;
55
56
    /**
57
     * @var int
58
     */
59
    protected $methodMetadataCount = 1;
60
61
    /**
62
     * @var int
63
     */
64
    protected $methodParameterMetadataCount = 1;
65
66
    /**
67
     * @var int
68
     */
69
    protected $functionMetadataCount = 1;
70
71
    /**
72
     * @var int
73
     */
74
    protected $functionParameterMetadataCount = 1;
75
76
    public static function setUpBeforeClass(): void
77
    {
78
        parent::setUpBeforeClass();
79
80
        require_once __DIR__ . '/Fixture/function.php';
81
        require_once __DIR__ . '/Fixture/UndefinedMeta.php';
82
    }
83
84
    /**
85
     * @param string $suffix
86
     * @return string
87
     */
88
    protected function fixture(string $suffix): string
89
    {
90
        return '\\Spiral\\Tests\\Attributes\\Reader\\Fixture\\' . \trim($suffix, '\\');
91
    }
92
93
    public function testClassMetadataCount(): void
94
    {
95
        $this->assertCount($this->classMetadataCount,
96
            $this->getClassMetadata(AnnotatedClass::class)
97
        );
98
99
        $this->assertCount($this->classMetadataCount,
100
            $this->getClassMetadata(ClassWithAnnotatedTrait::class)
101
        );
102
    }
103
104
    public function testClassMetadataObjects(): void
105
    {
106
        $expected = $this->newAnnotation(ClassAnnotation::class, [
107
            'field' => 'value'
108
        ]);
109
110
        if ($this->classMetadataCount === 0) {
111
            $this->expectNotToPerformAssertions();
112
        }
113
114
        foreach ($this->getClassMetadata(AnnotatedClass::class) as $actual) {
115
            $this->assertEquals($expected, $actual);
116
        }
117
118
        foreach ($this->getClassMetadata(ClassWithAnnotatedTrait::class) as $actual) {
119
            $this->assertEquals($expected, $actual);
120
        }
121
    }
122
123
    public function testConstantMetadataCount(): void
124
    {
125
        $this->assertCount($this->constantMetadataCount,
126
            $this->getConstantMetadata(AnnotatedClass::class, 'CONSTANT')
127
        );
128
    }
129
130
    public function testConstantMetadataObjects(): void
131
    {
132
        $expected = $this->newAnnotation(ConstantAnnotation::class, [
133
            'field' => 'value'
134
        ]);
135
136
        if ($this->constantMetadataCount === 0) {
137
            $this->expectNotToPerformAssertions();
138
        }
139
140
        foreach ($this->getConstantMetadata(AnnotatedClass::class, 'CONSTANT') as $actual) {
141
            $this->assertEquals($expected, $actual);
142
        }
143
    }
144
145
    public function testPropertyMetadataCount(): void
146
    {
147
        $this->assertCount($this->propertyMetadataCount,
148
            $this->getPropertyMetadata(AnnotatedClass::class, 'property')
149
        );
150
    }
151
152
    public function testPropertyMetadataObjects(): void
153
    {
154
        $expected = $this->newAnnotation(PropertyAnnotation::class, [
155
            'field' => 'value'
156
        ]);
157
158
        if ($this->propertyMetadataCount === 0) {
159
            $this->expectNotToPerformAssertions();
160
        }
161
162
        foreach ($this->getPropertyMetadata(AnnotatedClass::class, 'property') as $actual) {
163
            $this->assertEquals($expected, $actual);
164
        }
165
    }
166
167
    public function testMethodMetadataCount(): void
168
    {
169
        $this->assertCount($this->methodMetadataCount,
170
            $this->getMethodMetadata(AnnotatedClass::class, 'method')
171
        );
172
    }
173
174
    public function testMethodMetadataObjects(): void
175
    {
176
        $expected = $this->newAnnotation(MethodAnnotation::class, [
177
            'field' => 'value'
178
        ]);
179
180
        if ($this->methodMetadataCount === 0) {
181
            $this->expectNotToPerformAssertions();
182
        }
183
184
        foreach ($this->getMethodMetadata(AnnotatedClass::class, 'method') as $actual) {
185
            $this->assertEquals($expected, $actual);
186
        }
187
    }
188
189
    public function testMethodParameterMetadataCount(): void
190
    {
191
        $this->assertCount($this->methodParameterMetadataCount,
192
            $this->getMethodParameterMetadata(AnnotatedClass::class, 'method', 'parameter')
193
        );
194
    }
195
196
    public function testMethodParameterMetadataObjects(): void
197
    {
198
        $expected = $this->newAnnotation(MethodParameterAnnotation::class, [
199
            'field' => 'value'
200
        ]);
201
202
        if ($this->methodParameterMetadataCount === 0) {
203
            $this->expectNotToPerformAssertions();
204
        }
205
206
        foreach ($this->getMethodParameterMetadata(AnnotatedClass::class, 'method', 'parameter') as $actual) {
207
            $this->assertEquals($expected, $actual);
208
        }
209
    }
210
211
    public function testFunctionMetadataCount(): void
212
    {
213
        $this->assertCount($this->functionMetadataCount,
214
            $this->getFunctionMetadata($this->fixture('annotated_function'))
215
        );
216
    }
217
218
    public function testFunctionMetadataObjects(): void
219
    {
220
        $expected = $this->newAnnotation(FunctionAnnotation::class, [
221
            'field' => 'value'
222
        ]);
223
224
        if ($this->functionMetadataCount === 0) {
225
            $this->expectNotToPerformAssertions();
226
        }
227
228
        foreach ($this->getFunctionMetadata($this->fixture('annotated_function')) as $actual) {
229
            $this->assertEquals($expected, $actual);
230
        }
231
    }
232
233
    public function testFunctionParameterMetadataCount(): void
234
    {
235
        $this->assertCount($this->functionParameterMetadataCount,
236
            $this->getFunctionParameterMetadata($this->fixture('annotated_function'), 'parameter')
237
        );
238
    }
239
240
    public function testFunctionParameterMetadataObjects(): void
241
    {
242
        $expected = $this->newAnnotation(FunctionParameterAnnotation::class, [
243
            'field' => 'value'
244
        ]);
245
246
        if ($this->functionParameterMetadataCount === 0) {
247
            $this->expectNotToPerformAssertions();
248
        }
249
250
        $function = $this->fixture('annotated_function');
251
        foreach ($this->getFunctionParameterMetadata($function, 'parameter') as $actual) {
252
            $this->assertEquals($expected, $actual);
253
        }
254
    }
255
}
256