From 9d086740e6ba1618000a7a15db914f7eba615b35 Mon Sep 17 00:00:00 2001 From: Wiktor Ramut Date: Tue, 1 Oct 2024 12:40:47 +0200 Subject: [PATCH] Support symfony 7 --- .github/workflows/phpunit.yml | 8 +- .github/workflows/static-analysis.yml | 4 +- composer.json | 23 +++-- phpunit.xml.dist | 16 +--- src/Client/AuthenticationDecorator.php | 13 +-- src/Client/Client.php | 10 +- src/Client/RequestTransformer.php | 13 +-- src/Command/FindOffice.php | 11 +-- src/Command/GetAllPostCodes.php | 11 +-- src/Command/GetAllSites.php | 11 +-- src/ConfigProvider.php | 38 ++------ src/Enum/CargoType.php | 25 +---- src/Enum/OfficeType.php | 18 +--- src/Request/FindOfficeRequest.php | 17 ++-- src/Request/GetAllPostCodesRequest.php | 5 +- src/Request/GetAllSitesRequest.php | 5 +- src/Request/Request.php | 48 +--------- src/Response/ErrorResponse.php | 23 ++--- src/Response/FindOfficeResponse.php | 14 +-- src/Response/GetAllPostCodesResponse.php | 19 ++-- src/Response/GetAllSitesResponse.php | 12 +-- src/Response/Struct/Office.php | 92 ++++++++++--------- src/Response/Struct/OfficeCollection.php | 10 +- src/Response/Struct/OpeningSchedule.php | 16 ++-- src/Response/Struct/OpeningTime.php | 13 +-- src/Response/Struct/PostCode.php | 9 +- src/Response/Struct/PostCodeCollection.php | 10 +- src/Response/Struct/ShipmentAddress.php | 1 - src/Response/Struct/ShipmentParcelSize.php | 22 +++-- src/Response/Struct/SiteCollection.php | 10 +- src/Response/Struct/WorkingTime.php | 16 ++-- tests/Integration/Command/FindOfficeTest.php | 18 ++-- .../Command/GetAllPostCodesTest.php | 6 +- tests/Integration/Command/GetAllSitesTest.php | 6 +- tests/MockGuzzleTrait.php | 2 +- .../Client/AuthenticationDecoratorTest.php | 13 ++- tests/Unit/Client/RequestTransformerTest.php | 11 +-- tests/Unit/Client/SerializerTest.php | 28 +++--- tests/Unit/ConfigProviderTest.php | 13 ++- 39 files changed, 224 insertions(+), 416 deletions(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 35acac9..c9157a8 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -11,14 +11,10 @@ jobs: strategy: matrix: php-version: - - "7.4" - - "8.0" - - "8.1" + - "8.2" deps: - "normal" - include: - - deps: "low" - php-version: "7.4" + - "low" steps: - name: "Checkout" diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index 8b0c53e..b6bcaf6 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -11,9 +11,7 @@ jobs: strategy: matrix: php-version: - - "7.4" - - "8.0" - - "8.1" + - "8.2" steps: - name: "Checkout" diff --git a/composer.json b/composer.json index 9faae6e..0946e4b 100755 --- a/composer.json +++ b/composer.json @@ -4,23 +4,22 @@ "type": "symfony-bundle", "license": "MIT", "require": { - "php": ">=7.4|^8.0", + "php": "^8.2", "ext-json": "*", "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" + "symfony/http-kernel": "^6.0|^7.0", + "symfony/property-access": "^6.0|^7.0", + "symfony/serializer": "^6.0|^7.0", + "webmozart/assert": "^1.11" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^3.4", - "phpro/grumphp": "^1.5.0", - "phpstan/phpstan": "^1.4", - "phpstan/phpstan-webmozart-assert": "^1.0", - "phpunit/phpunit": "^9.5", + "friendsofphp/php-cs-fixer": "^3.64", + "phpro/grumphp": "^2.8", + "phpstan/phpstan": "^1.12", + "phpstan/phpstan-webmozart-assert": "^1.2", + "phpunit/phpunit": "^10.5", "roave/security-advisories": "dev-master", - "symfony/phpunit-bridge": "6.1.*" + "symfony/phpunit-bridge": "6.1.*|^7.0" }, "autoload": { "psr-4": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ea4e9d0..98f6d18 100755 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,29 +1,21 @@ + - ./tests - - - - diff --git a/src/Client/AuthenticationDecorator.php b/src/Client/AuthenticationDecorator.php index 6700a61..2f448a0 100644 --- a/src/Client/AuthenticationDecorator.php +++ b/src/Client/AuthenticationDecorator.php @@ -9,18 +9,15 @@ class AuthenticationDecorator { - private ConfigProvider $configuration; - - public function __construct(ConfigProvider $configuration) + public function __construct(private ConfigProvider $configuration) { - $this->configuration = $configuration; } public function decorate(Request $request): void { - $request->setUserName($this->configuration->getUsername()); - $request->setPassword($this->configuration->getPassword()); - $request->setLanguage($this->configuration->getLanguage()); - $request->setClientSystemId($this->configuration->getClientSystemId()); + $request->userName = $this->configuration->username; + $request->password = $this->configuration->password; + $request->language = $this->configuration->language; + $request->clientSystemId = $this->configuration->clientSystemId; } } diff --git a/src/Client/Client.php b/src/Client/Client.php index ae82680..4953662 100644 --- a/src/Client/Client.php +++ b/src/Client/Client.php @@ -16,20 +16,16 @@ class Client { private const CONNECTION_TIMEOUT = 10; private const TIMEOUT = 30; - - private ConfigProvider $configuration; - private ClientInterface $client; public function __construct( - ConfigProvider $configuration, - ?ClientInterface $client = null + private ConfigProvider $configuration, + private ?ClientInterface $client = null, ) { - $this->configuration = $configuration; $this->client = $client ?? new GuzzleClient( [ 'base_uri' => $configuration->getUrl(), 'timeout' => self::TIMEOUT, - 'connect_timeout' => self::CONNECTION_TIMEOUT + 'connect_timeout' => self::CONNECTION_TIMEOUT, ] ); } diff --git a/src/Client/RequestTransformer.php b/src/Client/RequestTransformer.php index 4372857..a084e8a 100644 --- a/src/Client/RequestTransformer.php +++ b/src/Client/RequestTransformer.php @@ -11,18 +11,11 @@ class RequestTransformer { - private Serializer $serializer; - private AuthenticationDecorator $decorator; - private ConfigProvider $configuration; - public function __construct( - Serializer $serializer, - AuthenticationDecorator $decorator, - ConfigProvider $configuration + private Serializer $serializer, + private AuthenticationDecorator $decorator, + private ConfigProvider $configuration, ) { - $this->serializer = $serializer; - $this->decorator = $decorator; - $this->configuration = $configuration; } public function transform(Request $request): HttpRequest diff --git a/src/Command/FindOffice.php b/src/Command/FindOffice.php index a70d4df..17eaa43 100644 --- a/src/Command/FindOffice.php +++ b/src/Command/FindOffice.php @@ -11,13 +11,10 @@ class FindOffice extends AbstractCommand { - private Client $client; - private RequestTransformer $transformer; - - public function __construct(Client $client, RequestTransformer $transformer) - { - $this->client = $client; - $this->transformer = $transformer; + public function __construct( + private Client $client, + private RequestTransformer $transformer, + ) { } public function findOffice(FindOfficeRequest $request): FindOfficeResponse diff --git a/src/Command/GetAllPostCodes.php b/src/Command/GetAllPostCodes.php index 724b71c..22a93c6 100644 --- a/src/Command/GetAllPostCodes.php +++ b/src/Command/GetAllPostCodes.php @@ -12,13 +12,10 @@ class GetAllPostCodes extends AbstractCommand { - private Client $client; - private RequestTransformer $transformer; - - public function __construct(Client $client, RequestTransformer $transformer) - { - $this->client = $client; - $this->transformer = $transformer; + public function __construct( + private Client $client, + private RequestTransformer $transformer, + ) { } public function getAllPostCodes(GetAllPostCodesRequest $request): GetAllPostCodesResponse diff --git a/src/Command/GetAllSites.php b/src/Command/GetAllSites.php index 9ed967e..0bb100b 100644 --- a/src/Command/GetAllSites.php +++ b/src/Command/GetAllSites.php @@ -12,13 +12,10 @@ 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 __construct( + private Client $client, + private RequestTransformer $transformer, + ) { } public function getAllSites(GetAllSitesRequest $request): GetAllSitesResponse diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index c302ff8..97cada9 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -9,16 +9,14 @@ class ConfigProvider private const URL = 'https://api.speedy.bg/'; private const API_VERSION = 'v1'; - private string $username; - private string $password; - private string $language; - private ?int $clientSystemId; - - public function __construct(string $username, string $password, string $language, ?string $clientSystemId = null) - { - $this->username = $username; - $this->password = $password; - $this->language = $language; + public readonly ?int $clientSystemId; + + public function __construct( + public readonly string $username, + public readonly string $password, + public readonly string $language, + ?string $clientSystemId = null, + ) { $this->clientSystemId = empty($clientSystemId) || !\is_numeric($clientSystemId) ? null : (int) $clientSystemId; } @@ -31,24 +29,4 @@ public function getApiVersion(): string { return self::API_VERSION; } - - public function getUsername(): string - { - return $this->username; - } - - public function getPassword(): string - { - return $this->password; - } - - public function getLanguage(): string - { - return $this->language; - } - - public function getClientSystemId(): ?int - { - return $this->clientSystemId; - } } diff --git a/src/Enum/CargoType.php b/src/Enum/CargoType.php index 53db7cd..7a416d5 100644 --- a/src/Enum/CargoType.php +++ b/src/Enum/CargoType.php @@ -4,26 +4,9 @@ namespace Answear\SpeedyBundle\Enum; -use MabeEnum\Enum; - -class CargoType extends Enum +enum CargoType: string { - public const PARCEL = 'PARCEL'; - public const PALLET = 'PALLET'; - public const TYRE = 'TYRE'; - - public static function parcel(): self - { - return static::get(static::PARCEL); - } - - public static function pallet(): self - { - return static::get(static::PALLET); - } - - public static function tyre(): self - { - return static::get(static::PALLET); - } + case Parcel = 'PARCEL'; + case Pallet = 'PALLET'; + case Tyre = 'TYRE'; } diff --git a/src/Enum/OfficeType.php b/src/Enum/OfficeType.php index e1d8a6f..c731e47 100644 --- a/src/Enum/OfficeType.php +++ b/src/Enum/OfficeType.php @@ -4,20 +4,8 @@ namespace Answear\SpeedyBundle\Enum; -use MabeEnum\Enum; - -class OfficeType extends Enum +enum OfficeType: string { - public const OFFICE = 'OFFICE'; - public const APT = 'APT'; - - public static function office(): self - { - return static::get(static::OFFICE); - } - - public static function apt(): self - { - return static::get(static::APT); - } + case Office = 'OFFICE'; + case Apt = 'APT'; } diff --git a/src/Request/FindOfficeRequest.php b/src/Request/FindOfficeRequest.php index 3c29b4e..51ba279 100644 --- a/src/Request/FindOfficeRequest.php +++ b/src/Request/FindOfficeRequest.php @@ -9,17 +9,12 @@ class FindOfficeRequest extends Request private const ENDPOINT = '/location/office'; private const HTTP_METHOD = 'POST'; - private ?int $countryId; - private ?int $siteId; - private ?string $name; - private ?int $limit; - - public function __construct(?int $countryId = null, ?int $siteId = null, ?string $name = null, ?int $limit = null) - { - $this->countryId = $countryId; - $this->siteId = $siteId; - $this->name = $name; - $this->limit = $limit; + public function __construct( + private ?int $countryId = null, + private ?int $siteId = null, + private ?string $name = null, + private ?int $limit = null, + ) { } public function getEndpoint(): string diff --git a/src/Request/GetAllPostCodesRequest.php b/src/Request/GetAllPostCodesRequest.php index 10942cd..1d5ede0 100644 --- a/src/Request/GetAllPostCodesRequest.php +++ b/src/Request/GetAllPostCodesRequest.php @@ -9,11 +9,8 @@ class GetAllPostCodesRequest extends Request private const ENDPOINT = '/location/postcode/csv'; private const HTTP_METHOD = 'POST'; - private int $countryId; - - public function __construct(int $countryId) + public function __construct(public readonly int $countryId) { - $this->countryId = $countryId; } public function getEndpoint(): string diff --git a/src/Request/GetAllSitesRequest.php b/src/Request/GetAllSitesRequest.php index 2f51b8b..3e3c5eb 100644 --- a/src/Request/GetAllSitesRequest.php +++ b/src/Request/GetAllSitesRequest.php @@ -9,11 +9,8 @@ class GetAllSitesRequest extends Request private const ENDPOINT = '/location/site/csv'; private const HTTP_METHOD = 'POST'; - private ?int $countryId; - - public function __construct(?int $countryId = null) + public function __construct(public readonly ?int $countryId = null) { - $this->countryId = $countryId; } public function getEndpoint(): string diff --git a/src/Request/Request.php b/src/Request/Request.php index e25c780..088c6d0 100644 --- a/src/Request/Request.php +++ b/src/Request/Request.php @@ -6,52 +6,12 @@ abstract class Request { - private string $userName; - private string $password; - private string $language; - private ?int $clientSystemId; + public string $userName; + public string $password; + public string $language; + public ?int $clientSystemId; abstract public function getEndpoint(): string; abstract public function getMethod(): string; - - public function getUserName(): string - { - return $this->userName; - } - - public function setUserName(string $userName): void - { - $this->userName = $userName; - } - - public function getPassword(): string - { - return $this->password; - } - - public function setPassword(string $password): void - { - $this->password = $password; - } - - public function getLanguage(): string - { - return $this->language; - } - - public function setLanguage(string $language): void - { - $this->language = $language; - } - - public function getClientSystemId(): int - { - return $this->clientSystemId; - } - - public function setClientSystemId(?int $clientSystemId): void - { - $this->clientSystemId = $clientSystemId; - } } diff --git a/src/Response/ErrorResponse.php b/src/Response/ErrorResponse.php index b08be7c..e672922 100644 --- a/src/Response/ErrorResponse.php +++ b/src/Response/ErrorResponse.php @@ -6,26 +6,15 @@ use Webmozart\Assert\Assert; -class ErrorResponse +readonly class ErrorResponse { - public string $message; - public string $id; - public int $code; - public ?string $context; - public ?string $component; - public function __construct( - string $message, - string $id, - int $code, - ?string $context = null, - ?string $component = null + public string $message, + public string $id, + public int $code, + public ?string $context = null, + public ?string $component = null, ) { - $this->message = $message; - $this->id = $id; - $this->code = $code; - $this->context = $context; - $this->component = $component; } public static function isErrorResponse(array $response): bool diff --git a/src/Response/FindOfficeResponse.php b/src/Response/FindOfficeResponse.php index cbea3d1..dccd1a8 100644 --- a/src/Response/FindOfficeResponse.php +++ b/src/Response/FindOfficeResponse.php @@ -8,18 +8,10 @@ use Answear\SpeedyBundle\Response\Struct\OfficeCollection; use Webmozart\Assert\Assert; -class FindOfficeResponse +readonly class FindOfficeResponse { - public OfficeCollection $offices; - - public function __construct(OfficeCollection $offices) - { - $this->offices = $offices; - } - - public function getOffices(): OfficeCollection + public function __construct(public OfficeCollection $offices) { - return $this->offices; } public static function fromArray(array $arrayResponse): self @@ -29,7 +21,7 @@ public static function fromArray(array $arrayResponse): self return new self( new OfficeCollection( array_map( - fn ($officeData) => Office::fromArray($officeData), + static fn ($officeData) => Office::fromArray($officeData), $arrayResponse['offices'] ) ) diff --git a/src/Response/GetAllPostCodesResponse.php b/src/Response/GetAllPostCodesResponse.php index b8cc0ba..366d145 100644 --- a/src/Response/GetAllPostCodesResponse.php +++ b/src/Response/GetAllPostCodesResponse.php @@ -8,18 +8,10 @@ use Answear\SpeedyBundle\Response\Struct\PostCodeCollection; use Answear\SpeedyBundle\Service\CsvUtil; -class GetAllPostCodesResponse +readonly class GetAllPostCodesResponse { - public PostCodeCollection $postCodes; - - public function __construct(PostCodeCollection $postCodes) - { - $this->postCodes = $postCodes; - } - - public function getPostCodes(): PostCodeCollection + public function __construct(public PostCodeCollection $postCodes) { - return $this->postCodes; } public static function fromCsv(string $csv): self @@ -32,9 +24,10 @@ public static function fromCsv(string $csv): self continue; } - $postCode = new PostCode(); - $postCode->postCode = $row[0]; - $postCode->siteId = (int) $row[1]; + $postCode = new PostCode( + $row[0], + (int) $row[1] + ); $collection[] = $postCode; } diff --git a/src/Response/GetAllSitesResponse.php b/src/Response/GetAllSitesResponse.php index 2b2d4b3..c2c3c89 100644 --- a/src/Response/GetAllSitesResponse.php +++ b/src/Response/GetAllSitesResponse.php @@ -11,18 +11,10 @@ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Serializer; -class GetAllSitesResponse +readonly class GetAllSitesResponse { - public SiteCollection $sites; - - public function __construct(SiteCollection $sites) - { - $this->sites = $sites; - } - - public function getSites(): SiteCollection + public function __construct(public SiteCollection $sites) { - return $this->sites; } public static function fromCsv(string $csv): self diff --git a/src/Response/Struct/Office.php b/src/Response/Struct/Office.php index 106c9b3..3534229 100644 --- a/src/Response/Struct/Office.php +++ b/src/Response/Struct/Office.php @@ -8,28 +8,33 @@ use Answear\SpeedyBundle\Enum\OfficeType; use Webmozart\Assert\Assert; -class Office +readonly class Office { - public int $id; - public string $name; - public string $nameEn; - public int $siteId; - public OfficeAddress $address; - public OpeningSchedule $openingSchedule; - public WorkingTimeSchedule $workingTimeSchedule; - public ShipmentParcelSize $maxParcelDimension; - public float $maxParcelWeight; - public OfficeType $type; - public ?int $nearbyOfficeId; - public bool $palletOffice; - public bool $cardPaymentAllowed; - public bool $cashPaymentAllowed; - public \DateTimeInterface $validFrom; - public \DateTimeInterface $validTo; - /** @var CargoType[] */ - public array $cargoTypesAllowed; - public bool $pickUpAllowed; - public bool $dropOffAllowed; + /** + * @param CargoType[] $cargoTypesAllowed + */ + public function __construct( + public int $id, + public string $name, + public string $nameEn, + public int $siteId, + public OfficeAddress $address, + public OpeningSchedule $openingSchedule, + public WorkingTimeSchedule $workingTimeSchedule, + public ShipmentParcelSize $maxParcelDimension, + public float $maxParcelWeight, + public OfficeType $type, + public ?int $nearbyOfficeId, + public bool $palletOffice, + public bool $cardPaymentAllowed, + public bool $cashPaymentAllowed, + public \DateTimeInterface $validFrom, + public \DateTimeInterface $validTo, + public array $cargoTypesAllowed, + public bool $pickUpAllowed, + public bool $dropOffAllowed, + ) { + } public static function fromArray(array $officeData): self { @@ -51,27 +56,28 @@ public static function fromArray(array $officeData): self Assert::boolean($officeData['pickUpAllowed']); Assert::boolean($officeData['dropOffAllowed']); - $office = new self(); - $office->id = $officeData['id']; - $office->name = $officeData['name']; - $office->nameEn = $officeData['nameEn']; - $office->siteId = $officeData['siteId']; - $office->address = OfficeAddress::fromArray($officeData['address']); - $office->openingSchedule = OpeningSchedule::fromArray($officeData); - $office->workingTimeSchedule = WorkingTimeSchedule::fromArray($officeData); - $office->maxParcelDimension = ShipmentParcelSize::fromArray($officeData['maxParcelDimensions']); - $office->maxParcelWeight = $officeData['maxParcelWeight']; - $office->type = OfficeType::get($officeData['type']); - $office->nearbyOfficeId = $officeData['nearbyOfficeId'] ?? null; - $office->palletOffice = $officeData['palletOffice']; - $office->cardPaymentAllowed = $officeData['cardPaymentAllowed']; - $office->cashPaymentAllowed = $officeData['cashPaymentAllowed']; - $office->validFrom = new \DateTimeImmutable($officeData['validFrom']); - $office->validTo = new \DateTimeImmutable($officeData['validTo']); - $office->pickUpAllowed = $officeData['pickUpAllowed']; - $office->dropOffAllowed = $officeData['dropOffAllowed']; - $office->cargoTypesAllowed = array_map(fn ($type) => CargoType::get($type), $officeData['cargoTypesAllowed']); - - return $office; + return new self( + $officeData['id'], + $officeData['name'], + $officeData['nameEn'], + $officeData['siteId'], + OfficeAddress::fromArray($officeData['address']), + OpeningSchedule::fromArray($officeData), + WorkingTimeSchedule::fromArray($officeData), + ShipmentParcelSize::fromArray($officeData['maxParcelDimensions']), + $officeData['maxParcelWeight'], + OfficeType::from($officeData['type']), + $officeData['nearbyOfficeId'] ?? null, + $officeData['palletOffice'], + $officeData['cardPaymentAllowed'], + $officeData['cashPaymentAllowed'], + new \DateTimeImmutable($officeData['validFrom']), + new \DateTimeImmutable($officeData['validTo']), + array_map(static fn($type) => CargoType::from($type), + $officeData['cargoTypesAllowed'] + ), + $officeData['pickUpAllowed'], + $officeData['dropOffAllowed'], + ); } } diff --git a/src/Response/Struct/OfficeCollection.php b/src/Response/Struct/OfficeCollection.php index 048f2dd..96c5e05 100644 --- a/src/Response/Struct/OfficeCollection.php +++ b/src/Response/Struct/OfficeCollection.php @@ -11,18 +11,14 @@ * * @url https://api.speedy.bg/web-api.html#href-ds-shipment-address */ -class OfficeCollection implements \Countable, \IteratorAggregate +readonly class OfficeCollection implements \Countable, \IteratorAggregate { /** - * @var Office[] + * @param Office[] $offices */ - private array $offices; - - public function __construct(array $offices) + public function __construct(private array $offices) { Assert::allIsInstanceOf($offices, Office::class); - - $this->offices = $offices; } /** diff --git a/src/Response/Struct/OpeningSchedule.php b/src/Response/Struct/OpeningSchedule.php index a116fd1..255cb06 100644 --- a/src/Response/Struct/OpeningSchedule.php +++ b/src/Response/Struct/OpeningSchedule.php @@ -6,17 +6,13 @@ use Webmozart\Assert\Assert; -class OpeningSchedule +readonly class OpeningSchedule { - public OpeningTime $weekday; - public OpeningTime $saturday; - public OpeningTime $sunday; - - public function __construct(OpeningTime $weekday, OpeningTime $saturday, OpeningTime $sunday) - { - $this->weekday = $weekday; - $this->saturday = $saturday; - $this->sunday = $sunday; + public function __construct( + public OpeningTime $weekday, + public OpeningTime $saturday, + public OpeningTime $sunday, + ) { } public static function fromArray(array $officeData): self diff --git a/src/Response/Struct/OpeningTime.php b/src/Response/Struct/OpeningTime.php index 305e05d..1c7c5d0 100644 --- a/src/Response/Struct/OpeningTime.php +++ b/src/Response/Struct/OpeningTime.php @@ -4,15 +4,12 @@ namespace Answear\SpeedyBundle\Response\Struct; -class OpeningTime +readonly class OpeningTime { - public string $from; - public string $to; - - public function __construct(string $from, string $to) - { - $this->from = $from; - $this->to = $to; + public function __construct( + public string $from, + public string $to, + ) { } public function isClosed(): bool diff --git a/src/Response/Struct/PostCode.php b/src/Response/Struct/PostCode.php index 8f6a836..4c89b79 100644 --- a/src/Response/Struct/PostCode.php +++ b/src/Response/Struct/PostCode.php @@ -4,8 +4,11 @@ namespace Answear\SpeedyBundle\Response\Struct; -class PostCode +readonly class PostCode { - public string $postCode; - public int $siteId; + public function __construct( + public string $postCode, + public int $siteId, + ) { + } } diff --git a/src/Response/Struct/PostCodeCollection.php b/src/Response/Struct/PostCodeCollection.php index ecd5225..c0ee3fc 100644 --- a/src/Response/Struct/PostCodeCollection.php +++ b/src/Response/Struct/PostCodeCollection.php @@ -6,18 +6,14 @@ use Webmozart\Assert\Assert; -class PostCodeCollection implements \Countable, \IteratorAggregate +readonly class PostCodeCollection implements \Countable, \IteratorAggregate { /** - * @var PostCode[] + * @param PostCode[] $postCodes */ - private array $postCodes; - - public function __construct(array $postCodes) + public function __construct(public array $postCodes) { Assert::allIsInstanceOf($postCodes, PostCode::class); - - $this->postCodes = $postCodes; } /** diff --git a/src/Response/Struct/ShipmentAddress.php b/src/Response/Struct/ShipmentAddress.php index 262c449..e3d9e3f 100644 --- a/src/Response/Struct/ShipmentAddress.php +++ b/src/Response/Struct/ShipmentAddress.php @@ -47,7 +47,6 @@ protected function setBaseShipmentProperties(array $addressData): void $this->streetId = $addressData['streetId'] ?? null; $this->streetType = $addressData['streetType'] ?? null; $this->streetName = $addressData['streetName'] ?? null; - $this->streetType = $addressData['streetType'] ?? null; $this->streetNo = $addressData['streetNo'] ?? null; $this->latitude = $addressData['y']; $this->longitude = $addressData['x']; diff --git a/src/Response/Struct/ShipmentParcelSize.php b/src/Response/Struct/ShipmentParcelSize.php index bf43041..cd769bf 100644 --- a/src/Response/Struct/ShipmentParcelSize.php +++ b/src/Response/Struct/ShipmentParcelSize.php @@ -6,11 +6,14 @@ use Webmozart\Assert\Assert; -class ShipmentParcelSize +readonly class ShipmentParcelSize { - public int $width; - public int $height; - public int $depth; + public function __construct( + public int $width, + public int $height, + public int $depth, + ) { + } public static function fromArray(array $data): self { @@ -18,11 +21,10 @@ public static function fromArray(array $data): self Assert::keyExists($data, 'height'); Assert::keyExists($data, 'depth'); - $size = new self(); - $size->width = $data['width']; - $size->height = $data['height']; - $size->depth = $data['depth']; - - return $size; + return new self( + $data['width'], + $data['height'], + $data['depth'], + ); } } diff --git a/src/Response/Struct/SiteCollection.php b/src/Response/Struct/SiteCollection.php index 630d9c5..2b8faf9 100644 --- a/src/Response/Struct/SiteCollection.php +++ b/src/Response/Struct/SiteCollection.php @@ -6,18 +6,14 @@ use Webmozart\Assert\Assert; -class SiteCollection implements \Countable, \IteratorAggregate +readonly class SiteCollection implements \Countable, \IteratorAggregate { /** - * @var Site[] + * @param Site[] $sites */ - private array $sites; - - public function __construct(array $sites) + public function __construct(public array $sites) { Assert::allIsInstanceOf($sites, Site::class); - - $this->sites = $sites; } /** diff --git a/src/Response/Struct/WorkingTime.php b/src/Response/Struct/WorkingTime.php index 157211a..cc9ddc9 100644 --- a/src/Response/Struct/WorkingTime.php +++ b/src/Response/Struct/WorkingTime.php @@ -4,18 +4,16 @@ namespace Answear\SpeedyBundle\Response\Struct; -class WorkingTime +readonly class WorkingTime { public \DateTimeInterface $date; - public string $timeFrom; - public string $timeTo; - public bool $standardSchedule; - public function __construct(string $date, string $timeFrom, string $timeTo, bool $standardSchedule) - { + public function __construct( + string $date, + public string $timeFrom, + public string $timeTo, + public bool $standardSchedule, + ) { $this->date = new \DateTimeImmutable($date); - $this->timeFrom = $timeFrom; - $this->timeTo = $timeTo; - $this->standardSchedule = $standardSchedule; } } diff --git a/tests/Integration/Command/FindOfficeTest.php b/tests/Integration/Command/FindOfficeTest.php index 387e05e..26c5fe6 100644 --- a/tests/Integration/Command/FindOfficeTest.php +++ b/tests/Integration/Command/FindOfficeTest.php @@ -15,6 +15,7 @@ use Answear\SpeedyBundle\Tests\ConfigProviderTrait; use Answear\SpeedyBundle\Tests\MockGuzzleTrait; use GuzzleHttp\Psr7\Response; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; class FindOfficeTest extends TestCase @@ -31,9 +32,7 @@ public function setUp(): void $this->client = new Client($this->getConfiguration(), $this->setupGuzzleClient()); } - /** - * @test - */ + #[Test] public function successfulFindOffice(): void { $command = $this->getCommand(); @@ -41,13 +40,11 @@ public function successfulFindOffice(): void $response = $command->findOffice(new FindOfficeRequest()); - $this->assertCount(1, $response->getOffices()); + $this->assertCount(1, $response->offices); $this->assertOffice($response); } - /** - * @test - */ + #[Test] public function responseWithError(): void { $this->expectException(MalformedResponseException::class); @@ -58,13 +55,13 @@ public function responseWithError(): void $response = $command->findOffice(new FindOfficeRequest()); - $this->assertCount(1, $response->getOffices()); + $this->assertCount(1, $response->offices); $this->assertOffice($response); } private function assertOffice(FindOfficeResponse $response): void { - $office = $response->getOffices()->get(0); + $office = $response->offices->get(0); $this->assertNotNull($office); $this->assertSame($office->id, 1); @@ -114,7 +111,8 @@ private function getErrorBody(): string 'id' => 'EE20210317183038558FZEMSKVR', 'code' => 1, ], - ] + ], + JSON_THROW_ON_ERROR ); } } diff --git a/tests/Integration/Command/GetAllPostCodesTest.php b/tests/Integration/Command/GetAllPostCodesTest.php index 2fc9aae..895d61c 100644 --- a/tests/Integration/Command/GetAllPostCodesTest.php +++ b/tests/Integration/Command/GetAllPostCodesTest.php @@ -40,7 +40,7 @@ public function successfulGetAllPostcodes(): void $response = $command->getAllPostCodes(new GetAllPostCodesRequest(100)); - $this->assertCount(9, $response->getPostCodes()); + $this->assertCount(9, $response->postCodes); $this->assertPostCodes($response); } @@ -54,12 +54,12 @@ public function responseWithError(): void $response = $command->getAllPostCodes(new GetAllPostCodesRequest(100)); - $this->assertCount(0, $response->getPostCodes()); + $this->assertCount(0, $response->postCodes); } private function assertPostCodes(GetAllPostCodesResponse $response): void { - $postCode = $response->getPostCodes()->get(0); + $postCode = $response->postCodes->get(0); $this->assertSame( '1000,68134', diff --git a/tests/Integration/Command/GetAllSitesTest.php b/tests/Integration/Command/GetAllSitesTest.php index 40a0984..d3ac79a 100644 --- a/tests/Integration/Command/GetAllSitesTest.php +++ b/tests/Integration/Command/GetAllSitesTest.php @@ -40,7 +40,7 @@ public function successfulGetAllSites(): void $response = $command->getAllSites(new GetAllSitesRequest()); - $this->assertCount(13, $response->getSites()); + $this->assertCount(13, $response->sites); $this->assertSites($response); } @@ -54,12 +54,12 @@ public function responseWithError(): void $response = $command->getAllSites(new GetAllSitesRequest()); - $this->assertCount(0, $response->getSites()); + $this->assertCount(0, $response->sites); } private function assertSites(GetAllSitesResponse $response): void { - $site = $response->getSites()->get(0); + $site = $response->sites->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', diff --git a/tests/MockGuzzleTrait.php b/tests/MockGuzzleTrait.php index 5dbef10..5488155 100644 --- a/tests/MockGuzzleTrait.php +++ b/tests/MockGuzzleTrait.php @@ -27,7 +27,7 @@ public function setupGuzzleClient(): Client return new Client(['handler' => $handlerStack]); } - public function mockGuzzleResponse(Response $response) + public function mockGuzzleResponse(Response $response): void { $this->guzzleHandler->append($response); } diff --git a/tests/Unit/Client/AuthenticationDecoratorTest.php b/tests/Unit/Client/AuthenticationDecoratorTest.php index e40daa5..628aaa9 100644 --- a/tests/Unit/Client/AuthenticationDecoratorTest.php +++ b/tests/Unit/Client/AuthenticationDecoratorTest.php @@ -7,15 +7,14 @@ use Answear\SpeedyBundle\Client\AuthenticationDecorator; use Answear\SpeedyBundle\Request\FindOfficeRequest; use Answear\SpeedyBundle\Tests\ConfigProviderTrait; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; class AuthenticationDecoratorTest extends TestCase { use ConfigProviderTrait; - /** - * @test - */ + #[Test] public function requestAuthentication(): void { $request = new FindOfficeRequest(); @@ -24,9 +23,9 @@ public function requestAuthentication(): void $authenticator = new AuthenticationDecorator($configuration); $authenticator->decorate($request); - $this->assertSame($configuration->getUsername(), $request->getUserName()); - $this->assertSame($configuration->getPassword(), $request->getPassword()); - $this->assertSame($configuration->getLanguage(), $request->getLanguage()); - $this->assertSame($configuration->getClientSystemId(), $request->getClientSystemId()); + $this->assertSame($configuration->username, $request->userName); + $this->assertSame($configuration->password, $request->password); + $this->assertSame($configuration->language, $request->language); + $this->assertSame($configuration->clientSystemId, $request->clientSystemId); } } diff --git a/tests/Unit/Client/RequestTransformerTest.php b/tests/Unit/Client/RequestTransformerTest.php index 48ca612..dc71c66 100644 --- a/tests/Unit/Client/RequestTransformerTest.php +++ b/tests/Unit/Client/RequestTransformerTest.php @@ -10,6 +10,8 @@ use Answear\SpeedyBundle\Request\FindOfficeRequest; use Answear\SpeedyBundle\Request\Request; use Answear\SpeedyBundle\Tests\ConfigProviderTrait; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; class RequestTransformerTest extends TestCase @@ -17,10 +19,7 @@ class RequestTransformerTest extends TestCase use ConfigProviderTrait; private RequestTransformer $transformer; - /** - * @var Serializer|\PHPUnit\Framework\MockObject\MockObject - */ - private $serializer; + private Serializer|MockObject $serializer; public function setUp(): void { @@ -34,9 +33,7 @@ public function setUp(): void ); } - /** - * @test - */ + #[Test] public function requestTransformed(): void { $request = new FindOfficeRequest(); diff --git a/tests/Unit/Client/SerializerTest.php b/tests/Unit/Client/SerializerTest.php index 9a8f5f9..19f1702 100644 --- a/tests/Unit/Client/SerializerTest.php +++ b/tests/Unit/Client/SerializerTest.php @@ -7,6 +7,8 @@ use Answear\SpeedyBundle\Client\Serializer; use Answear\SpeedyBundle\Request\FindOfficeRequest; use Answear\SpeedyBundle\Request\Request; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; class SerializerTest extends TestCase @@ -16,10 +18,8 @@ public function setUp(): void parent::setUp(); } - /** - * @test - * @dataProvider provider - */ + #[Test] + #[DataProvider('provider')] public function requestAuthentication(Request $request, string $expectedBody): void { $serializer = new Serializer(); @@ -27,35 +27,35 @@ public function requestAuthentication(Request $request, string $expectedBody): v $this->assertSame($expectedBody, $serializer->serialize($request)); } - public function provider(): iterable + public static function provider(): iterable { yield 'requestFields' => [ - $this->getAuthenticatedRequest(100, 1, 'office_name', 100), + self::getAuthenticatedRequest(100, 1, 'office_name', 100), '{"countryId":100,"siteId":1,"name":"office_name","limit":100,"userName":"username","password":"password","language":"language","clientSystemId":999}', ]; yield 'skipNullValues' => [ - $this->getAuthenticatedRequest(100, null, 'office_name'), + self::getAuthenticatedRequest(100, null, 'office_name'), '{"countryId":100,"name":"office_name","userName":"username","password":"password","language":"language","clientSystemId":999}', ]; yield 'serializeAuthenticationData' => [ - $this->getAuthenticatedRequest(), + self::getAuthenticatedRequest(), '{"userName":"username","password":"password","language":"language","clientSystemId":999}', ]; } - private function getAuthenticatedRequest( + private static function getAuthenticatedRequest( ?int $countryId = null, ?int $siteId = null, ?string $name = null, - ?int $limit = null + ?int $limit = null, ): Request { $request = new FindOfficeRequest($countryId, $siteId, $name, $limit); - $request->setUserName('username'); - $request->setPassword('password'); - $request->setLanguage('language'); - $request->setClientSystemId(999); + $request->userName = 'username'; + $request->password = 'password'; + $request->language = 'language'; + $request->clientSystemId = 999; return $request; } diff --git a/tests/Unit/ConfigProviderTest.php b/tests/Unit/ConfigProviderTest.php index 8da795b..57ad928 100644 --- a/tests/Unit/ConfigProviderTest.php +++ b/tests/Unit/ConfigProviderTest.php @@ -5,21 +5,20 @@ namespace Answear\SpeedyBundle\Tests\Unit; use Answear\SpeedyBundle\ConfigProvider; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; class ConfigProviderTest extends TestCase { - /** - * @test - */ + #[Test] public function gettersAreValid(): void { $configuration = new ConfigProvider('1username', '2password', '3language', '4'); - $this->assertSame('1username', $configuration->getUsername()); - $this->assertSame('2password', $configuration->getPassword()); - $this->assertSame('3language', $configuration->getLanguage()); - $this->assertSame(4, $configuration->getClientSystemId()); + $this->assertSame('1username', $configuration->username); + $this->assertSame('2password', $configuration->password); + $this->assertSame('3language', $configuration->language); + $this->assertSame(4, $configuration->clientSystemId); $this->assertSame('https://api.speedy.bg/', $configuration->getUrl()); $this->assertSame('v1', $configuration->getApiVersion()); }