Completed
Push — master ( 12ab54...d03da3 )
by Ryuichi
02:26
created

okConsoleWriteOffsetLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 30 and the first side effect is on line 4.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
    public function setUp()
35
    {
36
    }
37
38
    /**
39
     * 正常系
40
     * ファイルに書き込みができること
41
     * @test
42
     * @dataProvider writeProvider
43
     */
44 View Code Duplication
    public function okFileWriteFromFilePath($filePath)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        @unlink($filePath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
47
        $stream = new FileOutputStream($filePath);
48
        $writer = new OutputStreamWriter($stream);
49
        $writer->write("test");
50
        $writer->write("test");
51
        $writer->flush();
52
        $writer->close();
53
54
        $reader = new FileReader($filePath);
55
        $this->assertEquals($reader->read(), "testtest");
56
    }
57
58
    /**
59
     * 正常系
60
     * ファイルに書き込みができること
61
     * ファイルオブジェクトを指定
62
     * @test
63
     * @dataProvider writeProvider
64
     */
65 View Code Duplication
    public function okFileWriteFromFileObject($filePath)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    {
67
        @unlink($filePath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
68
        $stream = new FileOutputStream(new File($filePath));
69
        $writer = new OutputStreamWriter($stream);
70
        $writer->write("test");
71
        $writer->write("test");
72
        $writer->flush();
73
        $writer->close();
74
75
        $reader = new FileReader($filePath);
76
        $this->assertEquals($reader->read(), "testtest");
77
    }
78
79
    /**
80
     * 正常系
81
     * Offset指定でファイルに書き込みができること
82
     * @test
83
     * @dataProvider writeProvider
84
     */
85 View Code Duplication
    public function okFileWriteOffset($filePath)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
    {
87
        unlink($filePath);
88
        $stream = new FileOutputStream($filePath);
89
        $writer = new OutputStreamWriter($stream);
90
        $writer->write("123");
91
        $writer->write("123456", 3);
92
        $writer->flush();
93
        $writer->close();
94
95
        $reader = new FileReader($filePath);
96
        $this->assertEquals($reader->read(), "123456");
97
    }
98
99
    /**
100
     * 正常系
101
     * Length指定でファイルに書き込みができること
102
     * @test
103
     * @dataProvider writeProvider
104
     */
105 View Code Duplication
    public function okFileWriteLength($filePath)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
    {
107
        @unlink($filePath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
108
        $stream = new FileOutputStream($filePath);
109
        $writer = new OutputStreamWriter($stream);
110
        $writer->write("123");
111
        $writer->write("123456789", null, 3);
112
        $writer->flush();
113
        $writer->close();
114
115
        $reader = new FileReader($filePath);
116
        $this->assertEquals($reader->read(), "123123");
117
    }
118
119
    /**
120
     * 正常系
121
     * Offset,Length指定でファイルに書き込みができること
122
     * @test
123
     * @dataProvider writeProvider
124
     */
125 View Code Duplication
    public function okFileWriteOffsetLength($filePath)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127
        @unlink($filePath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
128
        $stream = new FileOutputStream($filePath);
129
        $writer = new OutputStreamWriter($stream);
130
        $writer->write("123");
131
        $writer->write("123456789", 3, 3);
132
        $writer->flush();
133
        $writer->close();
134
135
        $reader = new FileReader($filePath);
136
        $this->assertEquals($reader->read(), "123456");
137
    }
138
139
    /**
140
     * 正常系
141
     * ファイルに追記できること
142
     * @test
143
     * @dataProvider writeProvider
144
     */
145
    public function okFileWriteCharAppend($filePath)
146
    {
147
        @unlink($filePath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
148
        $stream = new FileOutputStream($filePath);
149
        $writer = new OutputStreamWriter($stream);
150
        $writer->write("test");
151
        $writer->flush();
152
        $writer->close();
153
154
        $stream = new FileOutputStream($filePath, true);
155
        $writer = new OutputStreamWriter($stream);
156
        $writer->write("test");
157
        $writer->flush();
158
        $writer->close();
159
160
        $reader = new FileReader($filePath);
161
        $this->assertEquals($reader->read(), "testtest");
162
    }
163
164
    /**
165
     * 正常系
166
     * コンソールに書き込みができること
167
     * @test
168
     */
169 View Code Duplication
    public function okConsoleWrite()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170
    {
171
        $stream = new ConsoleOutputStream();
172
        $writer = new OutputStreamWriter($stream);
173
        $writer->write("test");
174
        $writer->write("test");
175
        $writer->flush();
176
        $writer->close();
177
178
        $this->expectOutputString("testtest");
179
    }
180
181
    /**
182
     * 正常系
183
     * offset指定でコンソールに書き込めること
184
     * @test
185
     */
186 View Code Duplication
    public function okConsoleWriteOffset()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
    {
188
        $stream = new ConsoleOutputStream();
189
        $writer = new OutputStreamWriter($stream);
190
        $writer->write("123");
191
        $writer->write("123456", 3);
192
        $writer->flush();
193
        $writer->close();
194
195
        $this->expectOutputString("123456");
196
    }
197
198
    /**
199
     * 正常系
200
     * Length指定でコンソールに書き込みができること
201
     * @test
202
     */
203 View Code Duplication
    public function okConsoleWriteLength()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
204
    {
205
        $stream = new ConsoleOutputStream();
206
        $writer = new OutputStreamWriter($stream);
207
        $writer->write("123");
208
        $writer->write("123456789", null, 3);
209
        $writer->flush();
210
        $writer->close();
211
212
        $this->expectOutputString("123123");
213
    }
214
215
    /**
216
     * 正常系
217
     * Offset,Length指定でコンソールに書き込みができること
218
     * @test
219
     */
220 View Code Duplication
    public function okConsoleWriteOffsetLength()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
221
    {
222
        $stream = new ConsoleOutputStream();
223
        $writer = new OutputStreamWriter($stream);
224
        $writer->write("123");
225
        $writer->write("123456789", 3, 3);
226
        $writer->flush();
227
        $writer->close();
228
229
        $this->expectOutputString("123456");
230
    }
231
232
    /**
233
     * 異常系
234
     * ファイルオブジェクト、ファイルパス以外を指定した場合、例外が発生すること
235
     * @test
236
     * @expectedException WebStream\Exception\Extend\InvalidArgumentException
237
     */
238
    public function ngInvalidFileType()
239
    {
240
        $stream = new FileOutputStream(1);
0 ignored issues
show
Unused Code introduced by
$stream is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
241
        $this->assertTrue(false);
242
    }
243
244
    /**
245
     * 異常系
246
     * ファイルがロックされている状態でストリームオブジェクトを作成した場合、例外が発生すること
247
     * @test
248
     * @dataProvider writeProvider
249
     * @expectedException WebStream\Exception\Extend\IOException
250
     */
251
    public function ngAlreadyFileLocked($filePath)
252
    {
253
        @unlink($filePath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
254
        // ファイルを作る
255
        $stream = new FileOutputStream($filePath);
256
        $writer = new OutputStreamWriter($stream);
257
        $writer->write("test");
258
        $writer->flush();
259
        $writer->close();
260
261
        // ロックをかける
262
        $resource = fopen($filePath, 'wb');
263
        flock($resource, LOCK_EX);
264
265
        new FileOutputStream($filePath);
266
        $this->assertTrue(false);
267
    }
268
269
    /**
270
     * 異常系
271
     * flush済みの状態でflushすると例外が発生すること
272
     * @test
273
     * @dataProvider writeProvider
274
     * @expectedException WebStream\Exception\Extend\IOException
275
     */
276
    public function ngInvalidFlush($filePath)
277
    {
278
        @unlink($filePath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
279
        $stream = new FileOutputStream($filePath);
280
        $writer = new OutputStreamWriter($stream);
281
        $writer->write("test");
282
        $writer->flush();
283
        $writer->close();
284
285
        $writer->flush();
286
        $this->assertTrue(false);
287
    }
288
}
289