Passed
Push — main ( 82104f...ee6fb2 )
by Thierry
05:16
created

ExportAssetsInLibTest::setUp()   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\Tests\TestRegistrationApp;
4
5
require_once dirname(__DIR__) . '/src/classes.php';
6
7
use Jaxon\Exception\RequestException;
8
use Jaxon\Exception\SetupException;
9
use Jaxon\Plugin\Code\MinifierInterface;
10
use Nyholm\Psr7Server\ServerRequestCreator;
11
use Psr\Http\Message\ServerRequestInterface;
12
use PHPUnit\Framework\TestCase;
13
14
use function Jaxon\Dialogs\_register as register_dialogs;
15
use function Jaxon\Storage\_register as register_storage;
16
17
/**
18
 * Tests with the assets export options in the "lib" section of the config file.
19
 */
20
class ExportAssetsInLibTest extends TestCase
21
{
22
    private $jsDir = '';
23
24
    public function setUp(): void
25
    {
26
        $this->jsDir = realpath(dirname(__DIR__) . '/src/js');
27
        register_dialogs();
28
        register_storage();
29
        jaxon()->app()->setup(dirname(__DIR__) . '/config/app/assets.lib.php');
30
        // Set the assets options in the "lib" section of the config.
31
        jaxon()->setOptions([
32
            'export' => true,
33
            'minify' => true,
34
            'uri' => 'http://example.test/js',
35
            'dir' => $this->jsDir,
36
        ], 'js.app');
37
    }
38
39
    /**
40
     * @throws SetupException
41
     */
42
    public function tearDown(): void
43
    {
44
        // Delete the generated js files
45
        $sHash = jaxon()->di()->getCodeGenerator()->getHash();
46
        @unlink("{$this->jsDir}/$sHash.js");
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

46
        /** @scrutinizer ignore-unhandled */ @unlink("{$this->jsDir}/$sHash.js");

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...
47
        @unlink("{$this->jsDir}/$sHash.min.js");
48
        @unlink("{$this->jsDir}/assets.js");
49
        @unlink("{$this->jsDir}/assets.min.js");
50
51
        jaxon()->reset();
52
        parent::tearDown();
53
    }
54
55
    public function testExportFileContent()
56
    {
57
        jaxon()->setOptions(['export' => false, 'minify' => false], 'js.app');
58
        $sScript = jaxon()->getScript();
59
        // file_put_contents(dirname(__DIR__) . '/src/js/assets.lib.html', $sScript);
60
        $this->assertEquals(file_get_contents(dirname(__DIR__) . '/src/js/assets.lib.html'), $sScript);
61
    }
62
63
    public function testScriptExportMinified()
64
    {
65
        jaxon()->setOption('js.app.minify', true);
66
        $sScript = jaxon()->getScript();
67
        // file_put_contents(dirname(__DIR__) . '/src/js/app.link.min.html', $sScript);
68
        // Check that the return value is a file URI, and not js code.
69
        $this->assertStringNotContainsString('SamplePackageClass = {', $sScript);
70
        $this->assertStringContainsString('http://example.test/js', $sScript);
71
        $this->assertStringContainsString('.min.js', $sScript);
72
    }
73
74
    public function testScriptExportNotMinified()
75
    {
76
        jaxon()->setOption('js.app.minify', false);
77
        $sScript = jaxon()->getScript();
78
        // file_put_contents(dirname(__DIR__) . '/src/js/app.link.html', $sScript);
79
        // Check that the return value is a file URI, and not js code.
80
        $this->assertStringNotContainsString('SamplePackageClass = {', $sScript);
81
        $this->assertStringContainsString('http://example.test/js', $sScript);
82
        $this->assertStringNotContainsString('.min.js', $sScript);
83
        $this->assertStringContainsString('.js', $sScript);
84
    }
85
86
    public function testScriptErrorMinifier()
87
    {
88
        // Register a minifier that always fails.
89
        jaxon()->di()->set(MinifierInterface::class, function() {
90
            return new class implements MinifierInterface {
91
                public function minifyJsCode(string $sCode): string|false
92
                {
93
                    return false;
94
                }
95
                public function minifyCssCode(string $sCode): string|false
96
                {
97
                    return false;
98
                }
99
            };
100
        });
101
        // The js file must be generated but not minified.
102
        jaxon()->setOption('js.app.minify', true);
103
        $sScript = jaxon()->getScript();
104
        // Check that the return value is a file URI, and not js code.
105
        $this->assertStringNotContainsString('SamplePackageClass = {', $sScript);
106
        $this->assertStringContainsString('http://example.test/js', $sScript);
107
        $this->assertStringNotContainsString('.min.js', $sScript);
108
        $this->assertStringContainsString('.js', $sScript);
109
    }
110
111
    public function testScriptExportErrorIncorrectDir()
112
    {
113
        // Change the js dir
114
        jaxon()->setOption('js.app.dir', dirname(__DIR__) . '/src/script'); // This dir must not exist.
115
        $sScript = jaxon()->getScript();
116
        $this->assertStringContainsString('SamplePackageClass = {', $sScript);
117
    }
118
119
    public function testScriptExportErrorIncorrectFile()
120
    {
121
        // The js subdir path is corrupted (with the '\0' char).
122
        jaxon()->setAppOption('assets.js.file', "\0js/app");
123
        $sScript = jaxon()->getScript();
124
        $this->assertStringContainsString('SamplePackageClass = {', $sScript);
125
    }
126
127
    public function testSetupIncorrectFile()
128
    {
129
        $this->expectException(SetupException::class);
130
        jaxon()->app()->setup(dirname(__DIR__) . '/config/app/not-found.php');
131
    }
132
133
    public function testSetupIncorrectConfig()
134
    {
135
        $this->expectException(SetupException::class);
136
        jaxon()->app()->setup(dirname(__DIR__) . '/config/app/app-error.php');
137
    }
138
139
    public function testJaxonClassAnnotations()
140
    {
141
        // The server request
142
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
143
            return $c->g(ServerRequestCreator::class)
144
                ->fromGlobals()
145
                ->withParsedBody([
146
                    'jxncall' => json_encode([
147
                        'type' => 'class',
148
                        'name' => 'Jaxon.NsTests.DirB.ClassB',
149
                        'method' => 'methodBa',
150
                        'args' => [],
151
                    ]),
152
                ])
153
                ->withMethod('POST');
154
        });
155
156
        $this->assertTrue(jaxon()->canProcessRequest());
157
        jaxon()->di()->getCallableClassPlugin()->processRequest();
158
    }
159
160
    public function testRequestToJaxonClass()
161
    {
162
        // The server request
163
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
164
            return $c->g(ServerRequestCreator::class)
165
                ->fromGlobals()
166
                ->withParsedBody([
167
                    'jxncall' => json_encode([
168
                        'type' => 'class',
169
                        'name' => 'Jaxon.NsTests.DirB.ClassB',
170
                        'method' => 'methodBb',
171
                        'args' => [],
172
                    ]),
173
                ])
174
                ->withMethod('POST');
175
        });
176
177
        $this->assertTrue(jaxon()->canProcessRequest());
178
        $this->expectException(RequestException::class);
179
        // The processRequest() method now calls httpResponse(), which throws an exception.
180
        jaxon()->processRequest();
181
        // $this->expectException(RequestException::class);
182
        // jaxon()->httpResponse();
183
    }
184
}
185