Completed
Pull Request — master (#75)
by
unknown
01:26
created

IntsTest::filterNonStringOrInt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace TraderInteractive\Filter;
4
5
use PHPUnit\Framework\TestCase;
6
7
/**
8
 * @coversDefaultClass \TraderInteractive\Filter\Ints
9
 */
10
final class IntsTest extends TestCase
11
{
12
    /**
13
     * @test
14
     * @covers ::filter
15
     */
16
    public function filterAllowNullIsTrueAndNullValue()
17
    {
18
        $result = Ints::filter(null, true);
19
        $this->assertSame(null, $result);
20
    }
21
22
    /**
23
     * @test
24
     * @covers ::filter
25
     */
26
    public function filterPositiveInt()
27
    {
28
        $this->assertSame(123, Ints::filter(123));
29
    }
30
31
    /**
32
     * @test
33
     * @covers ::filter
34
     */
35
    public function filterNegativeInt()
36
    {
37
        $this->assertSame(-123, Ints::filter(-123));
38
    }
39
40
    /**
41
     * @test
42
     * @covers ::filter
43
     */
44
    public function filterZeroInt()
45
    {
46
        $positiveZero = + 0;
47
        $this->assertSame(0, Ints::filter($positiveZero));
48
        $this->assertSame(0, Ints::filter(-0));
49
    }
50
51
    /**
52
     * @test
53
     * @covers ::filter
54
     */
55
    public function filterPositiveString()
56
    {
57
        $this->assertSame(123, Ints::filter('   123 '));
58
        $this->assertSame(123, Ints::filter('   +123 '));
59
        $this->assertSame(0, Ints::filter('   +0 '));
60
    }
61
62
    /**
63
     * @test
64
     * @covers ::filter
65
     */
66
    public function filterNegativeString()
67
    {
68
        $this->assertSame(-123, Ints::filter('   -123 '));
69
        $this->assertSame(0, Ints::filter('   -0 '));
70
    }
71
72
    /**
73
     * @test
74
     * @covers ::filter
75
     * @expectedException \TraderInteractive\Filter\Exception
76
     * @expectedExceptionMessage "true" $value is not a string
77
     */
78
    public function filterNonStringOrInt()
79
    {
80
        Ints::filter(true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string|integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
81
    }
82
83
    /**
84
     * @test
85
     * @covers ::filter
86
     * @expectedException \TraderInteractive\Filter\Exception
87
     * @expectedExceptionMessage $value string length is zero
88
     */
89
    public function filterEmptyString()
90
    {
91
        Ints::filter('');
92
    }
93
94
    /**
95
     * @test
96
     * @covers ::filter
97
     * @expectedException \TraderInteractive\Filter\Exception
98
     * @expectedExceptionMessage $value string length is zero
99
     */
100
    public function filterWhitespaceString()
101
    {
102
        Ints::filter('   ');
103
    }
104
105
    /**
106
     * @test
107
     * @covers ::filter
108
     */
109
    public function nonDigitString()
110
    {
111
        try {
112
            Ints::filter('123.4');
113
            $this->fail("No exception thrown");
114
        } catch (Exception $e) {
115
            $this->assertSame(
116
                "123.4 does not contain all digits, optionally prepended by a '+' or '-' and optionally surrounded by "
117
                . "whitespace",
118
                $e->getMessage()
119
            );
120
        }
121
    }
122
123
    /**
124
     * @test
125
     * @covers ::filter
126
     * @expectedException \TraderInteractive\Filter\Exception
127
     */
128
    public function filterGreaterThanPhpIntMax()
129
    {
130
        //32, 64 and 128 bit and their +1 's
131
        $maxes = [
132
            '2147483647' => '2147483648',
133
            '9223372036854775807' => '9223372036854775808',
134
            '170141183460469231731687303715884105727' => '170141183460469231731687303715884105728',
135
        ];
136
        $oneOverMax = $maxes[(string)PHP_INT_MAX];
137
        Ints::filter($oneOverMax);
138
    }
139
140
    /**
141
     * @test
142
     * @covers ::filter
143
     * @expectedException \TraderInteractive\Filter\Exception
144
     */
145
    public function filterLessThanPhpIntMin()
146
    {
147
        //32, 64 and 128 bit and their -1 's
148
        $mins = [
149
            '-2147483648' => '-2147483649',
150
            '-9223372036854775808' => '-9223372036854775809',
151
            '-170141183460469231731687303715884105728' => '-170141183460469231731687303715884105729',
152
        ];
153
        $oneUnderMin = $mins[(string)~PHP_INT_MAX];
154
        Ints::filter($oneUnderMin);
155
    }
156
157
    /**
158
     * @test
159
     * @covers ::filter
160
     * @expectedException \TraderInteractive\Filter\Exception
161
     * @expectedExceptionMessage -1 is less than 0
162
     */
163
    public function filterLessThanMin()
164
    {
165
        Ints::filter(-1, false, 0);
166
    }
167
168
    /**
169
     * @test
170
     * @covers ::filter
171
     */
172
    public function filterEqualToMin()
173
    {
174
        $this->assertSame(0, Ints::filter(0, false, 0));
175
    }
176
177
    /**
178
     * @test
179
     * @covers ::filter
180
     * @expectedException \TraderInteractive\Filter\Exception
181
     * @expectedExceptionMessage 1 is greater than 0
182
     */
183
    public function filterGreaterThanMax()
184
    {
185
        Ints::filter(1, false, null, 0);
186
    }
187
188
    /**
189
     * @test
190
     * @covers ::filter
191
     */
192
    public function filterEqualToMax()
193
    {
194
        $this->assertSame(0, Ints::filter(0, false, null, 0));
195
    }
196
}
197