|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Test\DiTesto; |
|
4
|
|
|
|
|
5
|
|
|
use LazyEight\DiTesto\Exceptions\IOException; |
|
6
|
|
|
use LazyEight\DiTesto\Line; |
|
7
|
|
|
use LazyEight\DiTesto\TextFile; |
|
8
|
|
|
use LazyEight\DiTesto\TextFileWriter; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
|
|
11
|
|
|
class TextFileWriterTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $file = './tests/files/urls.txt'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $path = './tests/files'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $newFilename = 'newFilename.txt'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $testLine = 'THIS IS A TEST LINE'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @covers \LazyEight\DiTesto\TextFileWriter::writeFile |
|
35
|
|
|
* @uses \LazyEight\DiTesto\TextFileWriter |
|
36
|
|
|
*/ |
|
37
|
|
|
public function testWriteFile() |
|
38
|
|
|
{ |
|
39
|
|
|
$newFilename = $this->path . '/' . $this->newFilename; |
|
40
|
|
|
$textFile = new TextFile($newFilename); |
|
41
|
|
|
$textFile[] = new Line($this->testLine); |
|
42
|
|
|
|
|
43
|
|
|
$textFileWriter = new TextFileWriter(); |
|
44
|
|
|
$textFileWriter->writeFile($textFile); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertTrue($textFile->exists()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @covers \LazyEight\DiTesto\TextFileWriter::writeFile |
|
51
|
|
|
* @uses \LazyEight\DiTesto\TextFileWriter |
|
52
|
|
|
* @expectException IOException |
|
53
|
|
|
*/ |
|
54
|
|
|
public function testCantWriteFile() |
|
55
|
|
|
{ |
|
56
|
|
|
$path = $this->path . '/' . 'newPath'; |
|
57
|
|
|
mkdir($path); |
|
58
|
|
|
chmod($path, 0555); |
|
59
|
|
|
|
|
60
|
|
|
$newFilename = $path . '/' . $this->newFilename; |
|
61
|
|
|
$textFile = new TextFile($newFilename); |
|
62
|
|
|
$textFile[] = new Line($this->testLine); |
|
63
|
|
|
|
|
64
|
|
|
$this->expectException(IOException::class); |
|
65
|
|
|
(new TextFileWriter())->writeFile($textFile); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @inheritDoc |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function tearDownAfterClass() |
|
72
|
|
|
{ |
|
73
|
|
|
$file = './tests/files/newFilename.txt'; |
|
74
|
|
|
$newFilePath = './tests/files/newPath'; |
|
75
|
|
|
|
|
76
|
|
|
if (file_exists($file)) { |
|
77
|
|
|
unlink($file); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (file_exists($newFilePath)) { |
|
81
|
|
|
rmdir($newFilePath); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
parent::tearDownAfterClass(); // TODO: Change the autogenerated stub |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|