Skip to content

Commit 3d4b46f

Browse files
committed
Handle alone variables
1 parent 0aeb92d commit 3d4b46f

File tree

6 files changed

+64
-22
lines changed

6 files changed

+64
-22
lines changed

examples/varname.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
user

examples/varname.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$user;

phpunit.xml

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
<phpunit
2-
bootstrap="tests/bootstrap.php"
3-
backupGlobals="false"
4-
colors="true"
5-
convertErrorsToExceptions="true"
6-
convertNoticesToExceptions="true"
7-
convertWarningsToExceptions="true"
8-
>
9-
<testsuites>
10-
<testsuite name="Examples">
11-
<file>tests/examples.php</file>
12-
</testsuite>
13-
</testsuites>
14-
<filter>
15-
<whitelist processUncoveredFilesFromWhitelist="true">
16-
<directory suffix=".php">src</directory>
17-
</whitelist>
18-
</filter>
2+
bootstrap="tests/bootstrap.php"
3+
backupGlobals="false"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
>
9+
<testsuites>
10+
<testsuite name="render">
11+
<file>tests/render.php</file>
12+
</testsuite>
13+
<testsuite name="compile">
14+
<file>tests/compile.php</file>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist processUncoveredFilesFromWhitelist="true">
19+
<directory suffix=".php">src</directory>
20+
</whitelist>
21+
</filter>
1922
</phpunit>

src/JsPhpize/Parser/Visitor.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ protected function visitNumber($token)
1919
protected function visitVariable($token)
2020
{
2121
$variable = (substr($token->value, 0, 1) === '$' ? '' : '$') . $token->value;
22-
$next = $this->current();
22+
if (!($next = $this->current())) {
23+
return $variable;
24+
}
2325
if ($next->type === '(') {
2426
$this->skip();
2527

tests/compile.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use JsPhpize\JsPhpize;
4+
5+
class CompileTest extends \PHPUnit_Framework_TestCase
6+
{
7+
public function caseProvider()
8+
{
9+
$cases = array();
10+
11+
$examples = __DIR__ . '/../examples';
12+
foreach (scandir($examples) as $file) {
13+
if (substr($file, -4) === '.php') {
14+
$cases[] = array($examples . '/' . $file, $examples . '/' . substr($file, 0, -4) . '.js');
15+
}
16+
}
17+
18+
return $cases;
19+
}
20+
21+
/**
22+
* @dataProvider caseProvider
23+
*/
24+
public function testJsPhpizeGeneration($phpFile, $jsFile)
25+
{
26+
$jsPhpize = new JsPhpize();
27+
$expected = file_get_contents($phpFile);
28+
$result = $jsPhpize->compile($jsFile);
29+
30+
$expected = trim($expected);
31+
$actual = trim($result);
32+
33+
$this->assertSame($expected, $actual, $jsFile . ' should compile into ' . $expected);
34+
}
35+
}

tests/examples.php renamed to tests/render.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
use JsPhpize\JsPhpize;
44

5-
class ExamplesTest extends \PHPUnit_Framework_TestCase
5+
class RenderTest extends \PHPUnit_Framework_TestCase
66
{
77
public function caseProvider()
88
{
99
$cases = array();
1010

1111
$examples = __DIR__ . '/../examples';
1212
foreach (scandir($examples) as $file) {
13-
if (substr($file, -3) === '.js') {
14-
$cases[] = array($examples . '/' . substr($file, 0, -3) . '.return', $examples . '/' . $file);
13+
if (substr($file, -7) === '.return') {
14+
$cases[] = array($examples . '/' . $file, $examples . '/' . substr($file, 0, -7) . '.js');
1515
}
1616
}
1717

@@ -30,6 +30,6 @@ public function testJsPhpizeGeneration($returnFile, $jsFile)
3030
$expected = trim($expected);
3131
$actual = trim($result);
3232

33-
$this->assertSame($expected, $actual, $jsFile . ' should return ' . $expected);
33+
$this->assertSame($expected, $actual, $jsFile . ' should render ' . $expected);
3434
}
3535
}

0 commit comments

Comments
 (0)