Passed
Push — main ( 7f530d...7aae9b )
by Thierry
05:54
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\FuncComponent;
7
use Jaxon\Annotations\Tests\Attr\Ajax\Component\NodeComponent;
8
use Jaxon\Annotations\Tests\Attr\Ajax\Component\NodeBaseComponent;
9
use Jaxon\Annotations\Tests\Attr\Ajax\Component\PageComponent;
10
use Jaxon\Exception\SetupException;
11
use PHPUnit\Framework\TestCase;
12
13
use ReflectionClass;
14
use function Jaxon\Annotations\_register;
15
use function Jaxon\jaxon;
16
17
class ComponentTest extends TestCase
18
{
19
    use AnnotationTrait;
20
21
    /**
22
     * @var string
23
     */
24
    private $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 testNodeComponentExportMethods()
64
    {
65
        $xMetadata = $this->getAttributes(NodeComponent::class,
66
            ['item', 'html', 'render', 'clear', 'visible'], []);
67
        $bExcluded = $xMetadata->isExcluded();
68
        $aExcluded = $xMetadata->getExceptMethods();
69
        $aBaseMethods = $xMetadata->getExportBaseMethods();
70
        $aOnlyMethods = $xMetadata->getExportOnlyMethods();
71
72
        $this->assertFalse($bExcluded);
73
        $this->assertCount(0, $aExcluded);
74
        $this->assertCount(0, $aBaseMethods);
75
        $this->assertCount(0, $aOnlyMethods);
76
    }
77
78
    /**
79
     * @throws SetupException
80
     */
81
    public function testPageComponentExportMethods()
82
    {
83
        $xMetadata = $this->getAttributes(PageComponent::class,
84
            ['item', 'html', 'render', 'clear', 'visible'], []);
85
        $bExcluded = $xMetadata->isExcluded();
86
        $aExcluded = $xMetadata->getExceptMethods();
87
        $aBaseMethods = $xMetadata->getExportBaseMethods();
88
        $aOnlyMethods = $xMetadata->getExportOnlyMethods();
89
90
        $this->assertFalse($bExcluded);
91
        $this->assertCount(0, $aExcluded);
92
        $this->assertCount(0, $aBaseMethods);
93
        $this->assertCount(0, $aOnlyMethods);
94
    }
95
96
    /**
97
     * @throws SetupException
98
     */
99
    public function testFuncComponentExportMethods()
100
    {
101
        $xMetadata = $this->getAttributes(FuncComponent::class,
102
            ['paginator'], []);
103
        $bExcluded = $xMetadata->isExcluded();
104
        $aExcluded = $xMetadata->getExceptMethods();
105
        $aBaseMethods = $xMetadata->getExportBaseMethods();
106
        $aOnlyMethods = $xMetadata->getExportOnlyMethods();
107
108
        $this->assertFalse($bExcluded);
109
        $this->assertCount(0, $aExcluded);
110
        $this->assertCount(0, $aBaseMethods);
111
        $this->assertCount(0, $aOnlyMethods);
112
    }
113
114
    /**
115
     * @throws SetupException
116
     */
117
    public function testNodeComponentExportBaseMethods()
118
    {
119
        // The attribute exports the 'html' and 'render' methods,
120
        // but only the 'render' method shall be exported.
121
        $xClass = new ReflectionClass(NodeBaseComponent::class);
122
        $aMethods = ['item', 'html', 'render', 'clear', 'visible'];
123
        $xMetadata = $this->getAttributes($xClass, $aMethods, []);
124
        $aBaseMethods = $xMetadata->getExportBaseMethods();
125
126
        // The 'html' and 'render' methods are returned.
127
        $this->assertCount(2, $aBaseMethods);
128
129
        $xOptions = $this->getOptions($xClass);
130
        $aPublicMethods = $xOptions->getPublicMethods();
131
132
        // Only the 'render' method is returned.
133
        $this->assertCount(1, $aPublicMethods);
134
        $this->assertEquals('render', $aPublicMethods[0]);
135
    }
136
137
    // public function testContainerAttributeErrorTwoDi()
138
    // {
139
    //     $this->expectException(SetupException::class);
140
    //     $this->getAttributes(PropertyAttribute::class, [], ['errorTwoDi']);
141
    // }
142
143
    // public function testContainerAttributeErrorDiClass()
144
    // {
145
    //     $this->expectException(SetupException::class);
146
    //     $this->getAttributes(PropertyAttribute::class, ['errorDiClass']);
147
    // }
148
}
149