Failed Conditions
Pull Request — master (#1)
by
unknown
01:55
created

IntsTest::nonDigitString()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

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