diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 35acac9..408e66b 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -47,4 +47,4 @@ jobs: if: "${{ matrix.deps == 'low' }}" - name: "Run PHPUnit" - run: "vendor/bin/phpunit" + run: "SYMFONY_DEPRECATIONS_HELPER=weak vendor/bin/phpunit" diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index c51474e..2393b5a 100755 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -20,7 +20,7 @@ 'strict_param' => false, 'array_syntax' => ['syntax' => 'short'], 'concat_space' => ['spacing' => 'one'], - 'phpdoc_align' => [], + 'phpdoc_align' => ['align' => 'left'], 'phpdoc_summary' => false, 'void_return' => false, 'phpdoc_var_without_name' => false, diff --git a/composer.json b/composer.json index 7fe5065..9faae6e 100755 --- a/composer.json +++ b/composer.json @@ -9,6 +9,7 @@ "guzzlehttp/guzzle": "^6.0|^7.0", "marc-mabe/php-enum": "^3.0|^4.3", "symfony/http-kernel": "^5.4|^6.0", + "symfony/property-access": "^5.4|^6.0", "symfony/serializer": "^5.4|^6.0", "webmozart/assert": "^1.3" }, diff --git a/src/Command/GetAllSites.php b/src/Command/GetAllSites.php new file mode 100644 index 0000000..9ed967e --- /dev/null +++ b/src/Command/GetAllSites.php @@ -0,0 +1,37 @@ +client = $client; + $this->transformer = $transformer; + } + + public function getAllSites(GetAllSitesRequest $request): GetAllSitesResponse + { + $httpRequest = $this->transformer->transform($request); + $response = $this->client->request($httpRequest); + + try { + $result = GetAllSitesResponse::fromCsv($response->getBody()->getContents()); + } catch (\Throwable $throwable) { + throw new MalformedResponseException($throwable->getMessage(), $response, $throwable); + } + + return $result; + } +} diff --git a/src/Request/GetAllSitesRequest.php b/src/Request/GetAllSitesRequest.php new file mode 100644 index 0000000..2f51b8b --- /dev/null +++ b/src/Request/GetAllSitesRequest.php @@ -0,0 +1,28 @@ +countryId = $countryId; + } + + public function getEndpoint(): string + { + return self::ENDPOINT . '/' . $this->countryId; + } + + public function getMethod(): string + { + return self::HTTP_METHOD; + } +} diff --git a/src/Response/GetAllSitesResponse.php b/src/Response/GetAllSitesResponse.php new file mode 100644 index 0000000..2b2d4b3 --- /dev/null +++ b/src/Response/GetAllSitesResponse.php @@ -0,0 +1,38 @@ +sites = $sites; + } + + public function getSites(): SiteCollection + { + return $this->sites; + } + + public static function fromCsv(string $csv): self + { + $serializer = new Serializer([new ObjectNormalizer(), new ArrayDenormalizer()], [new CsvEncoder()]); + + $sites = $serializer->deserialize($csv, Site::class . '[]', CsvEncoder::FORMAT); + + return new self( + new SiteCollection($sites) + ); + } +} diff --git a/src/Response/Struct/Site.php b/src/Response/Struct/Site.php new file mode 100644 index 0000000..feeb4e7 --- /dev/null +++ b/src/Response/Struct/Site.php @@ -0,0 +1,27 @@ +sites = $sites; + } + + /** + * @return \Traversable + */ + public function getIterator(): \Traversable + { + foreach ($this->sites as $key => $site) { + yield $key => $site; + } + } + + public function get($key): ?Site + { + return $this->sites[$key] ?? null; + } + + public function count(): int + { + return \count($this->sites); + } +} diff --git a/tests/Integration/Command/FindOfficeTest.php b/tests/Integration/Command/FindOfficeTest.php index 3ad70d3..387e05e 100644 --- a/tests/Integration/Command/FindOfficeTest.php +++ b/tests/Integration/Command/FindOfficeTest.php @@ -101,7 +101,7 @@ private function getCommand(): FindOffice private function getSuccessfulBody(): string { - return file_get_contents(__DIR__ . '/data/exampleResponse.json'); + return file_get_contents(__DIR__ . '/data/findOfficeResponse.json'); } private function getErrorBody(): string diff --git a/tests/Integration/Command/GetAllSitesTest.php b/tests/Integration/Command/GetAllSitesTest.php new file mode 100644 index 0000000..c4f3e3d --- /dev/null +++ b/tests/Integration/Command/GetAllSitesTest.php @@ -0,0 +1,121 @@ +client = new Client($this->getConfiguration(), $this->setupGuzzleClient()); + } + + /** + * @test + */ + public function successfulFindOffice(): void + { + $command = $this->getCommand(); + $this->mockGuzzleResponse(new Response(200, [], $this->getSuccessfulBody())); + + $response = $command->getAllSites(new GetAllSitesRequest()); + + $this->assertCount(13, $response->getSites()); + $this->assertOffice($response); + } + + /** + * @test + */ + public function responseWithError(): void + { + $command = $this->getCommand(); + $this->mockGuzzleResponse(new Response(200, [], $this->getErrorBody())); + + $response = $command->getAllSites(new GetAllSitesRequest()); + + $this->assertCount(0, $response->getSites()); + } + + private function assertOffice(GetAllSitesResponse $response): void + { + $site = $response->getSites()->get(0); + + $this->assertSame( + '642202552,642,0,s.,s.,1 DECEMBRIE,1 DECEMBRIE,COM. 1 DECEMBRIE,COM. 1 DECEMBRIE,ILFOV,ILFOV,077005,1,26.060031,44.288061,1111110,959,959', + implode( + ',', + [ + $site->id, + $site->countryId, + $site->mainSiteId, + $site->type, + $site->typeEn, + $site->name, + $site->nameEn, + $site->municipality, + $site->municipalityEn, + $site->region, + $site->regionEn, + $site->postCode, + $site->addressNomenclature, + $site->x, + $site->y, + $site->servingDays, + $site->servingOfficeId, + $site->servingHubOfficeId, + ], + ) + ); + } + + private function getCommand(): GetAllSites + { + $transformer = new RequestTransformer( + new Serializer(), + new AuthenticationDecorator($this->getConfiguration()), + $this->getConfiguration() + ); + + return new GetAllSites($this->client, $transformer); + } + + private function getSuccessfulBody(): string + { + return file_get_contents(__DIR__ . '/data/getAllSitesResponse.csv'); + } + + private function getErrorBody(): string + { + return \json_encode( + [ + 'error' => [ + 'context' => 'user.expired', + 'message' => 'Потребителят е с изтекла валидност', + 'id' => 'EE20210317183038558FZEMSKVR', + 'code' => 1, + ], + ] + ); + } +} diff --git a/tests/Integration/Command/data/exampleResponse.json b/tests/Integration/Command/data/findOfficeResponse.json similarity index 100% rename from tests/Integration/Command/data/exampleResponse.json rename to tests/Integration/Command/data/findOfficeResponse.json diff --git a/tests/Integration/Command/data/getAllSitesResponse.csv b/tests/Integration/Command/data/getAllSitesResponse.csv new file mode 100644 index 0000000..44ce530 --- /dev/null +++ b/tests/Integration/Command/data/getAllSitesResponse.csv @@ -0,0 +1,14 @@ +id,countryId,mainSiteId,type,typeEn,name,nameEn,municipality,municipalityEn,region,regionEn,postCode,addressNomenclature,x,y,servingDays,servingOfficeId,servingHubOfficeId +642202552,642,0,s.,s.,1 DECEMBRIE,1 DECEMBRIE,COM. 1 DECEMBRIE,COM. 1 DECEMBRIE,ILFOV,ILFOV,077005,1,26.060031,44.288061,1111110,959,959 +642262318,642,0,s.,s.,1 DECEMBRIE,1 DECEMBRIE,COM. BANCA,COM. BANCA,VASLUI,VASLUI,737026,0,27.809418,46.359791,0100100,911,904 +642160669,642,0,s.,s.,2 MAI,2 MAI,COM. LIMANU,COM. LIMANU,CONSTANTA,CONSTANTA,907161,0,28.575294,43.786635,1111110,960,926 +642154680,642,0,s.,s.,23 AUGUST,23 AUGUST,COM. ZAVOI,COM. ZAVOI,CARAS-SEVERIN,CARAS-SEVERIN,327436,0,22.396515,45.523393,1111110,928,901 +642160605,642,0,s.,s.,23 AUGUST,23 AUGUST,COM. 23 AUGUST,COM. 23 AUGUST,CONSTANTA,CONSTANTA,907005,0,28.583639,43.918723,1111110,960,926 +642212735,642,0,s.,s.,23 AUGUST,23 AUGUST,COM. MALOVAT,COM. MALOVAT,MEHEDINTI,MEHEDINTI,227316,0,22.762506,44.716151,1111110,938,914 +642127089,642,0,s.,s.,ABRAM,ABRAM,COM. ABRAM,COM. ABRAM,BIHOR,BIHOR,417005,0,22.379994,47.316792,1111100,921,921 +642127178,642,0,s.,s.,ABRAMUT,ABRAMUT,COM. ABRAMUT,COM. ABRAMUT,BIHOR,BIHOR,417015,0,22.254395,47.322958,1111100,921,921 +642101160,642,0,or.,or.,ABRUD,ABRUD,OR. ABRUD,OR. ABRUD,ALBA,ALBA,515100,0,23.065029,46.274675,1111110,902,901 +642160909,642,0,s.,s.,ABRUD,ABRUD,COM. ADAMCLISI,COM. ADAMCLISI,CONSTANTA,CONSTANTA,907011,0,27.978891,44.145523,0100100,913,913 +642101179,642,0,s.,s.,ABRUD-SAT,ABRUD-SAT,OR. ABRUD,OR. ABRUD,ALBA,ALBA,515101,0,23.061093,46.283967,1111110,902,901 +642189446,642,0,s.,s.,ABUCEA,ABUCEA,COM. DOBRA,COM. DOBRA,HUNEDOARA,HUNEDOARA,337216,0,22.540973,45.905754,1111100,915,901 +642217060,642,0,s.,s.,ABUD,ABUD,COM. GHINDARI,COM. GHINDARI,MURES,MURES,547266,0,24.916361,46.525105,0101000,933,901