Skip to content

Commit 88a1c42

Browse files
committed
test: add Account tests
1 parent 118e4af commit 88a1c42

File tree

3 files changed

+153
-21
lines changed

3 files changed

+153
-21
lines changed

src/Api/Account.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,65 +13,73 @@ public function generate(): array
1313
]);
1414
}
1515

16-
public function address(string $publicKey): array
16+
public function address(string $publicKey): string
1717
{
1818
return $this->get(self::API_PATH, [
1919
'q' => 'getAddress',
2020
'public_key' => $publicKey,
2121
]);
2222
}
2323

24-
public function alias(string $address): array
24+
public function alias(string $address): string
2525
{
2626
return $this->get(self::API_PATH, [
2727
'q' => 'getAlias',
2828
'account' => $address,
2929
]);
3030
}
3131

32-
public function publicKey(string $address): array
32+
public function publicKey(string $address): string
3333
{
3434
return $this->get(self::API_PATH, [
3535
'q' => 'getPublicKey',
3636
'account' => $address,
3737
]);
3838
}
3939

40-
public function balance(string $address): array
40+
public function balance(string $address): float
4141
{
42-
return $this->get(self::API_PATH, [
42+
return (float) $this->get(self::API_PATH, [
4343
'q' => 'getBalance',
4444
'account' => $address,
4545
]);
4646
}
4747

48-
public function balanceByAlias(string $alias): array
48+
public function balanceByAlias(string $alias): float
4949
{
50-
return $this->get(self::API_PATH, [
50+
return (float) $this->get(self::API_PATH, [
5151
'q' => 'getBalance',
5252
'alias' => $alias,
5353
]);
5454
}
5555

56-
public function balanceByPublicKey(string $publicKey): array
56+
public function balanceForPublicKey(string $publicKey): float
5757
{
58-
return $this->get(self::API_PATH, [
58+
return (float) $this->get(self::API_PATH, [
5959
'q' => 'getBalance',
6060
'public_key' => $publicKey,
6161
]);
6262
}
6363

64-
public function pendingBalance(string $address): array
64+
public function pendingBalance(string $address): float
6565
{
66-
return $this->get(self::API_PATH, [
66+
return (float) $this->get(self::API_PATH, [
6767
'q' => 'getPendingBalance',
6868
'account' => $address,
6969
]);
7070
}
7171

72-
public function checkAddress(string $address, ?string $publicKey = null): array
72+
public function pendingBalanceForPublicKey(string $publicKey): float
7373
{
74-
return $this->get(self::API_PATH, [
74+
return (float) $this->get(self::API_PATH, [
75+
'q' => 'getPendingBalance',
76+
'public_key' => $publicKey,
77+
]);
78+
}
79+
80+
public function checkAddress(string $address, ?string $publicKey = null): bool
81+
{
82+
return (bool) $this->get(self::API_PATH, [
7583
'q' => 'checkAddress',
7684
'account' => $address,
7785
'public_key' => $publicKey,

src/HttpClient/Message/ResponseMediator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace OwenVoke\Arionum\HttpClient\Message;
44

5+
use OwenVoke\Arionum\Exception\RuntimeException;
56
use Psr\Http\Message\ResponseInterface;
67

78
final class ResponseMediator
@@ -13,7 +14,7 @@ public static function getContent(ResponseInterface $response): array|string
1314
if (str_starts_with($response->getHeaderLine('Content-Type'), 'application/json')) {
1415
$content = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
1516
if (JSON_ERROR_NONE === json_last_error()) {
16-
return $content;
17+
return $content['data'] ?? throw new RuntimeException("Error: Field \"data\" was not set", $content);
1718
}
1819
}
1920

tests/Api/AccountTest.php

Lines changed: 130 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,139 @@
2828
expect($api->generate())->toBe($response);
2929
});
3030

31-
it('can get an address by public key');
31+
it('can get an address by public key', function () {
32+
$api = $this->getApiMock();
33+
34+
$api->expects($this->once())
35+
->method('get')
36+
->with('/api.php', [
37+
'q' => 'getAddress',
38+
'public_key' => 'PZ8Tyr4Nx8MHsRAGMpZmZ6TWY63dXWSCyPP8v9FpH75La76KNgkUpZ2KiMHxHQsUKFytyEMXFPkh3yC25p5JoiR1dEsTgJJkNSrrkTM96BcMae5h6NeSdrH1',
39+
])
40+
->willReturn('2tR6BWvwpwrQLUWN8GpVP4b6srCxgR2PnrsXs7jYfdeBj3FimJ5Tjd4xzWcWe8y59ZtEBXgQJxe6Uibux1cCDfxL');
41+
42+
/** @var Account $api */
43+
expect(
44+
$api->address('PZ8Tyr4Nx8MHsRAGMpZmZ6TWY63dXWSCyPP8v9FpH75La76KNgkUpZ2KiMHxHQsUKFytyEMXFPkh3yC25p5JoiR1dEsTgJJkNSrrkTM96BcMae5h6NeSdrH1')
45+
)->toBe('2tR6BWvwpwrQLUWN8GpVP4b6srCxgR2PnrsXs7jYfdeBj3FimJ5Tjd4xzWcWe8y59ZtEBXgQJxe6Uibux1cCDfxL');
46+
});
47+
48+
it('can get the balance for an address', function () {
49+
$api = $this->getApiMock();
50+
51+
$api->expects($this->once())
52+
->method('get')
53+
->with('/api.php', [
54+
'q' => 'getBalance',
55+
'account' => '2tR6BWvwpwrQLUWN8GpVP4b6srCxgR2PnrsXs7jYfdeBj3FimJ5Tjd4xzWcWe8y59ZtEBXgQJxe6Uibux1cCDfxL',
56+
])
57+
->willReturn('0');
58+
59+
/** @var Account $api */
60+
expect(
61+
$api->balance('2tR6BWvwpwrQLUWN8GpVP4b6srCxgR2PnrsXs7jYfdeBj3FimJ5Tjd4xzWcWe8y59ZtEBXgQJxe6Uibux1cCDfxL')
62+
)->toBe(0.0);
63+
});
64+
65+
it('can get the balance for an alias', function () {
66+
$api = $this->getApiMock();
67+
68+
$api->expects($this->once())
69+
->method('get')
70+
->with('/api.php', [
71+
'q' => 'getBalance',
72+
'alias' => 'dev',
73+
])
74+
->willReturn('0');
75+
76+
/** @var Account $api */
77+
expect(
78+
$api->balanceByAlias('dev')
79+
)->toBe(0.0);
80+
});
81+
82+
it('can get the balance for a public key', function () {
83+
$api = $this->getApiMock();
84+
85+
$api->expects($this->once())
86+
->method('get')
87+
->with('/api.php', [
88+
'q' => 'getBalance',
89+
'public_key' => 'PZ8Tyr4Nx8MHsRAGMpZmZ6TWY63dXWSCyPP8v9FpH75La76KNgkUpZ2KiMHxHQsUKFytyEMXFPkh3yC25p5JoiR1dEsTgJJkNSrrkTM96BcMae5h6NeSdrH1',
90+
])
91+
->willReturn('0');
3292

33-
it('can get the balance for an address');
93+
/** @var Account $api */
94+
expect(
95+
$api->balanceForPublicKey('PZ8Tyr4Nx8MHsRAGMpZmZ6TWY63dXWSCyPP8v9FpH75La76KNgkUpZ2KiMHxHQsUKFytyEMXFPkh3yC25p5JoiR1dEsTgJJkNSrrkTM96BcMae5h6NeSdrH1')
96+
)->toBe(0.0);
97+
});
98+
99+
it('can get the pending balance for an address', function () {
100+
$api = $this->getApiMock();
101+
102+
$api->expects($this->once())
103+
->method('get')
104+
->with('/api.php', [
105+
'q' => 'getPendingBalance',
106+
'account' => '2tR6BWvwpwrQLUWN8GpVP4b6srCxgR2PnrsXs7jYfdeBj3FimJ5Tjd4xzWcWe8y59ZtEBXgQJxe6Uibux1cCDfxL',
107+
])
108+
->willReturn('0');
109+
110+
/** @var Account $api */
111+
expect(
112+
$api->pendingBalance('2tR6BWvwpwrQLUWN8GpVP4b6srCxgR2PnrsXs7jYfdeBj3FimJ5Tjd4xzWcWe8y59ZtEBXgQJxe6Uibux1cCDfxL')
113+
)->toBe(0.0);
114+
});
115+
116+
it('can get the pending balance for a public key', function () {
117+
$api = $this->getApiMock();
118+
119+
$api->expects($this->once())
120+
->method('get')
121+
->with('/api.php', [
122+
'q' => 'getPendingBalance',
123+
'public_key' => 'PZ8Tyr4Nx8MHsRAGMpZmZ6TWY63dXWSCyPP8v9FpH75La76KNgkUpZ2KiMHxHQsUKFytyEMXFPkh3yC25p5JoiR1dEsTgJJkNSrrkTM96BcMae5h6NeSdrH1',
124+
])
125+
->willReturn('0');
34126

35-
it('can get the balance for an alias');
127+
/** @var Account $api */
128+
expect(
129+
$api->pendingBalanceForPublicKey('PZ8Tyr4Nx8MHsRAGMpZmZ6TWY63dXWSCyPP8v9FpH75La76KNgkUpZ2KiMHxHQsUKFytyEMXFPkh3yC25p5JoiR1dEsTgJJkNSrrkTM96BcMae5h6NeSdrH1')
130+
)->toBe(0.0);
131+
});
36132

37-
it('can get the balance for a public key');
133+
it('can get the alias for an address', function () {
134+
$api = $this->getApiMock();
38135

39-
it('can get the pending balance for a public key');
136+
$api->expects($this->once())
137+
->method('get')
138+
->with('/api.php', [
139+
'q' => 'getAlias',
140+
'account' => '2tR6BWvwpwrQLUWN8GpVP4b6srCxgR2PnrsXs7jYfdeBj3FimJ5Tjd4xzWcWe8y59ZtEBXgQJxe6Uibux1cCDfxL',
141+
])
142+
->willReturn('dev');
40143

41-
it('can get the alias for an address');
144+
/** @var Account $api */
145+
expect(
146+
$api->alias('2tR6BWvwpwrQLUWN8GpVP4b6srCxgR2PnrsXs7jYfdeBj3FimJ5Tjd4xzWcWe8y59ZtEBXgQJxe6Uibux1cCDfxL')
147+
)->toBe('dev');
148+
});
42149

43-
it('can get check that an address is valid');
150+
it('can get check that an address is valid', function () {
151+
$api = $this->getApiMock();
152+
153+
$api->expects($this->once())
154+
->method('get')
155+
->with('/api.php', [
156+
'q' => 'checkAddress',
157+
'public_key' => null,
158+
'account' => '2tR6BWvwpwrQLUWN8GpVP4b6srCxgR2PnrsXs7jYfdeBj3FimJ5Tjd4xzWcWe8y59ZtEBXgQJxe6Uibux1cCDfxL',
159+
])
160+
->willReturn('true');
161+
162+
/** @var Account $api */
163+
expect(
164+
$api->checkAddress('2tR6BWvwpwrQLUWN8GpVP4b6srCxgR2PnrsXs7jYfdeBj3FimJ5Tjd4xzWcWe8y59ZtEBXgQJxe6Uibux1cCDfxL')
165+
)->toBe(true);
166+
});

0 commit comments

Comments
 (0)