Skip to content

Commit b0e83f1

Browse files
committed
Validate Register účtovných závierok API responses with nette/schema
Similar to how it's done with ARES.
1 parent 9af70cc commit b0e83f1

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

app/psalm-baseline.xml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<files psalm-version="5.26.1@d747f6500b38ac4f7dfc5edbcae6e4b637d7add0">
3-
<file src="src/CompanyInfo/CompanyRegisterRegisterUz.php">
4-
<MixedArgument>
5-
<code><![CDATA[$unit->ico]]></code>
6-
<code><![CDATA[$unit->mesto]]></code>
7-
<code><![CDATA[$unit->nazovUJ]]></code>
8-
<code><![CDATA[$unit->psc]]></code>
9-
<code><![CDATA[$unit->ulica]]></code>
10-
<code><![CDATA[$units->id]]></code>
11-
</MixedArgument>
12-
</file>
133
<file src="src/EasterEgg/WinterIsComing.php">
144
<MixedArgument>
155
<code><![CDATA[$input->getValue()]]></code>

app/src/CompanyInfo/CompanyRegisterRegisterUz.php

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
use MichalSpacekCz\Http\Client\HttpClientRequest;
1010
use MichalSpacekCz\Http\Exceptions\HttpClientRequestException;
1111
use Nette\Http\IResponse;
12+
use Nette\Schema\Elements\Structure;
13+
use Nette\Schema\Expect;
14+
use Nette\Schema\Processor;
15+
use Nette\Schema\ValidationException;
1216
use Nette\Utils\Json;
1317
use Nette\Utils\JsonException;
1418
use Override;
@@ -28,6 +32,7 @@
2832

2933

3034
public function __construct(
35+
private Processor $schemaProcessor,
3136
private HttpClient $httpClient,
3237
) {
3338
}
@@ -54,25 +59,54 @@ public function getDetails(string $companyId): CompanyInfoDetails
5459
if (empty($units->id)) {
5560
throw new CompanyNotFoundException();
5661
}
57-
$unit = $this->call('uctovna-jednotka', ['id' => reset($units->id)]);
58-
62+
try {
63+
/** @var Structure $expectArray */
64+
$expectArray = $this->schemaProcessor->process(
65+
Expect::type(Structure::class),
66+
Expect::array([
67+
Expect::int()->required(),
68+
]),
69+
);
70+
$schema = Expect::structure([
71+
'id' => $expectArray->otherItems()->required(),
72+
])->otherItems();
73+
/** @var object{id:array{0:int}} $data */
74+
$data = $this->schemaProcessor->process($schema, $units);
75+
} catch (ValidationException $e) {
76+
throw new CompanyInfoException($e->getMessage(), previous: $e);
77+
}
78+
$unit = $this->call('uctovna-jednotka', ['id' => $data->id[0]]);
79+
try {
80+
$schema = Expect::structure([
81+
'ico' => Expect::string()->required(),
82+
'dic' => Expect::string(),
83+
'nazovUJ' => Expect::string()->required(),
84+
'ulica' => Expect::string()->required(),
85+
'mesto' => Expect::string()->required(),
86+
'psc' => Expect::string()->required(),
87+
])->otherItems();
88+
/** @var object{ico:string, dic?:string, nazovUJ:string, ulica:string, mesto:string, psc:string} $data */
89+
$data = $this->schemaProcessor->process($schema, $unit);
90+
} catch (ValidationException $e) {
91+
throw new CompanyInfoException($e->getMessage(), previous: $e);
92+
}
5993
return new CompanyInfoDetails(
6094
IResponse::S200_OK,
6195
'OK',
62-
$unit->ico,
63-
isset($unit->dic) && is_string($unit->dic) ? strtoupper(self::COUNTRY_CODE) . $unit->dic : '',
64-
$unit->nazovUJ,
65-
$unit->ulica,
66-
$unit->mesto,
67-
$unit->psc,
96+
$data->ico,
97+
isset($data->dic) ? strtoupper(self::COUNTRY_CODE) . $data->dic : '',
98+
$data->nazovUJ,
99+
$data->ulica,
100+
$data->mesto,
101+
$data->psc,
68102
self::COUNTRY_CODE,
69103
);
70104
}
71105

72106

73107
/**
74108
* @param string $method
75-
* @param array<string, string> $parameters
109+
* @param array<string, string|int> $parameters
76110
* @return stdClass JSON object
77111
* @throws CompanyInfoException
78112
*/

app/tests/CompanyInfo/CompanyRegisterRegisterUzTest.phpt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace MichalSpacekCz\CompanyInfo;
77
use MichalSpacekCz\CompanyInfo\Exceptions\CompanyNotFoundException;
88
use MichalSpacekCz\Http\Client\HttpClient;
99
use MichalSpacekCz\Test\TestCaseRunner;
10+
use Nette\Schema\Processor;
1011
use Tester\Assert;
1112
use Tester\TestCase;
1213

@@ -19,9 +20,11 @@ class CompanyRegisterRegisterUzTest extends TestCase
1920
private readonly CompanyRegisterRegisterUz $registerUz;
2021

2122

22-
public function __construct()
23-
{
24-
$this->registerUz = new CompanyRegisterRegisterUz(new HttpClient());
23+
public function __construct(
24+
Processor $schemaProcessor,
25+
) {
26+
// Need a real HttpClient, not the mock one used in other tests
27+
$this->registerUz = new CompanyRegisterRegisterUz($schemaProcessor, new HttpClient());
2528
}
2629

2730

0 commit comments

Comments
 (0)