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