|
| 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