tigitz /
php-spellchecker
| 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
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
$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
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
$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
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') |
||||
| 107 | ->willReturn([$text = $this->generateTextMock()]); |
||||
| 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
$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
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 |