testSerializationOutput()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 16
rs 9.4285
1
<?php
2
namespace CodeReview\Tests;
3
4
class CodeReviewPhpFileParserTest extends \PHPUnit_Framework_TestCase {
5
6
	public function testNotExistingFile() {
7
		$fileName = '/not/existing/file/path/to/php/file.php';
8
		$this->setExpectedException('\CodeReview\IOException', "File $fileName does not exists");
9
		new \CodeReview\PhpFileParser($fileName);
10
	}
11
12
	public function testNotAFile() {
13
		$fileName = dirname(__FILE__);//just a path to directory
14
		$this->setExpectedException('\CodeReview\IOException', "$fileName must be a file");
15
		new \CodeReview\PhpFileParser($fileName);
16
	}
17
18
	/**
19
	 * @requires PHP 5.3
20
	 */
21
	public function testUnserializeInternalErrorNoFileName() {
22
		$fileName = dirname(__FILE__) . '/test_files/php/input/sample1.php';
23
		$tokens = new \CodeReview\PhpFileParser($fileName);
24
25
		$reflection = new \ReflectionClass($tokens);
26
		$reflection_property = $reflection->getProperty('fileName');
27
		$reflection_property->setAccessible(true);
28
		$reflection_property->setValue($tokens, null);
29
30
		$this->setExpectedException('LogicException', "Missing file's path. Looks like severe internal error.");
31
32
		$serializedTokens = serialize($tokens);
33
		unserialize($serializedTokens);
34
	}
35
36
	/**
37
	 * @requires PHP 5.3
38
	 */
39
	public function testUnserializeInternalErrorBadFileName() {
40
		$fileName = dirname(__FILE__) . '/test_files/php/input/sample1.php';
41
		$badPath = dirname(__FILE__) . '/test_files/php/input/not_existing_file.php';
42
		$tokens = new \CodeReview\PhpFileParser($fileName);
43
44
		$reflection = new \ReflectionClass($tokens);
45
		$reflection_property = $reflection->getProperty('fileName');
46
		$reflection_property->setAccessible(true);
47
		$reflection_property->setValue($tokens, $badPath);
48
49
		$this->setExpectedException('\CodeReview\IOException', "File $badPath does not exists");
50
51
		$serializedTokens = serialize($tokens);
52
		unserialize($serializedTokens);
53
	}
54
55
	/**
56
	 * @requires PHP 5.3
57
	 */
58
	public function testUnserializeInternalErrorNoSha1() {
59
		$fileName = dirname(__FILE__) . '/test_files/php/input/sample1.php';
60
		$tokens = new \CodeReview\PhpFileParser($fileName);
61
62
		$reflection = new \ReflectionClass($tokens);
63
		$reflection_property = $reflection->getProperty('sha1hash');
64
		$reflection_property->setAccessible(true);
65
		$reflection_property->setValue($tokens, null);
66
67
		$this->setExpectedException('LogicException', "Missing file's SHA1 hash. Looks like severe internal error.");
68
69
		$serializedTokens = serialize($tokens);
70
		unserialize($serializedTokens);
71
	}
72
73
	public function testSerializationDataPreserve() {
74
		$tokens = new \CodeReview\PhpFileParser(__FILE__);
75
		$serializedTokens = serialize($tokens);
76
		$unserializedTokens = unserialize($serializedTokens);
77
		$this->assertEquals($tokens, $unserializedTokens);
78
	}
79
80
	/**
81
	 * @return array
82
	 */
83
	private function getTestsPhpFiles() {
84
		$tests = array(
85
			'sample1',
86
			'sample1',
87
		);
88
		$result = array();
89
		$dir = dirname(__FILE__) . '/test_files/php/';
90
		foreach ($tests as $test) {
91
			$result[$test] = array(
92
				$dir . 'input/' . $test . '.php',
93
				$dir . 'output/' . $test . '.php',
94
			);
95
		}
96
		return $result;
97
	}
98
99
	public function testSerializationOutput() {
100
		$tests = $this->getTestsPhpFiles();
101
		foreach ($tests as $test) {
102
			list($inPath, ) = $test;
103
			$tokens = new \CodeReview\PhpFileParser($inPath);
104
105
			$serializedClass = serialize($tokens);
106
			$this->assertTrue(is_string($serializedClass));
107
			$this->assertTrue(strlen($serializedClass) > 0);
108
109
			$actual = unserialize($serializedClass);
110
			$expected = new \CodeReview\PhpFileParser($inPath);
111
112
			$this->assertEquals($expected, $actual);
113
		}
114
	}
115
116
	public function testSourcePreserve() {
117
		$tests = $this->getTestsPhpFiles();
118
		foreach ($tests as $test) {
119
			list($inPath, ) = $test;
120
			$tokens = new \CodeReview\PhpFileParser($inPath);
121
122
			$actual = $tokens->exportPhp();
123
124
			$this->assertTrue(is_string($actual));
125
			$this->assertTrue(strlen($actual) > 0);
126
127
			$expected = file_get_contents($inPath);
128
129
			$this->assertEquals($expected, $actual);
130
		}
131
	}
132
133
	public function testArrayAndIteratorInterfaces() {
134
		$tests = $this->getTestsPhpFiles();
135
		foreach ($tests as $test) {
136
			list($inPath, ) = $test;
137
138
			$tokens = new \CodeReview\PhpFileParser($inPath);
139
140
			//testing repeated iteration and isset
141
			foreach (array(1,2) as $i) {
142
				$result = array();
143
				foreach ($tokens as $key => $token) {
144
					$result[] = array(isset($tokens[$key - 1]), isset($tokens[$key]), isset($tokens[$key + 1]));
145
				}
146
147
				$this->assertTrue(count($result) > 2);
148
				$first = array_shift($result);
149
				$this->assertEquals(array(false, true, true), $first);
150
				$last = array_pop($result);
151
				$this->assertEquals(array(true, true, false), $last);
152
153
				foreach ($result as $row) {
154
					$this->assertEquals(array(true, true, true), $row);
155
				}
156
			}
157
158
		}
159
	}
160
161
	public function testSerializationFileModifiedDetection() {
162
		$tests = $this->getTestsPhpFiles();
163
		foreach ($tests as $test) {
164
			list($inPath, ) = $test;
165
166
			$content = file_get_contents($inPath);
167
			$this->assertNotEmpty($content);
168
169
			$tmpPath = tempnam(sys_get_temp_dir(), 'phpfileparsertest');
170
171
			$this->assertNotEquals(file_put_contents($tmpPath, $content), false);
172
173
			$tokens = new \CodeReview\PhpFileParser($tmpPath);
174
			$serializedTokens = serialize($tokens);
175
176
			//modify file
177
			$this->assertNotEquals(file_put_contents($tmpPath, 'modified' . $content), false);
178
179
//			$this->setExpectedException('\CodeReview\IOException');
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
180
			try {
181
				//this shall fail
182
				unserialize($serializedTokens);
183
			} catch (\CodeReview\IOException $e) {
184
				$this->assertEquals("The file on disk has changed and this CodeReview\\PhpFileParser class instance is no longer valid for use. Please create fresh instance.", $e->getMessage());
185
				continue;
186
			}
187
			$this->fail('Expected \CodeReview\IOException to be thrown!');
188
		}
189
	}
190
}