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