Yaffs site version 1.1
[yaffs-website] / vendor / psy / psysh / test / Psy / Test / CodeCleanerTest.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 Justin Hileman
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Psy\Test;
13
14 use Psy\CodeCleaner;
15
16 class CodeCleanerTest extends \PHPUnit_Framework_TestCase
17 {
18     /**
19      * @dataProvider semicolonCodeProvider
20      */
21     public function testAutomaticSemicolons(array $lines, $requireSemicolons, $expected)
22     {
23         $cc = new CodeCleaner();
24         $this->assertEquals($expected, $cc->clean($lines, $requireSemicolons));
25     }
26
27     public function semicolonCodeProvider()
28     {
29         $values = array(
30             array(array('true'),  false, 'return true;'),
31             array(array('true;'), false, 'return true;'),
32             array(array('true;'), true,  'return true;'),
33             array(array('true'),  true,  false),
34
35             array(array('echo "foo";', 'true'), true,  false),
36         );
37
38         if (version_compare(PHP_VERSION, '5.4', '<')) {
39             $values[] = array(array('echo "foo";', 'true'), false, "echo 'foo';\nreturn true;");
40         } else {
41             $values[] = array(array('echo "foo";', 'true'), false, "echo \"foo\";\nreturn true;");
42         }
43
44         return $values;
45     }
46
47     /**
48      * @dataProvider unclosedStatementsProvider
49      */
50     public function testUnclosedStatements(array $lines, $isUnclosed)
51     {
52         $cc  = new CodeCleaner();
53         $res = $cc->clean($lines);
54
55         if ($isUnclosed) {
56             $this->assertFalse($res);
57         } else {
58             $this->assertNotFalse($res);
59         }
60     }
61
62     public function unclosedStatementsProvider()
63     {
64         return array(
65             array(array('echo "'),   true),
66             array(array('echo \''),  true),
67             array(array('if (1) {'), true),
68
69             array(array('echo ""'),   false),
70             array(array("echo ''"),   false),
71             array(array('if (1) {}'), false),
72
73             array(array('// closed comment'),    false),
74             array(array('function foo() { /**'), true),
75         );
76     }
77
78     /**
79      * @dataProvider moreUnclosedStatementsProvider
80      */
81     public function testMoreUnclosedStatements(array $lines)
82     {
83         if (defined('HHVM_VERSION')) {
84             $this->markTestSkipped('HHVM not supported.');
85         }
86
87         $cc  = new CodeCleaner();
88         $res = $cc->clean($lines);
89
90         $this->assertFalse($res);
91     }
92
93     public function moreUnclosedStatementsProvider()
94     {
95         return array(
96             array(array("\$content = <<<EOS\n")),
97             array(array("\$content = <<<'EOS'\n")),
98
99             array(array('/* unclosed comment')),
100             array(array('/** unclosed comment')),
101         );
102     }
103
104     /**
105      * @dataProvider invalidStatementsProvider
106      * @expectedException \Psy\Exception\ParseErrorException
107      */
108     public function testInvalidStatementsThrowParseErrors($code)
109     {
110         $cc = new CodeCleaner();
111         $cc->clean(array($code));
112     }
113
114     public function invalidStatementsProvider()
115     {
116         return array(
117             array('function "what'),
118             array("function 'what"),
119             array('echo }'),
120             array('echo {'),
121             array('if (1) }'),
122             array('echo """'),
123             array("echo '''"),
124             array('$foo "bar'),
125             array('$foo \'bar'),
126         );
127     }
128 }