Passed
Push — main ( 525366...99eb4f )
by Thierry
03:58
created

ClassExtendsTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 69
c 1
b 0
f 0
dl 0
loc 114
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 4 1
A testInheritedTraitAttributes() 0 46 1
A setUp() 0 9 1
A testInheritedAttributes() 0 28 1
1
<?php
2
3
namespace Jaxon\Attributes\Tests\TestAttributes;
4
5
use Jaxon\Attributes\Tests\AttributeTrait;
6
use Jaxon\Attributes\Tests\Attr\Ajax\ClassExtendsExcluded;
7
use Jaxon\Attributes\Tests\Attr\Ajax\ClassExtendsTraitExcluded;
8
use Jaxon\Exception\SetupException;
9
use PHPUnit\Framework\TestCase;
10
11
use function Jaxon\Attributes\_register;
12
use function Jaxon\jaxon;
13
14
class ClassExtendsTest extends TestCase
15
{
16
    use AttributeTrait;
17
18
    /**
19
     * @var string
20
     */
21
    private $sCacheDir;
22
23
    /**
24
     * @throws SetupException
25
     */
26
    public function setUp(): void
27
    {
28
        $this->sCacheDir = __DIR__ . '/../cache';
29
        @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

29
        /** @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...
30
31
        jaxon()->di()->getPluginManager()->registerPlugins();
32
        _register();
33
34
        jaxon()->di()->val('jaxon_attributes_cache_dir', $this->sCacheDir);
35
    }
36
37
    /**
38
     * @throws SetupException
39
     */
40
    public function tearDown(): void
41
    {
42
        jaxon()->reset();
43
        parent::tearDown();
44
    }
45
46
    /**
47
     * @throws SetupException
48
     */
49
    public function testInheritedAttributes()
50
    {
51
        $xMetadata = $this->getAttributes(ClassExtendsExcluded::class,
52
            ['doNot', 'withBags', 'cbSingle']);
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(2, $aProperties);
63
64
        $this->assertArrayHasKey('withBags', $aProperties);
65
        $this->assertCount(1, $aProperties['withBags']);
66
        $this->assertCount(2, $aProperties['withBags']['bags']);
67
        $this->assertEquals('user.name', $aProperties['withBags']['bags'][0]);
68
        $this->assertEquals('page.number', $aProperties['withBags']['bags'][1]);
69
70
        $this->assertArrayHasKey('cbSingle', $aProperties);
71
        $this->assertCount(1, $aProperties['cbSingle']['__before']);
72
        $this->assertArrayHasKey('funcBefore', $aProperties['cbSingle']['__before']);
73
        $this->assertIsArray($aProperties['cbSingle']['__before']['funcBefore']);
74
        $this->assertCount(1, $aProperties['cbSingle']['__after']);
75
        $this->assertArrayHasKey('funcAfter', $aProperties['cbSingle']['__after']);
76
        $this->assertIsArray($aProperties['cbSingle']['__after']['funcAfter']);
77
    }
78
79
    /**
80
     * @throws SetupException
81
     */
82
    public function testInheritedTraitAttributes()
83
    {
84
        $xMetadata = $this->getAttributes(ClassExtendsTraitExcluded::class,
85
            ['doNot', 'withBags', 'cbSingle', 'cbMultiple', 'withCallbacks']);
86
        $bExcluded = $xMetadata->isExcluded();
87
        $aProperties = $xMetadata->getProperties();
88
        $aProtected = $xMetadata->getProtectedMethods();
89
90
        $this->assertFalse($bExcluded);
91
        $this->assertEmpty($aProtected);
92
93
        $this->assertCount(4, $aProperties);
94
95
        $this->assertArrayHasKey('withBags', $aProperties);
96
        $this->assertCount(1, $aProperties['withBags']);
97
        $this->assertCount(2, $aProperties['withBags']['bags']);
98
        $this->assertEquals('user.name', $aProperties['withBags']['bags'][0]);
99
        $this->assertEquals('page.number', $aProperties['withBags']['bags'][1]);
100
101
        $this->assertArrayHasKey('cbSingle', $aProperties);
102
        $this->assertCount(1, $aProperties['cbSingle']['__before']);
103
        $this->assertArrayHasKey('funcBefore', $aProperties['cbSingle']['__before']);
104
        $this->assertIsArray($aProperties['cbSingle']['__before']['funcBefore']);
105
        $this->assertCount(1, $aProperties['cbSingle']['__after']);
106
        $this->assertArrayHasKey('funcAfter', $aProperties['cbSingle']['__after']);
107
        $this->assertIsArray($aProperties['cbSingle']['__after']['funcAfter']);
108
109
        $this->assertArrayHasKey('cbMultiple', $aProperties);
110
        $this->assertCount(2, $aProperties['cbMultiple']['__before']);
111
        $this->assertArrayHasKey('funcBefore1', $aProperties['cbMultiple']['__before']);
112
        $this->assertArrayHasKey('funcBefore2', $aProperties['cbMultiple']['__before']);
113
        $this->assertIsArray($aProperties['cbMultiple']['__before']['funcBefore1']);
114
        $this->assertIsArray($aProperties['cbMultiple']['__before']['funcBefore2']);
115
        $this->assertCount(3, $aProperties['cbMultiple']['__after']);
116
        $this->assertArrayHasKey('funcAfter1', $aProperties['cbMultiple']['__after']);
117
        $this->assertArrayHasKey('funcAfter2', $aProperties['cbMultiple']['__after']);
118
        $this->assertArrayHasKey('funcAfter3', $aProperties['cbMultiple']['__after']);
119
        $this->assertIsArray($aProperties['cbMultiple']['__after']['funcAfter1']);
120
        $this->assertIsArray($aProperties['cbMultiple']['__after']['funcAfter2']);
121
        $this->assertIsArray($aProperties['cbMultiple']['__after']['funcAfter3']);
122
123
        $this->assertArrayHasKey('withCallbacks', $aProperties);
124
        $this->assertCount(1, $aProperties['withCallbacks']);
125
        $this->assertCount(2, $aProperties['withCallbacks']['callback']);
126
        $this->assertEquals('jaxon.callback.first', $aProperties['withCallbacks']['callback'][0]);
127
        $this->assertEquals('jaxon.callback.second', $aProperties['withCallbacks']['callback'][1]);
128
    }
129
}
130