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

AttributeTest::testExcludeAttributeError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Jaxon\Attributes\Tests\TestAttributes;
4
5
use Jaxon\Attributes\Tests\AttributeTrait;
6
use Jaxon\Attributes\Tests\Attr\Ajax\Attribute;
7
use Jaxon\Attributes\Tests\Attr\Ajax\ClassAttribute;
8
use Jaxon\Attributes\Tests\Attr\Ajax\ClassExcluded;
9
use Jaxon\Exception\SetupException;
10
use PHPUnit\Framework\TestCase;
11
12
use function Jaxon\Attributes\_register;
13
use function Jaxon\jaxon;
14
15
class AttributeTest extends TestCase
16
{
17
    use AttributeTrait;
18
19
    /**
20
     * @var string
21
     */
22
    private $sCacheDir;
23
24
    /**
25
     * @throws SetupException
26
     */
27
    public function setUp(): void
28
    {
29
        $this->sCacheDir = __DIR__ . '/../cache';
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_attributes_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
47
    /**
48
     * @throws SetupException
49
     */
50
    public function testUploadAndExcludeAttribute()
51
    {
52
        $xMetadata = $this->getAttributes(Attribute::class, ['saveFiles', 'doNot']);
53
        $bExcluded = $xMetadata->isExcluded();
54
        $aProperties = $xMetadata->getProperties();
55
        $aProtected = $xMetadata->getProtectedMethods();
56
57
        $this->assertFalse($bExcluded);
58
59
        $this->assertCount(1, $aProtected);
60
        $this->assertEquals('doNot', $aProtected[0]);
61
62
        $this->assertCount(1, $aProperties);
63
        $this->assertArrayHasKey('saveFiles', $aProperties);
64
        $this->assertCount(1, $aProperties['saveFiles']);
65
        $this->assertEquals("'user-files'", $aProperties['saveFiles']['upload']);
66
    }
67
68
    /**
69
     * @throws SetupException
70
     */
71
    public function testDatabagAttribute()
72
    {
73
        $xMetadata = $this->getAttributes(Attribute::class, ['withBags']);
74
        $bExcluded = $xMetadata->isExcluded();
75
        $aProperties = $xMetadata->getProperties();
76
        $aProtected = $xMetadata->getProtectedMethods();
0 ignored issues
show
Unused Code introduced by
The assignment to $aProtected is dead and can be removed.
Loading history...
77
78
        $this->assertFalse($bExcluded);
79
80
        $this->assertCount(1, $aProperties);
81
        $this->assertArrayHasKey('withBags', $aProperties);
82
        $this->assertCount(1, $aProperties['withBags']);
83
        $this->assertCount(2, $aProperties['withBags']['bags']);
84
        $this->assertEquals('user.name', $aProperties['withBags']['bags'][0]);
85
        $this->assertEquals('page.number', $aProperties['withBags']['bags'][1]);
86
    }
87
88
    /**
89
     * @throws SetupException
90
     */
91
    public function testCallbacksAttribute()
92
    {
93
        $xMetadata = $this->getAttributes(Attribute::class, ['cbSingle', 'cbMultiple', 'cbParams']);
94
        $bExcluded = $xMetadata->isExcluded();
95
        $aProperties = $xMetadata->getProperties();
96
        $aProtected = $xMetadata->getProtectedMethods();
0 ignored issues
show
Unused Code introduced by
The assignment to $aProtected is dead and can be removed.
Loading history...
97
98
        $this->assertFalse($bExcluded);
99
100
        $this->assertCount(3, $aProperties);
101
        $this->assertArrayHasKey('cbSingle', $aProperties);
102
        $this->assertArrayHasKey('cbMultiple', $aProperties);
103
        $this->assertArrayHasKey('cbParams', $aProperties);
104
105
        $this->assertCount(1, $aProperties['cbSingle']['__before']);
106
        $this->assertCount(2, $aProperties['cbMultiple']['__before']);
107
        $this->assertCount(2, $aProperties['cbParams']['__before']);
108
        $this->assertArrayHasKey('funcBefore', $aProperties['cbSingle']['__before']);
109
        $this->assertArrayHasKey('funcBefore1', $aProperties['cbMultiple']['__before']);
110
        $this->assertArrayHasKey('funcBefore2', $aProperties['cbMultiple']['__before']);
111
        $this->assertArrayHasKey('funcBefore1', $aProperties['cbParams']['__before']);
112
        $this->assertArrayHasKey('funcBefore2', $aProperties['cbParams']['__before']);
113
        $this->assertIsArray($aProperties['cbSingle']['__before']['funcBefore']);
114
        $this->assertIsArray($aProperties['cbMultiple']['__before']['funcBefore1']);
115
        $this->assertIsArray($aProperties['cbMultiple']['__before']['funcBefore2']);
116
        $this->assertIsArray($aProperties['cbParams']['__before']['funcBefore1']);
117
        $this->assertIsArray($aProperties['cbParams']['__before']['funcBefore2']);
118
119
        $this->assertCount(1, $aProperties['cbSingle']['__after']);
120
        $this->assertCount(3, $aProperties['cbMultiple']['__after']);
121
        $this->assertCount(1, $aProperties['cbParams']['__after']);
122
        $this->assertArrayHasKey('funcAfter', $aProperties['cbSingle']['__after']);
123
        $this->assertArrayHasKey('funcAfter1', $aProperties['cbMultiple']['__after']);
124
        $this->assertArrayHasKey('funcAfter2', $aProperties['cbMultiple']['__after']);
125
        $this->assertArrayHasKey('funcAfter3', $aProperties['cbMultiple']['__after']);
126
        $this->assertArrayHasKey('funcAfter1', $aProperties['cbParams']['__after']);
127
        $this->assertIsArray($aProperties['cbSingle']['__after']['funcAfter']);
128
        $this->assertIsArray($aProperties['cbMultiple']['__after']['funcAfter1']);
129
        $this->assertIsArray($aProperties['cbMultiple']['__after']['funcAfter2']);
130
        $this->assertIsArray($aProperties['cbMultiple']['__after']['funcAfter3']);
131
        $this->assertIsArray($aProperties['cbParams']['__after']['funcAfter1']);
132
    }
133
134
    /**
135
     * @throws SetupException
136
     */
137
    public function testContainerAttribute()
138
    {
139
        $xMetadata = $this->getAttributes(Attribute::class, ['di1', 'di2']);
140
        $bExcluded = $xMetadata->isExcluded();
141
        $aProperties = $xMetadata->getProperties();
142
        $aProtected = $xMetadata->getProtectedMethods();
0 ignored issues
show
Unused Code introduced by
The assignment to $aProtected is dead and can be removed.
Loading history...
143
144
        $this->assertFalse($bExcluded);
145
146
        $this->assertCount(2, $aProperties);
147
        $this->assertArrayHasKey('di1', $aProperties);
148
        $this->assertArrayHasKey('di2', $aProperties);
149
        $this->assertCount(2, $aProperties['di1']['__di']);
150
        $this->assertCount(2, $aProperties['di2']['__di']);
151
        $this->assertEquals('Jaxon\Attributes\Tests\Service\ColorService', $aProperties['di1']['__di']['colorService']);
152
        $this->assertEquals('Jaxon\Attributes\Tests\Attr\Ajax\FontService', $aProperties['di1']['__di']['fontService']);
153
        $this->assertEquals('Jaxon\Attributes\Tests\Service\ColorService', $aProperties['di2']['__di']['colorService']);
154
        $this->assertEquals('Jaxon\Attributes\Tests\Service\TextService', $aProperties['di2']['__di']['textService']);
155
    }
156
157
    /**
158
     * @throws SetupException
159
     */
160
    public function testClassAttribute()
161
    {
162
        $xMetadata = $this->getAttributes(ClassAttribute::class, []);
163
        $bExcluded = $xMetadata->isExcluded();
164
        $aProperties = $xMetadata->getProperties();
165
        $aProtected = $xMetadata->getProtectedMethods();
0 ignored issues
show
Unused Code introduced by
The assignment to $aProtected is dead and can be removed.
Loading history...
166
167
        $this->assertFalse($bExcluded);
168
169
        $this->assertCount(1, $aProperties);
170
        $this->assertArrayHasKey('*', $aProperties);
171
        $this->assertCount(5, $aProperties['*']);
172
        $this->assertArrayHasKey('bags', $aProperties['*']);
173
        $this->assertArrayHasKey('callback', $aProperties['*']);
174
        $this->assertArrayHasKey('__before', $aProperties['*']);
175
        $this->assertArrayHasKey('__after', $aProperties['*']);
176
        $this->assertArrayHasKey('__di', $aProperties['*']);
177
178
        $this->assertCount(2, $aProperties['*']['bags']);
179
        $this->assertEquals('user.name', $aProperties['*']['bags'][0]);
180
        $this->assertEquals('page.number', $aProperties['*']['bags'][1]);
181
182
        $this->assertCount(2, $aProperties['*']['__before']);
183
        $this->assertArrayHasKey('funcBefore1', $aProperties['*']['__before']);
184
        $this->assertArrayHasKey('funcBefore2', $aProperties['*']['__before']);
185
        $this->assertIsArray($aProperties['*']['__before']['funcBefore1']);
186
        $this->assertIsArray($aProperties['*']['__before']['funcBefore2']);
187
188
        $this->assertCount(3, $aProperties['*']['__after']);
189
        $this->assertArrayHasKey('funcAfter1', $aProperties['*']['__after']);
190
        $this->assertArrayHasKey('funcAfter2', $aProperties['*']['__after']);
191
        $this->assertArrayHasKey('funcAfter3', $aProperties['*']['__after']);
192
        $this->assertIsArray($aProperties['*']['__after']['funcAfter1']);
193
        $this->assertIsArray($aProperties['*']['__after']['funcAfter2']);
194
        $this->assertIsArray($aProperties['*']['__after']['funcAfter3']);
195
196
        $this->assertCount(3, $aProperties['*']['__di']);
197
        $this->assertArrayHasKey('colorService', $aProperties['*']['__di']);
198
        $this->assertArrayHasKey('textService', $aProperties['*']['__di']);
199
        $this->assertArrayHasKey('fontService', $aProperties['*']['__di']);
200
        $this->assertEquals('Jaxon\Attributes\Tests\Service\ColorService', $aProperties['*']['__di']['colorService']);
201
        $this->assertEquals('Jaxon\Attributes\Tests\Service\TextService', $aProperties['*']['__di']['textService']);
202
        $this->assertEquals('Jaxon\Attributes\Tests\Attr\Ajax\FontService', $aProperties['*']['__di']['fontService']);
203
    }
204
205
    /**
206
     * @throws SetupException
207
     */
208
    public function testClassExcludeAttribute()
209
    {
210
        $xMetadata = $this->getAttributes(ClassExcluded::class, ['doNot', 'withBags', 'cbSingle']);
211
        $bExcluded = $xMetadata->isExcluded();
212
        $aProperties = $xMetadata->getProperties();
213
        $aProtected = $xMetadata->getProtectedMethods();
214
215
        $this->assertTrue($bExcluded);
216
        $this->assertEmpty($aProperties);
217
        $this->assertEmpty($aProtected);
218
    }
219
220
    public function testExcludeAttributeError()
221
    {
222
        $this->expectException(SetupException::class);
223
        $this->getAttributes(Attribute::class, ['doNotError']);
224
    }
225
226
    public function testDatabagAttributeError()
227
    {
228
        $this->expectException(SetupException::class);
229
        $this->getAttributes(Attribute::class, ['withBagsError']);
230
    }
231
232
    public function testUploadAttributeWrongName()
233
    {
234
        $this->expectException(SetupException::class);
235
        $this->getAttributes(Attribute::class, ['saveFilesWrongName']);
236
    }
237
238
    public function testUploadAttributeMultiple()
239
    {
240
        $this->expectException(SetupException::class);
241
        $this->getAttributes(Attribute::class, ['saveFilesMultiple']);
242
    }
243
244
    public function testCallbacksBeforeAttributeNoCall()
245
    {
246
        $this->expectException(SetupException::class);
247
        $this->getAttributes(Attribute::class, ['cbBeforeNoCall']);
248
    }
249
250
    public function testCallbacksBeforeAttributeUnknownAttr()
251
    {
252
        $this->expectException(SetupException::class);
253
        $this->getAttributes(Attribute::class, ['cbBeforeUnknownAttr']);
254
    }
255
256
    public function testCallbacksBeforeAttributeWrongAttrType()
257
    {
258
        $this->expectException(SetupException::class);
259
        $this->getAttributes(Attribute::class, ['cbBeforeWrongAttrType']);
260
    }
261
262
    public function testCallbacksAfterAttributeNoCall()
263
    {
264
        $this->expectException(SetupException::class);
265
        $this->getAttributes(Attribute::class, ['cbAfterNoCall']);
266
    }
267
268
    public function testCallbacksAfterAttributeUnknownAttr()
269
    {
270
        $this->expectException(SetupException::class);
271
        $this->getAttributes(Attribute::class, ['cbAfterUnknownAttr']);
272
    }
273
274
    public function testCallbacksAfterAttributeWrongAttrType()
275
    {
276
        $this->expectException(SetupException::class);
277
        $this->getAttributes(Attribute::class, ['cbAfterWrongAttrType']);
278
    }
279
280
    public function testContainerAttributeUnknownAttr()
281
    {
282
        $this->expectException(SetupException::class);
283
        $this->getAttributes(Attribute::class, ['diUnknownAttr']);
284
    }
285
286
    public function testContainerAttributeWrongAttrType()
287
    {
288
        $this->expectException(SetupException::class);
289
        $this->getAttributes(Attribute::class, ['diWrongAttrType']);
290
    }
291
292
    public function testContainerAttributeWrongClassType()
293
    {
294
        $this->expectException(SetupException::class);
295
        $this->getAttributes(Attribute::class, ['diWrongClassType']);
296
    }
297
}
298