1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of CaptainHook. |
4
|
|
|
* |
5
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace CaptainHook\App\Storage; |
11
|
|
|
|
12
|
|
|
class FileTest extends \PHPUnit_Framework_TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Tests File::getPath |
16
|
|
|
*/ |
17
|
|
|
public function testGetPath() |
18
|
|
|
{ |
19
|
|
|
$file = new File(__FILE__); |
20
|
|
|
$this->assertEquals(__FILE__, $file->getPath()); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Tests File::read |
25
|
|
|
*/ |
26
|
|
|
public function testRead() |
27
|
|
|
{ |
28
|
|
|
$file = new File(__FILE__); |
29
|
|
|
$content = $file->read(); |
30
|
|
|
|
31
|
|
|
$this->assertTrue((bool)strstr($content, '<?php')); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Tests File::read |
36
|
|
|
* |
37
|
|
|
* @expectedException \Exception |
38
|
|
|
*/ |
39
|
|
|
public function testReadFail() |
40
|
|
|
{ |
41
|
|
|
$file = new File(__FILE__ . '.absent'); |
42
|
|
|
$content = $file->read(); |
|
|
|
|
43
|
|
|
|
44
|
|
|
$this->assertTrue(false); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Tests File::write |
49
|
|
|
*/ |
50
|
|
|
public function testWrite() |
51
|
|
|
{ |
52
|
|
|
$tmpDir = sys_get_temp_dir(); |
53
|
|
|
$path = tempnam($tmpDir, 'foo'); |
54
|
|
|
$file = new File($path); |
55
|
|
|
$file->write('foo'); |
56
|
|
|
|
57
|
|
|
$this->assertEquals('foo', file_get_contents($path)); |
58
|
|
|
$this->assertTrue(unlink($path)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Tests File::write |
63
|
|
|
* |
64
|
|
|
* @expectedException \Exception |
65
|
|
|
*/ |
66
|
|
|
public function testWriteFailNoDir() |
67
|
|
|
{ |
68
|
|
|
$path = __FILE__ . DIRECTORY_SEPARATOR . 'foo.txt'; |
69
|
|
|
$file = new File($path); |
70
|
|
|
$file->write('foo'); |
71
|
|
|
|
72
|
|
|
$this->assertTrue(false); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Tests File::write |
77
|
|
|
* |
78
|
|
|
* @expectedException \Exception |
79
|
|
|
*/ |
80
|
|
|
public function testNoWritePermission() |
81
|
|
|
{ |
82
|
|
|
$path = tempnam(sys_get_temp_dir(), 'noPermission'); |
83
|
|
|
chmod($path, 0000); |
84
|
|
|
|
85
|
|
|
$file = new File($path); |
86
|
|
|
$file->write('test'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Tests File::write |
91
|
|
|
* |
92
|
|
|
* @expectedException \Exception |
93
|
|
|
*/ |
94
|
|
|
public function testCantCreateDirectory() |
95
|
|
|
{ |
96
|
|
|
$baseDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('basedir'); |
97
|
|
|
mkdir($baseDir, 0000); |
98
|
|
|
|
99
|
|
|
$path = $baseDir . '/foo/bar.txt'; |
100
|
|
|
$file = new File($path); |
101
|
|
|
$file->write('test'); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.