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