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

MisspellingFinderTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 77
dl 0
loc 129
rs 10
c 0
b 0
f 0
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A generateMisspellingMock() 0 15 1
A testFindFromInvalidArgument() 0 11 1
A generateTextMock() 0 11 1
A testFindFromString() 0 13 1
A testFindFromSource() 0 19 1
A testFindWithMisspelingHandler() 0 17 1
A testFindWithTextProcessor() 0 18 1
A setUp() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpSpellcheck\Tests;
6
7
use PhpSpellcheck\Exception\InvalidArgumentException;
8
use PhpSpellcheck\MisspellingFinder;
9
use PhpSpellcheck\MisspellingHandler\MisspellingHandlerInterface;
10
use PhpSpellcheck\MisspellingInterface;
11
use PhpSpellcheck\Source\SourceInterface;
12
use PhpSpellcheck\Spellchecker\SpellcheckerInterface;
13
use PhpSpellcheck\TextInterface;
14
use PhpSpellcheck\TextProcessor\TextProcessorInterface;
15
use PHPUnit\Framework\TestCase;
16
17
class MisspellingFinderTest extends TestCase
18
{
19
    private $misspellingHandler;
20
    private $textProcessor;
21
    private $spellChecker;
22
23
    public function setUp()
24
    {
25
        $this->spellChecker = $this->createMock(SpellcheckerInterface::class);
26
        $this->misspellingHandler = $this->createMock(MisspellingHandlerInterface::class);
27
        $this->textProcessor = $this->createMock(TextProcessorInterface::class);
28
    }
29
30
    public function testFindFromString()
31
    {
32
        $misspellingFinder = new MisspellingFinder(
33
            $this->spellChecker
34
        );
35
        $misspelling1 = $this->generateMisspellingMock();
36
37
        $this->spellChecker
38
            ->expects($this->once())
39
            ->method('check')
40
            ->willReturn([$misspelling1]);
41
42
        $this->assertSame([$misspelling1], iterator_to_array($misspellingFinder->find('mispell')));
0 ignored issues
show
Bug introduced by
$misspellingFinder->find('mispell') of type PhpSpellcheck\MisspellingInterface[] 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

42
        $this->assertSame([$misspelling1], iterator_to_array(/** @scrutinizer ignore-type */ $misspellingFinder->find('mispell')));
Loading history...
43
    }
44
45
    public function testFindFromInvalidArgument()
46
    {
47
        $this->expectException(InvalidArgumentException::class);
48
49
        $misspellingFinder = new MisspellingFinder(
50
            $this->spellChecker,
51
            $this->misspellingHandler,
52
            $this->textProcessor
53
        );
54
55
        $misspellingFinder->find(3);
56
    }
57
58
    public function testFindWithMisspelingHandler()
59
    {
60
        $this->misspellingHandler->expects($this->once())
61
            ->method('handle');
62
63
        $misspellingFinder = new MisspellingFinder(
64
            $this->spellChecker,
65
            $this->misspellingHandler
66
        );
67
        $misspelling1 = $this->generateMisspellingMock();
68
69
        $this->spellChecker
70
            ->expects($this->once())
71
            ->method('check')
72
            ->willReturn([$misspelling1]);
73
74
        $this->assertSame([$misspelling1], iterator_to_array($misspellingFinder->find('mispell')));
0 ignored issues
show
Bug introduced by
$misspellingFinder->find('mispell') of type PhpSpellcheck\MisspellingInterface[] 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

74
        $this->assertSame([$misspelling1], iterator_to_array(/** @scrutinizer ignore-type */ $misspellingFinder->find('mispell')));
Loading history...
75
    }
76
77
    public function testFindWithTextProcessor()
78
    {
79
        $this->textProcessor->expects($this->once())
80
            ->method('process');
81
82
        $misspellingFinder = new MisspellingFinder(
83
            $this->spellChecker,
84
            null,
85
            $this->textProcessor
86
        );
87
        $misspelling1 = $this->generateMisspellingMock();
88
89
        $this->spellChecker
90
            ->expects($this->once())
91
            ->method('check')
92
            ->willReturn([$misspelling1]);
93
94
        $this->assertSame([$misspelling1], iterator_to_array($misspellingFinder->find('mispell')));
0 ignored issues
show
Bug introduced by
$misspellingFinder->find('mispell') of type PhpSpellcheck\MisspellingInterface[] 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

94
        $this->assertSame([$misspelling1], iterator_to_array(/** @scrutinizer ignore-type */ $misspellingFinder->find('mispell')));
Loading history...
95
    }
96
97
    public function testFindFromSource()
98
    {
99
        $misspelling1 = $this->generateMisspellingMock();
100
        $this->spellChecker
101
            ->expects($this->once())
102
            ->method('check')
103
            ->willReturn([$misspelling1]);
104
105
        $source = $this->createMock(SourceInterface::class);
106
        $source->method('toTexts')
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

106
        $source->/** @scrutinizer ignore-call */ 
107
                 method('toTexts')

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...
107
            ->willReturn([$text = $this->generateTextMock()]);
0 ignored issues
show
Unused Code introduced by
The assignment to $text is dead and can be removed.
Loading history...
108
109
        $misspellingFinder = new MisspellingFinder(
110
            $this->spellChecker,
111
            null,
112
            $this->textProcessor
113
        );
114
115
        $this->assertSame([$misspelling1], iterator_to_array($misspellingFinder->find('mispell')));
0 ignored issues
show
Bug introduced by
$misspellingFinder->find('mispell') of type PhpSpellcheck\MisspellingInterface[] 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

115
        $this->assertSame([$misspelling1], iterator_to_array(/** @scrutinizer ignore-type */ $misspellingFinder->find('mispell')));
Loading history...
116
    }
117
118
    private function generateMisspellingMock()
119
    {
120
        $mispelling = $this->createMock(MisspellingInterface::class);
121
        $mispelling->method('getWord')
122
            ->willReturn('mispelled');
123
        $mispelling->method('getContext')
124
            ->willReturn([]);
125
        $mispelling->method('getLineNumber')
126
            ->willReturn(1);
127
        $mispelling->method('getOffset')
128
            ->willReturn(1);
129
        $mispelling->method('getSuggestions')
130
            ->willReturn(['misspelled', 'misspelling']);
131
132
        return $mispelling;
133
    }
134
135
    private function generateTextMock()
136
    {
137
        $text = $this->createMock(TextInterface::class);
138
        $text->method('getContent')
139
            ->willReturn('mispell');
140
        $text->method('getContext')
141
            ->willReturn([]);
142
        $text->method('getEncoding')
143
            ->willReturn('utf-8');
144
145
        return $text;
146
    }
147
}
148