Passed
Push — main ( 87fed9...27211a )
by Thierry
04:04
created

ExtendAnnotationTest::testDatabagAnnotation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 14
rs 9.9332
1
<?php
2
3
namespace Jaxon\Annotations\Tests\TestAnnotation;
4
5
use Jaxon\Annotations\Tests\AnnotationTrait;
6
use Jaxon\Annotations\Tests\App\Ajax\ExtendAnnotated;
7
use Jaxon\Annotations\Tests\App\Ajax\ClassExcluded;
8
use Jaxon\Annotations\Tests\App\Ajax\ExtendClassAnnotated;
9
use Jaxon\Exception\SetupException;
10
use PHPUnit\Framework\TestCase;
11
12
use function Jaxon\jaxon;
13
use function Jaxon\Annotations\_register;
14
15
class ExtendAnnotationTest extends TestCase
16
{
17
    use AnnotationTrait;
18
19
    /**
20
     * @var string
21
     */
22
    protected $sCacheDir;
23
24
    /**
25
     * @throws SetupException
26
     */
27
    public function setUp(): void
28
    {
29
        $this->sCacheDir = __DIR__ . '/../tmp';
30
        @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

30
        /** @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...
31
32
        jaxon()->di()->getPluginManager()->registerPlugins();
33
        _register();
34
35
        jaxon()->di()->val('jaxon_annotations_cache_dir', $this->sCacheDir);
36
    }
37
38
    /**
39
     * @throws SetupException
40
     */
41
    public function tearDown(): void
42
    {
43
        jaxon()->reset();
44
        parent::tearDown();
45
46
        // Delete the temp dir and all its content
47
        $aFiles = scandir($this->sCacheDir);
48
        foreach ($aFiles as $sFile)
49
        {
50
            if($sFile !== '.' && $sFile !== '..')
51
            {
52
                @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

52
                /** @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...
53
            }
54
        }
55
        @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

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