Completed
Push — master ( c9812e...73d93a )
by Ryuichi
02:42
created

InputStreamReaderTest::okClose()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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 21 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__) . '/../InputStream.php';
5
require_once dirname(__FILE__) . '/../FileInputStream.php';
6
require_once dirname(__FILE__) . '/../StringInputStream.php';
7
require_once dirname(__FILE__) . '/../Reader/InputStreamReader.php';
8
require_once dirname(__FILE__) . '/../Test/Providers/InputStreamReaderProvider.php';
9
require_once dirname(__FILE__) . '/../Test/Modules/IOException.php';
10
require_once dirname(__FILE__) . '/../Test/Modules/InvalidArgumentException.php';
11
12
use WebStream\IO\Reader\InputStreamReader;
13
use WebStream\IO\Test\Providers\InputStreamReaderProvider;
14
15
/**
16
 * InputStreamReaderTest
17
 * @author Ryuichi TANAKA.
18
 * @since 2016/08/18
19
 * @version 0.7
20
 */
21
class InputStreamReaderTest extends \PHPUnit_Framework_TestCase
22
{
23
    use InputStreamReaderProvider;
24
25
    /**
26
     * 正常系
27
     * バイト単位で読み込みできること
28
     * EOFを超えるサイズの読み込み時は、ファイルの場合改行が含まれる
29
     * @test
30
     * @dataProvider readCharProvider
31
     */
32
    public function okReadChar($stream, $result, $byteLength)
33
    {
34
        $reader = new InputStreamReader($stream);
35
        $this->assertEquals($reader->read($byteLength), $result);
36
    }
37
38
    /**
39
     * 正常系
40
     * 行単位でデータが読み込めること
41
     * @test
42
     * @dataProvider readLineProvider
43
     */
44
    public function okReadLine($stream, $result1, $result2)
45
    {
46
        $reader = new InputStreamReader($stream);
47
        $this->assertEquals($reader->readLine(), $result1);
48
        $this->assertEquals($reader->readLine(), $result2);
49
        $this->assertEquals($reader->readLine(), null);
50
    }
51
52
    /**
53
     * 正常系
54
     * 入力ストリームをクローズできること
55
     * @test
56
     * @dataProvider closeProvider
57
     */
58
    public function okClose($stream)
59
    {
60
        $reader = new InputStreamReader($stream);
61
        $reader->close();
62
        $this->assertNull($reader->read());
63
    }
64
65
    /**
66
     * 正常系
67
     * 指定バイト数だけスキップできること
68
     * EOFを超えるサイズの読み込み時は、ファイルの場合改行が含まれる
69
     * @test
70
     * @dataProvider skipProvider
71
     */
72
    public function okSkip($stream, $result, $pos)
73
    {
74
        $reader = new InputStreamReader($stream);
75
        $this->assertEquals($reader->skip($pos), $pos);
76
        $this->assertEquals($reader->read(), $result);
77
    }
78
79
    /**
80
     * 正常系
81
     * 終端を越えたスキップをしたとき
82
     * 1回目のreadは空文字を返し、2回目のreadはnullを返すこと
83
     * @test
84
     * @dataProvider overSkipAndReadProvider
85
     */
86
    public function okOverSkipAndRead($stream, $skipNum)
87
    {
88
        $reader = new InputStreamReader($stream);
89
        $this->assertEquals($reader->skip($skipNum), $skipNum);
90
        $this->assertEmpty($reader->read());
91
        $this->assertNull($reader->read());
92
    }
93
94
    /**
95
     * 正常系
96
     * ポインタを後方に移動できること
97
     * @test
98
     * @dataProvider frontSkipProvider
99
     */
100
    public function okFrontSkip($stream, $skipNum1, $skipNum2, $result)
101
    {
102
        $reader = new InputStreamReader($stream);
103
        $reader->skip($skipNum1);
0 ignored issues
show
Unused Code introduced by
The call to the method WebStream\IO\Reader\InputStreamReader::skip() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
104
        $reader->skip($skipNum2);
0 ignored issues
show
Unused Code introduced by
The call to the method WebStream\IO\Reader\InputStreamReader::skip() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
105
        $this->assertEquals($reader->read(), $result);
106
    }
107
108
    /**
109
     * 正常系
110
     * ポインタ位置が負になった場合、移動量は常に-1になること
111
     * @test
112
     * @dataProvider overFrontSkipProvider
113
     */
114
    public function okOverFrontSkip($stream, $pos)
115
    {
116
        $reader = new InputStreamReader($stream);
117
        $this->assertEquals($reader->skip($pos), -1);
118
    }
119
120
    /**
121
     * 正常系
122
     * リセットすると初期位置にポインタが移動すること
123
     * @test
124
     * @dataProvider resetProvider
125
     */
126
    public function okReset($stream, $skipNum, $result)
127
    {
128
        $reader = new InputStreamReader($stream);
129
        $reader->skip($skipNum);
0 ignored issues
show
Unused Code introduced by
The call to the method WebStream\IO\Reader\InputStreamReader::skip() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
130
        $reader->reset();
131
        $this->assertEquals($reader->read(), $result);
132
    }
133
134
    /**
135
     * 正常系
136
     * リセットするとマーク位置にポインタが移動すること
137
     * @test
138
     * @dataProvider markAndResetProvider
139
     */
140
    public function okMarkAndReset($stream, $skipNum, $result)
141
    {
142
        $reader = new InputStreamReader($stream);
143
        $reader->skip($skipNum);
0 ignored issues
show
Unused Code introduced by
The call to the method WebStream\IO\Reader\InputStreamReader::skip() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
144
        $reader->mark();
145
        $reader->reset();
146
        $this->assertEquals($reader->read(), $result);
147
    }
148
149
    /**
150
     * 異常系
151
     * 読み込みサイズに不正値を渡した時、例外が発生すること
152
     * @test
153
     * @dataProvider invalidLengthProvider
154
     * @expectedException WebStream\Exception\Extend\InvalidArgumentException
155
     */
156
    public function ngInvalidLength($stream)
157
    {
158
        $reader = new InputStreamReader($stream);
159
        $reader->read("dummy");
160
        $this->assertTrue(false);
161
    }
162
}
163