|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Test\DiTesto\FileSystem; |
|
4
|
|
|
|
|
5
|
|
|
use LazyEight\DiTesto\FileSystem\Exceptions\FileSystemException; |
|
6
|
|
|
use LazyEight\DiTesto\FileSystem\Exceptions\InvalidPathException; |
|
7
|
|
|
use LazyEight\DiTesto\FileSystem\FileSystemHandler; |
|
8
|
|
|
use LazyEight\DiTesto\FileSystem\FileSystemPath; |
|
9
|
|
|
use LazyEight\DiTesto\FileSystem\FileSystemWriter; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
|
|
12
|
|
|
class FileSystemWriterTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $file = './tests/files/urls.txt'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $path = './tests/files'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $newFilename = 'newFilename.txt'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $notReadable = './tests/files/urls_not_readable.txt'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @covers \LazyEight\DiTesto\FileSystem\FileSystemWriter::__construct |
|
36
|
|
|
* @uses \LazyEight\DiTesto\FileWriter |
|
37
|
|
|
*/ |
|
38
|
|
|
public function testCanContruct() |
|
39
|
|
|
{ |
|
40
|
|
|
$newFilename = $this->path . '/' . $this->newFilename; |
|
41
|
|
|
$handler = new FileSystemWriter(new FileSystemPath($newFilename)); |
|
42
|
|
|
|
|
43
|
|
|
$this->assertInstanceOf(FileSystemWriter::class, $handler); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @covers \LazyEight\DiTesto\FileSystem\FileSystemWriter::__construct |
|
48
|
|
|
* @covers \LazyEight\DiTesto\FileSystem\FileSystemWriter::write |
|
49
|
|
|
* @covers \LazyEight\DiTesto\FileSystem\FileSystemWriter::validate |
|
50
|
|
|
* @uses \LazyEight\DiTesto\FileWriter |
|
51
|
|
|
*/ |
|
52
|
|
|
public function testWriteNewFile() |
|
53
|
|
|
{ |
|
54
|
|
|
$content = 'TEST'; |
|
55
|
|
|
$newFilename = $this->path . '/' . $this->newFilename; |
|
56
|
|
|
$handler = new FileSystemWriter(new FileSystemPath($newFilename)); |
|
57
|
|
|
$handler->write($content); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertFileExists($newFilename); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @covers \LazyEight\DiTesto\FileSystem\FileSystemWriter::isWritable |
|
64
|
|
|
* @uses \LazyEight\DiTesto\FileWriter |
|
65
|
|
|
*/ |
|
66
|
|
|
public function testIsWriteable() |
|
67
|
|
|
{ |
|
68
|
|
|
$newFilename = $this->path . '/' . $this->newFilename; |
|
69
|
|
|
|
|
70
|
|
|
$handler = new FileSystemWriter(new FileSystemPath($newFilename)); |
|
71
|
|
|
$errorHandler = new FileSystemWriter(new FileSystemPath($this->notReadable)); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertTrue($handler->isWritable()); |
|
74
|
|
|
$this->assertFalse($errorHandler->isWritable()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @covers \LazyEight\DiTesto\FileSystem\FileSystemWriter::validate |
|
79
|
|
|
* @covers \LazyEight\DiTesto\FileSystem\FileSystemWriter::isWritablePath |
|
80
|
|
|
* @expectedException \LazyEight\DiTesto\FileSystem\Exceptions\FileSystemException |
|
81
|
|
|
*/ |
|
82
|
|
|
public function testCantWrite() |
|
83
|
|
|
{ |
|
84
|
|
|
$notReadableHandler = new FileSystemWriter(new FileSystemPath($this->notReadable)); |
|
85
|
|
|
$pathHandler = new FileSystemWriter(new FileSystemPath($this->path)); |
|
86
|
|
|
|
|
87
|
|
|
$this->expectException(FileSystemException::class); |
|
88
|
|
|
$notReadableHandler->write('TEST'); |
|
89
|
|
|
|
|
90
|
|
|
$this->expectException(FileSystemException::class); |
|
91
|
|
|
$pathHandler->write('TEST'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @covers \LazyEight\DiTesto\FileSystem\FileSystemWriter::validate |
|
96
|
|
|
* @covers \LazyEight\DiTesto\FileSystem\FileSystemWriter::isWritablePath |
|
97
|
|
|
* @expectedException \LazyEight\DiTesto\FileSystem\Exceptions\InvalidPathException |
|
98
|
|
|
*/ |
|
99
|
|
|
public function testCantWriteDirectory() |
|
100
|
|
|
{ |
|
101
|
|
|
$pathHandler = new FileSystemWriter(new FileSystemPath($this->path)); |
|
102
|
|
|
|
|
103
|
|
|
$this->expectException(InvalidPathException::class); |
|
104
|
|
|
$pathHandler->write('TEST'); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @covers \LazyEight\DiTesto\FileSystem\FileSystemWriter::validate |
|
109
|
|
|
* @covers \LazyEight\DiTesto\FileSystem\FileSystemWriter::isWritablePath |
|
110
|
|
|
* @expectedException \LazyEight\DiTesto\FileSystem\Exceptions\InvalidPathException |
|
111
|
|
|
*/ |
|
112
|
|
|
public function testCantWriteNoPermission() |
|
113
|
|
|
{ |
|
114
|
|
|
$path = $this->path . '/' . 'newPath'; |
|
115
|
|
|
mkdir($path); |
|
116
|
|
|
chmod($path, 0555); |
|
117
|
|
|
|
|
118
|
|
|
$newFilename = $path . '/' . $this->newFilename; |
|
119
|
|
|
$handler = new FileSystemWriter(new FileSystemPath($newFilename)); |
|
120
|
|
|
|
|
121
|
|
|
$this->expectException(FileSystemException::class); |
|
122
|
|
|
$handler->write('TEST'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @inheritDoc |
|
127
|
|
|
*/ |
|
128
|
|
|
public static function tearDownAfterClass() |
|
129
|
|
|
{ |
|
130
|
|
|
$file = './tests/files/newFilename.txt'; |
|
131
|
|
|
$newFilePath = './tests/files/newPath'; |
|
132
|
|
|
|
|
133
|
|
|
if (file_exists($file)) { |
|
134
|
|
|
unlink($file); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
if (file_exists($newFilePath)) { |
|
138
|
|
|
rmdir($newFilePath); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
parent::tearDownAfterClass(); // TODO: Change the autogenerated stub |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|