-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathUtilsTest.php
More file actions
103 lines (81 loc) · 2.58 KB
/
UtilsTest.php
File metadata and controls
103 lines (81 loc) · 2.58 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
declare(strict_types=1);
namespace Sysix\LexOffice\Tests;
use GuzzleHttp\Psr7\Response;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Sysix\LexOffice\Utils;
final class UtilsTest extends TestCase
{
public function testGetJsonFromResponseWithoutAnHeader(): void
{
$response = new Response();
$json = Utils::getJsonFromResponse($response);
$this->assertNull($json);
}
public function testGetJsonFromResponseWithInvalidHeader(): void
{
$response = new Response(200, [
'Content-Type' => 'application/xml'
]);
$json = Utils::getJsonFromResponse($response);
$this->assertNull($json);
}
public function testGetJsonFromResponseWithValidHeader(): void
{
$response = new Response(200, [
'Content-Type' => 'application/json'
], (string) json_encode([
'test' => true
]));
/** @var object $json */
$json = Utils::getJsonFromResponse($response);
$this->assertEquals((object)[
'test' => true
], $json);
}
public function testGetJsonFromResponseWithValidCharsetHeader(): void
{
$response = new Response(200, [
'Content-Type' => 'application/json; charset=utf-8'
], (string) json_encode([
'test' => true
]));
/** @var object $json */
$json = Utils::getJsonFromResponse($response);
$this->assertEquals((object)[
'test' => true
], $json);
}
public function testJsonDecodeValid(): void
{
/** @var object $json */
$json = Utils::jsonDecode('{"content":"test","object":{"content":"test2"}}');
$this->assertEquals($json, (object)[
'content' => 'test',
'object' => (object) [
'content' => 'test2'
]
]);
}
public function testJsonDecodeInValid(): void
{
$this->expectException(InvalidArgumentException::class);
Utils::jsonDecode('{content:"test"}'); // missing quotes for key
}
public function testJsonEncodeValid(): void
{
$jsonString = Utils::jsonEncode((object)[
'content' => 'test',
'object' => (object) [
'content' => 'test2'
]
]);
$this->assertEquals('{"content":"test","object":{"content":"test2"}}', $jsonString);
}
public function testJsonEncodeInValid(): void
{
$this->expectException(InvalidArgumentException::class);
Utils::jsonEncode(fopen('php://temp', 'r'));
}
}