-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLocalResponseV2Test.php
More file actions
56 lines (48 loc) · 2.3 KB
/
LocalResponseV2Test.php
File metadata and controls
56 lines (48 loc) · 2.3 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
<?php
namespace V2\Input;
use Mindee\Input\LocalResponse;
use Mindee\Parsing\V2\InferenceResponse;
use PHPUnit\Framework\TestCase;
class LocalResponseV2Test extends TestCase
{
private string $filePath;
protected function setUp(): void
{
$this->filePath = \TestingUtilities::getV2DataDir() . '/inference/standard_field_types.json';
}
protected function assertLocalResponse(LocalResponse $localResponse): void
{
$fakeHMACSigning = "ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH";
$signature = "b82a515c832fd2c4f4ce3a7e6f53c12e8d10e19223f6cf0e3a9809a7a3da26be";
$reflectedLocalResponse = new \ReflectionClass($localResponse);
$reflectedFile = $reflectedLocalResponse->getProperty('file');
$reflectedFile->setAccessible(true);
$this->assertNotNull($reflectedFile);
$this->assertFalse($localResponse->isValidHMACSignature($fakeHMACSigning, "fake HMAC signature"));
$this->assertEquals($signature, $localResponse->getHmacSignature($fakeHMACSigning));
$this->assertTrue($localResponse->isValidHMACSignature($fakeHMACSigning, $signature));
$response = $localResponse->deserializeResponse(InferenceResponse::class);
$this->assertInstanceOf(InferenceResponse::class, $response);
$this->assertNotNull($response->inference);
$this->assertNotNull($response->inference->result);
$this->assertNotNull($response->inference->result->fields);
}
public function testValidFileLocalResponse(){
$file = fopen($this->filePath, 'rb');
$localResponse = new LocalResponse($file);
fclose($file);
$this->assertLocalResponse($localResponse);
}
public function testValidPathLocalResponse(){
$localResponse = new LocalResponse($this->filePath);
$this->assertLocalResponse($localResponse);
}
public function testValidBytesLocalResponse(){
$raw = file_get_contents($this->filePath);
$encoding = mb_detect_encoding($raw, ['UTF-8','UTF-16','UTF-32','ISO-8859-1','Windows-1252'], true) ?: 'UTF-8';
$utf8 = ($encoding === 'UTF-8') ? $raw : mb_convert_encoding($raw, 'UTF-8', $encoding);
$utf8 = preg_replace('/^\xEF\xBB\xBF/', '', $utf8);
$localResponse = new LocalResponse($utf8);
$this->assertLocalResponse($localResponse);
}
}