1
|
|
|
<?php |
2
|
|
|
/* Copyright (C) 2017 Michael Giesler |
3
|
|
|
* |
4
|
|
|
* This file is part of Dembelo. |
5
|
|
|
* |
6
|
|
|
* Dembelo is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Affero General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* Dembelo is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU Affero General Public License 3 for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU Affero General Public License 3 |
17
|
|
|
* along with Dembelo. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace DembeloMain\Tests\Service; |
21
|
|
|
|
22
|
|
|
use DembeloMain\Service\FileHandler; |
23
|
|
|
use PHPUnit\Framework\TestCase; |
24
|
|
|
use Symfony\Component\Filesystem\Exception\FileNotFoundException; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class FileHandlerTest |
28
|
|
|
*/ |
29
|
|
|
class FileHandlerTest extends TestCase |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var FileHandler |
33
|
|
|
*/ |
34
|
|
|
private $fileHandler; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $tmpFileName; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function setUp(): void |
45
|
|
|
{ |
46
|
|
|
$this->fileHandler = new FileHandler(); |
47
|
|
|
$this->tmpFileName = @tempnam('/tmp/phpunit', 'fileHandlerTest'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
public function tearDown(): void |
54
|
|
|
{ |
55
|
|
|
$this->fileHandler = null; |
56
|
|
|
if (file_exists($this->tmpFileName)) { |
57
|
|
|
unlink($this->tmpFileName); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function testOpenThrowsExceptionWhenFileDoesNotExist(): void |
65
|
|
|
{ |
66
|
|
|
$this->expectException(FileNotFoundException::class); |
67
|
|
|
$this->fileHandler->open('unknownFile', 'r'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
|
public function testOpenReturnsNewObjectWhenFileExists(): void |
74
|
|
|
{ |
75
|
|
|
touch($this->tmpFileName); |
76
|
|
|
$newObject = $this->fileHandler->open($this->tmpFileName, 'r'); |
77
|
|
|
self::assertInstanceOf(FileHandler::class, $newObject); |
78
|
|
|
self::assertNotSame($this->fileHandler, $newObject); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
public function testCloseWhenNotInitialized(): void |
85
|
|
|
{ |
86
|
|
|
self::assertFalse($this->fileHandler->close()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
|
|
public function testCloseWhenInitialized(): void |
93
|
|
|
{ |
94
|
|
|
touch($this->tmpFileName); |
95
|
|
|
$fileHandler = $this->fileHandler->open($this->tmpFileName, 'r'); |
96
|
|
|
self::assertTrue($fileHandler->close()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
public function testEofOnEmptyFile(): void |
103
|
|
|
{ |
104
|
|
|
touch($this->tmpFileName); |
105
|
|
|
$fileHandler = $this->fileHandler->open($this->tmpFileName, 'r'); |
106
|
|
|
$fileHandler->read(1); |
107
|
|
|
self::assertTrue($fileHandler->eof()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
|
|
public function testEofForNotEmptyFile(): void |
114
|
|
|
{ |
115
|
|
|
file_put_contents($this->tmpFileName, 'one'); |
116
|
|
|
$fileHandler = $this->fileHandler->open($this->tmpFileName, 'r'); |
117
|
|
|
$fileHandler->read(2); |
118
|
|
|
self::assertFalse($fileHandler->eof()); |
119
|
|
|
$fileHandler->read(2); |
120
|
|
|
self::assertTrue($fileHandler->eof()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
public function testRead(): void |
127
|
|
|
{ |
128
|
|
|
file_put_contents($this->tmpFileName, 'onetwothree'); |
129
|
|
|
$fileHandler = $this->fileHandler->open($this->tmpFileName, 'r'); |
130
|
|
|
self::assertEquals('one', $fileHandler->read(3)); |
131
|
|
|
self::assertEquals('two', $fileHandler->read(3)); |
132
|
|
|
self::assertEquals('three', $fileHandler->read(6)); |
133
|
|
|
self::assertEquals('', $fileHandler->read(1)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
public function testSeek(): void |
140
|
|
|
{ |
141
|
|
|
file_put_contents($this->tmpFileName, 'onetwothree'); |
142
|
|
|
$fileHandler = $this->fileHandler->open($this->tmpFileName, 'r'); |
143
|
|
|
$fileHandler->seek(3); |
144
|
|
|
self::assertEquals('two', $fileHandler->read(3)); |
145
|
|
|
$fileHandler->seek(0); |
146
|
|
|
self::assertEquals('one', $fileHandler->read(3)); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return void |
151
|
|
|
*/ |
152
|
|
|
public function testWrite(): void |
153
|
|
|
{ |
154
|
|
|
file_put_contents($this->tmpFileName, 'one'); |
155
|
|
|
$fileHandler = $this->fileHandler->open($this->tmpFileName, 'r+'); |
156
|
|
|
self::assertEquals('one', $fileHandler->read(3)); |
157
|
|
|
$fileHandler->write('two'); |
158
|
|
|
$fileHandler->close(); |
159
|
|
|
self::assertStringEqualsFile($this->tmpFileName, 'onetwo'); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|