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

MultiSpellcheckerTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 52
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 3
1
<?php
2
3
declare(strict_types=1);
4
5
use PhpSpellcheck\Misspelling;
6
use PhpSpellcheck\Spellchecker\MultiSpellchecker;
7
use PhpSpellcheck\Spellchecker\SpellcheckerInterface;
8
use PhpSpellcheck\Utils\TextEncoding;
9
use PHPUnit\Framework\TestCase;
10
11
class MultiSpellcheckerTest extends TestCase
12
{
13
    public function testCheckAndMergeSuggestions(): void
14
    {
15
        $spellChecker1 = $this->createMock(SpellcheckerInterface::class);
16
        $spellChecker2 = $this->createMock(SpellcheckerInterface::class);
17
18
        $misspelling1 = new Misspelling('mispelling1', 1);
19
        $misspelling2a = new Misspelling('mispelling2', 2, 2, ['suggestionA']);
20
        $misspelling2b = new Misspelling('mispelling2', 2, 2, ['suggestionB']);
21
        $misspelling3 = new Misspelling('mispelling3', 3, 3);
22
23
        $spellChecker1->method('check')
24
            ->willReturn([$misspelling1, $misspelling2a]);
25
        $spellChecker2->method('check')
26
            ->willReturn([$misspelling2b, $misspelling3]);
27
        $spellChecker1->method('getSupportedLanguages')
28
            ->willReturn(['en_US']);
29
        $spellChecker2->method('getSupportedLanguages')
30
            ->willReturn(['en_US']);
31
32
        $multiSpellchecker = new MultiSpellchecker([$spellChecker1, $spellChecker2]);
33
34
        $misspellings = $multiSpellchecker->check('test', ['en_US'], ['ctx'], TextEncoding::UTF8);
35
        $this->assertEquals(
36
            [
37
                $misspelling1->setContext(['ctx']),
38
                new Misspelling('mispelling2', 2, 2, ['suggestionA', 'suggestionB'], ['ctx']),
39
                $misspelling3->setContext(['ctx']),
40
            ],
41
            $misspellings
42
        );
43
    }
44
45
    public function testCheckAndNotMergeSuggestions(): void
46
    {
47
        $spellChecker1 = $this->createMock(SpellcheckerInterface::class);
48
        $spellChecker2 = $this->createMock(SpellcheckerInterface::class);
49
50
        $misspelling1 = new Misspelling('mispelling1', 1);
51
        $misspelling2a = new Misspelling('mispelling2', 2, 2, ['suggestionA']);
52
        $misspelling2b = new Misspelling('mispelling2', 2, 2, ['suggestionB']);
53
        $misspelling3 = new Misspelling('mispelling3', 3, 3);
54
55
        $spellChecker1->method('check')
56
            ->willReturn([$misspelling1, $misspelling2a]);
57
        $spellChecker2->method('check')
58
            ->willReturn([$misspelling2b, $misspelling3]);
59
        $spellChecker1->method('getSupportedLanguages')
60
            ->willReturn(['en_US']);
61
        $spellChecker2->method('getSupportedLanguages')
62
            ->willReturn(['en_US']);
63
64
        $multiSpellchecker = new MultiSpellchecker([$spellChecker1, $spellChecker2], false);
65
66
        $misspellings = $multiSpellchecker->check('test', ['en_US'], ['ctx'], TextEncoding::UTF8);
67
        $this->assertEquals(
68
            [
69
                $misspelling1->setContext(['ctx']),
70
                $misspelling2a->setContext(['ctx']),
71
                $misspelling2b->setContext(['ctx']),
72
                $misspelling3->setContext(['ctx']),
73
            ],
74
            iterator_to_array($misspellings)
75
        );
76
    }
77
78
    public function testGetSupportedLanguages(): void
79
    {
80
        $spellChecker1 = $this->createMock(SpellcheckerInterface::class);
81
        $spellChecker1->method('getSupportedLanguages')
82
            ->willReturn(['en', 'fr']);
83
        $spellChecker2 = $this->createMock(SpellcheckerInterface::class);
84
        $spellChecker2->method('getSupportedLanguages')
85
            ->willReturn(['fr', 'ru']);
86
87
        $multipleSpellchecker = new MultiSpellchecker([$spellChecker1, $spellChecker2]);
88
89
        $this->assertSame(['en', 'fr', 'ru'], $multipleSpellchecker->getSupportedLanguages());
90
    }
91
}
92