Completed
Push — master ( a013c3...06a8f0 )
by Ryuichi
02:24
created

OutputStreamWriterTest::dummy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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
    /**
35
     * 正常系
36
     * ファイルに書き込みができること
37
     * @test
38
     * @dataProvider writeProvider
39
     */
40 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...
41
    {
42
        @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...
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 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...
62
    {
63
        @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...
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 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...
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 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...
102
    {
103
        @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...
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 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...
122
    {
123
        @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...
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);
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...
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 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...
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 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...
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 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...
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 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...
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);
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...
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);
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...
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
     * @test
267
     */
268
    public function dummy()
269
    {
270
        $this->assertTrue(true);
271
    }
272
273
    /**
274
     * 異常系
275
     * flush済みの状態でflushすると例外が発生すること
276
     * @test
277
     * @dataProvider writeProvider
278
     * @expectedException WebStream\Exception\Extend\IOException
279
     */
280
    public function ngInvalidFlush($filePath)
281
    {
282
        @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...
283
        $stream = new FileOutputStream($filePath);
284
        $writer = new OutputStreamWriter($stream);
285
        $writer->write("test");
286
        $writer->flush();
287
        $writer->close();
288
289
        $writer->flush();
290
        $this->assertTrue(false);
291
    }
292
}
293