Skip to content

Commit

Permalink
ANS-17105 get all sites request
Browse files Browse the repository at this point in the history
  • Loading branch information
ropczan committed May 14, 2024
1 parent 9215d3a commit 5a13d35
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ jobs:
if: "${{ matrix.deps == 'low' }}"

- name: "Run PHPUnit"
run: "vendor/bin/phpunit"
run: "SYMFONY_DEPRECATIONS_HELPER=weak vendor/bin/phpunit"
2 changes: 1 addition & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
37 changes: 37 additions & 0 deletions src/Command/GetAllSites.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Answear\SpeedyBundle\Command;

use Answear\SpeedyBundle\Client\Client;
use Answear\SpeedyBundle\Client\RequestTransformer;
use Answear\SpeedyBundle\Exception\MalformedResponseException;
use Answear\SpeedyBundle\Request\GetAllSitesRequest;
use Answear\SpeedyBundle\Response\GetAllSitesResponse;

class GetAllSites extends AbstractCommand
{
private Client $client;
private RequestTransformer $transformer;

public function __construct(Client $client, RequestTransformer $transformer)
{
$this->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;
}
}
28 changes: 28 additions & 0 deletions src/Request/GetAllSitesRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Answear\SpeedyBundle\Request;

class GetAllSitesRequest extends Request
{
private const ENDPOINT = '/location/site/csv';
private const HTTP_METHOD = 'POST';

private ?int $countryId;

public function __construct(?int $countryId = null)
{
$this->countryId = $countryId;
}

public function getEndpoint(): string
{
return self::ENDPOINT . '/' . $this->countryId;
}

public function getMethod(): string
{
return self::HTTP_METHOD;
}
}
38 changes: 38 additions & 0 deletions src/Response/GetAllSitesResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Answear\SpeedyBundle\Response;

use Answear\SpeedyBundle\Response\Struct\Site;
use Answear\SpeedyBundle\Response\Struct\SiteCollection;
use Symfony\Component\Serializer\Encoder\CsvEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

class GetAllSitesResponse
{
public SiteCollection $sites;

public function __construct(SiteCollection $sites)
{
$this->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)
);
}
}
27 changes: 27 additions & 0 deletions src/Response/Struct/Site.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Answear\SpeedyBundle\Response\Struct;

class Site
{
public int $id;
public int $countryId;
public int $mainSiteId;
public string $type;
public string $typeEn;
public string $name;
public string $nameEn;
public string $municipality;
public string $municipalityEn;
public string $region;
public string $regionEn;
public string $postCode;
public int $addressNomenclature;
public float $x;
public float $y;
public string $servingDays;
public int $servingOfficeId;
public int $servingHubOfficeId;
}
42 changes: 42 additions & 0 deletions src/Response/Struct/SiteCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Answear\SpeedyBundle\Response\Struct;

use Webmozart\Assert\Assert;

class SiteCollection implements \Countable, \IteratorAggregate
{
/**
* @var Site[]
*/
private array $sites;

public function __construct(array $sites)
{
Assert::allIsInstanceOf($sites, Site::class);

$this->sites = $sites;
}

/**
* @return \Traversable<Site>
*/
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);
}
}
2 changes: 1 addition & 1 deletion tests/Integration/Command/FindOfficeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
121 changes: 121 additions & 0 deletions tests/Integration/Command/GetAllSitesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

declare(strict_types=1);

namespace Answear\SpeedyBundle\Tests\Integration\Command;

use Answear\SpeedyBundle\Client\AuthenticationDecorator;
use Answear\SpeedyBundle\Client\Client;
use Answear\SpeedyBundle\Client\RequestTransformer;
use Answear\SpeedyBundle\Client\Serializer;
use Answear\SpeedyBundle\Command\GetAllSites;
use Answear\SpeedyBundle\Request\GetAllSitesRequest;
use Answear\SpeedyBundle\Response\GetAllSitesResponse;
use Answear\SpeedyBundle\Tests\ConfigProviderTrait;
use Answear\SpeedyBundle\Tests\MockGuzzleTrait;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;

class GetAllSitesTest extends TestCase
{
use ConfigProviderTrait;
use MockGuzzleTrait;

private Client $client;

public function setUp(): void
{
parent::setUp();

$this->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,
],
]
);
}
}
14 changes: 14 additions & 0 deletions tests/Integration/Command/data/getAllSitesResponse.csv
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5a13d35

Please sign in to comment.