4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Test\CodeCleaner;
14 use PhpParser\NodeTraverser;
15 use Psy\CodeCleaner\LoopContextPass;
17 class LoopContextPassTest extends CodeCleanerTestCase
19 public function setUp()
21 $this->pass = new LoopContextPass();
22 $this->traverser = new NodeTraverser();
23 $this->traverser->addVisitor($this->pass);
27 * @dataProvider invalidStatements
28 * @expectedException \Psy\Exception\FatalErrorException
30 public function testProcessStatementFails($code)
32 $stmts = $this->parse($code);
33 $this->traverser->traverse($stmts);
36 public function invalidStatements()
41 array('if (true) { continue; }'),
42 array('if (true) { break; }'),
43 array('if (false) { continue; }'),
44 array('if (false) { break; }'),
45 array('function foo() { break; }'),
46 array('function foo() { continue; }'),
48 // actually enforce break/continue depth argument
49 array('do { break 2; } while (true)'),
50 array('do { continue 2; } while (true)'),
51 array('for ($a; $b; $c) { break 2; }'),
52 array('for ($a; $b; $c) { continue 2; }'),
53 array('foreach ($a as $b) { break 2; }'),
54 array('foreach ($a as $b) { continue 2; }'),
55 array('switch (true) { default: break 2; }'),
56 array('switch (true) { default: continue 2; }'),
57 array('while (true) { break 2; }'),
58 array('while (true) { continue 2; }'),
60 // invalid in 5.4+ because they're floats
61 // ... in 5.3 because the number is too big
62 array('while (true) { break 2.0; }'),
63 array('while (true) { continue 2.0; }'),
65 // and once with nested loops, just for good measure
66 array('while (true) { while (true) { break 3; } }'),
67 array('while (true) { while (true) { continue 3; } }'),
72 * @dataProvider invalidPHP54Statements
73 * @expectedException \Psy\Exception\FatalErrorException
75 public function testPHP54ProcessStatementFails($code)
77 if (version_compare(PHP_VERSION, '5.4.0', '<')) {
78 $this->markTestSkipped();
81 $stmts = $this->parse($code);
82 $this->traverser->traverse($stmts);
85 public function invalidPHP54Statements()
88 // In PHP 5.4+, only positive literal integers are allowed
89 array('while (true) { break $n; }'),
90 array('while (true) { continue $n; }'),
91 array('while (true) { break N; }'),
92 array('while (true) { continue N; }'),
93 array('while (true) { break 0; }'),
94 array('while (true) { continue 0; }'),
95 array('while (true) { break -1; }'),
96 array('while (true) { continue -1; }'),
97 array('while (true) { break 1.0; }'),
98 array('while (true) { continue 1.0; }'),
103 * @dataProvider validStatements
105 public function testProcessStatementPasses($code)
107 $stmts = $this->parse($code);
108 $this->traverser->traverse($stmts);
111 public function validStatements()
114 array('do { break; } while (true)'),
115 array('do { continue; } while (true)'),
116 array('for ($a; $b; $c) { break; }'),
117 array('for ($a; $b; $c) { continue; }'),
118 array('foreach ($a as $b) { break; }'),
119 array('foreach ($a as $b) { continue; }'),
120 array('switch (true) { default: break; }'),
121 array('switch (true) { default: continue; }'),
122 array('while (true) { break; }'),
123 array('while (true) { continue; }'),
125 // `break 1` is redundant, but not invalid
126 array('while (true) { break 1; }'),
127 array('while (true) { continue 1; }'),
129 // and once with nested loops just for good measure
130 array('while (true) { while (true) { break 2; } }'),
131 array('while (true) { while (true) { continue 2; } }'),