Failed Conditions
Push — master ( 01d6ae...9ca6d9 )
by Philippe
534:14 queued 469:10
created

testFindLowerOrHigher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 64
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 45
nc 1
nop 0
dl 0
loc 64
rs 9.2
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpSpellcheck\Tests\Utils;
6
7
use PhpSpellcheck\Utils\SortedNumericArrayNearestValueFinder;
8
use PHPUnit\Framework\TestCase;
9
10
class SortedNumericArrayNearestValueFinderTest extends TestCase
11
{
12
13
    public function testFindLowerOrHigher()
14
    {
15
        $foundNearestLowerIndex = SortedNumericArrayNearestValueFinder::findIndex(
16
            1337,
17
            [1000, 2000, 3000],
18
            SortedNumericArrayNearestValueFinder::FIND_LOWER
19
        );
20
        $this->assertSame(0, $foundNearestLowerIndex);
21
22
        $foundNearestLowerIndex = SortedNumericArrayNearestValueFinder::findIndex(
23
            1337,
24
            [1000, 2000, 3000],
25
            SortedNumericArrayNearestValueFinder::FIND_HIGHER
26
        );
27
        $this->assertSame(1, $foundNearestLowerIndex);
28
29
        $foundNearestLowerIndex = SortedNumericArrayNearestValueFinder::findIndex(
30
            2000,
31
            [1000, 2000, 3000],
32
            SortedNumericArrayNearestValueFinder::FIND_LOWER
33
        );
34
        $this->assertSame(1, $foundNearestLowerIndex);
35
36
        $foundNearestLowerIndex = SortedNumericArrayNearestValueFinder::findIndex(
37
            1500,
38
            [1000, 2000, 3000],
39
            SortedNumericArrayNearestValueFinder::FIND_DEFAULT
40
        );
41
        $this->assertSame(1, $foundNearestLowerIndex);
42
43
        $foundNearestLowerIndex = SortedNumericArrayNearestValueFinder::findIndex(
44
            1337,
45
            [1000, 2000, 3000],
46
            SortedNumericArrayNearestValueFinder::FIND_DEFAULT
47
        );
48
        $this->assertSame(1, $foundNearestLowerIndex);
49
50
        $foundNearestLowerIndex = SortedNumericArrayNearestValueFinder::findIndex(
51
            4000,
52
            [1000, 2000, 3000],
53
            SortedNumericArrayNearestValueFinder::FIND_DEFAULT
54
        );
55
        $this->assertSame(2, $foundNearestLowerIndex);
56
57
        $foundNearestLowerIndex = SortedNumericArrayNearestValueFinder::findIndex(
58
            4000,
59
            [1000, 2000, 3000],
60
            SortedNumericArrayNearestValueFinder::FIND_HIGHER
61
        );
62
        $this->assertSame(2, $foundNearestLowerIndex);
63
64
        $foundNearestLowerIndex = SortedNumericArrayNearestValueFinder::findIndex(
65
            50,
66
            [1000, 2000, 3000],
67
            SortedNumericArrayNearestValueFinder::FIND_DEFAULT
68
        );
69
        $this->assertSame(0, $foundNearestLowerIndex);
70
71
        $foundNearestLowerIndex = SortedNumericArrayNearestValueFinder::findIndex(
72
            50,
73
            [1000, 2000, 3000],
74
            SortedNumericArrayNearestValueFinder::FIND_LOWER
75
        );
76
        $this->assertSame(0, $foundNearestLowerIndex);
77
    }
78
79
    public function testFindEmptyArray()
80
    {
81
        $this->expectException(\InvalidArgumentException::class);
82
83
        SortedNumericArrayNearestValueFinder::findIndex(
84
            1500,
85
            [],
86
            SortedNumericArrayNearestValueFinder::FIND_DEFAULT
87
        );
88
    }
89
90
    public function testFindInNonIntArray()
91
    {
92
        $this->expectException(\InvalidArgumentException::class);
93
94
        SortedNumericArrayNearestValueFinder::findIndex(
95
            1500,
96
            ['foo'],
97
            SortedNumericArrayNearestValueFinder::FIND_DEFAULT
98
        );
99
    }
100
101
    public function testFindInMixedTypedArray()
102
    {
103
        $this->expectException(\InvalidArgumentException::class);
104
105
        SortedNumericArrayNearestValueFinder::findIndex(
106
            1500,
107
            ['foo', 1, 'bar'],
108
            SortedNumericArrayNearestValueFinder::FIND_DEFAULT
109
        );
110
    }
111
}
112