Skip to content

Commit

Permalink
Added support for Italy parcel points
Browse files Browse the repository at this point in the history
  • Loading branch information
yekovalenkoa committed Feb 1, 2024
1 parent 602ba87 commit fa80564
Show file tree
Hide file tree
Showing 12 changed files with 334 additions and 110 deletions.
12 changes: 3 additions & 9 deletions src/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Answear\InpostBundle\Client;

use Answear\InpostBundle\ConfigProvider;
use Answear\InpostBundle\Exception\ServiceUnavailableException;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface;
Expand All @@ -16,14 +15,9 @@ class Client
{
private ClientInterface $client;

public function __construct(
?ClientInterface $client = null,
) {
$this->client = $client ?? new GuzzleClient(
[
'base_uri' => ConfigProvider::BASE_URL,
]
);
public function __construct(?ClientInterface $client = null)
{
$this->client = $client ?? new GuzzleClient();
}

public function request(Request $request): ResponseInterface
Expand Down
30 changes: 19 additions & 11 deletions src/Command/FindPoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,36 @@

use Answear\InpostBundle\Client\Client;
use Answear\InpostBundle\Client\Serializer;
use Answear\InpostBundle\ConfigProvider;
use Answear\InpostBundle\DeliveryCountry\DeliveryCountryInterface;
use Answear\InpostBundle\Request\FindPointsRequest;
use Answear\InpostBundle\Response\FindPointsResponse;
use GuzzleHttp\Psr7\Request as HttpRequest;
use GuzzleHttp\Psr7\Uri;

class FindPoints extends AbstractCommand
{
private Client $client;
private Serializer $serializer;

public function __construct(Client $client, Serializer $serializer)
{
$this->client = $client;
$this->serializer = $serializer;
public function __construct(
private Client $client,
private Serializer $serializer,
) {
}

public function findPoints(FindPointsRequest $request): FindPointsResponse
{
public function findPoints(
DeliveryCountryInterface $deliveryCountry,
FindPointsRequest $request,
): FindPointsResponse {
$url = new Uri(
sprintf(
'%s%s%s',
$deliveryCountry->getEndpoint(),
$deliveryCountry->getApiVersion(),
$request->getRequestUrl()
)
);

$httpRequest = new HttpRequest(
$request->getMethod(),
new Uri(ConfigProvider::API_VERSION . $request->getRequestUrl()),
$url,
[
'Content-type' => 'application/json',
],
Expand Down
11 changes: 0 additions & 11 deletions src/ConfigProvider.php

This file was deleted.

10 changes: 10 additions & 0 deletions src/DeliveryCountry/DeliveryCountryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Answear\InpostBundle\DeliveryCountry;

interface DeliveryCountryInterface
{
public function getEndpoint(): string;

public function getApiVersion(): string;
}
19 changes: 19 additions & 0 deletions src/DeliveryCountry/DeliveryItaly.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Answear\InpostBundle\DeliveryCountry;

class DeliveryItaly implements DeliveryCountryInterface
{
private const BASE_URL = 'https://api-it-local-points.easypack24.net/';
private const API_VERSION = 'v1';

public function getEndpoint(): string
{
return self::BASE_URL;
}

public function getApiVersion(): string
{
return self::API_VERSION;
}
}
19 changes: 19 additions & 0 deletions src/DeliveryCountry/DeliveryPoland.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Answear\InpostBundle\DeliveryCountry;

class DeliveryPoland implements DeliveryCountryInterface
{
private const BASE_URL = 'https://api-shipx-pl.easypack24.net/';
private const API_VERSION = 'v1';

public function getEndpoint(): string
{
return self::BASE_URL;
}

public function getApiVersion(): string
{
return self::API_VERSION;
}
}
1 change: 1 addition & 0 deletions src/Enum/PointType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ enum PointType: string
case Pop = 'pop';
case ParcelLockerOnly = 'parcel_locker_only';
case ParcelLockerSuperpop = 'parcel_locker_superpop';
case Pok = 'pok';
}
2 changes: 1 addition & 1 deletion src/Response/FindPointsResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static function fromArray(array $arrayResponse): self
return new self(
new ItemCollection(
array_map(
fn ($pointData) => Item::fromArray($pointData),
static fn($pointData) => Item::fromArray($pointData),
$arrayResponse['items']
)
),
Expand Down
78 changes: 68 additions & 10 deletions tests/Integration/Command/FindPointsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Answear\InpostBundle\Client\Client;
use Answear\InpostBundle\Client\Serializer;
use Answear\InpostBundle\Command\FindPoints;
use Answear\InpostBundle\DeliveryCountry\DeliveryCountryInterface;
use Answear\InpostBundle\DeliveryCountry\DeliveryItaly;
use Answear\InpostBundle\DeliveryCountry\DeliveryPoland;
use Answear\InpostBundle\Enum\PointFunctionsType;
use Answear\InpostBundle\Enum\PointType;
use Answear\InpostBundle\Request\FindPointsRequest;
Expand All @@ -25,6 +28,9 @@ class FindPointsTest extends TestCase

private Client $client;

private const POLAND = 'Poland';
private const ITALY = 'Italy';

public function setUp(): void
{
parent::setUp();
Expand All @@ -34,20 +40,25 @@ public function setUp(): void

/**
* @test
* @dataProvider getCountries
*/
public function successfulFindPoints(): void
public function successfulFindPoints(DeliveryCountryInterface $deliveryCountry, string $countryCode): void
{
$command = $this->getCommand();
$this->mockGuzzleResponse(new Response(200, [], $this->getSuccessfulBody()));

$response = $command->findPoints(new FindPointsRequest());
$responseFile = file_get_contents(__DIR__ . sprintf('/data/exampleResponse_%s.json', $countryCode));

$this->mockGuzzleResponse(new Response(200, [], $responseFile));

$response = $command->findPoints($deliveryCountry, new FindPointsRequest());

$this->assertCount(1, $response->getItems());
$this->assertSame(1, $response->getTotalItemsCount());
$this->assertPoint($response);

call_user_func([$this, 'assertPointFor' . $countryCode], $response);
}

private function assertPoint(FindPointsResponse $response): void
private function assertPointForPoland(FindPointsResponse $response): void
{
/** @var Item $point */
$point = $response->getItems()->get(0);
Expand All @@ -66,7 +77,6 @@ private function assertPoint(FindPointsResponse $response): void
$this->assertNull($point->locationDescription2);
$this->assertNull($point->distance);
$this->assertSame($point->openingHours, '24/7');
$this->assertSame($point->openingHours, '24/7');
$this->assertInstanceOf(ItemAddress::class, $point->address);
$this->assertSame($point->address->line1, 'Kościuszki 27');
$this->assertSame($point->address->line2, '21-412 Adamów');
Expand All @@ -89,13 +99,61 @@ private function assertPoint(FindPointsResponse $response): void
$this->assertTrue($point->location247);
}

private function getCommand(): FindPoints
private function assertPointForItaly(FindPointsResponse $response): void
{
return new FindPoints($this->client, new Serializer());
/** @var Item $point */
$point = $response->getItems()->get(0);

$this->assertNotNull($point);
$this->assertSame($point->id, 'ITAAQ01570P');
$this->assertSame($point->name, 'ITAAQ01570P');
$this->assertSame($point->type, [PointType::Pok, PointType::Pop]);
$this->assertSame($point->status, 'Operating');
$this->assertInstanceOf(ItemLocation::class, $point->location);
$this->assertSame($point->location->longitude, 13.47289);
$this->assertSame($point->location->latitude, 42.35751);
$this->assertSame($point->locationType, 'Indoor');
$this->assertSame($point->locationDescription, 'presso Dottor Tech');
$this->assertNull($point->locationDescription1);
$this->assertNull($point->locationDescription2);
$this->assertNull($point->distance);
$this->assertSame($point->openingHours, 'Lun-Ven: 10:00 - 13:00 - 16:00 - 19:00 ');
$this->assertInstanceOf(ItemAddress::class, $point->address);
$this->assertSame($point->address->line1, 'Via Ten. Antonio Rossi Tascione in Str. Vicinale di Paganica 7');
$this->assertSame($point->address->line2, '67100 L\'Aquila');
$this->assertInstanceOf(ItemAddressDetails::class, $point->addressDetails);
$this->assertSame($point->addressDetails->city, 'L\'Aquila');
$this->assertSame($point->addressDetails->province, 'AQ');
$this->assertSame($point->addressDetails->postCode, '67100');
$this->assertSame($point->addressDetails->street, 'Via Ten. Antonio Rossi Tascione in Str. Vicinale di Paganica');
$this->assertSame($point->addressDetails->buildingNumber, '7');
$this->assertNull($point->addressDetails->flatNumber);
$this->assertNull($point->phoneNumber);
$this->assertSame($point->paymentPointDescr, '');
$this->assertSame($point->functions, [
PointFunctionsType::Parcel,
PointFunctionsType::ParcelCollect,
PointFunctionsType::ParcelReverseReturnSend,
PointFunctionsType::ParcelSend,
PointFunctionsType::StandardCourierSend,
]);
$this->assertSame($point->partnerId, 1);
$this->assertFalse($point->isNext);
$this->assertTrue($point->paymentAvailable);
$this->assertSame($point->paymentType, ['3' => 'Payment by cash and card']);
$this->assertSame($point->virtual, '3');
$this->assertNull($point->recommendedLowInterestBoxMachinesList);
$this->assertFalse($point->location247);
}

private function getSuccessfulBody(): string|false
private function getCountries(): \Generator
{
return file_get_contents(__DIR__ . '/data/exampleResponse.json');
yield 'Poland' => [new DeliveryPoland(), self::POLAND];
yield 'Italy' => [new DeliveryItaly(), self::ITALY];
}

private function getCommand(): FindPoints
{
return new FindPoints($this->client, new Serializer());
}
}
68 changes: 0 additions & 68 deletions tests/Integration/Command/data/exampleResponse.json

This file was deleted.

Loading

0 comments on commit fa80564

Please sign in to comment.