This repository was archived by the owner on May 17, 2025. It is now read-only.
forked from bunq/sdk_php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonParserTest.php
More file actions
142 lines (128 loc) · 4.88 KB
/
Copy pathJsonParserTest.php
File metadata and controls
142 lines (128 loc) · 4.88 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace bunq\test;
use bunq\Http\BunqResponseRaw;
use bunq\Model\Core\BunqModel;
use bunq\Model\Core\BunqResponseInstallation;
use bunq\Model\Core\Installation;
use bunq\Model\Generated\Endpoint\BunqResponseInt;
use bunq\Model\Generated\Endpoint\BunqResponseString;
use bunq\Model\Generated\Endpoint\BunqResponseUserCompany;
use bunq\Model\Generated\Endpoint\UserCompany;
use bunq\Model\Generated\Object\Amount;
use bunq\Util\FileUtil;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
/**
* @author Gerben Oolbekkink <gerben@bunq.com>
* @since 20170630 Initial creation.
*/
class JsonParserTest extends TestCase
{
/**
* Test constants.
*/
const JSON_ID = '{"Response": [{"Id": {"id": 37}}]}';
const EXPECTED_ID = 37;
const JSON_UUID = '{"Response": [{"Uuid": {"uuid": "5a442b1d-3d43-4285-b532-dbb21055824a"}}]}';
const EXPECTED_UUID = '5a442b1d-3d43-4285-b532-dbb21055824a';
const EXPECTED_TOKEN = 'a4f9d888eea84f52722b9baf2f17c289d549edf6e0eccdbf868eb922be306fb6';
const EXPECTED_PUBLIC_KEY = 'My server public key';
const EXPECTED_NAME_BUNQ = 'bunq';
const EXPECTED_EMAIL_BRAVO = 'bravo@bunq.com';
const EXPECTED_INSTALLATION_ID = 26;
/**
* Resource location constants.
*/
const RESOURCE_INSTALLATION_JSON = '/Resource/installation.json';
const RESOURCE_USER_COMPANY_JSON = '/Resource/user_company.json';
/**
* Function constants.
*/
const FUNCTION_CLASS_FROM_JSON = 'classFromJson';
const FUNCTION_FROM_JSON = 'fromJson';
const FUNCTION_PROCESS_FOR_ID = 'processForId';
const FUNCTION_PROCESS_FOR_UUID = 'processForUuid';
/**
* Test creation of Id.
*/
public function testCreateIdFromJson()
{
/** @var BunqResponseInt $bunqResponseId */
$bunqResponseId = $this->callPrivateStaticMethod(
BunqModel::class,
self::FUNCTION_PROCESS_FOR_ID,
[new BunqResponseRaw(self::JSON_ID, [])]
);
$id = $bunqResponseId->getValue();
static::assertEquals(self::EXPECTED_ID, $id);
}
/**
* Call a private static method on a class.
*
* @param string $class
* @param string $method
* @param mixed[] $args
*
* @return mixed
*/
private function callPrivateStaticMethod(string $class, string $method, array $args)
{
$reflectionClass = new ReflectionClass($class);
$createFromJsonMethod = $reflectionClass->getMethod($method);
$createFromJsonMethod->setAccessible(true);
return $createFromJsonMethod->invokeArgs(null, $args);
}
/**
* Test creation of Uuid.
*/
public function testCreateUuidFromJson()
{
/** @var BunqResponseString $bunqResponseUuid */
$bunqResponseUuid = $this->callPrivateStaticMethod(
BunqModel::class,
self::FUNCTION_PROCESS_FOR_UUID,
[new BunqResponseRaw(self::JSON_UUID, [])]
);
$uuid = $bunqResponseUuid->getValue();
static::assertEquals(self::EXPECTED_UUID, $uuid);
}
/**
* Test creation of UserCompany.
*/
public function testCreateFromJson()
{
$userCompanyJson = FileUtil::getFileContents(__DIR__ . self::RESOURCE_USER_COMPANY_JSON);
/** @var BunqResponseUserCompany $bunqResponseUserCompany */
$bunqResponseUserCompany = $this->callPrivateStaticMethod(
UserCompany::class,
self::FUNCTION_FROM_JSON,
[new BunqResponseRaw($userCompanyJson, []), UserCompany::OBJECT_TYPE_GET]
);
$userCompany = $bunqResponseUserCompany->getValue();
static::assertInstanceOf(UserCompany::class, $userCompany);
static::assertEquals(self::EXPECTED_NAME_BUNQ, $userCompany->getName());
static::assertEquals(self::EXPECTED_EMAIL_BRAVO, $userCompany->getAlias()[0]->getValue());
static::assertInstanceOf(Amount::class, $userCompany->getDailyLimitWithoutConfirmationLogin());
}
/**
* Test creating a predefined class from json.
*/
public function testCreateClassFormJson()
{
$installationJson = FileUtil::getFileContents(__DIR__ . self::RESOURCE_INSTALLATION_JSON);
/** @var BunqResponseInstallation $bunqResponseInstallation */
$bunqResponseInstallation = $this->callPrivateStaticMethod(
Installation::class,
self::FUNCTION_CLASS_FROM_JSON,
[new BunqResponseRaw($installationJson, [])]
);
$installation = $bunqResponseInstallation->getValue();
static::assertInstanceOf(Installation::class, $installation);
static::assertEquals(self::EXPECTED_INSTALLATION_ID, $installation->getId()->getId());
static::assertEquals(self::EXPECTED_TOKEN, $installation->getToken()->getToken());
static::assertEquals(
self::EXPECTED_PUBLIC_KEY,
$installation->getServerPublicKey()->getServerPublicKey()->getKey()
);
}
}