JSMinTest.php
168 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JSMin\Test; |
| 4 | |
| 5 | use JSMin\JSMin; |
| 6 | |
| 7 | /** |
| 8 | * Copyright (c) 2009, Robert Hafner |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without |
| 12 | * modification, are permitted provided that the following conditions are met: |
| 13 | * Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * Neither the name of the Stash Project nor the |
| 19 | * names of its contributors may be used to endorse or promote products |
| 20 | * derived from this software without specific prior written permission. |
| 21 | * |
| 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 25 | * DISCLAIMED. IN NO EVENT SHALL Robert Hafner BE LIABLE FOR ANY |
| 26 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 28 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 29 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | * |
| 33 | */ |
| 34 | class JSMinTest extends TestCase { |
| 35 | |
| 36 | /** |
| 37 | * @group minify |
| 38 | * @dataProvider minifyProvider |
| 39 | */ |
| 40 | public function testMinify($testName, $input, $expected, $actualFile) |
| 41 | { |
| 42 | $actual = JSMin::minify($input); |
| 43 | if ($actual !== $expected && is_writable(dirname($actualFile))) { |
| 44 | file_put_contents($actualFile, $actual); |
| 45 | } |
| 46 | $this->assertEquals($expected, $actual, 'Running Minify Test: ' . $testName); |
| 47 | } |
| 48 | |
| 49 | public function testWhitespace() { |
| 50 | $this->assertEquals("hello;", JSMin::minify("\r\n\r\n \t\v\fhello;\r\n")); |
| 51 | } |
| 52 | |
| 53 | public function testBomRemoval() { |
| 54 | $this->assertEquals("hello;", JSMin::minify("\xEF\xBB\xBFhello;")); |
| 55 | } |
| 56 | |
| 57 | public function testFuncOverload() { |
| 58 | if (!function_exists('mb_strlen') || !((int)ini_get('mbstring.func_overload') & 2)) { |
| 59 | $this->markTestIncomplete('Cannot be tested unless mbstring.func_overload is used'); |
| 60 | } |
| 61 | |
| 62 | $input = 'function(s) { return /^[£$€?.]/.test(s); }'; |
| 63 | $expected = 'function(s){return/^[£$€?.]/.test(s);}'; |
| 64 | $this->assertEquals($expected, JSMin::minify($input)); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @dataProvider exceptionProvider |
| 69 | */ |
| 70 | public function testExpections($input, $label, $expClass, $expMessage) { |
| 71 | $eClass = $eMsg = ''; |
| 72 | try { |
| 73 | JSMin::minify($input); |
| 74 | } catch (\Exception $e) { |
| 75 | $eClass = get_class($e); |
| 76 | $eMsg = $e->getMessage(); |
| 77 | } |
| 78 | $this->assertTrue( |
| 79 | $eClass === $expClass && $eMsg === $expMessage, |
| 80 | 'JSMin : throw on ' . $label |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | public function exceptionProvider() { |
| 85 | return array( |
| 86 | array( |
| 87 | '"Hello' |
| 88 | ,'Unterminated String' |
| 89 | ,'JSMin\\UnterminatedStringException' |
| 90 | ,"JSMin: Unterminated String at byte 5: \"Hello"), |
| 91 | array( |
| 92 | "return /regexp\n}" |
| 93 | ,'Unterminated RegExp' |
| 94 | ,'JSMin\\UnterminatedRegExpException' |
| 95 | ,"JSMin: Unterminated RegExp at byte 14: /regexp\n"), |
| 96 | array( |
| 97 | "return/regexp\n}" |
| 98 | ,'Unterminated RegExp' |
| 99 | ,'JSMin\\UnterminatedRegExpException' |
| 100 | ,"JSMin: Unterminated RegExp at byte 13: /regexp\n"), |
| 101 | array( |
| 102 | ";return/regexp\n}" |
| 103 | ,'Unterminated RegExp' |
| 104 | ,'JSMin\\UnterminatedRegExpException' |
| 105 | ,"JSMin: Unterminated RegExp at byte 14: /regexp\n"), |
| 106 | array( |
| 107 | ";return /regexp\n}" |
| 108 | ,'Unterminated RegExp' |
| 109 | ,'JSMin\\UnterminatedRegExpException' |
| 110 | ,"JSMin: Unterminated RegExp at byte 15: /regexp\n"), |
| 111 | array( |
| 112 | "typeof/regexp\n}" |
| 113 | ,'Unterminated RegExp' |
| 114 | ,'JSMin\\UnterminatedRegExpException' |
| 115 | ,"JSMin: Unterminated RegExp at byte 13: /regexp\n"), |
| 116 | array( |
| 117 | "/* Comment " |
| 118 | ,'Unterminated Comment' |
| 119 | ,'JSMin\\UnterminatedCommentException' |
| 120 | ,"JSMin: Unterminated comment at byte 11: /* Comment "), |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * This function loads all of the test cases from the specified group. |
| 126 | * Groups are created simply by populating the appropriate directories: |
| 127 | * |
| 128 | * /tests/Resources/GROUPNAME/input/ |
| 129 | * /tests/Resources/GROUPNAME/output/ |
| 130 | * |
| 131 | * Each test case should have two identically named files, with the raw |
| 132 | * javascript going in the test folder and the expected results to be in |
| 133 | * the output folder. |
| 134 | * |
| 135 | * @param $group string |
| 136 | * @return array |
| 137 | */ |
| 138 | public function getTestFiles($group) |
| 139 | { |
| 140 | $baseDir = __DIR__ . '/Resources/' . $group . '/'; |
| 141 | $testDir = $baseDir . 'input/'; |
| 142 | $expectDir = $baseDir . 'expected/'; |
| 143 | $actualDir = $baseDir . 'actual/'; |
| 144 | |
| 145 | $returnData = array(); |
| 146 | |
| 147 | $testFiles = scandir($testDir); |
| 148 | foreach ($testFiles as $testFile) { |
| 149 | if (substr($testFile, -3) !== '.js' || !file_exists(($expectDir . $testFile))) { |
| 150 | continue; |
| 151 | } |
| 152 | |
| 153 | $testInput = file_get_contents($testDir . $testFile); |
| 154 | $expectedOutput = file_get_contents($expectDir . $testFile); |
| 155 | $actualFile = $actualDir . $testFile; |
| 156 | |
| 157 | $returnData[] = array($testFile, $testInput, $expectedOutput, $actualFile); |
| 158 | } |
| 159 | |
| 160 | return $returnData; |
| 161 | } |
| 162 | |
| 163 | public function minifyProvider() |
| 164 | { |
| 165 | return $this->getTestFiles('minify'); |
| 166 | } |
| 167 | } |
| 168 |