diff --git a/src/Parsing/V2/Field/SimpleField.php b/src/Parsing/V2/Field/SimpleField.php index d7d2b521..f5177f2b 100644 --- a/src/Parsing/V2/Field/SimpleField.php +++ b/src/Parsing/V2/Field/SimpleField.php @@ -8,7 +8,7 @@ class SimpleField extends BaseField { /** - * @var string | integer | float | boolean | null Value contained in the field. + * @var string|float|boolean|null Value contained in the field. */ public $value; @@ -20,6 +20,9 @@ public function __construct(array $serverResponse, int $indentLevel = 0) { parent::__construct($serverResponse, $indentLevel); $this->value = array_key_exists('value', $serverResponse) ? $serverResponse['value'] : null; + if (is_int($this->value)) { + $this->value = (float) $this->value; + } } /** @@ -31,12 +34,8 @@ public function __toString(): string return $this->value ? 'True' : 'False'; } if (is_numeric($this->value)) { - $value = (float)$this->value; - return $value == (int)$value ? - number_format($value, 1, '.', '') : - rtrim(rtrim(number_format($value, 5, '.', ''), '0'), '.'); + return number_format($this->value, 1, '.', ''); } - return $this->value !== null ? (string)$this->value : ''; } } diff --git a/tests/Input/URLInputSourceTest.php b/tests/Input/URLInputSourceTest.php index ed453052..6d42ab03 100644 --- a/tests/Input/URLInputSourceTest.php +++ b/tests/Input/URLInputSourceTest.php @@ -23,20 +23,24 @@ protected function setUp(): void getenv('GITHUB_WORKSPACE') ?: "." ) . "/tests/resources/file_types/"; } + protected function tearDown(): void { putenv('MINDEE_API_KEY=' . $this->oldKey); } + public function testInputFromHTTPShouldNotThrow() { $inputDoc = $this->dummyClient->sourceFromUrl("https://example.com/invoice.pdf"); $this->assertInstanceOf(URLInputSource::class, $inputDoc); } + public function testInputFromHTTPShouldThrow() { $this->expectException(MindeeSourceException::class); - $this->dummyClient->sourceFromUrl("http://example.com/invoice.pdf"); + new URLInputSource(url: "http://example.com/invoice.pdf"); } + public function testDownloadFileFails(){ $dummyAddress = "addressthatdoesntworkforcipurposes"; $urlSource = $this->dummyClient->sourceFromUrl("https://$dummyAddress"); @@ -44,6 +48,7 @@ public function testDownloadFileFails(){ $this->expectExceptionMessage("Failed to download file: Could not resolve host: $dummyAddress"); $urlSource->asLocalInputSource("test.pdf"); } + public function testInvalidFileName(){ $urlSource = $this->dummyClient->sourceFromUrl("https://addressthatdoesntworkforcipurposes"); $this->expectException(MindeeSourceException::class); diff --git a/tests/Parsing/V2/InferenceTest.php b/tests/Parsing/V2/InferenceTest.php index 57cb51b2..e6b16f34 100644 --- a/tests/Parsing/V2/InferenceTest.php +++ b/tests/Parsing/V2/InferenceTest.php @@ -282,6 +282,10 @@ public function testRawTextsMustBeAccessible(): void $first = $rawText->pages[0]; $this->assertEquals('This is the raw text of the first page...', $first->content); + + foreach ($rawText->pages as $page) { + $this->assertEquals('string', gettype($page->content)); + } } /** @@ -305,7 +309,9 @@ public function testCoordinatesAndLocationDataMustBeAccessible(): void $inference = $response->inference; $this->assertNotNull($inference); - $dateField = $inference->result->fields->getSimpleField('date'); + $fields = $response->inference->result->fields; + + $dateField = $fields->getSimpleField('date'); $this->assertCount(1, $dateField->locations); $location = $dateField->locations[0];