Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of Psy Shell.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) 2012-2017 Justin Hileman
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Psy\Test\CodeCleaner;
|
Chris@0
|
13
|
Chris@0
|
14 use PhpParser\NodeTraverser;
|
Chris@0
|
15 use Psy\CodeCleaner\PassableByReferencePass;
|
Chris@0
|
16
|
Chris@0
|
17 class PassableByReferencePassTest extends CodeCleanerTestCase
|
Chris@0
|
18 {
|
Chris@0
|
19 public function setUp()
|
Chris@0
|
20 {
|
Chris@0
|
21 $this->pass = new PassableByReferencePass();
|
Chris@0
|
22 $this->traverser = new NodeTraverser();
|
Chris@0
|
23 $this->traverser->addVisitor($this->pass);
|
Chris@0
|
24 }
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * @dataProvider invalidStatements
|
Chris@0
|
28 * @expectedException \Psy\Exception\FatalErrorException
|
Chris@0
|
29 */
|
Chris@0
|
30 public function testProcessStatementFails($code)
|
Chris@0
|
31 {
|
Chris@0
|
32 $stmts = $this->parse($code);
|
Chris@0
|
33 $this->traverser->traverse($stmts);
|
Chris@0
|
34 }
|
Chris@0
|
35
|
Chris@0
|
36 public function invalidStatements()
|
Chris@0
|
37 {
|
Chris@0
|
38 return array(
|
Chris@0
|
39 array('array_pop(array())'),
|
Chris@0
|
40 array('array_pop(array($foo))'),
|
Chris@0
|
41 array('array_shift(array())'),
|
Chris@0
|
42 );
|
Chris@0
|
43 }
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * @dataProvider validStatements
|
Chris@0
|
47 */
|
Chris@0
|
48 public function testProcessStatementPasses($code)
|
Chris@0
|
49 {
|
Chris@0
|
50 $stmts = $this->parse($code);
|
Chris@0
|
51 $this->traverser->traverse($stmts);
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 public function validStatements()
|
Chris@0
|
55 {
|
Chris@0
|
56 return array(
|
Chris@0
|
57 array('array_pop(json_decode("[]"))'),
|
Chris@0
|
58 array('array_pop($foo)'),
|
Chris@0
|
59 array('array_pop($foo->bar)'),
|
Chris@0
|
60 array('array_pop($foo::baz)'),
|
Chris@0
|
61 array('array_pop(Foo::qux)'),
|
Chris@0
|
62 );
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 /**
|
Chris@0
|
66 * @dataProvider validArrayMultisort
|
Chris@0
|
67 */
|
Chris@0
|
68 public function testArrayMultisort($code)
|
Chris@0
|
69 {
|
Chris@0
|
70 $stmts = $this->parse($code);
|
Chris@0
|
71 $this->traverser->traverse($stmts);
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 public function validArrayMultisort()
|
Chris@0
|
75 {
|
Chris@0
|
76 return array(
|
Chris@0
|
77 array('array_multisort($a)'),
|
Chris@0
|
78 array('array_multisort($a, $b)'),
|
Chris@0
|
79 array('array_multisort($a, SORT_NATURAL, $b)'),
|
Chris@0
|
80 array('array_multisort($a, SORT_NATURAL | SORT_FLAG_CASE, $b)'),
|
Chris@0
|
81 array('array_multisort($a, SORT_ASC, SORT_NATURAL | SORT_FLAG_CASE, $b)'),
|
Chris@0
|
82 array('array_multisort($a, SORT_NATURAL | SORT_FLAG_CASE, SORT_ASC, $b)'),
|
Chris@0
|
83 array('array_multisort($a, $b, SORT_ASC, SORT_NATURAL | SORT_FLAG_CASE)'),
|
Chris@0
|
84 array('array_multisort($a, SORT_NATURAL | SORT_FLAG_CASE, $b, SORT_ASC, SORT_NATURAL | SORT_FLAG_CASE)'),
|
Chris@0
|
85 array('array_multisort($a, 1, $b)'),
|
Chris@0
|
86 array('array_multisort($a, 1 + 2, $b)'),
|
Chris@0
|
87 array('array_multisort($a, getMultisortFlags(), $b)'),
|
Chris@0
|
88 );
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 /**
|
Chris@0
|
92 * @dataProvider invalidArrayMultisort
|
Chris@0
|
93 * @expectedException \Psy\Exception\FatalErrorException
|
Chris@0
|
94 */
|
Chris@0
|
95 public function testInvalidArrayMultisort($code)
|
Chris@0
|
96 {
|
Chris@0
|
97 $stmts = $this->parse($code);
|
Chris@0
|
98 $this->traverser->traverse($stmts);
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 public function invalidArrayMultisort()
|
Chris@0
|
102 {
|
Chris@0
|
103 return array(
|
Chris@0
|
104 array('array_multisort(1)'),
|
Chris@0
|
105 array('array_multisort(array(1, 2, 3))'),
|
Chris@0
|
106 array('array_multisort($a, SORT_NATURAL, SORT_ASC, SORT_NATURAL, $b)'),
|
Chris@0
|
107 );
|
Chris@0
|
108 }
|
Chris@0
|
109 }
|