tigitz /
php-spellchecker
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace PhpSpellcheck\Tests\Spellchecker; |
||
| 6 | |||
| 7 | use PhpSpellcheck\Exception\ProcessHasErrorOutputException; |
||
| 8 | use PhpSpellcheck\Misspelling; |
||
| 9 | use PhpSpellcheck\Spellchecker\Hunspell; |
||
| 10 | use PhpSpellcheck\Tests\TextTest; |
||
| 11 | use PhpSpellcheck\Utils\CommandLine; |
||
| 12 | use PHPUnit\Framework\TestCase; |
||
| 13 | |||
| 14 | class HunspellTest extends TestCase |
||
| 15 | { |
||
| 16 | private const FAKE_BINARIES_PATH = [PHP_BINARY, __DIR__ . '/../Fixtures/Hunspell/bin/hunspell.php']; |
||
| 17 | |||
| 18 | public function testSpellcheckFromFakeBinaries() |
||
| 19 | { |
||
| 20 | $this->assertWorkingSpellcheck(self::FAKE_BINARIES_PATH); |
||
| 21 | } |
||
| 22 | |||
| 23 | public function testGetSupportedLanguagesFromFakeBinaries() |
||
| 24 | { |
||
| 25 | $this->assertWorkingSupportedLanguages(self::FAKE_BINARIES_PATH); |
||
| 26 | } |
||
| 27 | |||
| 28 | public function testBadCheckRequest() |
||
| 29 | { |
||
| 30 | $this->expectException(ProcessHasErrorOutputException::class); |
||
| 31 | (new Hunspell(new CommandLine(IspellTest::FAKE_BAD_BINARIES_PATH)))->check('bla'); |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @group integration |
||
| 36 | */ |
||
| 37 | public function testSpellcheckFromRealBinariesLanguage() |
||
| 38 | { |
||
| 39 | $hunspell = new Hunspell(new CommandLine(self::realBinaryPath())); |
||
| 40 | $misspellings = iterator_to_array($hunspell->check('mispell', ['en_US'])); |
||
| 41 | $this->assertInstanceOf(Misspelling::class, $misspellings[0]); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @group integration |
||
| 46 | */ |
||
| 47 | public function testSpellcheckFromRealBinaries() |
||
| 48 | { |
||
| 49 | $this->assertWorkingSpellcheck(self::realBinaryPath()); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @group integration |
||
| 54 | */ |
||
| 55 | public function testGetSupportedLanguagesFromRealBinaries() |
||
| 56 | { |
||
| 57 | $this->assertWorkingSupportedLanguages(self::realBinaryPath()); |
||
| 58 | } |
||
| 59 | |||
| 60 | public function getTextInput() |
||
| 61 | { |
||
| 62 | return TextTest::CONTENT_STUB; |
||
| 63 | } |
||
| 64 | |||
| 65 | public function getFakeDicts() |
||
| 66 | { |
||
| 67 | return explode(PHP_EOL, file_get_contents(__DIR__ . '/../Fixtures/Hunspell/dicts.txt')); |
||
| 68 | } |
||
| 69 | |||
| 70 | |||
| 71 | /** |
||
| 72 | * @param string|array $binaries |
||
| 73 | */ |
||
| 74 | private function assertWorkingSpellcheck($binaries) |
||
| 75 | { |
||
| 76 | $hunspell = new Hunspell(new CommandLine($binaries)); |
||
| 77 | /** @var Misspelling[] $misspellings */ |
||
| 78 | $misspellings = iterator_to_array($hunspell->check($this->getTextInput(), ['en_US'], ['ctx'])); |
||
| 79 | |||
| 80 | $this->assertSame(['ctx'], $misspellings[0]->getContext()); |
||
| 81 | $this->assertSame('Tigr', $misspellings[0]->getWord()); |
||
| 82 | $this->assertSame(0, $misspellings[0]->getOffset()); |
||
| 83 | $this->assertSame(1, $misspellings[0]->getLineNumber()); |
||
| 84 | $this->assertNotEmpty($misspellings[0]->getSuggestions()); |
||
| 85 | |||
| 86 | $this->assertSame(['ctx'], $misspellings[1]->getContext()); |
||
| 87 | $this->assertSame('страх', $misspellings[1]->getWord()); |
||
| 88 | $this->assertSame(21, $misspellings[1]->getOffset()); |
||
| 89 | $this->assertSame(1, $misspellings[1]->getLineNumber()); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param string|array $binaries |
||
| 94 | */ |
||
| 95 | public function assertWorkingSupportedLanguages($binaries) |
||
| 96 | { |
||
| 97 | $hunspell = new Hunspell(new CommandLine($binaries)); |
||
| 98 | $this->assertNotFalse(array_search('en_US', $hunspell->getSupportedLanguages())); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 99 | } |
||
| 100 | |||
| 101 | public static function realBinaryPath(): string |
||
| 102 | { |
||
| 103 | if (getenv('HUNSPELL_BINARY_PATH') === false) { |
||
| 104 | throw new \RuntimeException('"HUNSPELL_BINARY_PATH" env must be set to find the executable to run tests on'); |
||
| 105 | } |
||
| 106 | |||
| 107 | return getenv('HUNSPELL_BINARY_PATH'); |
||
| 108 | } |
||
| 109 | } |
||
| 110 |