@@ -40,26 +40,44 @@ public function testAsyncPredictWhenEmptyMustHaveValidProperties(): void
4040
4141 $ this ->assertCount (21 , $ fields , 'Expected 21 fields ' );
4242
43- $ taxes = $ fields ->get ('taxes ' );
44- $ this ->assertNotNull ($ taxes , "'taxes' field must exist " );
45- $ this ->assertInstanceOf (ListField::class, $ taxes , "'taxes' must be a ListField " );
46- $ this ->assertEmpty ($ taxes ->items , "'taxes' list must be empty " );
43+ $ this ->assertInstanceOf (
44+ SimpleField::class,
45+ $ fields ['total_amount ' ],
46+ "Field 'total_amount' must be a SimpleField "
47+ );
48+ $ totalAmount = $ fields ->getSimpleField ('total_amount ' );
49+ $ this ->assertEmpty ($ totalAmount ->value );
4750
48- $ supplierAddress = $ fields ->get ('supplier_address ' );
49- $ this ->assertNotNull ($ supplierAddress , "'supplier_address' field must exist " );
50- $ this ->assertInstanceOf (ObjectField::class, $ supplierAddress , "'supplier_address' must be an ObjectField " );
51+ $ this ->assertInstanceOf (
52+ ListField::class,
53+ $ fields ['taxes ' ],
54+ "Field 'taxes' must be a ListField "
55+ );
56+ $ taxes = $ fields ->getListField ('taxes ' );
57+ $ this ->assertEmpty ($ taxes ->items );
58+
59+ $ this ->assertInstanceOf (
60+ ObjectField::class,
61+ $ fields ['supplier_address ' ],
62+ "Field 'supplier_address' must be an ObjectField "
63+ );
64+ $ supplierAddress = $ fields ->getObjectField ('supplier_address ' );
65+ $ this ->assertCount (9 , $ supplierAddress ->fields );
5166
52- foreach ($ fields as $ key => $ value ) {
53- if ($ value === null ) {
67+ foreach ($ fields as $ fieldName => $ field ) {
68+ if ($ field === null ) {
5469 continue ;
5570 }
56-
57- if ($ value instanceof ListField) {
58- $ this ->assertInstanceOf (ListField::class, $ value , "$ key – ListField expected " );
59- } elseif ($ value instanceof ObjectField) {
60- $ this ->assertInstanceOf (ObjectField::class, $ value , "$ key – ObjectField expected " );
71+ if ($ field instanceof ListField) {
72+ $ this ->assertEmpty ($ field ->items , "Field $ fieldName.items must be empty " );
73+ } elseif ($ field instanceof ObjectField) {
74+ foreach ($ field ->fields as $ subFieldName => $ subField ) {
75+ $ this ->assertEmpty ($ subField ->value , "Field $ fieldName. $ subFieldName must be empty " );
76+ }
77+ } elseif ($ field instanceof SimpleField) {
78+ $ this ->assertIsNotObject ($ field ->value , "Field $ fieldName must be a scalar value " );
6179 } else {
62- $ this ->assertInstanceOf (SimpleField::class, $ value , " $ key – SimpleField expected " );
80+ $ this ->fail ( " Unknown field type: $ fieldName " );
6381 }
6482 }
6583 }
@@ -93,7 +111,7 @@ public function testAsyncPredictWhenCompleteMustExposeAllProperties(): void
93111 $ this ->assertInstanceOf (SimpleField::class, $ date );
94112 $ this ->assertEquals ('2019-11-02 ' , $ date ->value , "'date' value mismatch " );
95113
96- $ taxes = $ fields ->get ('taxes ' );
114+ $ taxes = $ fields ->getListField ('taxes ' );
97115 $ this ->assertNotNull ($ taxes , "'taxes' field must exist " );
98116 $ this ->assertInstanceOf (ListField::class, $ taxes , "'taxes' must be a ListField " );
99117 $ this ->assertCount (1 , $ taxes ->items , "'taxes' list must contain exactly one item " );
@@ -107,7 +125,7 @@ public function testAsyncPredictWhenCompleteMustExposeAllProperties(): void
107125 $ this ->assertEquals (31.5 , $ baseTax ->value , "'taxes.base' value mismatch " );
108126 $ this ->assertNotNull (strval ($ taxes ), "'taxes'.__toString() must not be null " );
109127
110- $ supplierAddress = $ fields ->get ('supplier_address ' );
128+ $ supplierAddress = $ fields ->getObjectField ('supplier_address ' );
111129 $ this ->assertNotNull ($ supplierAddress , "'supplier_address' field must exist " );
112130 $ this ->assertInstanceOf (ObjectField::class, $ supplierAddress , "'supplier_address' must be an ObjectField " );
113131
@@ -270,45 +288,46 @@ public function testRawTextsMustBeAccessible(): void
270288 */
271289 public function testRstDisplayMustBeAccessible (): void
272290 {
273- $ resp = $ this ->loadFromResource ('v2/inference/standard_field_types.json ' );
274- $ rstRef = $ this ->readFileAsString ('v2/inference/standard_field_types.rst ' );
275- $ inf = $ resp ->inference ;
276- $ this ->assertNotNull ($ inf );
277- $ this ->assertEquals ($ rstRef , strval ($ resp ->inference ));
291+ $ response = $ this ->loadFromResource ('v2/inference/standard_field_types.json ' );
292+ $ expectedRst = $ this ->readFileAsString ('v2/inference/standard_field_types.rst ' );
293+ $ inference = $ response ->inference ;
294+ $ this ->assertNotNull ($ inference );
295+ $ this ->assertEquals ($ expectedRst , strval ($ response ->inference ));
278296 }
279297
280298 /**
281299 * Coordinates & location data must be parsed and exposed.
282300 */
283301 public function testCoordinatesAndLocationDataMustBeAccessible (): void
284302 {
285- $ resp = $ this ->loadFromResource ('v2/products/financial_document/complete_with_coordinates.json ' );
286- $ inf = $ resp ->inference ;
287- $ this ->assertNotNull ($ inf );
288- $ this ->assertNotNull ($ inf ->result ->fields ->get ('date ' )->locations );
289- $ this ->assertNotNull ($ inf ->result ->fields ->get ('date ' )->locations [0 ]);
290- $ this ->assertEquals (0 , $ inf ->result ->fields ->get ('date ' )->locations [0 ]->page );
303+ $ response = $ this ->loadFromResource ('v2/products/financial_document/complete_with_coordinates.json ' );
304+ $ inference = $ response ->inference ;
305+ $ this ->assertNotNull ($ inference );
306+ $ dateField = $ inference ->result ->fields ->getSimpleField ('date ' );
307+ $ this ->assertNotNull ($ dateField ->locations );
308+ $ this ->assertNotNull ($ dateField ->locations [0 ]);
309+ $ this ->assertEquals (0 , $ dateField ->locations [0 ]->page );
291310 $ this ->assertEquals (
292311 0.948979073166918 ,
293- $ inf -> result -> fields -> get ( ' date ' )-> locations [0 ]->polygon ->coordinates [0 ][ 0 ]
312+ $ dateField -> locations [0 ]->polygon ->coordinates [0 ]-> x
294313 );
295314 $ this ->assertEquals (
296315 0.23097924535067715 ,
297- $ inf -> result -> fields -> get ( ' date ' ) ->locations [0 ]->polygon ->coordinates [0 ][1 ]
316+ $ dateField ->locations [0 ]->polygon ->coordinates [0 ][1 ]
298317 );
299- $ this ->assertEquals (0.85422 , $ inf -> result -> fields -> get ( ' date ' ) ->locations [0 ]->polygon ->coordinates [1 ][0 ]);
300- $ this ->assertEquals (0.230072 , $ inf -> result -> fields -> get ( ' date ' ) ->locations [0 ]->polygon ->coordinates [1 ][1 ]);
318+ $ this ->assertEquals (0.85422 , $ dateField ->locations [0 ]->polygon ->coordinates [1 ][0 ]);
319+ $ this ->assertEquals (0.230072 , $ dateField ->locations [0 ]->polygon ->coordinates [1 ][1 ]);
301320 $ this ->assertEquals (
302321 0.8540899268330819 ,
303- $ inf -> result -> fields -> get ( ' date ' ) ->locations [0 ]->polygon ->coordinates [2 ][0 ]
322+ $ dateField ->locations [0 ]->polygon ->coordinates [2 ][0 ]
304323 );
305324 $ this ->assertEquals (
306325 0.24365775464932288 ,
307- $ inf -> result -> fields -> get ( ' date ' ) ->locations [0 ]->polygon ->coordinates [2 ][1 ]
326+ $ dateField ->locations [0 ]->polygon ->coordinates [2 ][1 ]
308327 );
309- $ this ->assertEquals (0.948849 , $ inf -> result -> fields -> get ( ' date ' ) ->locations [0 ]->polygon ->coordinates [3 ][0 ]);
310- $ this ->assertEquals (0.244565 , $ inf -> result -> fields -> get ( ' date ' ) ->locations [0 ]->polygon ->coordinates [3 ][1 ]);
311- $ this ->assertEquals (FieldConfidence::MEDIUM , $ inf -> result -> fields -> get ( ' date ' ) ->confidence );
312- $ this ->assertEquals (" Medium " , $ inf -> result -> fields -> get ( ' date ' ) ->confidence ->getValue ());
328+ $ this ->assertEquals (0.948849 , $ dateField ->locations [0 ]->polygon ->coordinates [3 ][0 ]);
329+ $ this ->assertEquals (0.244565 , $ dateField ->locations [0 ]->polygon ->coordinates [3 ][1 ]);
330+ $ this ->assertEquals (FieldConfidence::MEDIUM , $ dateField ->confidence );
331+ $ this ->assertEquals (' Medium ' , $ dateField ->confidence ->getValue ());
313332 }
314333}
0 commit comments