Passed
Push — master ( b2b167...7e189a )
by Philippe
01:56
created

MisspellingFinderTest::testFindFromSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 19
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
use PhpSpellcheck\Exception\InvalidArgumentException;
6
use PhpSpellcheck\MisspellingFinder;
7
use PhpSpellcheck\MisspellingHandler\MisspellingHandlerInterface;
8
use PhpSpellcheck\MisspellingInterface;
9
use PhpSpellcheck\Source\SourceInterface;
10
use PhpSpellcheck\Spellchecker\SpellcheckerInterface;
11
use PhpSpellcheck\TextInterface;
12
use PhpSpellcheck\TextProcessor\TextProcessorInterface;
13
use PHPUnit\Framework\MockObject\MockObject;
14
use PHPUnit\Framework\TestCase;
15
16
class MisspellingFinderTest extends TestCase
17
{
18
    /** @var MisspellingHandlerInterface|MockObject */
19
    private $misspellingHandler;
20
21
    /** @var MockObject|TextProcessorInterface */
22
    private $textProcessor;
23
24
    /** @var MockObject|SpellcheckerInterface */
25
    private $spellChecker;
26
27
    public function setUp(): void
28
    {
29
        $this->spellChecker = $this->createMock(SpellcheckerInterface::class);
30
        $this->misspellingHandler = $this->createMock(MisspellingHandlerInterface::class);
31
        $this->textProcessor = $this->createMock(TextProcessorInterface::class);
32
    }
33
34
    public function testFindFromString(): void
35
    {
36
        $misspellingFinder = new MisspellingFinder(
37
            $this->spellChecker
38
        );
39
        $misspelling1 = $this->generateMisspellingMock();
40
41
        $this->spellChecker
42
            ->expects($this->once())
43
            ->method('check')
44
            ->willReturn([$misspelling1]);
45
46
        $this->assertSame([$misspelling1], iterator_to_array($misspellingFinder->find('mispell')));
47
    }
48
49
    public function testFindFromInvalidArgument(): void
50
    {
51
        $this->expectException(InvalidArgumentException::class);
52
53
        $misspellingFinder = new MisspellingFinder(
54
            $this->spellChecker,
55
            $this->misspellingHandler,
56
            $this->textProcessor
57
        );
58
59
        $misspellingFinder->find(3);
60
    }
61
62
    public function testFindWithMisspelingHandler(): void
63
    {
64
        $this->misspellingHandler->expects($this->once())
65
            ->method('handle');
66
67
        $misspellingFinder = new MisspellingFinder(
68
            $this->spellChecker,
69
            $this->misspellingHandler
70
        );
71
        $misspelling1 = $this->generateMisspellingMock();
72
73
        $this->spellChecker
74
            ->expects($this->once())
75
            ->method('check')
76
            ->willReturn([$misspelling1]);
77
78
        $this->assertSame([$misspelling1], iterator_to_array($misspellingFinder->find('mispell')));
79
    }
80
81
    public function testFindWithTextProcessor(): void
82
    {
83
        $this->textProcessor->expects($this->once())
84
            ->method('process');
85
86
        $misspellingFinder = new MisspellingFinder(
87
            $this->spellChecker,
88
            null,
89
            $this->textProcessor
90
        );
91
        $misspelling1 = $this->generateMisspellingMock();
92
93
        $this->spellChecker
94
            ->expects($this->once())
95
            ->method('check')
96
            ->willReturn([$misspelling1]);
97
98
        $this->assertSame([$misspelling1], iterator_to_array($misspellingFinder->find('mispell')));
99
    }
100
101
    public function testFindFromSource(): void
102
    {
103
        $misspelling1 = $this->generateMisspellingMock();
104
        $this->spellChecker
105
            ->expects($this->once())
106
            ->method('check')
107
            ->willReturn([$misspelling1]);
108
109
        $source = $this->createMock(SourceInterface::class);
110
        $source->method('toTexts')
111
            ->willReturn([$text = $this->generateTextMock()]);
112
113
        $misspellingFinder = new MisspellingFinder(
114
            $this->spellChecker,
115
            null,
116
            $this->textProcessor
117
        );
118
119
        $this->assertSame([$misspelling1], iterator_to_array($misspellingFinder->find('mispell')));
120
    }
121
122
    private function generateMisspellingMock(): MockObject
123
    {
124
        $mispelling = $this->createMock(MisspellingInterface::class);
125
        $mispelling->method('getWord')
126
            ->willReturn('mispelled');
127
        $mispelling->method('getContext')
128
            ->willReturn([]);
129
        $mispelling->method('getLineNumber')
130
            ->willReturn(1);
131
        $mispelling->method('getOffset')
132
            ->willReturn(1);
133
        $mispelling->method('getSuggestions')
134
            ->willReturn(['misspelled', 'misspelling']);
135
136
        return $mispelling;
137
    }
138
139
    private function generateTextMock(): MockObject
140
    {
141
        $text = $this->createMock(TextInterface::class);
142
        $text->method('getContent')
143
            ->willReturn('mispell');
144
        $text->method('getContext')
145
            ->willReturn([]);
146
        $text->method('getEncoding')
147
            ->willReturn('utf-8');
148
149
        return $text;
150
    }
151
}
152