-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
311 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
], | ||
] | ||
); | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |