1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpSpellcheck\Tests\Spellchecker; |
6
|
|
|
|
7
|
|
|
use PhpSpellcheck\Misspelling; |
8
|
|
|
use PhpSpellcheck\Spellchecker\LanguageTool; |
9
|
|
|
use PhpSpellcheck\Spellchecker\LanguageTool\LanguageToolApiClient; |
10
|
|
|
use PhpSpellcheck\Tests\TextTest; |
11
|
|
|
use PhpSpellcheck\Utils\TextEncoding; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
|
14
|
|
|
class LanguageToolTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
public function getClientMock() |
17
|
|
|
{ |
18
|
|
|
$mock = $this->getMockBuilder(LanguageToolApiClient::class) |
19
|
|
|
->disableOriginalConstructor() |
20
|
|
|
->getMock(); |
21
|
|
|
|
22
|
|
|
return $mock; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testSpellcheck() |
26
|
|
|
{ |
27
|
|
|
$client = $this->getClientMock(); |
28
|
|
|
$client->expects($this->once()) |
29
|
|
|
->method('spellCheck') |
30
|
|
|
->willReturn([ |
31
|
|
|
'matches' => [ |
32
|
|
|
[ |
33
|
|
|
'message' => 'Possible spelling mistake found', |
34
|
|
|
'replacements' => [['value' => 'Tier']], |
35
|
|
|
'offset' => 0, |
36
|
|
|
'length' => 4, |
37
|
|
|
'context' => [ |
38
|
|
|
'text' => 'Tigr, tiger, burning страх. In theforests of...', |
39
|
|
|
'offset' => 0, |
40
|
|
|
'length' => 4, |
41
|
|
|
], |
42
|
|
|
'sentence' => 'Tigr, tiger, burning страх.', |
43
|
|
|
'rule' => [ |
44
|
|
|
'description' => 'Possible spelling mistake', |
45
|
|
|
'issueType' => 'misspelling', |
46
|
|
|
], |
47
|
|
|
], |
48
|
|
|
[ |
49
|
|
|
'message' => 'Possible spelling mistake found', |
50
|
|
|
'replacements' => [['value' => 'Could']], |
51
|
|
|
'offset' => 81, |
52
|
|
|
'length' => 6, |
53
|
|
|
'context' => [ |
54
|
|
|
'text' => '... of the night, What imortal hand or eey CCould frame thy fearful symmetry?', |
55
|
|
|
'offset' => 43, |
56
|
|
|
'length' => 6, |
57
|
|
|
], |
58
|
|
|
'sentence' => "In theforests of the night,\nWhat imortal hand or eey\nCCould frame thy fearful symmetry?", |
59
|
|
|
'rule' => [ |
60
|
|
|
'description' => 'Possible spelling mistake', |
61
|
|
|
'issueType' => 'misspelling', |
62
|
|
|
], |
63
|
|
|
], |
64
|
|
|
], |
65
|
|
|
]); |
66
|
|
|
|
67
|
|
|
$this->assertWorkingSpellcheck($client); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testGetSupportedLanguages() |
71
|
|
|
{ |
72
|
|
|
$client = $this->getClientMock(); |
73
|
|
|
$client->expects($this->once()) |
74
|
|
|
->method('getSupportedLanguages') |
75
|
|
|
->willReturn(['en']); |
76
|
|
|
|
77
|
|
|
$this->assertWorkingSupportedLanguages($client); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @group integration |
82
|
|
|
*/ |
83
|
|
|
public function testSpellcheckFromRealAPI() |
84
|
|
|
{ |
85
|
|
|
$this->assertWorkingSpellcheck(new LanguageToolApiClient(self::realAPIEndpoint())); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @group integration |
90
|
|
|
*/ |
91
|
|
|
public function testGetSupportedLanguagesFromRealBinaries() |
92
|
|
|
{ |
93
|
|
|
$this->assertWorkingSupportedLanguages(new LanguageToolApiClient(self::realAPIEndpoint())); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getTextInput() |
97
|
|
|
{ |
98
|
|
|
return TextTest::CONTENT_STUB; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function assertWorkingSpellcheck(LanguageToolApiClient $apiClient) |
102
|
|
|
{ |
103
|
|
|
$languageTool = new LanguageTool($apiClient); |
104
|
|
|
/** @var Misspelling[] $misspellings */ |
105
|
|
|
$misspellings = iterator_to_array( |
106
|
|
|
$languageTool->check( |
107
|
|
|
$this->getTextInput(), |
108
|
|
|
['en-US'], |
109
|
|
|
['ctx' => 'ctx'], |
110
|
|
|
TextEncoding::UTF8 |
111
|
|
|
) |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
// test first line offset computation |
115
|
|
|
$this->assertArrayHasKey('ctx', $misspellings[0]->getContext()); |
116
|
|
|
$this->assertSame($misspellings[0]->getWord(), 'Tigr'); |
117
|
|
|
$this->assertSame($misspellings[0]->getOffset(), 0); |
118
|
|
|
$this->assertSame($misspellings[0]->getLineNumber(), 1); |
119
|
|
|
$this->assertNotEmpty($misspellings[0]->getSuggestions()); |
120
|
|
|
|
121
|
|
|
end($misspellings); |
122
|
|
|
$lastKey = key($misspellings); |
123
|
|
|
// test last line offset computation |
124
|
|
|
$this->assertArrayHasKey('ctx', $misspellings[$lastKey]->getContext()); |
125
|
|
|
$this->assertSame($misspellings[$lastKey]->getWord(), 'CCould'); |
126
|
|
|
$this->assertSame($misspellings[$lastKey]->getOffset(), 1); |
127
|
|
|
$this->assertSame($misspellings[$lastKey]->getLineNumber(), 4); |
128
|
|
|
$this->assertNotEmpty($misspellings[$lastKey]->getSuggestions()); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function assertWorkingSupportedLanguages(LanguageToolApiClient $apiClient) |
132
|
|
|
{ |
133
|
|
|
$languageTool = new LanguageTool($apiClient); |
134
|
|
|
$this->assertNotFalse(array_search('en', $languageTool->getSupportedLanguages())); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
private static function realAPIEndpoint(): string |
138
|
|
|
{ |
139
|
|
|
if (getenv('LANGUAGETOOLS_ENDPOINT') === false) { |
140
|
|
|
throw new \RuntimeException('"LANGUAGETOOLS_ENDPOINT" env must be set to run the tests on'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return getenv('LANGUAGETOOLS_ENDPOINT'); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|