-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathOcrControllerTest.php
More file actions
152 lines (144 loc) · 4.23 KB
/
OcrControllerTest.php
File metadata and controls
152 lines (144 loc) · 4.23 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
<?php
declare( strict_types = 1 );
namespace App\Tests\Controller;
use App\Controller\OcrController;
use App\Engine\EngineFactory;
use App\Engine\GoogleCloudVisionEngine;
use App\Engine\TesseractEngine;
use App\Engine\TranskribusClient;
use App\Engine\TranskribusEngine;
use App\Tests\OcrTestCase;
use Krinkle\Intuition\Intuition;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\NullAdapter;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use thiagoalessio\TesseractOCR\TesseractOCR;
class OcrControllerTest extends OcrTestCase {
/**
* @dataProvider provideGetLang
* @covers OcrController::getLangs
* @param string[] $getParams
* @param string[] $expectedLangs
*/
public function testGetLang( array $getParams, array $expectedLangs ): void {
$request = new Request( $getParams );
$requestStack = new RequestStack();
$requestStack->push( $request );
$request->setSession( new Session( new MockArraySessionStorage() ) );
$intuition = new Intuition( [] );
$gcv = new GoogleCloudVisionEngine(
dirname( __DIR__ ) . '/fixtures/google-account-keyfile.json',
$intuition,
$this->projectDir,
new MockHttpClient()
);
$controller = new OcrController(
$requestStack,
$intuition,
new EngineFactory(
$gcv,
new TesseractEngine( new MockHttpClient(), $intuition, $this->projectDir, new TesseractOCR() ),
new TranskribusEngine(
new TranskribusClient(
getenv( 'APP_TRANSKRIBUS_USERNAME' ),
getenv( 'APP_TRANSKRIBUS_PASSWORD' ),
new MockHttpClient(),
new NullAdapter(),
new NullAdapter()
),
$intuition,
$this->projectDir,
new MockHttpClient()
),
),
new FilesystemAdapter()
);
$this->assertSame( $expectedLangs, $controller->getLangs( $request ) );
}
/**
* @return mixed[]
*/
public function provideGetLang(): array {
return [
[
[ 'lang' => 'ar' ],
[ 'ar' ],
],
[
[ 'langs' => [ 'a|b', 'c!', 'ab' ] ],
[ 'ab', 'c' ],
],
'special characters' => [
[ 'langs' => [ 'sr-Latn', 'Canadian_Aboriginal' ] ],
[ 'sr-Latn', 'Canadian_Aboriginal' ],
],
'numbers' => [
[ 'langs' => [ 'ru-petr1708' ] ],
[ 'ru-petr1708' ],
],
];
}
/**
* @dataProvider provideRotationNormalization
* @covers OcrController
* @param int $inputRotation
* @param int $expectedRotation
*/
public function testRotationNormalization( int $inputRotation, int $expectedRotation ): void {
$request = new Request( [ 'rotate' => $inputRotation ] );
$requestStack = new RequestStack();
$requestStack->push( $request );
$request->setSession( new Session( new MockArraySessionStorage() ) );
$intuition = new Intuition( [] );
$controller = new OcrController(
$requestStack,
$intuition,
new EngineFactory(
new GoogleCloudVisionEngine(
dirname( __DIR__ ) . '/fixtures/google-account-keyfile.json',
$intuition,
$this->projectDir,
new MockHttpClient()
),
new TesseractEngine( new MockHttpClient(), $intuition, $this->projectDir, new TesseractOCR() ),
new TranskribusEngine(
new TranskribusClient(
getenv( 'APP_TRANSKRIBUS_USERNAME' ),
getenv( 'APP_TRANSKRIBUS_PASSWORD' ),
new MockHttpClient(),
new NullAdapter(),
new NullAdapter()
),
$intuition,
$this->projectDir,
new MockHttpClient()
),
),
new FilesystemAdapter()
);
// Trigger the setup to process rotation
$reflection = new \ReflectionMethod( $controller, 'setup' );
$reflection->setAccessible( true );
$reflection->invoke( $controller );
$this->assertSame( $expectedRotation, OcrController::$params['rotate'] );
}
/**
* @return int[][]
*/
public function provideRotationNormalization(): array {
return [
'zero rotation' => [ 0, 0 ],
'positive rotation' => [ 90, 90 ],
'negative rotation' => [ -90, 270 ],
'large positive rotation' => [ 450, 90 ],
'large negative rotation' => [ -450, 270 ],
'boundary 359' => [ 359, 359 ],
'boundary 360' => [ 360, 0 ],
'boundary -360' => [ -360, 0 ],
];
}
}