|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of CaptainHook |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace CaptainHook\App\Storage; |
|
13
|
|
|
|
|
14
|
|
|
use Exception; |
|
15
|
|
|
use org\bovigo\vfs\vfsStream; |
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
|
17
|
|
|
use RuntimeException; |
|
18
|
|
|
|
|
19
|
|
|
class FileTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Tests File::getRoot |
|
23
|
|
|
*/ |
|
24
|
|
|
public function testGetPath(): void |
|
25
|
|
|
{ |
|
26
|
|
|
$file = new File(__FILE__); |
|
27
|
|
|
|
|
28
|
|
|
$this->assertEquals(__FILE__, $file->getPath()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Tests File::read |
|
33
|
|
|
*/ |
|
34
|
|
|
public function testRead(): void |
|
35
|
|
|
{ |
|
36
|
|
|
$file = new File(__FILE__); |
|
37
|
|
|
$content = $file->read(); |
|
38
|
|
|
|
|
39
|
|
|
$this->assertStringContainsString('<?php', $content); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Tests File::read |
|
44
|
|
|
*/ |
|
45
|
|
|
public function testReadFail(): void |
|
46
|
|
|
{ |
|
47
|
|
|
$this->expectException(Exception::class); |
|
48
|
|
|
|
|
49
|
|
|
$file = new File(__FILE__ . '.absent'); |
|
50
|
|
|
$file->read(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Tests File::write |
|
55
|
|
|
*/ |
|
56
|
|
|
public function testWrite(): void |
|
57
|
|
|
{ |
|
58
|
|
|
$tmpDir = sys_get_temp_dir(); |
|
59
|
|
|
$path = tempnam($tmpDir, 'foo'); |
|
60
|
|
|
$file = new File($path); |
|
61
|
|
|
$file->write('foo'); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertEquals('foo', file_get_contents($path)); |
|
64
|
|
|
$this->assertTrue(unlink($path)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Tests File::isLink |
|
69
|
|
|
*/ |
|
70
|
|
|
public function testIsLink(): void |
|
71
|
|
|
{ |
|
72
|
|
|
$tmpDir = sys_get_temp_dir(); |
|
73
|
|
|
$link = $tmpDir . '/fooLink'; |
|
74
|
|
|
$target = tempnam($tmpDir, 'bar'); |
|
75
|
|
|
|
|
76
|
|
|
symlink($target, $link); |
|
77
|
|
|
|
|
78
|
|
|
$file = new File($link); |
|
79
|
|
|
$this->assertTrue($file->isLink()); |
|
80
|
|
|
$this->assertEquals($target, $file->linkTarget()); |
|
81
|
|
|
|
|
82
|
|
|
$this->assertTrue(unlink($link)); |
|
83
|
|
|
$this->assertTrue(unlink($target)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Tests File::linkTarget |
|
88
|
|
|
*/ |
|
89
|
|
|
public function testLinkTarget(): void |
|
90
|
|
|
{ |
|
91
|
|
|
$this->expectException(RuntimeException::class); |
|
92
|
|
|
|
|
93
|
|
|
$file = new File(__FILE__); |
|
94
|
|
|
$file->linkTarget(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Tests File::write |
|
99
|
|
|
*/ |
|
100
|
|
|
public function testWriteFailNoDir(): void |
|
101
|
|
|
{ |
|
102
|
|
|
$this->expectException(Exception::class); |
|
103
|
|
|
|
|
104
|
|
|
$path = __FILE__ . DIRECTORY_SEPARATOR . 'foo.txt'; |
|
105
|
|
|
$file = new File($path); |
|
106
|
|
|
$file->write('foo'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Tests File::write |
|
111
|
|
|
*/ |
|
112
|
|
|
public function testNoWritePermission(): void |
|
113
|
|
|
{ |
|
114
|
|
|
$this->expectException(Exception::class); |
|
115
|
|
|
|
|
116
|
|
|
vfsStream::setup('exampleDir', 0000); |
|
117
|
|
|
$file = new File(vfsStream::url('exampleDir')); |
|
118
|
|
|
$file->write('test'); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Tests File::write |
|
123
|
|
|
*/ |
|
124
|
|
|
public function testCantCreateDirectory(): void |
|
125
|
|
|
{ |
|
126
|
|
|
$this->expectException(Exception::class); |
|
127
|
|
|
|
|
128
|
|
|
vfsStream::setup('exampleDir', 0000); |
|
129
|
|
|
$baseDir = vfsStream::url('exampleDir'); |
|
130
|
|
|
|
|
131
|
|
|
$path = $baseDir . '/foo/bar.txt'; |
|
132
|
|
|
$file = new File($path); |
|
133
|
|
|
$file->write('test'); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|