Passed
Push — main ( 10601e...ca0571 )
by Thierry
04:19
created

AnnotationTest::testDatabagAnnotation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 14
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Jaxon\Annotations\Tests\TestAnnotation;
4
5
use Jaxon\Annotations\Tests\AnnotationTrait;
6
use Jaxon\Annotations\Tests\App\Ajax\Annotated;
7
use Jaxon\Annotations\Tests\App\Ajax\CallbackError;
8
use Jaxon\Annotations\Tests\App\Ajax\ClassAnnotated;
9
use Jaxon\Annotations\Tests\App\Ajax\ClassExcluded;
10
use Jaxon\Annotations\Tests\App\Ajax\ContainerError;
11
use Jaxon\Exception\SetupException;
12
use PHPUnit\Framework\TestCase;
13
14
use function Jaxon\jaxon;
15
use function Jaxon\Annotations\_register;
16
17
class AnnotationTest extends TestCase
18
{
19
    use AnnotationTrait;
20
21
    /**
22
     * @var string
23
     */
24
    protected $sCacheDir;
25
26
    /**
27
     * @throws SetupException
28
     */
29
    public function setUp(): void
30
    {
31
        $this->sCacheDir = __DIR__ . '/../tmp';
32
        @mkdir($this->sCacheDir);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

32
        /** @scrutinizer ignore-unhandled */ @mkdir($this->sCacheDir);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
33
34
        jaxon()->di()->getPluginManager()->registerPlugins();
35
        _register();
36
37
        jaxon()->di()->val('jaxon_annotations_cache_dir', $this->sCacheDir);
38
    }
39
40
    /**
41
     * @throws SetupException
42
     */
43
    public function tearDown(): void
44
    {
45
        jaxon()->reset();
46
        parent::tearDown();
47
48
        // Delete the temp dir and all its content
49
        $aFiles = scandir($this->sCacheDir);
50
        foreach ($aFiles as $sFile)
51
        {
52
            if($sFile !== '.' && $sFile !== '..')
53
            {
54
                @unlink($this->sCacheDir . DIRECTORY_SEPARATOR . $sFile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

54
                /** @scrutinizer ignore-unhandled */ @unlink($this->sCacheDir . DIRECTORY_SEPARATOR . $sFile);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
55
            }
56
        }
57
        @rmdir($this->sCacheDir);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for rmdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

57
        /** @scrutinizer ignore-unhandled */ @rmdir($this->sCacheDir);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
58
    }
59
60
    /**
61
     * @throws SetupException
62
     */
63
    public function testUploadAndExcludeAnnotation()
64
    {
65
        $xMetadata = $this->getAttributes(Annotated::class, ['saveFiles', 'doNot']);
66
        $bExcluded = $xMetadata->isExcluded();
67
        $aProperties = $xMetadata->getProperties();
68
        $aProtected = $xMetadata->getProtectedMethods();
69
70
        $this->assertFalse($bExcluded);
71
72
        $this->assertCount(1, $aProperties);
73
        $this->assertArrayHasKey('saveFiles', $aProperties);
74
        $this->assertCount(1, $aProperties['saveFiles']);
75
        $this->assertEquals("'user-files'", $aProperties['saveFiles']['upload']);
76
77
        $this->assertCount(1, $aProtected);
78
        $this->assertEquals('doNot', $aProtected[0]);
79
    }
80
81
    /**
82
     * @throws SetupException
83
     */
84
    public function testDatabagAnnotation()
85
    {
86
        $xMetadata = $this->getAttributes(Annotated::class, ['withBags']);
87
        $bExcluded = $xMetadata->isExcluded();
88
        $aProperties = $xMetadata->getProperties();
89
90
        $this->assertFalse($bExcluded);
91
92
        $this->assertCount(1, $aProperties);
93
        $this->assertArrayHasKey('withBags', $aProperties);
94
        $this->assertCount(1, $aProperties['withBags']);
95
        $this->assertCount(2, $aProperties['withBags']['bags']);
96
        $this->assertEquals('user.name', $aProperties['withBags']['bags'][0]);
97
        $this->assertEquals('page.number', $aProperties['withBags']['bags'][1]);
98
    }
99
100
    /**
101
     * @throws SetupException
102
     */
103
    public function testServerCallbacksAnnotation()
104
    {
105
        $xMetadata = $this->getAttributes(Annotated::class,
106
            ['cbSingle', 'cbMultiple', 'cbParams']);
107
        $bExcluded = $xMetadata->isExcluded();
108
        $aProperties = $xMetadata->getProperties();
109
110
        $this->assertFalse($bExcluded);
111
112
        $this->assertCount(3, $aProperties);
113
        $this->assertArrayHasKey('cbSingle', $aProperties);
114
        $this->assertArrayHasKey('cbMultiple', $aProperties);
115
        $this->assertArrayHasKey('cbParams', $aProperties);
116
117
        $this->assertCount(1, $aProperties['cbSingle']['__before']);
118
        $this->assertCount(2, $aProperties['cbMultiple']['__before']);
119
        $this->assertCount(2, $aProperties['cbParams']['__before']);
120
        $this->assertArrayHasKey('funcBefore', $aProperties['cbSingle']['__before']);
121
        $this->assertArrayHasKey('funcBefore1', $aProperties['cbMultiple']['__before']);
122
        $this->assertArrayHasKey('funcBefore2', $aProperties['cbMultiple']['__before']);
123
        $this->assertArrayHasKey('funcBefore1', $aProperties['cbParams']['__before']);
124
        $this->assertArrayHasKey('funcBefore2', $aProperties['cbParams']['__before']);
125
        $this->assertIsArray($aProperties['cbSingle']['__before']['funcBefore']);
126
        $this->assertIsArray($aProperties['cbMultiple']['__before']['funcBefore1']);
127
        $this->assertIsArray($aProperties['cbMultiple']['__before']['funcBefore2']);
128
        $this->assertIsArray($aProperties['cbParams']['__before']['funcBefore1']);
129
        $this->assertIsArray($aProperties['cbParams']['__before']['funcBefore2']);
130
131
        $this->assertCount(1, $aProperties['cbSingle']['__after']);
132
        $this->assertCount(3, $aProperties['cbMultiple']['__after']);
133
        $this->assertCount(1, $aProperties['cbParams']['__after']);
134
        $this->assertArrayHasKey('funcAfter', $aProperties['cbSingle']['__after']);
135
        $this->assertArrayHasKey('funcAfter1', $aProperties['cbMultiple']['__after']);
136
        $this->assertArrayHasKey('funcAfter2', $aProperties['cbMultiple']['__after']);
137
        $this->assertArrayHasKey('funcAfter3', $aProperties['cbMultiple']['__after']);
138
        $this->assertArrayHasKey('funcAfter1', $aProperties['cbParams']['__after']);
139
        $this->assertIsArray($aProperties['cbSingle']['__after']['funcAfter']);
140
        $this->assertIsArray($aProperties['cbMultiple']['__after']['funcAfter1']);
141
        $this->assertIsArray($aProperties['cbMultiple']['__after']['funcAfter2']);
142
        $this->assertIsArray($aProperties['cbMultiple']['__after']['funcAfter3']);
143
        $this->assertIsArray($aProperties['cbParams']['__after']['funcAfter1']);
144
    }
145
146
    /**
147
     * @throws SetupException
148
     */
149
    public function testContainerAnnotation()
150
    {
151
        $xMetadata = $this->getAttributes(Annotated::class, ['di1', 'di2']);
152
        $bExcluded = $xMetadata->isExcluded();
153
        $aProperties = $xMetadata->getProperties();
154
155
        $this->assertFalse($bExcluded);
156
157
        $this->assertCount(2, $aProperties);
158
        $this->assertArrayHasKey('di1', $aProperties);
159
        $this->assertArrayHasKey('di2', $aProperties);
160
        $this->assertCount(2, $aProperties['di1']['__di']);
161
        $this->assertCount(2, $aProperties['di2']['__di']);
162
        $this->assertEquals('Jaxon\Annotations\Tests\Service\ColorService', $aProperties['di1']['__di']['colorService']);
163
        $this->assertEquals('Jaxon\Annotations\Tests\App\Ajax\FontService', $aProperties['di1']['__di']['fontService']);
164
        $this->assertEquals('Jaxon\Annotations\Tests\Service\ColorService', $aProperties['di2']['__di']['colorService']);
165
        $this->assertEquals('Jaxon\Annotations\Tests\Service\TextService', $aProperties['di2']['__di']['textService']);
166
    }
167
168
    /**
169
     * @throws SetupException
170
     */
171
    public function testClassAnnotation()
172
    {
173
        $xMetadata = $this->getAttributes(ClassAnnotated::class, []);
174
        $bExcluded = $xMetadata->isExcluded();
175
        $aProperties = $xMetadata->getProperties();
176
177
        $this->assertFalse($bExcluded);
178
179
        $this->assertCount(1, $aProperties);
180
        $this->assertArrayHasKey('*', $aProperties);
181
        $this->assertCount(5, $aProperties['*']);
182
        $this->assertArrayHasKey('bags', $aProperties['*']);
183
        $this->assertArrayHasKey('callback', $aProperties['*']);
184
        $this->assertArrayHasKey('__before', $aProperties['*']);
185
        $this->assertArrayHasKey('__after', $aProperties['*']);
186
    }
187
188
    /**
189
     * @throws SetupException
190
     */
191
    public function testClassBagsAnnotation()
192
    {
193
        $xMetadata = $this->getAttributes(ClassAnnotated::class, []);
194
        $aProperties = $xMetadata->getProperties();
195
196
        $this->assertCount(2, $aProperties['*']['bags']);
197
        $this->assertEquals('user.name', $aProperties['*']['bags'][0]);
198
        $this->assertEquals('page.number', $aProperties['*']['bags'][1]);
199
    }
200
201
    /**
202
     * @throws SetupException
203
     */
204
    public function testClassCallbackAnnotation()
205
    {
206
        $xMetadata = $this->getAttributes(ClassAnnotated::class, []);
207
        $aProperties = $xMetadata->getProperties();
208
209
        $this->assertIsArray($aProperties['*']['callback']);
210
        $this->assertEquals('jaxon.callback.global', $aProperties['*']['callback'][0]);
211
    }
212
213
    /**
214
     * @throws SetupException
215
     */
216
    public function testClassBeforeAnnotation()
217
    {
218
        $xMetadata = $this->getAttributes(ClassAnnotated::class, []);
219
        $aProperties = $xMetadata->getProperties();
220
221
        $this->assertCount(2, $aProperties['*']['__before']);
222
        $this->assertArrayHasKey('funcBefore1', $aProperties['*']['__before']);
223
        $this->assertArrayHasKey('funcBefore2', $aProperties['*']['__before']);
224
        $this->assertIsArray($aProperties['*']['__before']['funcBefore1']);
225
        $this->assertIsArray($aProperties['*']['__before']['funcBefore2']);
226
    }
227
228
    /**
229
     * @throws SetupException
230
     */
231
    public function testClassAfterAnnotation()
232
    {
233
        $xMetadata = $this->getAttributes(ClassAnnotated::class, []);
234
        $aProperties = $xMetadata->getProperties();
235
236
        $this->assertCount(3, $aProperties['*']['__after']);
237
        $this->assertArrayHasKey('funcAfter1', $aProperties['*']['__after']);
238
        $this->assertArrayHasKey('funcAfter2', $aProperties['*']['__after']);
239
        $this->assertArrayHasKey('funcAfter3', $aProperties['*']['__after']);
240
        $this->assertIsArray($aProperties['*']['__after']['funcAfter1']);
241
        $this->assertIsArray($aProperties['*']['__after']['funcAfter2']);
242
        $this->assertIsArray($aProperties['*']['__after']['funcAfter3']);
243
    }
244
245
    /**
246
     * @throws SetupException
247
     */
248
    public function testClassDiAnnotation()
249
    {
250
        $xMetadata = $this->getAttributes(ClassAnnotated::class, []);
251
        $aProperties = $xMetadata->getProperties();
252
253
        $this->assertCount(3, $aProperties['*']['__di']);
254
        $this->assertArrayHasKey('colorService', $aProperties['*']['__di']);
255
        $this->assertArrayHasKey('textService', $aProperties['*']['__di']);
256
        $this->assertArrayHasKey('fontService', $aProperties['*']['__di']);
257
        $this->assertEquals('Jaxon\Annotations\Tests\Service\ColorService', $aProperties['*']['__di']['colorService']);
258
        $this->assertEquals('Jaxon\Annotations\Tests\Service\TextService', $aProperties['*']['__di']['textService']);
259
        $this->assertEquals('Jaxon\Annotations\Tests\App\Ajax\FontService', $aProperties['*']['__di']['fontService']);
260
    }
261
262
    /**
263
     * @throws SetupException
264
     */
265
    public function testClassExcludeAnnotation()
266
    {
267
        $xMetadata = $this->getAttributes(ClassExcluded::class,
268
            ['doNot', 'withBags', 'cbSingle']);
269
        $bExcluded = $xMetadata->isExcluded();
270
        $aProperties = $xMetadata->getProperties();
271
        $aProtected = $xMetadata->getProtectedMethods();
272
273
        $this->assertTrue($bExcluded);
274
        $this->assertEmpty($aProperties);
275
        $this->assertEmpty($aProtected);
276
    }
277
278
    public function testExcludeAnnotationError()
279
    {
280
        $this->expectException(SetupException::class);
281
        $this->getAttributes(Annotated::class, ['doNotError']);
282
    }
283
284
    public function testDatabagAnnotationError()
285
    {
286
        $this->expectException(SetupException::class);
287
        $this->getAttributes(Annotated::class, ['withBagsError']);
288
    }
289
290
    public function testUploadAnnotationWrongName()
291
    {
292
        $this->expectException(SetupException::class);
293
        $this->getAttributes(Annotated::class, ['saveFilesWrongName']);
294
    }
295
296
    public function testUploadAnnotationMultiple()
297
    {
298
        $this->expectException(SetupException::class);
299
        $this->getAttributes(Annotated::class, ['saveFilesMultiple']);
300
    }
301
302
    public function testCallbacksBeforeAnnotationNoCall()
303
    {
304
        $this->expectException(SetupException::class);
305
        $this->getAttributes(Annotated::class, ['cbBeforeNoCall']);
306
    }
307
308
    public function testCallbacksBeforeAnnotationUnknownAttr()
309
    {
310
        $this->expectException(SetupException::class);
311
        $this->getAttributes(Annotated::class, ['cbBeforeUnknownAttr']);
312
    }
313
314
    public function testCallbacksBeforeAnnotationWrongAttrType()
315
    {
316
        $this->expectException(SetupException::class);
317
        $this->getAttributes(Annotated::class, ['cbBeforeWrongAttrType']);
318
    }
319
320
    public function testCallbacksAfterAnnotationNoCall()
321
    {
322
        $this->expectException(SetupException::class);
323
        $this->getAttributes(Annotated::class, ['cbAfterNoCall']);
324
    }
325
326
    public function testCallbacksAfterAnnotationUnknownAttr()
327
    {
328
        $this->expectException(SetupException::class);
329
        $this->getAttributes(Annotated::class, ['cbAfterUnknownAttr']);
330
    }
331
332
    public function testCallbacksAfterAnnotationWrongAttrType()
333
    {
334
        $this->expectException(SetupException::class);
335
        $this->getAttributes(Annotated::class, ['cbAfterWrongAttrType']);
336
    }
337
338
    public function testContainerAnnotationUnknownAttr()
339
    {
340
        $this->expectException(SetupException::class);
341
        $this->getAttributes(Annotated::class, ['diUnknownAttr']);
342
    }
343
344
    public function testContainerAnnotationWrongAttrType()
345
    {
346
        $this->expectException(SetupException::class);
347
        $this->getAttributes(Annotated::class, ['diWrongAttrType']);
348
    }
349
350
    public function testContainerAnnotationWrongClassType()
351
    {
352
        $this->expectException(SetupException::class);
353
        $this->getAttributes(Annotated::class, ['diWrongClassType']);
354
    }
355
356
    public function testContainerAnnotationWrongVarCount()
357
    {
358
        $this->expectException(SetupException::class);
359
        $this->getAttributes(Annotated::class, ['diWrongVarCount']);
360
    }
361
362
    public function testContainerErrorTwiceOnProp()
363
    {
364
        $this->expectException(SetupException::class);
365
        $this->getAttributes(ContainerError::class, [], ['prop']);
366
    }
367
368
    public function testCallbackErrorNoName()
369
    {
370
        $this->expectException(SetupException::class);
371
        $this->getAttributes(CallbackError::class, ['noName']);
372
    }
373
374
    public function testCallbackErrorWrongNameType()
375
    {
376
        $this->expectException(SetupException::class);
377
        $this->getAttributes(CallbackError::class, ['wrongNameType']);
378
    }
379
380
    public function testCallbackErrorWrongNameAttr()
381
    {
382
        $this->expectException(SetupException::class);
383
        $this->getAttributes(CallbackError::class, ['wrongNameAttr']);
384
    }
385
386
    public function testCallbackErrorNameWithSpace()
387
    {
388
        $this->expectException(SetupException::class);
389
        $this->getAttributes(CallbackError::class, ['nameWithSpace']);
390
    }
391
392
    public function testCallbackErrorStartWithInt()
393
    {
394
        $this->expectException(SetupException::class);
395
        $this->getAttributes(CallbackError::class, ['startWithInt']);
396
    }
397
}
398