1
|
|
|
<?php |
2
|
|
|
namespace WebStream\IO\Test; |
3
|
|
|
|
4
|
|
|
require_once dirname(__FILE__) . '/../File.php'; |
5
|
|
|
require_once dirname(__FILE__) . '/../InputStream.php'; |
6
|
|
|
require_once dirname(__FILE__) . '/../OutputStream.php'; |
7
|
|
|
require_once dirname(__FILE__) . '/../FileInputStream.php'; |
8
|
|
|
require_once dirname(__FILE__) . '/../FileOutputStream.php'; |
9
|
|
|
require_once dirname(__FILE__) . '/../ConsoleOutputStream.php'; |
10
|
|
|
require_once dirname(__FILE__) . '/../Reader/InputStreamReader.php'; |
11
|
|
|
require_once dirname(__FILE__) . '/../Reader/FileReader.php'; |
12
|
|
|
require_once dirname(__FILE__) . '/../Writer/OutputStreamWriter.php'; |
13
|
|
|
require_once dirname(__FILE__) . '/../Test/Providers/OutputStreamWriterProvider.php'; |
14
|
|
|
require_once dirname(__FILE__) . '/../Test/Modules/IOException.php'; |
15
|
|
|
require_once dirname(__FILE__) . '/../Test/Modules/InvalidArgumentException.php'; |
16
|
|
|
|
17
|
|
|
use WebStream\IO\File; |
18
|
|
|
use WebStream\IO\FileOutputStream; |
19
|
|
|
use WebStream\IO\ConsoleOutputStream; |
20
|
|
|
use WebStream\IO\Reader\FileReader; |
21
|
|
|
use WebStream\IO\Writer\OutputStreamWriter; |
22
|
|
|
use WebStream\IO\Test\Providers\OutputStreamWriterProvider; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* OutputStreamWriterTest |
26
|
|
|
* @author Ryuichi TANAKA. |
27
|
|
|
* @since 2016/08/19 |
28
|
|
|
* @version 0.7 |
29
|
|
|
*/ |
30
|
|
|
class OutputStreamWriterTest extends \PHPUnit_Framework_TestCase |
31
|
|
|
{ |
32
|
|
|
use OutputStreamWriterProvider; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* 正常系 |
36
|
|
|
* ファイルに書き込みができること |
37
|
|
|
* @test |
38
|
|
|
* @dataProvider writeProvider |
39
|
|
|
*/ |
40
|
|
|
public function okFileWriteFromFilePath($filePath) |
41
|
|
|
{ |
42
|
|
|
@unlink($filePath); |
43
|
|
|
$stream = new FileOutputStream($filePath); |
44
|
|
|
$writer = new OutputStreamWriter($stream); |
45
|
|
|
$writer->write("test"); |
46
|
|
|
$writer->write("test"); |
47
|
|
|
$writer->flush(); |
48
|
|
|
$writer->close(); |
49
|
|
|
|
50
|
|
|
$reader = new FileReader($filePath); |
51
|
|
|
$this->assertEquals($reader->read(), "testtest"); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* 正常系 |
56
|
|
|
* ファイルに書き込みができること |
57
|
|
|
* ファイルオブジェクトを指定 |
58
|
|
|
* @test |
59
|
|
|
* @dataProvider writeProvider |
60
|
|
|
*/ |
61
|
|
|
public function okFileWriteFromFileObject($filePath) |
62
|
|
|
{ |
63
|
|
|
@unlink($filePath); |
64
|
|
|
$stream = new FileOutputStream(new File($filePath)); |
65
|
|
|
$writer = new OutputStreamWriter($stream); |
66
|
|
|
$writer->write("test"); |
67
|
|
|
$writer->write("test"); |
68
|
|
|
$writer->flush(); |
69
|
|
|
$writer->close(); |
70
|
|
|
|
71
|
|
|
$reader = new FileReader($filePath); |
72
|
|
|
$this->assertEquals($reader->read(), "testtest"); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* 正常系 |
77
|
|
|
* Offset指定でファイルに書き込みができること |
78
|
|
|
* @test |
79
|
|
|
* @dataProvider writeProvider |
80
|
|
|
*/ |
81
|
|
|
public function okFileWriteOffset($filePath) |
82
|
|
|
{ |
83
|
|
|
unlink($filePath); |
84
|
|
|
$stream = new FileOutputStream($filePath); |
85
|
|
|
$writer = new OutputStreamWriter($stream); |
86
|
|
|
$writer->write("123"); |
87
|
|
|
$writer->write("123456", 3); |
88
|
|
|
$writer->flush(); |
89
|
|
|
$writer->close(); |
90
|
|
|
|
91
|
|
|
$reader = new FileReader($filePath); |
92
|
|
|
$this->assertEquals($reader->read(), "123456"); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* 正常系 |
97
|
|
|
* Length指定でファイルに書き込みができること |
98
|
|
|
* @test |
99
|
|
|
* @dataProvider writeProvider |
100
|
|
|
*/ |
101
|
|
|
public function okFileWriteLength($filePath) |
102
|
|
|
{ |
103
|
|
|
@unlink($filePath); |
104
|
|
|
$stream = new FileOutputStream($filePath); |
105
|
|
|
$writer = new OutputStreamWriter($stream); |
106
|
|
|
$writer->write("123"); |
107
|
|
|
$writer->write("123456789", null, 3); |
108
|
|
|
$writer->flush(); |
109
|
|
|
$writer->close(); |
110
|
|
|
|
111
|
|
|
$reader = new FileReader($filePath); |
112
|
|
|
$this->assertEquals($reader->read(), "123123"); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* 正常系 |
117
|
|
|
* Offset,Length指定でファイルに書き込みができること |
118
|
|
|
* @test |
119
|
|
|
* @dataProvider writeProvider |
120
|
|
|
*/ |
121
|
|
|
public function okFileWriteOffsetLength($filePath) |
122
|
|
|
{ |
123
|
|
|
@unlink($filePath); |
124
|
|
|
$stream = new FileOutputStream($filePath); |
125
|
|
|
$writer = new OutputStreamWriter($stream); |
126
|
|
|
$writer->write("123"); |
127
|
|
|
$writer->write("123456789", 3, 3); |
128
|
|
|
$writer->flush(); |
129
|
|
|
$writer->close(); |
130
|
|
|
|
131
|
|
|
$reader = new FileReader($filePath); |
132
|
|
|
$this->assertEquals($reader->read(), "123456"); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* 正常系 |
137
|
|
|
* ファイルに追記できること |
138
|
|
|
* @test |
139
|
|
|
* @dataProvider writeProvider |
140
|
|
|
*/ |
141
|
|
|
public function okFileWriteCharAppend($filePath) |
142
|
|
|
{ |
143
|
|
|
@unlink($filePath); |
144
|
|
|
$stream = new FileOutputStream($filePath); |
145
|
|
|
$writer = new OutputStreamWriter($stream); |
146
|
|
|
$writer->write("test"); |
147
|
|
|
$writer->flush(); |
148
|
|
|
$writer->close(); |
149
|
|
|
|
150
|
|
|
$stream = new FileOutputStream($filePath, true); |
151
|
|
|
$writer = new OutputStreamWriter($stream); |
152
|
|
|
$writer->write("test"); |
153
|
|
|
$writer->flush(); |
154
|
|
|
$writer->close(); |
155
|
|
|
|
156
|
|
|
$reader = new FileReader($filePath); |
157
|
|
|
$this->assertEquals($reader->read(), "testtest"); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* 正常系 |
162
|
|
|
* コンソールに書き込みができること |
163
|
|
|
* @test |
164
|
|
|
*/ |
165
|
|
|
public function okConsoleWrite() |
166
|
|
|
{ |
167
|
|
|
$stream = new ConsoleOutputStream(); |
168
|
|
|
$writer = new OutputStreamWriter($stream); |
169
|
|
|
$writer->write("test"); |
170
|
|
|
$writer->write("test"); |
171
|
|
|
$writer->flush(); |
172
|
|
|
$writer->close(); |
173
|
|
|
|
174
|
|
|
$this->expectOutputString("testtest"); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* 正常系 |
179
|
|
|
* offset指定でコンソールに書き込めること |
180
|
|
|
* @test |
181
|
|
|
*/ |
182
|
|
|
public function okConsoleWriteOffset() |
183
|
|
|
{ |
184
|
|
|
$stream = new ConsoleOutputStream(); |
185
|
|
|
$writer = new OutputStreamWriter($stream); |
186
|
|
|
$writer->write("123"); |
187
|
|
|
$writer->write("123456", 3); |
188
|
|
|
$writer->flush(); |
189
|
|
|
$writer->close(); |
190
|
|
|
|
191
|
|
|
$this->expectOutputString("123456"); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* 正常系 |
196
|
|
|
* Length指定でコンソールに書き込みができること |
197
|
|
|
* @test |
198
|
|
|
*/ |
199
|
|
|
public function okConsoleWriteLength() |
200
|
|
|
{ |
201
|
|
|
$stream = new ConsoleOutputStream(); |
202
|
|
|
$writer = new OutputStreamWriter($stream); |
203
|
|
|
$writer->write("123"); |
204
|
|
|
$writer->write("123456789", null, 3); |
205
|
|
|
$writer->flush(); |
206
|
|
|
$writer->close(); |
207
|
|
|
|
208
|
|
|
$this->expectOutputString("123123"); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* 正常系 |
213
|
|
|
* Offset,Length指定でコンソールに書き込みができること |
214
|
|
|
* @test |
215
|
|
|
*/ |
216
|
|
|
public function okConsoleWriteOffsetLength() |
217
|
|
|
{ |
218
|
|
|
$stream = new ConsoleOutputStream(); |
219
|
|
|
$writer = new OutputStreamWriter($stream); |
220
|
|
|
$writer->write("123"); |
221
|
|
|
$writer->write("123456789", 3, 3); |
222
|
|
|
$writer->flush(); |
223
|
|
|
$writer->close(); |
224
|
|
|
|
225
|
|
|
$this->expectOutputString("123456"); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* 異常系 |
230
|
|
|
* ファイルオブジェクト、ファイルパス以外を指定した場合、例外が発生すること |
231
|
|
|
* @test |
232
|
|
|
* @expectedException WebStream\Exception\Extend\InvalidArgumentException |
233
|
|
|
*/ |
234
|
|
|
public function ngInvalidFileType() |
235
|
|
|
{ |
236
|
|
|
$stream = new FileOutputStream(1); |
237
|
|
|
$this->assertTrue(false); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* 異常系 |
242
|
|
|
* ファイルがロックされている状態でストリームオブジェクトを作成した場合、例外が発生すること |
243
|
|
|
* @test |
244
|
|
|
* @dataProvider writeProvider |
245
|
|
|
* @expectedException WebStream\Exception\Extend\IOException |
246
|
|
|
*/ |
247
|
|
|
public function ngAlreadyFileLocked($filePath) |
248
|
|
|
{ |
249
|
|
|
@unlink($filePath); |
250
|
|
|
// ファイルを作る |
251
|
|
|
$stream = new FileOutputStream($filePath); |
252
|
|
|
$writer = new OutputStreamWriter($stream); |
253
|
|
|
$writer->write("test"); |
254
|
|
|
$writer->flush(); |
255
|
|
|
$writer->close(); |
256
|
|
|
|
257
|
|
|
// ロックをかける |
258
|
|
|
$resource = fopen($filePath, 'wb'); |
259
|
|
|
flock($resource, LOCK_EX); |
260
|
|
|
|
261
|
|
|
new FileOutputStream($filePath); |
262
|
|
|
$this->assertTrue(false); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* 異常系 |
267
|
|
|
* flush済みの状態でflushすると例外が発生すること |
268
|
|
|
* @test |
269
|
|
|
* @dataProvider writeProvider |
270
|
|
|
* @expectedException WebStream\Exception\Extend\IOException |
271
|
|
|
*/ |
272
|
|
|
public function ngInvalidFlush($filePath) |
273
|
|
|
{ |
274
|
|
|
@unlink($filePath); |
275
|
|
|
$stream = new FileOutputStream($filePath); |
276
|
|
|
$writer = new OutputStreamWriter($stream); |
277
|
|
|
$writer->write("test"); |
278
|
|
|
$writer->flush(); |
279
|
|
|
$writer->close(); |
280
|
|
|
|
281
|
|
|
$writer->flush(); |
282
|
|
|
$this->assertTrue(false); |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|