tigitz /
php-spellchecker
| 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 MultipleSpellcheckersTest extends TestCase |
||
| 14 | { |
||
| 15 | |||
| 16 | public function testCheckAndMergeSuggestions() |
||
| 17 | { |
||
| 18 | $spellChecker1 = $this->createMock(SpellcheckerInterface::class); |
||
| 19 | $misspelling1 = new Misspelling('mispelling1', 1); |
||
| 20 | $misspelling2a = new Misspelling('mispelling2', 2, 2, ['suggestionA']); |
||
| 21 | $misspelling2b = new Misspelling('mispelling2', 2, 2, ['suggestionB']); |
||
| 22 | |||
| 23 | $spellChecker1->method('check') |
||
| 24 | ->willReturn([$misspelling1, $misspelling2a]); |
||
| 25 | $spellChecker2 = $this->createMock(SpellcheckerInterface::class); |
||
| 26 | $misspelling3 = new Misspelling('mispelling3', 3, 3); |
||
| 27 | $spellChecker2->method('check') |
||
| 28 | ->willReturn([$misspelling2b, $misspelling3]); |
||
| 29 | |||
| 30 | $multiSpellchecker = new MultiSpellchecker([$spellChecker1, $spellChecker2]); |
||
| 31 | |||
| 32 | $misspellings = $multiSpellchecker->check('test', ['en'], ['ctx'], TextEncoding::UTF8); |
||
| 33 | $this->assertEquals( |
||
| 34 | [ |
||
| 35 | $misspelling1->setContext(['ctx']), |
||
| 36 | new Misspelling('mispelling2', 2, 2, ['suggestionA', 'suggestionB'], ['ctx']), |
||
| 37 | $misspelling3->setContext(['ctx']), |
||
| 38 | ], |
||
| 39 | $misspellings |
||
| 40 | ); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function testCheckAndNotMergeSuggestions() |
||
| 44 | { |
||
| 45 | $spellChecker1 = $this->createMock(SpellcheckerInterface::class); |
||
| 46 | $misspelling1 = new Misspelling('mispelling1', 1); |
||
| 47 | $misspelling2a = new Misspelling('mispelling2', 2, 2, ['suggestionA']); |
||
| 48 | $misspelling2b = new Misspelling('mispelling2', 2, 2, ['suggestionB']); |
||
| 49 | |||
| 50 | $spellChecker1->method('check') |
||
| 51 | ->willReturn([$misspelling1, $misspelling2a]); |
||
| 52 | $spellChecker2 = $this->createMock(SpellcheckerInterface::class); |
||
| 53 | $misspelling3 = new Misspelling('mispelling3', 3, 3); |
||
| 54 | $spellChecker2->method('check') |
||
| 55 | ->willReturn([$misspelling2b, $misspelling3]); |
||
| 56 | |||
| 57 | $multiSpellchecker = new MultiSpellchecker([$spellChecker1, $spellChecker2], false); |
||
| 58 | |||
| 59 | $misspellings = $multiSpellchecker->check('test', ['en'], ['ctx'], TextEncoding::UTF8); |
||
| 60 | $this->assertEquals( |
||
| 61 | [ |
||
| 62 | $misspelling1->setContext(['ctx']), |
||
| 63 | $misspelling2a->setContext(['ctx']), |
||
| 64 | $misspelling2b->setContext(['ctx']), |
||
| 65 | $misspelling3->setContext(['ctx']), |
||
| 66 | ], |
||
| 67 | iterator_to_array($misspellings) |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 68 | ); |
||
| 69 | } |
||
| 70 | |||
| 71 | public function testGetSupportedLanguages() |
||
| 72 | { |
||
| 73 | $spellChecker1 = $this->createMock(SpellcheckerInterface::class); |
||
| 74 | $spellChecker1->method('getSupportedLanguages') |
||
| 75 | ->willReturn(['en', 'fr']); |
||
| 76 | $spellChecker2 = $this->createMock(SpellcheckerInterface::class); |
||
| 77 | $spellChecker2->method('getSupportedLanguages') |
||
| 78 | ->willReturn(['fr', 'ru']); |
||
| 79 | |||
| 80 | $multipleSpellchecker = new MultiSpellchecker([$spellChecker1, $spellChecker2]); |
||
| 81 | |||
| 82 | $this->assertSame(['en', 'fr', 'ru'], $multipleSpellchecker->getSupportedLanguages()); |
||
| 83 | } |
||
| 84 | } |
||
| 85 |