-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathClientTest.php
More file actions
162 lines (138 loc) · 5.87 KB
/
Copy pathClientTest.php
File metadata and controls
162 lines (138 loc) · 5.87 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
<?php
declare(strict_types=1);
use Mindee\ClientOptions\PollingOptions;
use Mindee\Error\MindeeApiException;
use Mindee\Error\MindeeMimeTypeException;
use Mindee\Input\LocalResponse;
use Mindee\Input\PageOptions;
use Mindee\Input\PathInput;
use Mindee\Input\UrlInputSource;
use Mindee\V1\Client;
use Mindee\V1\ClientOptions\PredictMethodOptions;
use Mindee\V1\Error\MindeeV1HttpException;
use Mindee\V1\Product\Generated\GeneratedV1;
use Mindee\V1\Product\Invoice\InvoiceV4;
use Mindee\V1\Product\InvoiceSplitter\InvoiceSplitterV1;
use Mindee\V1\Product\MultiReceiptsDetector\MultiReceiptsDetectorV1;
use Mindee\V1\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 $multiReceiptsDetectorPath;
private string $failedJobPath;
protected function setUp(): void
{
$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->multiReceiptsDetectorPath = (
TestingUtilities::getV1DataDir() . "/products/multi_receipts_detector/response_v1/complete.json"
);
$this->failedJobPath = (
TestingUtilities::getV1DataDir() . "/async/get_failed_job_error.json"
);
}
protected function tearDown(): void
{
putenv('MINDEE_API_KEY=' . $this->oldKey);
}
public function testParsePathWithoutToken(): void
{
$this->expectException(MindeeV1HttpException::class);
$inputDoc = new PathInput(TestingUtilities::getFileTypesDir() . "/pdf/blank.pdf");
$this->emptyClient->parse(InvoiceV4::class, $inputDoc);
}
public function testParsePathWithEnvToken(): void
{
$this->expectException(MindeeV1HttpException::class);
$inputDoc = new PathInput(TestingUtilities::getFileTypesDir() . "/pdf/blank.pdf");
$this->envClient->parse(InvoiceV4::class, $inputDoc);
}
public function testParsePathWithWrongFileType(): void
{
$this->expectException(MindeeMimeTypeException::class);
new PathInput(TestingUtilities::getFileTypesDir() . "/receipt.txt");
}
public function testParsePathWithWrongToken(): void
{
$this->expectException(MindeeV1HttpException::class);
$inputDoc = new PathInput(TestingUtilities::getFileTypesDir() . "/pdf/blank.pdf");
$this->dummyClient->parse(InvoiceV4::class, $inputDoc);
}
public function testInterfaceVersion(): void
{
$dummyEndpoint = $this->dummyClient->createEndpoint("dummy", "dummy", "1.1");
$inputDoc = new PathInput(TestingUtilities::getFileTypesDir() . "/pdf/blank.pdf");
$predictOptions = new PredictMethodOptions();
self::assertSame("1.1", $dummyEndpoint->settings->version);
$this->expectException(MindeeV1HttpException::class);
$this->dummyClient->parse(
GeneratedV1::class,
$inputDoc,
$predictOptions->setEndpoint($dummyEndpoint),
);
}
public function testCutOptions(): void
{
$inputDoc = new PathInput(TestingUtilities::getFileTypesDir() . "/pdf/multipage.pdf");
$this->expectException(MindeeV1HttpException::class);
$pageOptions = new PageOptions(range(0, 4));
$this->dummyClient->parse(ReceiptV5::class, $inputDoc, null, $pageOptions);
self::assertSame(5, $inputDoc->pageCount);
}
public function testAsyncWrongInitialDelay(): void
{
$this->expectException(MindeeApiException::class);
$asyncParseOptions = new PollingOptions();
$asyncParseOptions->setInitialDelaySec(0);
}
public function testAsyncWrongPollingDelay(): void
{
$this->expectException(MindeeApiException::class);
$asyncParseOptions = new PollingOptions();
$asyncParseOptions->setDelaySec(0);
}
public function testPredictOptionsWrongInputType(): void
{
$pageOptions = new PageOptions([0, 1]);
self::assertFalse($pageOptions->isEmpty());
$predictOptions = new PredictMethodOptions();
$predictOptions->setPageOptions($pageOptions);
$urlInputSource = new UrlInputSource("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(): void
{
$predictOptions = new PredictMethodOptions();
self::assertTrue($predictOptions->pageOptions->isEmpty());
$inputDoc = new PathInput(TestingUtilities::getFileTypesDir() . "/pdf/blank.pdf");
$this->expectException(MindeeV1HttpException::class);
$this->dummyClient->parse(InvoiceV4::class, $inputDoc, $predictOptions);
$this->expectException(MindeeV1HttpException::class);
$this->dummyClient->enqueue(InvoiceSplitterV1::class, $inputDoc, $predictOptions);
}
public function testLoadLocalResponse(): void
{
$localResponse = new LocalResponse($this->multiReceiptsDetectorPath);
$res = $this->dummyClient->loadPrediction(MultiReceiptsDetectorV1::class, $localResponse);
self::assertNotNull($res);
self::assertSame(1, $res->document->nPages);
}
public function testLoadFailedLocalResponse(): void
{
$localResponse = new LocalResponse($this->failedJobPath);
$res = $this->dummyClient->loadPrediction(InvoiceV4::class, $localResponse);
self::assertNotNull($res);
self::assertSame("failed", $res->job->status);
}
}