-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathClientTest.php
More file actions
163 lines (140 loc) · 5.95 KB
/
Copy pathClientTest.php
File metadata and controls
163 lines (140 loc) · 5.95 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
<?php
use Mindee\Client;
use Mindee\Error\MindeeApiException;
use Mindee\Error\MindeeHttpClientException;
use Mindee\Error\MindeeHttpException;
use Mindee\Input\LocalResponse;
use Mindee\Input\PageOptions;
use Mindee\Input\PollingOptions;
use Mindee\Input\PredictMethodOptions;
use Mindee\Product\Generated\GeneratedV1;
use Mindee\Product\Invoice\InvoiceV4;
use Mindee\Product\InvoiceSplitter\InvoiceSplitterV1;
use Mindee\Product\MultiReceiptsDetector\MultiReceiptsDetectorV1;
use Mindee\Product\Receipt\ReceiptV5;
use PHPUnit\Framework\TestCase;
class ClientTest extends TestCase
{
private Client $emptyClient;
private Client $dummyClient;
private Client $envClient;
private string $oldKey;
private string $fileTypesDir;
private string $multiReceiptsDetectorPath;
private string $failedJobPath;
protected function setUp(): void
{
$rootPath = (getenv('GITHUB_WORKSPACE') ?: ".");
$this->oldKey = getenv('MINDEE_API_KEY');
$this->dummyClient = new Client("dummy-key");
putenv('MINDEE_API_KEY=');
$this->emptyClient = new Client();
putenv('MINDEE_API_KEY=dummy-env-key');
$this->envClient = new Client();
$this->fileTypesDir = (
$rootPath . "/tests/resources/file_types/"
);
$this->multiReceiptsDetectorPath = (
$rootPath . "/tests/resources/products/multi_receipts_detector/response_v1/complete.json"
);
$this->failedJobPath = (
$rootPath . "/tests/resources/async/get_failed_job_error.json"
);
}
protected function tearDown(): void
{
putenv('MINDEE_API_KEY=' . $this->oldKey);
}
public function testParsePathWithoutToken()
{
$this->expectException(MindeeHttpClientException::class);
$inputDoc = $this->emptyClient->sourceFromPath($this->fileTypesDir . "pdf/blank.pdf");
$this->emptyClient->parse(InvoiceV4::class, $inputDoc);
}
public function testParsePathWithEnvToken()
{
$this->expectException(MindeeHttpException::class);
$inputDoc = $this->envClient->sourceFromPath($this->fileTypesDir . "pdf/blank.pdf");
$this->envClient->parse(InvoiceV4::class, $inputDoc);
}
public function testParsePathWithWrongFileType()
{
$this->expectException(Mindee\Error\MindeeMimeTypeException::class);
$inputDoc = $this->dummyClient->sourceFromPath($this->fileTypesDir . "receipt.txt");
}
public function testParsePathWithWrongToken()
{
$this->expectException(MindeeHttpClientException::class);
$inputDoc = $this->dummyClient->sourceFromPath($this->fileTypesDir . "pdf/blank.pdf");
$this->dummyClient->parse(InvoiceV4::class, $inputDoc);
}
public function testInterfaceVersion()
{
$dummyEndpoint = $this->dummyClient->createEndpoint("dummy", "dummy", "1.1");
$inputDoc = $this->dummyClient->sourceFromPath($this->fileTypesDir . "pdf/blank.pdf");
$predictOptions = new PredictMethodOptions();
$this->assertEquals("1.1", $dummyEndpoint->settings->version);
$this->expectException(MindeeHTTPClientException::class);
$this->dummyClient->parse(
GeneratedV1::class,
$inputDoc,
$predictOptions->setEndpoint($dummyEndpoint),
);
}
public function testCutOptions()
{
$inputDoc = $this->dummyClient->sourceFromPath($this->fileTypesDir . "pdf/multipage.pdf");
$this->expectException(MindeeHttpClientException::class);
$pageOptions = new PageOptions(range(0, 4));
$this->dummyClient->parse(ReceiptV5::class, $inputDoc, null, $pageOptions);
$this->assertEquals(5, $inputDoc->getPageCount());
}
public function testAsyncWrongInitialDelay()
{
$this->expectException(MindeeApiException::class);
$asyncParseOptions = new PollingOptions();
$asyncParseOptions->setInitialDelaySec(0);
}
public function testAsyncWrongPollingDelay()
{
$this->expectException(MindeeApiException::class);
$asyncParseOptions = new PollingOptions();
$asyncParseOptions->setDelaySec(0);
}
public function testPredictOptionsWrongInputType()
{
$pageOptions = new PageOptions([0, 1]);
$this->assertFalse($pageOptions->isEmpty());
$predictOptions = new PredictMethodOptions();
$predictOptions->setPageOptions($pageOptions);
$urlInputSource = $this->dummyClient->sourceFromUrl("https://dummy");
$this->expectException(MindeeApiException::class);
$this->dummyClient->parse(InvoiceV4::class, $urlInputSource, $predictOptions);
$this->expectException(MindeeApiException::class);
$this->dummyClient->enqueue(InvoiceSplitterV1::class, $urlInputSource, $predictOptions);
}
public function testPredictOptionsValidInputType()
{
$predictOptions = new PredictMethodOptions();
$this->assertTrue($predictOptions->pageOptions->isEmpty());
$inputDoc = $this->dummyClient->sourceFromPath($this->fileTypesDir . "pdf/blank.pdf");
$this->expectException(MindeeHttpClientException::class);
$this->dummyClient->parse(InvoiceV4::class, $inputDoc, $predictOptions);
$this->expectException(MindeeHttpClientException::class);
$this->dummyClient->enqueue(InvoiceSplitterV1::class, $inputDoc, $predictOptions);
}
public function testLoadLocalResponse()
{
$localResponse = new LocalResponse($this->multiReceiptsDetectorPath);
$res = $this->dummyClient->loadPrediction(MultiReceiptsDetectorV1::class, $localResponse);
$this->assertNotNull($res);
$this->assertEquals(1, $res->document->nPages);
}
public function testLoadFailedLocalResponse()
{
$localResponse = new LocalResponse($this->failedJobPath);
$res = $this->dummyClient->loadPrediction(InvoiceV4::class, $localResponse);
$this->assertNotNull($res);
$this->assertEquals("failed", $res->job->status);
}
}