Skip to content

Commit e13ee0a

Browse files
add _some_ tests
1 parent faa0baf commit e13ee0a

9 files changed

Lines changed: 515 additions & 7 deletions

File tree

.github/workflows/_test-integrations.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ env:
1515
MINDEE_V2_API_KEY: ${{ secrets.MINDEE_V2_SE_TESTS_API_KEY }}
1616
MINDEE_V2_FINDOC_MODEL_ID: ${{ secrets.MINDEE_V2_SE_TESTS_FINDOC_MODEL_ID }}
1717
MINDEE_V2_SE_TESTS_BLANK_PDF_URL: ${{ secrets.MINDEE_V2_SE_TESTS_BLANK_PDF_URL }}
18+
MINDEE_V2_CLASSIFICATION_MODEL_ID: ${{ secrets.MINDEE_V2_SE_TESTS_CLASSIFICATION_MODEL_ID }}
19+
MINDEE_V2_CROP_MODEL_ID: ${{ secrets.MINDEE_V2_SE_TESTS_CROP_MODEL_ID }}
20+
MINDEE_V2_OCR_MODEL_ID: ${{ secrets.MINDEE_V2_SE_TESTS_OCR_MODEL_ID }}
21+
MINDEE_V2_SPLIT_MODEL_ID: ${{ secrets.MINDEE_V2_SE_TESTS_SPLIT_MODEL_ID }}
1822

1923
jobs:
2024
integration-tests-ubuntu:

src/Geometry/Polygon.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,11 @@ public function getCoordinates(): ?array
160160
/**
161161
* @return string String representation.
162162
*/
163-
public function __toString()
163+
public function __toString(): string
164164
{
165-
if (!$this->isEmpty()) {
166-
return 'Polygon with ' . count($this->getCoordinates()) . ' points.';
167-
}
168-
return '';
165+
$formattedPoints = array_map(fn ($p) => "({$p->getX()},{$p->getY()})", $this->coordinates);
166+
$joinedPoints = implode(", ", $formattedPoints);
167+
168+
return "($joinedPoints)";
169169
}
170170
}

src/Parsing/V2/Field/FieldLocation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ public function __construct(array $serverResponse)
3939
*/
4040
public function __toString(): string
4141
{
42-
return $this->polygon ? (string)$this->polygon : '';
42+
return $this->polygon ? $this->polygon . " on page $this->page" : '';
4343
}
4444
}

src/V2/Product/Classification/ClassificationResult.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ClassificationResult
1717
*/
1818
public function __construct(array $rawResponse)
1919
{
20-
$this->classification = new ClassificationClassifier($rawResponse['classifier']);
20+
$this->classification = new ClassificationClassifier($rawResponse['classification']);
2121
}
2222

