Passed
Push — main ( c94f3b...124c22 )
by Thierry
05:44
created

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

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

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

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