-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathEngineBaseTest.php
More file actions
268 lines (236 loc) · 8.02 KB
/
EngineBaseTest.php
File metadata and controls
268 lines (236 loc) · 8.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
declare( strict_types = 1 );
namespace App\Tests\Engine;
use App\Engine\EngineBase;
use App\Engine\GoogleCloudVisionEngine;
use App\Engine\TesseractEngine;
use App\Engine\TranskribusClient;
use App\Engine\TranskribusEngine;
use App\Exception\OcrException;
use App\Tests\OcrTestCase;
use Krinkle\Intuition\Intuition;
use Symfony\Component\Cache\Adapter\NullAdapter;
use Symfony\Component\HttpClient\MockHttpClient;
use thiagoalessio\TesseractOCR\TesseractOCR;
class EngineBaseTest extends OcrTestCase {
/** @var GoogleCloudVisionEngine */
private $googleEngine;
/** @var TesseractEngine */
private $tesseractEngine;
/** @var TranskribusEngine */
private $transkribusEngine;
public function setUp(): void {
parent::setUp();
$this->googleEngine = $this->instantiateEngine( 'google' );
$this->tesseractEngine = $this->instantiateEngine( 'tesseract' );
$this->transkribusEngine = $this->instantiateEngine( 'transkribus' );
}
/**
* @covers EngineBase::checkImageUrl
* @dataProvider provideCheckImageUrl()
*/
public function testCheckImageUrl( string $url, bool $exceptionExpected ): void {
$this->tesseractEngine->setImageHosts( 'upload.wikimedia.org,localhost' );
if ( $exceptionExpected ) {
static::expectException( OcrException::class );
}
$this->tesseractEngine->checkImageUrl( $url );
static::assertTrue( true );
}
/**
* @return mixed[][]
*/
public function provideCheckImageUrl(): array {
return [
// Pass:
[ 'https://upload.wikimedia.org/wikipedia/commons/a/a9/Example.jpg', false ],
[ 'https://upload.wikimedia.org/wikipedia/commons/file.jpg', false ],
// Fail:
[ 'https://foo.example.com/wikipedia/commons/a/a9/Example.jpg', true ],
[ 'https://en.wikisource.org/file.mov', true ],
[ 'https://localhosts/file.jpg', true ],
];
}
/**
* @covers EngineBase::filterValidLangs for Google Engine
*/
public function testFilterValidLangsGoogleEngine(): void {
$allValid = [ 'en', 'es' ];
$allValidPlusInvalid = array_merge( $allValid, [ 'this-is-invalid' ] );
$this->assertSame(
$allValid,
$this->googleEngine->filterValidLangs( $allValid, EngineBase::WARN_ON_INVALID_LANGS )[0]
);
$this->assertSame(
$allValid,
$this->googleEngine->filterValidLangs( $allValidPlusInvalid, EngineBase::WARN_ON_INVALID_LANGS )[0]
);
$this->expectException( OcrException::class );
$this->googleEngine->filterValidLangs( $allValidPlusInvalid, EngineBase::ERROR_ON_INVALID_LANGS );
}
/**
* @param EngineBase $engine
* @param string[] $langs Language codes
* @param string[] $validLangs
* @param string $invalidLangsMode
* @covers EngineBase::filterValidLangs for Tesseract Engine
* @dataProvider provideLangs
*/
public function testFilterValidLangs(
EngineBase $engine, array $langs, array $validLangs, string $invalidLangsMode
): void {
if ( EngineBase::WARN_ON_INVALID_LANGS === $invalidLangsMode ) {
$this->assertSame( $validLangs, $engine->filterValidLangs( $langs, $invalidLangsMode )[0] );
} else {
if ( $langs !== $validLangs ) {
$this->expectException( OcrException::class );
}
$engine->filterValidLangs( $langs, $invalidLangsMode );
$this->addToAssertionCount( 1 );
}
}
/**
* @return array
*/
public function provideLangs(): array {
return [
[
'engine' => $this->instantiateEngine( 'tesseract' ),
[ 'eng', 'fra' ],
[ 'eng', 'fra' ],
EngineBase::WARN_ON_INVALID_LANGS,
EngineBase::ERROR_ON_INVALID_LANGS,
],
[
'engine' => $this->instantiateEngine( 'transkribus' ),
[ 'en-b2022', 'fr-m1' ],
[ 'en-b2022', 'fr-m1' ],
EngineBase::WARN_ON_INVALID_LANGS,
EngineBase::ERROR_ON_INVALID_LANGS,
],
];
}
/**
* @covers EngineBase::getModelName
* @covers EngineBase::getValidModels
*/
public function testLangNames(): void {
// From Intuition.
static::assertSame( 'français', $this->tesseractEngine->getModelTitle( 'fr' ) );
// From models.json
static::assertSame( 'moyen français (1400-1600)', $this->tesseractEngine->getModelTitle( 'frm' ) );
// Make sure every language has a name.
foreach ( $this->tesseractEngine->getValidModels( true ) as $lang => $name ) {
static::assertNotEmpty( $name, "Missing lang name for '$lang'" );
}
foreach ( $this->googleEngine->getValidModels( true ) as $lang => $name ) {
static::assertNotEmpty( $name, "Missing lang name for '$lang'" );
}
}
/**
* @covers EngineBase::getValidLineIds
*/
public function testValidLineIds(): void {
static::assertNotEmpty( $this->transkribusEngine->getValidLineIds( false, false ), "Missing line IDs" );
static::assertNotEmpty( $this->transkribusEngine->getValidLineIds( false, true ), "Missing line ID langs" );
static::assertNotEmpty( $this->transkribusEngine->getValidLineIds( true, false ), "Missing line IDs" );
static::assertNotEmpty( $this->transkribusEngine->getValidLineIds( true, true ), "Missing line IDs" );
}
public function instantiateEngine( string $engineName ): EngineBase {
self::bootKernel();
$this->projectDir = self::$kernel->getProjectDir();
$intuition = new Intuition();
$engine = null;
switch ( $engineName ) {
case 'tesseract':
$tesseractOCR = $this->getMockBuilder( TesseractOCR::class )->disableOriginalConstructor()->getMock();
$tesseractOCR->method( 'availableLanguages' )
->will( $this->returnValue( [ 'eng', 'spa', 'tha', 'tir' ] ) );
$tesseractEngine = new TesseractEngine(
new MockHttpClient(),
$intuition,
$this->projectDir,
$tesseractOCR
);
$engine = $tesseractEngine;
break;
case 'transkribus':
$transkribusEngine = new TranskribusEngine(
new TranskribusClient(
getenv( 'APP_TRANSKRIBUS_USERNAME' ),
getenv( 'APP_TRANSKRIBUS_PASSWORD' ),
new MockHttpClient(),
new NullAdapter(),
new NullAdapter()
),
$intuition,
$this->projectDir,
new MockHttpClient()
);
$engine = $transkribusEngine;
break;
default:
$googleEngine = new GoogleCloudVisionEngine(
dirname( __DIR__ ) . '/fixtures/google-account-keyfile.json',
$intuition,
$this->projectDir,
new MockHttpClient()
);
$engine = $googleEngine;
break;
}
return $engine;
}
/**
* @covers EngineBase::getResult
* Test that all engines accept the rotation parameter without errors
*/
public function testEngineAcceptsRotationParameter(): void {
// Test that engines can be called with rotation parameter
// This is a smoke test to ensure rotation parameter doesn't break the interface
$this->tesseractEngine->setImageHosts( 'upload.wikimedia.org' );
// All engines should accept the rotation parameter in their signature
try {
// We don't expect actual OCR results here, just that the parameter is accepted
$reflection = new \ReflectionMethod( $this->tesseractEngine, 'getResult' );
$parameters = $reflection->getParameters();
// Check that 'rotate' parameter exists
$rotateParam = null;
foreach ( $parameters as $param ) {
if ( $param->getName() === 'rotate' ) {
$rotateParam = $param;
break;
}
}
$this->assertNotNull( $rotateParam, 'rotate parameter should exist in getResult()' );
$this->assertTrue( $rotateParam->isOptional(), 'rotate parameter should be optional' );
$this->assertSame( 0, $rotateParam->getDefaultValue(), 'rotate parameter should default to 0' );
} catch ( \Exception $e ) {
$this->fail( 'Failed to verify rotation parameter: ' . $e->getMessage() );
}
}
/**
* @covers EngineBase::getResult
* Test that rotation parameter is part of all engine interfaces
*/
public function testAllEnginesHaveRotationParameter(): void {
$engines = [
$this->googleEngine,
$this->tesseractEngine,
$this->transkribusEngine,
];
foreach ( $engines as $engine ) {
$reflection = new \ReflectionMethod( $engine, 'getResult' );
$parameters = $reflection->getParameters();
$paramNames = [];
foreach ( $parameters as $param ) {
$paramNames[] = $param->getName();
}
$this->assertContains(
'rotate',
$paramNames,
'Engine ' . get_class( $engine ) . ' should have rotate parameter'
);
}
}
}