Passed
Push — main ( 7aae9b...c94f3b )
by Thierry
05:42
created

ComponentTest::testFuncComponentExportMethods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
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 13
rs 9.9332
1
<?php
2
3
namespace Jaxon\Annotations\Tests\TestAnnotation;
4
5
use Jaxon\Annotations\Tests\AnnotationTrait;
6
use Jaxon\Annotations\Tests\Attr\Ajax\Component\DocBlockNodeBaseComponent;
7
use Jaxon\Annotations\Tests\Attr\Ajax\Component\FuncComponent;
8
use Jaxon\Annotations\Tests\Attr\Ajax\Component\NodeComponent;
9
use Jaxon\Annotations\Tests\Attr\Ajax\Component\NodeBaseComponent;
10
use Jaxon\Annotations\Tests\Attr\Ajax\Component\PageComponent;
11
use Jaxon\Exception\SetupException;
12
use PHPUnit\Framework\TestCase;
13
14
use ReflectionClass;
15
use function Jaxon\Annotations\_register;
16
use function Jaxon\jaxon;
17
18
class ComponentTest extends TestCase
19
{
20
    use AnnotationTrait;
21
22
    /**
23
     * @var string
24
     */
25
    private $sCacheDir;
26
27
    /**
28
     * @throws SetupException
29
     */
30
    public function setUp(): void
31
    {
32
        $this->sCacheDir = __DIR__ . '/../tmp';
33
        @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

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

55
                /** @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...
56
            }
57
        }
58
        @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

58
        /** @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...
59
    }
60
61
    /**
62
     * @throws SetupException
63
     */
64
    public function testNodeComponentExportMethods()
65
    {
66
        $xMetadata = $this->getAttributes(NodeComponent::class,
67
            ['item', 'html', 'render', 'clear', 'visible'], []);
68
        $bExcluded = $xMetadata->isExcluded();
69
        $aExcluded = $xMetadata->getExceptMethods();
70
        $aBaseMethods = $xMetadata->getExportBaseMethods();
71
        $aOnlyMethods = $xMetadata->getExportOnlyMethods();
72
73
        $this->assertFalse($bExcluded);
74
        $this->assertCount(0, $aExcluded);
75
        $this->assertCount(0, $aBaseMethods);
76
        $this->assertCount(0, $aOnlyMethods);
77
    }
78
79
    /**
80
     * @throws SetupException
81
     */
82
    public function testPageComponentExportMethods()
83
    {
84
        $xMetadata = $this->getAttributes(PageComponent::class,
85
            ['item', 'html', 'render', 'clear', 'visible'], []);
86
        $bExcluded = $xMetadata->isExcluded();
87
        $aExcluded = $xMetadata->getExceptMethods();
88
        $aBaseMethods = $xMetadata->getExportBaseMethods();
89
        $aOnlyMethods = $xMetadata->getExportOnlyMethods();
90
91
        $this->assertFalse($bExcluded);
92
        $this->assertCount(0, $aExcluded);
93
        $this->assertCount(0, $aBaseMethods);
94
        $this->assertCount(0, $aOnlyMethods);
95
    }
96
97
    /**
98
     * @throws SetupException
99
     */
100
    public function testFuncComponentExportMethods()
101
    {
102
        $xMetadata = $this->getAttributes(FuncComponent::class,
103
            ['paginator'], []);
104
        $bExcluded = $xMetadata->isExcluded();
105
        $aExcluded = $xMetadata->getExceptMethods();
106
        $aBaseMethods = $xMetadata->getExportBaseMethods();
107
        $aOnlyMethods = $xMetadata->getExportOnlyMethods();
108
109
        $this->assertFalse($bExcluded);
110
        $this->assertCount(0, $aExcluded);
111
        $this->assertCount(0, $aBaseMethods);
112
        $this->assertCount(0, $aOnlyMethods);
113
    }
114
115
    /**
116
     * @throws SetupException
117
     */
118
    public function testNodeComponentExportBaseMethods()
119
    {
120
        // The attribute exports the 'html' and 'render' methods,
121
        // but only the 'render' method shall be exported.
122
        $xClass = new ReflectionClass(NodeBaseComponent::class);
123
        $aMethods = ['item', 'html', 'render', 'clear', 'visible'];
124
        $xMetadata = $this->getAttributes($xClass, $aMethods, []);
125
        $aBaseMethods = $xMetadata->getExportBaseMethods();
126
127
        // The 'html' and 'render' methods are returned.
128
        $this->assertCount(2, $aBaseMethods);
129
130
        $xOptions = $this->getOptions($xClass);
131
        $aPublicMethods = $xOptions->getPublicMethods();
132
133
        // Only the 'render' method is returned.
134
        $this->assertCount(1, $aPublicMethods);
135
        $this->assertEquals('render', $aPublicMethods[0]);
136
    }
137
138
    /**
139
     * @throws SetupException
140
     */
141
    public function testDocBockNodeComponentExportBaseMethods()
142
    {
143
        // The attribute exports the 'html' and 'render' methods,
144
        // but only the 'render' method shall be exported.
145
        $xClass = new ReflectionClass(DocBlockNodeBaseComponent::class);
146
        $aMethods = ['item', 'html', 'render', 'clear', 'visible'];
147
        $xMetadata = $this->getAttributes($xClass, $aMethods, []);
148
        $aBaseMethods = $xMetadata->getExportBaseMethods();
149
150
        // The 'html' and 'render' methods are returned.
151
        $this->assertCount(2, $aBaseMethods);
152
153
        $xOptions = $this->getOptions($xClass);
154
        $aPublicMethods = $xOptions->getPublicMethods();
155
156
        // Only the 'render' method is returned.
157
        $this->assertCount(1, $aPublicMethods);
158
        $this->assertEquals('render', $aPublicMethods[0]);
159
    }
160
161
    // public function testContainerAttributeErrorTwoDi()
162
    // {
163
    //     $this->expectException(SetupException::class);
164
    //     $this->getAttributes(PropertyAttribute::class, [], ['errorTwoDi']);
165
    // }
166
167
    // public function testContainerAttributeErrorDiClass()
168
    // {
169
    //     $this->expectException(SetupException::class);
170
    //     $this->getAttributes(PropertyAttribute::class, ['errorDiClass']);
171
    // }
172
}
173