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

MultiSpellcheckerTest::testGetSupportedLanguages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpSpellcheck\Tests\Spellchecker;
6
7
use PhpSpellcheck\Misspelling;
8
use PhpSpellcheck\Spellchecker\MultiSpellchecker;
9
use PhpSpellcheck\Spellchecker\SpellcheckerInterface;
10
use PhpSpellcheck\Utils\TextEncoding;
11
use PHPUnit\Framework\TestCase;
12
13
class MultiSpellcheckerTest extends TestCase
14
{
15
    public function testCheckAndMergeSuggestions(): void
16
    {
17
        $spellChecker1 = $this->createMock(SpellcheckerInterface::class);
18
        $spellChecker2 = $this->createMock(SpellcheckerInterface::class);
19
20
        $misspelling1 = new Misspelling('mispelling1', 1);
21
        $misspelling2a = new Misspelling('mispelling2', 2, 2, ['suggestionA']);
22
        $misspelling2b = new Misspelling('mispelling2', 2, 2, ['suggestionB']);
23
        $misspelling3 = new Misspelling('mispelling3', 3, 3);
24
25
        $spellChecker1->method('check')
0 ignored issues
show
Bug introduced by
The method method() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

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

25
        $spellChecker1->/** @scrutinizer ignore-call */ 
26
                        method('check')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
26
            ->willReturn([$misspelling1, $misspelling2a]);
27
        $spellChecker2->method('check')
28
            ->willReturn([$misspelling2b, $misspelling3]);
29
        $spellChecker1->method('getSupportedLanguages')
30
            ->willReturn(['en_US']);
31
        $spellChecker2->method('getSupportedLanguages')
32
            ->willReturn(['en_US']);
33
34
        $multiSpellchecker = new MultiSpellchecker([$spellChecker1, $spellChecker2]);
35
36
        $misspellings = $multiSpellchecker->check('test', ['en_US'], ['ctx'], TextEncoding::UTF8);
37
        $this->assertEquals(
38
            [
39
                $misspelling1->setContext(['ctx']),
40
                new Misspelling('mispelling2', 2, 2, ['suggestionA', 'suggestionB'], ['ctx']),
41
                $misspelling3->setContext(['ctx']),
42
            ],
43
            $misspellings
44
        );
45
    }
46
47
    public function testCheckAndNotMergeSuggestions(): void
48
    {
49
        $spellChecker1 = $this->createMock(SpellcheckerInterface::class);
50
        $spellChecker2 = $this->createMock(SpellcheckerInterface::class);
51
52
        $misspelling1 = new Misspelling('mispelling1', 1);
53
        $misspelling2a = new Misspelling('mispelling2', 2, 2, ['suggestionA']);
54
        $misspelling2b = new Misspelling('mispelling2', 2, 2, ['suggestionB']);
55
        $misspelling3 = new Misspelling('mispelling3', 3, 3);
56
57
        $spellChecker1->method('check')
58
            ->willReturn([$misspelling1, $misspelling2a]);
59
        $spellChecker2->method('check')
60
            ->willReturn([$misspelling2b, $misspelling3]);
61
        $spellChecker1->method('getSupportedLanguages')
62
            ->willReturn(['en_US']);
63
        $spellChecker2->method('getSupportedLanguages')
64
            ->willReturn(['en_US']);
65
66
        $multiSpellchecker = new MultiSpellchecker([$spellChecker1, $spellChecker2], false);
67
68
        $misspellings = $multiSpellchecker->check('test', ['en_US'], ['ctx'], TextEncoding::UTF8);
69
        $this->assertEquals(
70
            [
71
                $misspelling1->setContext(['ctx']),
72
                $misspelling2a->setContext(['ctx']),
73
                $misspelling2b->setContext(['ctx']),
74
                $misspelling3->setContext(['ctx']),
75
            ],
76
            iterator_to_array($misspellings)
0 ignored issues
show
Bug introduced by
$misspellings of type array is incompatible with the type Traversable expected by parameter $iterator of iterator_to_array(). ( Ignorable by Annotation )

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

76
            iterator_to_array(/** @scrutinizer ignore-type */ $misspellings)
Loading history...
77
        );
78
    }
79
80
    public function testGetSupportedLanguages(): void
81
    {
82
        $spellChecker1 = $this->createMock(SpellcheckerInterface::class);
83
        $spellChecker1->method('getSupportedLanguages')
84
            ->willReturn(['en', 'fr']);
85
        $spellChecker2 = $this->createMock(SpellcheckerInterface::class);
86
        $spellChecker2->method('getSupportedLanguages')
87
            ->willReturn(['fr', 'ru']);
88
89
        $multipleSpellchecker = new MultiSpellchecker([$spellChecker1, $spellChecker2]);
90
91
        $this->assertSame(['en', 'fr', 'ru'], $multipleSpellchecker->getSupportedLanguages());
92
    }
93
}
94