|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Icinga\Web; |
| 4 | + |
| 5 | +use Icinga\Application\Icinga; |
| 6 | +use Icinga\Test\BaseTestCase; |
| 7 | +use Icinga\Web\JavaScript; |
| 8 | +use SplFileObject; |
| 9 | + |
| 10 | +class JavaScriptTest extends BaseTestCase |
| 11 | +{ |
| 12 | + protected $fileRoot; |
| 13 | + |
| 14 | + public function setUp(): void |
| 15 | + { |
| 16 | + parent::setUp(); |
| 17 | + |
| 18 | + $this->fileRoot = Icinga::app()->getBaseDir('test/config/JavaScriptTest'); |
| 19 | + } |
| 20 | + |
| 21 | + public function testLocalDefineOptimizations() |
| 22 | + { |
| 23 | + $expected = <<<'JS' |
| 24 | +/* Relative path, No extension */ |
| 25 | +define("JavaScriptTest/someThing", ["JavaScriptTest/someThing/Else"], function (Else) { |
| 26 | +
|
| 27 | +}); |
| 28 | + |
| 29 | +JS; |
| 30 | + $someThing = $this->getFile('someThing.js'); |
| 31 | + $this->assertSame($expected, $this->optimizeFile($someThing)); |
| 32 | + |
| 33 | + $expected = <<<'JS' |
| 34 | +/* Relative path outside the current directory, With extension */ |
| 35 | +define("JavaScriptTest/someThing/Else", ["JavaScriptTest/someOther"], function (someOther) { |
| 36 | +
|
| 37 | +}); |
| 38 | + |
| 39 | +JS; |
| 40 | + $someThingElse = $this->getFile('someThing/Else.js'); |
| 41 | + $this->assertSame($expected, $this->optimizeFile($someThingElse)); |
| 42 | + } |
| 43 | + |
| 44 | + public function testNoRequirementsOptimization() |
| 45 | + { |
| 46 | + $expected = <<<'JS' |
| 47 | +define("JavaScriptTest/noRequirements", [], function () { |
| 48 | +
|
| 49 | +}); |
| 50 | + |
| 51 | +JS; |
| 52 | + $source = <<<'JS' |
| 53 | +define(function () { |
| 54 | +
|
| 55 | +}); |
| 56 | + |
| 57 | +JS; |
| 58 | + $this->assertSame($expected, JavaScript::optimizeDefine( |
| 59 | + $source, |
| 60 | + 'JavaScriptTest/noRequirements', |
| 61 | + 'JavaScriptTest', |
| 62 | + 'JavaScriptTest' |
| 63 | + )); |
| 64 | + } |
| 65 | + |
| 66 | + public function testGlobalRequirementsOptimization() |
| 67 | + { |
| 68 | + $expected = <<<'JS' |
| 69 | +define("JavaScriptTest/globalRequirements", ["SomeOtherTest/Anything"], function (Anything) { |
| 70 | +
|
| 71 | +}); |
| 72 | + |
| 73 | +JS; |
| 74 | + $source = <<<'JS' |
| 75 | +define(["SomeOtherTest/Anything"], function (Anything) { |
| 76 | +
|
| 77 | +}); |
| 78 | + |
| 79 | +JS; |
| 80 | + $this->assertSame($expected, JavaScript::optimizeDefine( |
| 81 | + $source, |
| 82 | + 'JavaScriptTest/globalRequirements', |
| 83 | + 'JavaScriptTest', |
| 84 | + 'JavaScriptTest' |
| 85 | + )); |
| 86 | + } |
| 87 | + |
| 88 | + protected function optimizeFile(SplFileObject $file): string |
| 89 | + { |
| 90 | + return JavaScript::optimizeDefine( |
| 91 | + $file->fread($file->getSize()), |
| 92 | + $file->getRealPath(), |
| 93 | + $this->fileRoot, |
| 94 | + 'JavaScriptTest' |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + protected function getFile(string $file): SplFileObject |
| 99 | + { |
| 100 | + return new SplFileObject(join(DIRECTORY_SEPARATOR, [$this->fileRoot, $file])); |
| 101 | + } |
| 102 | +} |
0 commit comments