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