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