|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace JsPhpizeTest; |
| 4 | + |
| 5 | +use JsPhpize\JsPhpize; |
| 6 | + |
| 7 | +class JsPhpizeTest extends \PHPUnit_Framework_TestCase |
| 8 | +{ |
| 9 | + public function testCompileFile() |
| 10 | + { |
| 11 | + $jsPhpize = new JsPhpize(); |
| 12 | + $actual = $jsPhpize->compileFile(__DIR__ . '/../../examples/basic.js'); |
| 13 | + $expected = <<<'EOD' |
| 14 | +$GLOBALS["__jp_h_plus"] = function ($base) { |
| 15 | + foreach (array_slice(func_get_args(), 1) as $value) { |
| 16 | + $base = is_string($base) || is_string($value) ? $base . $value : $base + $value; |
| 17 | + } |
| 18 | +
|
| 19 | + return $base; |
| 20 | +}; |
| 21 | +$GLOBALS["__jp_h_dot"] = function ($base) { |
| 22 | + foreach (array_slice(func_get_args(), 1) as $key) { |
| 23 | + $base = is_array($base) |
| 24 | + ? (isset($base[$key]) ? $base[$key] : null) |
| 25 | + : (is_object($base) |
| 26 | + ? (isset($base->$key) |
| 27 | + ? $base->$key |
| 28 | + : (method_exists($base, $method = "get" . ucfirst($key)) |
| 29 | + ? $base->$method() |
| 30 | + : (method_exists($base, $key) |
| 31 | + ? $base->$key() |
| 32 | + : null |
| 33 | + ) |
| 34 | + ) |
| 35 | + ) |
| 36 | + : null |
| 37 | + ); |
| 38 | + } |
| 39 | +
|
| 40 | + return $base; |
| 41 | +}; |
| 42 | +$foo = array( 'bar' => array( "baz" => "hello" ) ); |
| 43 | +// Comment |
| 44 | +$biz = 'bar'; |
| 45 | +// com |
| 46 | +return call_user_func($GLOBALS["__jp_h_plus"], call_user_func($GLOBALS["__jp_h_dot"], $foo, 'bar', "baz"), ' ' , call_user_func($GLOBALS["__jp_h_plus"], call_user_func($GLOBALS["__jp_h_dot"], $foo, $biz, 'baz'), " " , call_user_func($GLOBALS["__jp_h_dot"], $foo, 'bar', 'baz'))); |
| 47 | +EOD; |
| 48 | + $actual = preg_replace('/\s/', '', $actual); |
| 49 | + $expected = preg_replace('/\s/', '', $expected); |
| 50 | + $this->assertSame($expected, $actual); |
| 51 | + $this->assertSame('', $jsPhpize->compileDependencies()); |
| 52 | + |
| 53 | + $actual = $jsPhpize->compileFile(__DIR__ . '/../../examples/basic.js', true); |
| 54 | + $expected = <<<'EOD' |
| 55 | +$foo = array( 'bar' => array( "baz" => "hello" ) ); |
| 56 | +// Comment |
| 57 | +$biz = 'bar'; |
| 58 | +// com |
| 59 | +return call_user_func($GLOBALS["__jp_h_plus"], call_user_func($GLOBALS["__jp_h_dot"], $foo, 'bar', "baz"), ' ' , call_user_func($GLOBALS["__jp_h_plus"], call_user_func($GLOBALS["__jp_h_dot"], $foo, $biz, 'baz'), " " , call_user_func($GLOBALS["__jp_h_dot"], $foo, 'bar', 'baz'))); |
| 60 | +EOD; |
| 61 | + $actual = preg_replace('/\s/', '', $actual); |
| 62 | + $expected = preg_replace('/\s/', '', $expected); |
| 63 | + $this->assertSame($expected, $actual); |
| 64 | + |
| 65 | + $actual = $jsPhpize->compileDependencies(); |
| 66 | + $expected = <<<'EOD' |
| 67 | +$GLOBALS["__jp_h_plus"] = function ($base) { |
| 68 | + foreach (array_slice(func_get_args(), 1) as $value) { |
| 69 | + $base = is_string($base) || is_string($value) ? $base . $value : $base + $value; |
| 70 | + } |
| 71 | +
|
| 72 | + return $base; |
| 73 | +}; |
| 74 | +$GLOBALS["__jp_h_dot"] = function ($base) { |
| 75 | + foreach (array_slice(func_get_args(), 1) as $key) { |
| 76 | + $base = is_array($base) |
| 77 | + ? (isset($base[$key]) ? $base[$key] : null) |
| 78 | + : (is_object($base) |
| 79 | + ? (isset($base->$key) |
| 80 | + ? $base->$key |
| 81 | + : (method_exists($base, $method = "get" . ucfirst($key)) |
| 82 | + ? $base->$method() |
| 83 | + : (method_exists($base, $key) |
| 84 | + ? $base->$key() |
| 85 | + : null |
| 86 | + ) |
| 87 | + ) |
| 88 | + ) |
| 89 | + : null |
| 90 | + ); |
| 91 | + } |
| 92 | +
|
| 93 | + return $base; |
| 94 | +}; |
| 95 | +EOD; |
| 96 | + $actual = preg_replace('/\s/', '', $actual); |
| 97 | + $expected = preg_replace('/\s/', '', $expected); |
| 98 | + $this->assertSame($expected, $actual); |
| 99 | + |
| 100 | + $jsPhpize->compileFile(__DIR__ . '/../../examples/calcul.js', true); |
| 101 | + $actual = $jsPhpize->compileDependencies(); |
| 102 | + $actual = preg_replace('/\s/', '', $actual); |
| 103 | + $this->assertSame($expected, $actual); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @expectedException \Exception |
| 108 | + * @expectedExceptionMessageRegExp /No such file/ |
| 109 | + */ |
| 110 | + public function testCompileFileMissing() |
| 111 | + { |
| 112 | + try { |
| 113 | + $jsPhpize = new JsPhpize(); |
| 114 | + $jsPhpize->compileFile('does/not/exists.js'); |
| 115 | + } catch (\Exception $e) { |
| 116 | + throw new \Exception($e->getMessage(), 1); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public function testCompileSource() |
| 121 | + { |
| 122 | + $jsPhpize = new JsPhpize(); |
| 123 | + $actual = $jsPhpize->compileCode('b = 8'); |
| 124 | + $expected = '$b = 8;'; |
| 125 | + $actual = preg_replace('/\s/', '', $actual); |
| 126 | + $expected = preg_replace('/\s/', '', $expected); |
| 127 | + $this->assertSame($expected, $actual); |
| 128 | + |
| 129 | + $dir = getcwd(); |
| 130 | + chdir(__DIR__ . '/../../examples'); |
| 131 | + $actual = $jsPhpize->compileCode('calcul.js'); |
| 132 | + chdir($dir); |
| 133 | + $expected = <<<'EOD' |
| 134 | +$GLOBALS["__jp_h_dot"] = function ($base) { |
| 135 | + foreach (array_slice(func_get_args(), 1) as $key) { |
| 136 | + $base = is_array($base) |
| 137 | + ? (isset($base[$key]) ? $base[$key] : null) |
| 138 | + : (is_object($base) |
| 139 | + ? (isset($base->$key) |
| 140 | + ? $base->$key |
| 141 | + : (method_exists($base, $method = "get" . ucfirst($key)) |
| 142 | + ? $base->$method() |
| 143 | + : (method_exists($base, $key) |
| 144 | + ? $base->$key() |
| 145 | + : null |
| 146 | + ) |
| 147 | + ) |
| 148 | + ) |
| 149 | + : null |
| 150 | + ); |
| 151 | + } |
| 152 | +
|
| 153 | + return $base; |
| 154 | +}; |
| 155 | +call_user_func($GLOBALS["__jp_h_dot"], $calcul, 'js'); |
| 156 | +EOD; |
| 157 | + $actual = preg_replace('/\s/', '', $actual); |
| 158 | + $expected = preg_replace('/\s/', '', $expected); |
| 159 | + $this->assertSame($expected, $actual); |
| 160 | + } |
| 161 | +} |
0 commit comments