2323
/**
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
4+
namespace V2\Product;
5+
6+
use Mindee\ClientV2;
7+
use Mindee\Input\PathInput;
8+
use Mindee\V2\Product\Classification\ClassificationResponse;
9+
use Mindee\V2\Product\Classification\Params\ClassificationParameters;
10+
use PHPUnit\Framework\TestCase;
11+
use TestingUtilities;
12+
13+
require_once(__DIR__ . "/../../TestingUtilities.php");
14+
15+
class ClassificationFunctional extends TestCase
16+
{
17+
private ClientV2 $client;
18+
private string $classificationModelId;
19+
20+
protected function setUp(): void
21+
{
22+
$apiKey = getenv('MINDEE_V2_API_KEY');
23+
$this->client = new ClientV2($apiKey);
24+
25+
$this->classificationModelId = getenv('MINDEE_V2_CLASSIFICATION_MODEL_ID') ?: '';
26+
}
27+
28+
/**
29+
* Tests the success of the classification process using a default sample file.
30+
*
31+
* @return void
32+
*/
33+
public function testClassificationDefaultSampleMustSucceed(): void
34+
{
35+
$inputSource = new PathInput(
36+
TestingUtilities::getV2ProductDir() . '/classification/default_invoice.jpg'
37+
);
38+
39+
$productParams = new ClassificationParameters($this->classificationModelId);
40+
$response = $this->client->enqueueAndGetResult(ClassificationResponse::class, $inputSource, $productParams);
41+
42+
$this->assertNotNull($response);
43+
$this->assertNotNull($response->inference);
44+
45+
$file = $response->inference->file;
46+
$this->assertNotNull($file);
47+
$this->assertSame("default_invoice.jpg", $file->name);
48+
49+
$result = $response->inference->result;
50+
$this->assertNotNull($result);
51+
52+
$classifications = $result->classification;
53+
$this->assertNotNull($classifications);
54+
}
55+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace V2\Product;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use TestingUtilities;
7+
use Mindee\V2\Product\Classification\ClassificationResponse;
8+
9+
require_once(__DIR__ . "/../../TestingUtilities.php");
10+
11+
/**
12+
* Classification unit tests.
13+
*/
14+
class ClassificationTest extends TestCase
15+
{
16+
/**
17+
* Load a JSON sample and return its decoded contents.
18+
*
19+
* @return array Decoded JSON data.
20+
*/
21+
private static function getInference(): array
22+
{
23+
$fullPath = TestingUtilities::getV2ProductDir() . "/classification/classification_single.json";
24+
$content = file_get_contents($fullPath);
25+
return json_decode($content, true);
26+
}
27+
28+
/**
29+
* Helper to assert the core inference response properties exist.
30+
* @param mixed $response The response object to check.
31+
* @return void
32+
*/
33+
private function assertInferenceResponse(mixed $response): void
34+
{
35+
$this->assertNotNull($response->inference);
36+
$this->assertNotNull($response->inference->id);
37+
$this->assertNotNull($response->inference->file);
38+
$this->assertNotNull($response->inference->result);
39+
}
40+
41+
/**
42+
* Should correctly map properties when reading a single classification JSON.
43+
* @return void
44+
*/
45+
public function testClassificationWhenSingleMustHaveValidProperties(): void
46+
{
47+
$jsonSample = self::getInference();
48+
49+
$response = new ClassificationResponse($jsonSample);
50+
51+
$this->assertInferenceResponse($response);
52+
53+
$inference = $response->inference;
54+
55+
$this->assertSame("12345678-1234-1234-1234-123456789abc", $inference->id);
56+
$this->assertSame("test-model-id", $inference->model->id);
57+
$this->assertSame("12345678-1234-1234-1234-jobid1234567", $inference->job->id);
58+
59+
$this->assertSame("default_sample.jpg", $inference->file->name);
60+
$this->assertSame(1, $inference->file->pageCount);
61+
$this->assertSame("image/jpeg", $inference->file->mimeType);
62+
63+
$classification = $inference->result->classification;
64+
$this->assertSame("invoice", $classification->documentType);
65+
}
66+
}

