Passed
Push — master ( a93e2d...567501 )
by Philippe
03:03
created

testThrowExceptionIfOffsetGivenIsHigherThanStringLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpSpellcheck\Tests\Utils;
6
7
use PhpSpellcheck\Exception\InvalidArgumentException;
8
use PhpSpellcheck\Utils\LineAndOffset;
9
use PHPUnit\Framework\TestCase;
10
11
class LineAndOffsetTest extends TestCase
12
{
13
    public function testFindFromFirstCharacterOffset(): void
14
    {
15
        $text = <<<TEXT
16
First line
17
Second line
18
Third line
19
Last line
20
TEXT;
21
22
        // offset 0  = before first character of 1st line
23
        $this->assertSame(
24
            [1, 0],
25
            LineAndOffset::findFromFirstCharacterOffset($text, 0)
26
        );
27
28
        // offset 10  = after last character of 1st line
29
        $this->assertSame(
30
            [1, 10],
31
            LineAndOffset::findFromFirstCharacterOffset($text, 10)
32
        );
33
34
        // offset 11 = before first character of 2nd line
35
        $this->assertSame(
36
            [2, 0],
37
            LineAndOffset::findFromFirstCharacterOffset($text, 11)
38
        );
39
        // offset 23 = before first character of 3rd line
40
        $this->assertSame(
41
            [3, 0],
42
            LineAndOffset::findFromFirstCharacterOffset($text, 23)
43
        );
44
        // offset 24 = after first character of 3rd line
45
        $this->assertSame(
46
            [3, 1],
47
            LineAndOffset::findFromFirstCharacterOffset($text, 24)
48
        );
49
50
        // offset 34 = after first character of 4th line
51
        $this->assertSame(
52
            [4, 0],
53
            LineAndOffset::findFromFirstCharacterOffset($text, 34)
54
        );
55
56
        // offset 34 = after last character of 4th line
57
        $this->assertSame(
58
            [4, 9],
59
            LineAndOffset::findFromFirstCharacterOffset($text, 43)
60
        );
61
    }
62
63
    public function testThrowExceptionIfOffsetGivenIsHigherThanStringLength(): void
64
    {
65
        $this->expectException(InvalidArgumentException::class);
66
        $this->assertSame(
67
            [4, 0],
68
            LineAndOffset::findFromFirstCharacterOffset('test', 50)
69
        );
70
    }
71
72
    public function testThrowExceptionIfOffsetisNegative(): void
73
    {
74
        $this->expectException(\InvalidArgumentException::class);
75
        $this->assertSame(
76
            [4, 0],
77
            LineAndOffset::findFromFirstCharacterOffset('test', -50)
78
        );
79
    }
80
}
81