1
|
|
|
<?php |
2
|
|
|
class CodeReviewPhpFileParserTest extends PHPUnit_Framework_TestCase { |
|
|
|
|
3
|
|
|
|
4
|
|
|
public function testNotExistingFile() { |
5
|
|
|
$fileName = '/not/existing/file/path/to/php/file.php'; |
6
|
|
|
$this->setExpectedException('CodeReview_IOException', "File $fileName does not exists"); |
7
|
|
|
new PhpFileParser($fileName); |
8
|
|
|
} |
9
|
|
|
|
10
|
|
|
public function testNotAFile() { |
11
|
|
|
$fileName = dirname(__FILE__);//just a path to directory |
12
|
|
|
$this->setExpectedException('CodeReview_IOException', "$fileName must be a file"); |
13
|
|
|
new PhpFileParser($fileName); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @requires PHP 5.3 |
18
|
|
|
*/ |
19
|
|
|
public function testUnserializeInternalErrorNoFileName() { |
20
|
|
|
$fileName = dirname(__FILE__) . '/test_files/php/input/sample1.php'; |
21
|
|
|
$tokens = new PhpFileParser($fileName); |
22
|
|
|
|
23
|
|
|
$reflection = new ReflectionClass($tokens); |
24
|
|
|
$reflection_property = $reflection->getProperty('fileName'); |
25
|
|
|
$reflection_property->setAccessible(true); |
26
|
|
|
$reflection_property->setValue($tokens, null); |
27
|
|
|
|
28
|
|
|
$this->setExpectedException('LogicException', "Missing file's path. Looks like severe internal error."); |
29
|
|
|
|
30
|
|
|
$serializedTokens = serialize($tokens); |
31
|
|
|
unserialize($serializedTokens); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @requires PHP 5.3 |
36
|
|
|
*/ |
37
|
|
|
public function testUnserializeInternalErrorBadFileName() { |
38
|
|
|
$fileName = dirname(__FILE__) . '/test_files/php/input/sample1.php'; |
39
|
|
|
$badPath = dirname(__FILE__) . '/test_files/php/input/not_existing_file.php'; |
40
|
|
|
$tokens = new PhpFileParser($fileName); |
41
|
|
|
|
42
|
|
|
$reflection = new ReflectionClass($tokens); |
43
|
|
|
$reflection_property = $reflection->getProperty('fileName'); |
44
|
|
|
$reflection_property->setAccessible(true); |
45
|
|
|
$reflection_property->setValue($tokens, $badPath); |
46
|
|
|
|
47
|
|
|
$this->setExpectedException('CodeReview_IOException', "File $badPath does not exists"); |
48
|
|
|
|
49
|
|
|
$serializedTokens = serialize($tokens); |
50
|
|
|
unserialize($serializedTokens); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @requires PHP 5.3 |
55
|
|
|
*/ |
56
|
|
|
public function testUnserializeInternalErrorNoSha1() { |
57
|
|
|
$fileName = dirname(__FILE__) . '/test_files/php/input/sample1.php'; |
58
|
|
|
$tokens = new PhpFileParser($fileName); |
59
|
|
|
|
60
|
|
|
$reflection = new ReflectionClass($tokens); |
61
|
|
|
$reflection_property = $reflection->getProperty('sha1hash'); |
62
|
|
|
$reflection_property->setAccessible(true); |
63
|
|
|
$reflection_property->setValue($tokens, null); |
64
|
|
|
|
65
|
|
|
$this->setExpectedException('LogicException', "Missing file's SHA1 hash. Looks like severe internal error."); |
66
|
|
|
|
67
|
|
|
$serializedTokens = serialize($tokens); |
68
|
|
|
unserialize($serializedTokens); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testSerializationDataPreserve() { |
72
|
|
|
$tokens = new PhpFileParser(__FILE__); |
73
|
|
|
$serializedTokens = serialize($tokens); |
74
|
|
|
$unserializedTokens = unserialize($serializedTokens); |
75
|
|
|
$this->assertEquals($tokens, $unserializedTokens); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
private function getTestsPhpFiles() { |
82
|
|
|
$tests = array( |
83
|
|
|
'sample1', |
84
|
|
|
'sample1', |
85
|
|
|
); |
86
|
|
|
$result = array(); |
87
|
|
|
$dir = dirname(__FILE__) . '/test_files/php/'; |
88
|
|
|
foreach ($tests as $test) { |
89
|
|
|
$result[$test] = array( |
90
|
|
|
$dir . 'input/' . $test . '.php', |
91
|
|
|
$dir . 'output/' . $test . '.php', |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
return $result; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testSerializationOutput() { |
98
|
|
|
$tests = $this->getTestsPhpFiles(); |
99
|
|
|
foreach ($tests as $test) { |
100
|
|
|
list($inPath, ) = $test; |
101
|
|
|
$tokens = new PhpFileParser($inPath); |
102
|
|
|
|
103
|
|
|
$serializedClass = serialize($tokens); |
104
|
|
|
$this->assertTrue(is_string($serializedClass)); |
105
|
|
|
$this->assertTrue(strlen($serializedClass) > 0); |
106
|
|
|
|
107
|
|
|
$actual = unserialize($serializedClass); |
108
|
|
|
$expected = new PhpFileParser($inPath); |
109
|
|
|
|
110
|
|
|
$this->assertEquals($expected, $actual); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testSourcePreserve() { |
115
|
|
|
$tests = $this->getTestsPhpFiles(); |
116
|
|
|
foreach ($tests as $test) { |
117
|
|
|
list($inPath, ) = $test; |
118
|
|
|
$tokens = new PhpFileParser($inPath); |
119
|
|
|
|
120
|
|
|
$actual = $tokens->exportPhp(); |
121
|
|
|
|
122
|
|
|
$this->assertTrue(is_string($actual)); |
123
|
|
|
$this->assertTrue(strlen($actual) > 0); |
124
|
|
|
|
125
|
|
|
$expected = file_get_contents($inPath); |
126
|
|
|
|
127
|
|
|
$this->assertEquals($expected, $actual); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testArrayAndIteratorInterfaces() { |
132
|
|
|
$tests = $this->getTestsPhpFiles(); |
133
|
|
|
foreach ($tests as $test) { |
134
|
|
|
list($inPath, ) = $test; |
135
|
|
|
|
136
|
|
|
$tokens = new PhpFileParser($inPath); |
137
|
|
|
|
138
|
|
|
//testing repeated iteration and isset |
139
|
|
|
foreach (array(1,2) as $i) { |
140
|
|
|
$result = array(); |
141
|
|
|
foreach ($tokens as $key => $token) { |
142
|
|
|
$result[] = array(isset($tokens[$key - 1]), isset($tokens[$key]), isset($tokens[$key + 1])); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$this->assertTrue(count($result) > 2); |
146
|
|
|
$first = array_shift($result); |
147
|
|
|
$this->assertEquals(array(false, true, true), $first); |
148
|
|
|
$last = array_pop($result); |
149
|
|
|
$this->assertEquals(array(true, true, false), $last); |
150
|
|
|
|
151
|
|
|
foreach ($result as $row) { |
152
|
|
|
$this->assertEquals(array(true, true, true), $row); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testSerializationFileModifiedDetection() { |
160
|
|
|
$tests = $this->getTestsPhpFiles(); |
161
|
|
|
foreach ($tests as $test) { |
162
|
|
|
list($inPath, ) = $test; |
163
|
|
|
|
164
|
|
|
$content = file_get_contents($inPath); |
165
|
|
|
$this->assertNotEmpty($content); |
166
|
|
|
|
167
|
|
|
$tmpPath = tempnam(sys_get_temp_dir(), 'phpfileparsertest'); |
168
|
|
|
|
169
|
|
|
$this->assertNotEquals(file_put_contents($tmpPath, $content), false); |
170
|
|
|
|
171
|
|
|
$tokens = new PhpFileParser($tmpPath); |
172
|
|
|
$serializedTokens = serialize($tokens); |
173
|
|
|
|
174
|
|
|
//modify file |
175
|
|
|
$this->assertNotEquals(file_put_contents($tmpPath, 'modified' . $content), false); |
176
|
|
|
|
177
|
|
|
// $this->setExpectedException('CodeReview_IOException'); |
|
|
|
|
178
|
|
|
try { |
179
|
|
|
//this shall fail |
180
|
|
|
unserialize($serializedTokens); |
181
|
|
|
} catch (CodeReview_IOException $e) { |
182
|
|
|
$this->assertEquals("The file on disk has changed and this PhpFileParser class instance is no longer valid for use. Please create fresh instance.", $e->getMessage()); |
183
|
|
|
continue; |
184
|
|
|
} |
185
|
|
|
$this->fail("Expected CodeReview_IOException to be thrown!"); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.