tests/V2/Product/CropTest.php

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<?php
2+
3+
4+
namespace V2\Product;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use TestingUtilities;
8+
use Mindee\V2\Product\Crop\CropResponse;
9+
use Mindee\Geometry\Point;
10+
11+
// Added for the polygon coordinate assertions
12+
13+
require_once(__DIR__ . "/../../TestingUtilities.php");
14+
15+
/**
16+
* Crop unit tests.
17+
*/
18+
class CropTest extends TestCase
19+
{
20+
/**
21+
* Load a JSON sample and return its decoded contents.
22+
*
23+
* @param string $path Path to the JSON file to load relative to the product dir.
24+
* @return array Decoded JSON data.
25+
*/
26+
private static function getInference(string $path): array
27+
{
28+
$fullPath = TestingUtilities::getV2ProductDir() . "/" . $path;
29+
$content = file_get_contents($fullPath);
30+
return json_decode($content, true);
31+
}
32+
33+
/**
34+
* Helper to assert the core inference response properties exist.
35+
* @param mixed $response The response object to check.
36+
* @return void
37+
*/
38+
private function assertInferenceResponse(mixed $response): void
39+
{
40+
$this->assertNotNull($response->inference);
41+
$this->assertNotNull($response->inference->id);
42+
$this->assertNotNull($response->inference->file);
43+
$this->assertNotNull($response->inference->result);
44+
}
45+
46+
/**
47+
* Ensures all line endings are identical before comparison so the test
48+
* behaves the same on every platform (LF vs CRLF).
49+
* @param string $input Input string to normalize.
50+
* @return string
51+
*/
52+
private static function normalizeLineEndings(string $input): string
53+
{
54+
return str_replace(["\r\n", "\r"], "\n", $input);
55+
}
56+
57+
/**
58+
* Should correctly map properties when reading a single crop JSON.
59+
* @return void
60+
*/
61+
public function testCropWhenSingleMustHaveValidProperties(): void
62+
{
63+
$jsonSample = self::getInference("crop/crop_single.json");
64+
$response = new CropResponse($jsonSample);
65+
66+
$this->assertInferenceResponse($response);
67+
68+
$inference = $response->inference;
69+
70+
$this->assertSame("12345678-1234-1234-1234-123456789abc", $inference->id);
71+
$this->assertSame("test-model-id", $inference->model->id);
72+
$this->assertSame("12345678-1234-1234-1234-jobid1234567", $inference->job->id);
73+
74+
$this->assertSame("sample.jpeg", $inference->file->name);
75+
$this->assertSame(1, $inference->file->pageCount);
76+
$this->assertSame("image/jpeg", $inference->file->mimeType);
77+
78+
$crops = $inference->result->crops;
79+
$this->assertNotNull($crops);
80+
$this->assertCount(1, $crops);
81+
82+
$firstCrop = $crops[0];
83+
$this->assertSame("invoice", $firstCrop->objectType);
84+
$this->assertSame(0, $firstCrop->location->page);
85+
86+
$polygon = $firstCrop->location->polygon;
87+
$this->assertCount(4, $polygon->getCoordinates());
88+
89+
// Note: Using assertEquals here instead of assertSame to allow for object value comparison
90+
$this->assertEquals(new Point(0.15, 0.254), $polygon->getCoordinates()[0]);
91+
$this->assertEquals(new Point(0.85, 0.254), $polygon->getCoordinates()[1]);
92+
$this->assertEquals(new Point(0.85, 0.947), $polygon->getCoordinates()[2]);
93+
$this->assertEquals(new Point(0.15, 0.947), $polygon->getCoordinates()[3]);
94+
}
95+
96+
/**
97+
* Should correctly map properties when reading a multiple crop JSON.
98+
* @return void
99+
*/
100+
public function testCropWhenMultipleMustHaveValidProperties(): void
101+
{
102+
$jsonSample = self::getInference("crop/crop_multiple.json");
103+
$response = new CropResponse($jsonSample);
104+
105+
$this->assertInferenceResponse($response);
106+
107+
$inference = $response->inference;
108+
109+
$job = $inference->job;
110+
$this->assertSame("12345678-1234-1234-1234-jobid1234567", $job->id);
111+
112+
$this->assertSame("12345678-1234-1234-1234-123456789abc", $inference->id);
113+
$this->assertSame("test-model-id", $inference->model->id);
114+
115+
$this->assertSame("default_sample.jpg", $inference->file->name);
116+
$this->assertSame(1, $inference->file->pageCount);
117+
$this->assertSame("image/jpeg", $inference->file->mimeType);
118+
119+
$crops = $inference->result->crops;
120+
$this->assertNotNull($crops);
121+
$this->assertCount(2, $crops);
122+
123+
$firstCrop = $crops[0];
124+
$this->assertSame("invoice", $firstCrop->objectType);
125+
$this->assertSame(0, $firstCrop->location->page);
126+
127+
$firstPolygon = $firstCrop->location->polygon;
128+
$this->assertCount(4, $firstPolygon->getCoordinates());
129+
$this->assertEquals(new Point(0.214, 0.079), $firstPolygon->getCoordinates()[0]);
130+
$this->assertEquals(new Point(0.476, 0.079), $firstPolygon->getCoordinates()[1]);
131+
$this->assertEquals(new Point(0.476, 0.979), $firstPolygon->getCoordinates()[2]);
132+
$this->assertEquals(new Point(0.214, 0.979), $firstPolygon->getCoordinates()[3]);
133+
134+
$secondCrop = $crops[1];
135+
$this->assertSame("receipt", $secondCrop->objectType);
136+
$this->assertSame(0, $secondCrop->location->page);
137+
138+
$secondPolygon = $secondCrop->location->polygon;
139+
$this->assertCount(4, $secondPolygon->getCoordinates());
140+
$this->assertEquals(new Point(0.547, 0.15), $secondPolygon->getCoordinates()[0]);
141+
$this->assertEquals(new Point(0.862, 0.15), $secondPolygon->getCoordinates()[1]);
142+
$this->assertEquals(new Point(0.862, 0.97), $secondPolygon->getCoordinates()[2]);
143+
$this->assertEquals(new Point(0.547, 0.97), $secondPolygon->getCoordinates()[3]);
144+
}
145+
146+
/**
147+
* crop_single.rst – RST display must be parsed and exposed
148+
* @return void
149+
*/
150+
public function testRstDisplayMustBeAccessible(): void
151+
{
152+
$jsonSample = self::getInference("crop/crop_single.json");
153+
$response = new CropResponse($jsonSample);
154+
155+
$rstReferencePath = TestingUtilities::getV2ProductDir() . "/crop/crop_single.rst";
156+
$rstReference = file_get_contents($rstReferencePath);
157+
158+
$inference = $response->inference;
159+
$this->assertNotNull($inference);
160+
161+
// Assumes your Inference class implements the __toString() magic method
162+
// which maps to C#'s ToString()
163+
$this->assertEquals(
164+
self::normalizeLineEndings($rstReference),
165+
self::normalizeLineEndings((string)$inference)
166+
);
167+
}
168+
}

0 commit comments

Comments
 (0)