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

ExportAssetsInAppTest.php$0 ➔ minifyJsCode()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
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 ExportAssetsInAppTest 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.app.php');
30
        // The asset() method sets the options in the "app" section of the config.
31
        jaxon()->app()->asset(true, true, 'http://example.test/js', $this->jsDir);
32
    }
33
34
    /**
35
     * @throws SetupException
36
     */
37
    public function tearDown(): void
38
    {
39
        // Delete the generated js files
40
        $sHash = jaxon()->di()->getCodeGenerator()->getHash();
41
        @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

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