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

DocBlockAnnotationTest::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\DocBlockAnnotated;
7
use Jaxon\Annotations\Tests\App\Ajax\DocBlockClassAnnotated;
8
use Jaxon\Annotations\Tests\App\Ajax\DocBlockClassExcluded;
9
use Jaxon\Exception\SetupException;
10
use PHPUnit\Framework\TestCase;
11
12
use function Jaxon\jaxon;
13
use function mkdir;
14
use function rmdir;
15
use function Jaxon\Annotations\_register;
16
17
class DocBlockAnnotationTest 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(DocBlockAnnotated::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, $aProtected);
73
        $this->assertEquals('doNot', $aProtected[0]);
74
75
        $this->assertCount(1, $aProperties);
76
        $this->assertArrayHasKey('saveFiles', $aProperties);
77
        $this->assertCount(1, $aProperties['saveFiles']);
78
        $this->assertEquals("'user-files'", $aProperties['saveFiles']['upload']);
79
    }
80
81
    /**
82
     * @throws SetupException
83
     */
84
    public function testDatabagAnnotation()
85
    {
86
        $xMetadata = $this->getAttributes(DocBlockAnnotated::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 testCallbackAnnotation()
104
    {
105
        $xMetadata = $this->getAttributes(DocBlockAnnotated::class, ['withCallback']);
106
        $bExcluded = $xMetadata->isExcluded();
107
        $aProperties = $xMetadata->getProperties();
108
109
        $this->assertFalse($bExcluded);
110
111
        $this->assertCount(1, $aProperties);
112
        $this->assertArrayHasKey('withCallback', $aProperties);
113
        $this->assertCount(1, $aProperties['withCallback']);
114
        $this->assertIsArray($aProperties['withCallback']['callback']);
115
        $this->assertEquals('jaxon.ajax.callback.test', $aProperties['withCallback']['callback'][0]);
116
    }
117
118
    /**
119
     * @throws SetupException
120
     */
121
    public function testHooksAnnotation()
122
    {
123
        $xMetadata = $this->getAttributes(DocBlockAnnotated::class,
124
            ['cbSingle', 'cbMultiple', 'cbParams']);
125
        $bExcluded = $xMetadata->isExcluded();
126
        $aProperties = $xMetadata->getProperties();
127
128
        $this->assertFalse($bExcluded);
129
130
        $this->assertCount(3, $aProperties);
131
        $this->assertArrayHasKey('cbSingle', $aProperties);
132
        $this->assertArrayHasKey('cbMultiple', $aProperties);
133
        $this->assertArrayHasKey('cbParams', $aProperties);
134
135
        $this->assertCount(1, $aProperties['cbSingle']['__before']);
136
        $this->assertCount(2, $aProperties['cbMultiple']['__before']);
137
        $this->assertCount(2, $aProperties['cbParams']['__before']);
138
        $this->assertArrayHasKey('funcBefore', $aProperties['cbSingle']['__before']);
139
        $this->assertArrayHasKey('funcBefore1', $aProperties['cbMultiple']['__before']);
140
        $this->assertArrayHasKey('funcBefore2', $aProperties['cbMultiple']['__before']);
141
        $this->assertArrayHasKey('funcBefore1', $aProperties['cbParams']['__before']);
142
        $this->assertArrayHasKey('funcBefore2', $aProperties['cbParams']['__before']);
143
        $this->assertIsArray($aProperties['cbSingle']['__before']['funcBefore']);
144
        $this->assertIsArray($aProperties['cbMultiple']['__before']['funcBefore1']);
145
        $this->assertIsArray($aProperties['cbMultiple']['__before']['funcBefore2']);
146
        $this->assertIsArray($aProperties['cbParams']['__before']['funcBefore1']);
147
        $this->assertIsArray($aProperties['cbParams']['__before']['funcBefore2']);
148
149
        $this->assertCount(1, $aProperties['cbSingle']['__after']);
150
        $this->assertCount(3, $aProperties['cbMultiple']['__after']);
151
        $this->assertCount(1, $aProperties['cbParams']['__after']);
152
        $this->assertArrayHasKey('funcAfter', $aProperties['cbSingle']['__after']);
153
        $this->assertArrayHasKey('funcAfter1', $aProperties['cbMultiple']['__after']);
154
        $this->assertArrayHasKey('funcAfter2', $aProperties['cbMultiple']['__after']);
155
        $this->assertArrayHasKey('funcAfter3', $aProperties['cbMultiple']['__after']);
156
        $this->assertArrayHasKey('funcAfter1', $aProperties['cbParams']['__after']);
157
        $this->assertIsArray($aProperties['cbSingle']['__after']['funcAfter']);
158
        $this->assertIsArray($aProperties['cbMultiple']['__after']['funcAfter1']);
159
        $this->assertIsArray($aProperties['cbMultiple']['__after']['funcAfter2']);
160
        $this->assertIsArray($aProperties['cbMultiple']['__after']['funcAfter3']);
161
        $this->assertIsArray($aProperties['cbParams']['__after']['funcAfter1']);
162
    }
163
164
    /**
165
     * @throws SetupException
166
     */
167
    public function testContainerAnnotation()
168
    {
169
        $xMetadata = $this->getAttributes(DocBlockAnnotated::class, ['di1', 'di2']);
170
        $bExcluded = $xMetadata->isExcluded();
171
        $aProperties = $xMetadata->getProperties();
172
173
        $this->assertFalse($bExcluded);
174
175
        $this->assertCount(2, $aProperties);
176
        $this->assertArrayHasKey('di1', $aProperties);
177
        $this->assertArrayHasKey('di2', $aProperties);
178
        $this->assertCount(2, $aProperties['di1']['__di']);
179
        $this->assertCount(2, $aProperties['di2']['__di']);
180
        $this->assertEquals('Jaxon\Annotations\Tests\Service\ColorService', $aProperties['di1']['__di']['colorService']);
181
        $this->assertEquals('Jaxon\Annotations\Tests\App\Ajax\FontService', $aProperties['di1']['__di']['fontService']);
182
        $this->assertEquals('Jaxon\Annotations\Tests\Service\ColorService', $aProperties['di2']['__di']['colorService']);
183
        $this->assertEquals('Jaxon\Annotations\Tests\Service\TextService', $aProperties['di2']['__di']['textService']);
184
    }
185
186
    /**
187
     * @throws SetupException
188
     */
189
    public function testClassAnnotation()
190
    {
191
        $xMetadata = $this->getAttributes(DocBlockClassAnnotated::class, []);
192
        // $this->assertEquals('', json_encode($aProperties));
193
        $bExcluded = $xMetadata->isExcluded();
194
        $aProperties = $xMetadata->getProperties();
195
196
        $this->assertFalse($bExcluded);
197
198
        $this->assertCount(1, $aProperties);
199
        $this->assertArrayHasKey('*', $aProperties);
200
        $this->assertCount(5, $aProperties['*']);
201
        $this->assertArrayHasKey('bags', $aProperties['*']);
202
        $this->assertArrayHasKey('callback', $aProperties['*']);
203
        $this->assertArrayHasKey('__before', $aProperties['*']);
204
        $this->assertArrayHasKey('__after', $aProperties['*']);
205
206
        $this->assertCount(2, $aProperties['*']['bags']);
207
        $this->assertEquals('user.name', $aProperties['*']['bags'][0]);
208
        $this->assertEquals('page.number', $aProperties['*']['bags'][1]);
209
210
        $this->assertIsArray($aProperties['*']['callback']);
211
        $this->assertEquals('jaxon.ajax.callback.test', $aProperties['*']['callback'][0]);
212
213
        $this->assertCount(2, $aProperties['*']['__before']);
214
        $this->assertArrayHasKey('funcBefore1', $aProperties['*']['__before']);
215
        $this->assertArrayHasKey('funcBefore2', $aProperties['*']['__before']);
216
        $this->assertIsArray($aProperties['*']['__before']['funcBefore1']);
217
        $this->assertIsArray($aProperties['*']['__before']['funcBefore2']);
218
219
        $this->assertCount(3, $aProperties['*']['__after']);
220
        $this->assertArrayHasKey('funcAfter1', $aProperties['*']['__after']);
221
        $this->assertArrayHasKey('funcAfter2', $aProperties['*']['__after']);
222
        $this->assertArrayHasKey('funcAfter3', $aProperties['*']['__after']);
223
        $this->assertIsArray($aProperties['*']['__after']['funcAfter1']);
224
        $this->assertIsArray($aProperties['*']['__after']['funcAfter2']);
225
        $this->assertIsArray($aProperties['*']['__after']['funcAfter3']);
226
227
        $this->assertCount(3, $aProperties['*']['__di']);
228
        $this->assertArrayHasKey('colorService', $aProperties['*']['__di']);
229
        $this->assertArrayHasKey('textService', $aProperties['*']['__di']);
230
        $this->assertArrayHasKey('fontService', $aProperties['*']['__di']);
231
        $this->assertEquals('Jaxon\Annotations\Tests\Service\ColorService', $aProperties['*']['__di']['colorService']);
232
        $this->assertEquals('Jaxon\Annotations\Tests\Service\TextService', $aProperties['*']['__di']['textService']);
233
        $this->assertEquals('Jaxon\Annotations\Tests\App\Ajax\FontService', $aProperties['*']['__di']['fontService']);
234
    }
235
236
    /**
237
     * @throws SetupException
238
     */
239
    public function testClassExcludeAnnotation()
240
    {
241
        $xMetadata = $this->getAttributes(DocBlockClassExcluded::class,
242
            ['doNot', 'withBags', 'cbSingle']);
243
        $bExcluded = $xMetadata->isExcluded();
244
        $aProperties = $xMetadata->getProperties();
245
        $aProtected = $xMetadata->getProtectedMethods();
246
247
        $this->assertTrue($bExcluded);
248
        $this->assertEmpty($aProperties);
249
        $this->assertEmpty($aProtected);
250
    }
251
252
    public function testUploadAnnotationErrorFieldName()
253
    {
254
        $this->expectException(SetupException::class);
255
        $this->getAttributes(DocBlockAnnotated::class, ['saveFileErrorFieldName']);
256
    }
257
258
    public function testUploadAnnotationErrorFieldNumber()
259
    {
260
        $this->expectException(SetupException::class);
261
        $this->getAttributes(DocBlockAnnotated::class, ['saveFileErrorFieldNumber']);
262
    }
263
264
    public function testDatabagAnnotationErrorName()
265
    {
266
        $this->expectException(SetupException::class);
267
        $this->getAttributes(DocBlockAnnotated::class, ['withBagsErrorName']);
268
    }
269
270
    public function testDatabagAnnotationErrorNumber()
271
    {
272
        $this->expectException(SetupException::class);
273
        $this->getAttributes(DocBlockAnnotated::class, ['withBagsErrorNumber']);
274
    }
275
276
    public function testContainerAnnotationErrorAttr()
277
    {
278
        $this->expectException(SetupException::class);
279
        $this->getAttributes(DocBlockAnnotated::class, ['diErrorAttr']);
280
    }
281
282
    public function testContainerAnnotationErrorClass()
283
    {
284
        $this->expectException(SetupException::class);
285
        $this->getAttributes(DocBlockAnnotated::class, ['diErrorClass']);
286
    }
287
288
    public function testContainerAnnotationErrorOneParam()
289
    {
290
        $this->expectException(SetupException::class);
291
        $this->getAttributes(DocBlockAnnotated::class, ['diErrorOneParam']);
292
    }
293
294
    public function testContainerAnnotationErrorThreeParams()
295
    {
296
        $this->expectException(SetupException::class);
297
        $this->getAttributes(DocBlockAnnotated::class, ['diErrorThreeParams']);
298
    }
299
300
    public function testCbBeforeAnnotationErrorName()
301
    {
302
        $this->expectException(SetupException::class);
303
        $this->getAttributes(DocBlockAnnotated::class, ['cbBeforeErrorName']);
304
    }
305
306
    public function testCbBeforeAnnotationErrorParam()
307
    {
308
        $this->expectException(SetupException::class);
309
        $this->getAttributes(DocBlockAnnotated::class, ['cbBeforeErrorParam']);
310
    }
311
312
    public function testCbBeforeAnnotationErrorNumber()
313
    {
314
        $this->expectException(SetupException::class);
315
        $this->getAttributes(DocBlockAnnotated::class, ['cbBeforeErrorNumber']);
316
    }
317
318
    public function testCbAfterAnnotationErrorName()
319
    {
320
        $this->expectException(SetupException::class);
321
        $this->getAttributes(DocBlockAnnotated::class, ['cbAfterErrorName']);
322
    }
323
324
    public function testCbAfterAnnotationErrorParam()
325
    {
326
        $this->expectException(SetupException::class);
327
        $this->getAttributes(DocBlockAnnotated::class, ['cbAfterErrorParam']);
328
    }
329
330
    public function testCbAfterAnnotationErrorNumber()
331
    {
332
        $this->expectException(SetupException::class);
333
        $this->getAttributes(DocBlockAnnotated::class, ['cbAfterErrorNumber']);
334
    }
335
}
336