From b283546c271e7a7343d21625066ad7b0bf5fc6a9 Mon Sep 17 00:00:00 2001 From: Wiktor Ramut Date: Mon, 30 Sep 2024 21:30:45 +0200 Subject: [PATCH 1/2] Support symfony 7 --- .github/workflows/phpunit.yml | 8 +- .github/workflows/static-analysis.yml | 4 +- .gitignore | 1 + composer.json | 31 ++++--- phpunit.xml.dist | 16 +--- src/Client/Client.php | 12 +-- src/Client/RequestTransformer.php | 11 +-- src/ConfigProvider.php | 17 ++-- src/Enum/DayType.php | 88 ++++++------------- src/Enum/EnvironmentEnum.php | 25 +++--- src/Enum/StatusResult.php | 27 ++---- src/Exception/BadRequestException.php | 18 ++-- src/Exception/MalformedResponseException.php | 18 ++-- src/Logger/OverseasLogger.php | 4 +- src/Request/GetPlaces.php | 14 ++- src/Response/DTO/Coordinates.php | 12 +-- src/Response/DTO/Error.php | 8 +- src/Response/DTO/ParcelShop.php | 2 +- src/Response/DTO/Place.php | 20 ++--- .../ResponsePartialDenormalizeTrait.php | 6 +- src/Serializer/Normalizer/EnumNormalizer.php | 26 +++--- src/Service/ParcelShopsService.php | 13 ++- src/Service/PlacesService.php | 13 ++- .../DependencyInjection/ConfigurationTest.php | 26 +++--- tests/Integration/Service/ParcelShopsTest.php | 17 ++-- tests/Integration/Service/PlacesTest.php | 24 +++-- tests/MockGuzzleTrait.php | 2 +- tests/Unit/Response/DTO/ParcelShopTest.php | 32 +++---- 28 files changed, 188 insertions(+), 307 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 7f7fb9d..f744fb4 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" uses: "actions/checkout@v2" diff --git a/.gitignore b/.gitignore index c4e8084..adbc2ea 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ composer.lock /.idea /.env* +/.phpunit.cache diff --git a/composer.json b/composer.json index 56022f1..7e55fc2 100755 --- a/composer.json +++ b/composer.json @@ -4,26 +4,25 @@ "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", - "psr/log": "^1.0", - "symfony/http-kernel": "5.4 - 6.4", - "symfony/property-access": "5.4 - 6.4", - "symfony/property-info": "5.4 - 6.4", - "symfony/serializer": "5.4 - 6.4", - "webmozart/assert": "^1.3" + "guzzlehttp/guzzle": "^6.0|^7.0", + "psr/log": "^1.1", + "symfony/http-kernel": "^6.4|^7.0", + "symfony/property-access": "^6.4|^7.0", + "symfony/property-info": "^6.4|^7.0", + "symfony/serializer": "^6.4|^7.0", + "webmozart/assert": "^1.11" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^3.4", - "matthiasnoback/symfony-config-test": "^4.2", - "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", + "matthiasnoback/symfony-config-test": "^5.2", + "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.4.*" + "symfony/phpunit-bridge": "6.4.*|^7.0" }, "autoload": { "psr-4": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b3a9508..9f36580 100755 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,29 +1,21 @@ + - ./tests - - - - diff --git a/src/Client/Client.php b/src/Client/Client.php index 43725c3..14a1604 100644 --- a/src/Client/Client.php +++ b/src/Client/Client.php @@ -16,17 +16,11 @@ class Client private const CONNECTION_TIMEOUT = 10; private const TIMEOUT = 30; - private ClientInterface $client; - private RequestTransformer $transformer; - private OverseasLogger $overseasLogger; - public function __construct( - RequestTransformer $transformer, - OverseasLogger $overseasLogger, - ?ClientInterface $client = null + private RequestTransformer $transformer, + private OverseasLogger $overseasLogger, + private ?ClientInterface $client = null, ) { - $this->transformer = $transformer; - $this->overseasLogger = $overseasLogger; $this->client = $client ?? new \GuzzleHttp\Client(['timeout' => self::TIMEOUT, 'connect_timeout' => self::CONNECTION_TIMEOUT]); } diff --git a/src/Client/RequestTransformer.php b/src/Client/RequestTransformer.php index 1d0536d..167ebbc 100644 --- a/src/Client/RequestTransformer.php +++ b/src/Client/RequestTransformer.php @@ -12,21 +12,16 @@ class RequestTransformer { - private Serializer $serializer; - private ConfigProvider $configuration; - public function __construct( - Serializer $serializer, - ConfigProvider $configuration + private Serializer $serializer, + private ConfigProvider $configuration, ) { - $this->serializer = $serializer; - $this->configuration = $configuration; } public function transform(RequestInterface $request): HttpRequest { $uri = $this->configuration->getUrl() . $request->getEndpoint() - . '?apiKey=' . $this->configuration->getApiKey(); + . '?apiKey=' . $this->configuration->apiKey; if (null !== $request->getUrlQuery()) { $uri .= '&' . $request->getUrlQuery(); diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index 12ea76a..44d47a9 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -12,26 +12,21 @@ class ConfigProvider private const TEST_URL = 'https://apitest.overseas.hr/'; private EnvironmentEnum $environment; - private string $apiKey; - public function __construct(string $environment, string $apiKey) - { - $this->environment = EnvironmentEnum::get($environment); - $this->apiKey = $apiKey; + public function __construct( + string $environment, + public string $apiKey, + ) { + $this->environment = EnvironmentEnum::from($environment); } public function getUrl(): string { - return EnvironmentEnum::prod()->is($this->environment) ? self::URL : self::TEST_URL; + return EnvironmentEnum::Prod === $this->environment ? self::URL : self::TEST_URL; } public function getEnvironment(): EnvironmentEnum { return $this->environment; } - - public function getApiKey(): string - { - return $this->apiKey; - } } diff --git a/src/Enum/DayType.php b/src/Enum/DayType.php index 404bfe9..d770542 100644 --- a/src/Enum/DayType.php +++ b/src/Enum/DayType.php @@ -4,65 +4,35 @@ namespace Answear\OverseasBundle\Enum; -use MabeEnum\Enum; -use MabeEnum\EnumSerializableTrait; - -class DayType extends Enum implements \Serializable +enum DayType: int implements \JsonSerializable { - use EnumSerializableTrait; - - public const WORKING_DAY = 1; - public const MONDAY = 7; - public const TUESDAY = 8; - public const WEDNESDAY = 9; - public const THURSDAY = 10; - public const FRIDAY = 11; - public const SATURDAY = 2; - public const SATURDAY_2 = 12; - public const SUNDAY = 3; - - public static function friday(): self - { - return static::get(static::FRIDAY); - } - - public static function thursday(): self - { - return static::get(static::THURSDAY); - } - - public static function wednesday(): self - { - return static::get(static::WEDNESDAY); - } - - public static function tuesday(): self - { - return static::get(static::TUESDAY); - } - - public static function monday(): self - { - return static::get(static::MONDAY); - } - - public static function sunday(): self - { - return static::get(static::SUNDAY); - } - - public static function saturday(): self - { - return static::get(static::SATURDAY); - } - - public static function workingDay(): self - { - return static::get(static::WORKING_DAY); - } - - public static function saturday2(): self - { - return static::get(static::SATURDAY_2); + case WorkingDay = 1; + case Monday = 7; + case Tuesday = 8; + case Wednesday = 9; + case Thursday = 10; + case Friday = 11; + case Saturday = 2; + case Saturday2 = 12; + case Sunday = 3; + + public function jsonSerialize(): int + { + return $this->value; + } + + public function getOrdinal(): int + { + return match ($this) { + self::WorkingDay => 1, + self::Monday => 2, + self::Tuesday => 3, + self::Wednesday => 4, + self::Thursday => 5, + self::Friday => 6, + self::Saturday => 7, + self::Saturday2 => 8, + self::Sunday => 9, + }; } } diff --git a/src/Enum/EnvironmentEnum.php b/src/Enum/EnvironmentEnum.php index 64c85a0..b63e93f 100644 --- a/src/Enum/EnvironmentEnum.php +++ b/src/Enum/EnvironmentEnum.php @@ -4,23 +4,24 @@ namespace Answear\OverseasBundle\Enum; -use MabeEnum\Enum; -use MabeEnum\EnumSerializableTrait; - -class EnvironmentEnum extends Enum implements \Serializable +enum EnvironmentEnum: string implements \JsonSerializable { - use EnumSerializableTrait; - - public const PROD = 'prod'; - public const TEST = 'test'; + case Prod = 'prod'; + case Test = 'test'; - public static function prod(): self + public function jsonSerialize(): string { - return static::get(static::PROD); + return $this->value; } - public static function test(): self + /** + * @return string[] + */ + public static function getValues(): array { - return static::get(static::TEST); + return [ + self::Prod->value, + self::Test->value, + ]; } } diff --git a/src/Enum/StatusResult.php b/src/Enum/StatusResult.php index 0f14f2c..b9c3385 100644 --- a/src/Enum/StatusResult.php +++ b/src/Enum/StatusResult.php @@ -4,29 +4,14 @@ namespace Answear\OverseasBundle\Enum; -use MabeEnum\Enum; -use MabeEnum\EnumSerializableTrait; - -class StatusResult extends Enum implements \Serializable +enum StatusResult: int implements \JsonSerializable { - use EnumSerializableTrait; - - public const OK = 0; - public const ERROR = 1; - public const VALIDATION_FAILED = 2; - - public static function validationFailed(): self - { - return static::get(static::VALIDATION_FAILED); - } - - public static function error(): self - { - return static::get(static::ERROR); - } + case Ok = 0; + case Error = 1; + case ValidationFailed = 2; - public static function ok(): self + public function jsonSerialize(): int { - return static::get(static::OK); + return $this->value; } } diff --git a/src/Exception/BadRequestException.php b/src/Exception/BadRequestException.php index fcde487..55b3020 100755 --- a/src/Exception/BadRequestException.php +++ b/src/Exception/BadRequestException.php @@ -9,21 +9,15 @@ class BadRequestException extends \RuntimeException { - private ResponseInterface $response; - - public function __construct(ResponseInterface $response, ?\Throwable $previous = null) - { + public function __construct( + public readonly ResponseInterface $response, + ?\Throwable $previous = null, + ) { $message = 'Error occurs.'; - if ($response->getStatus()->is(StatusResult::validationFailed())) { + if (StatusResult::ValidationFailed === $response->getStatus()) { $message = 'Validation failed.'; } - parent::__construct($message, 0, $previous); - $this->response = $response; - } - - public function getResponse(): ResponseInterface - { - return $this->response; + parent::__construct($message, 0, $previous); } } diff --git a/src/Exception/MalformedResponseException.php b/src/Exception/MalformedResponseException.php index 35414b8..5483c4b 100755 --- a/src/Exception/MalformedResponseException.php +++ b/src/Exception/MalformedResponseException.php @@ -4,19 +4,15 @@ namespace Answear\OverseasBundle\Exception; +use Psr\Http\Message\ResponseInterface; + class MalformedResponseException extends \RuntimeException { - private $response; - - public function __construct(string $message, $response, ?\Throwable $previous = null) - { + public function __construct( + string $message, + public readonly ResponseInterface $response, + ?\Throwable $previous = null, + ) { parent::__construct($message, 0, $previous); - - $this->response = $response; - } - - public function getResponse() - { - return $this->response; } } diff --git a/src/Logger/OverseasLogger.php b/src/Logger/OverseasLogger.php index 8445a3c..7c083bd 100644 --- a/src/Logger/OverseasLogger.php +++ b/src/Logger/OverseasLogger.php @@ -16,12 +16,10 @@ class OverseasLogger private const MAX_CONTENT_LENGTH = 3000; private const HUGE_CONTENT_SKIPPED = '--- HUGE CONTENT SKIPPED ---'; - private LoggerInterface $logger; private ?string $requestId = null; - public function __construct(LoggerInterface $logger) + public function __construct(private LoggerInterface $logger) { - $this->logger = $logger; } public function logRequest(string $endpoint, RequestInterface $request): void diff --git a/src/Request/GetPlaces.php b/src/Request/GetPlaces.php index 453dff9..235313f 100644 --- a/src/Request/GetPlaces.php +++ b/src/Request/GetPlaces.php @@ -9,15 +9,11 @@ class GetPlaces implements RequestInterface private const ENDPOINT = 'places'; private const HTTP_METHOD = 'GET'; - public ?string $zipCode; - public ?string $name; - public ?bool $approx; - - public function __construct(?string $zipCode = null, ?string $name = null, ?bool $approx = null) - { - $this->zipCode = $zipCode; - $this->name = $name; - $this->approx = $approx; + public function __construct( + public readonly ?string $zipCode = null, + public readonly ?string $name = null, + public readonly ?bool $approx = null, + ) { } public function getEndpoint(): string diff --git a/src/Response/DTO/Coordinates.php b/src/Response/DTO/Coordinates.php index 9b41916..af8f422 100644 --- a/src/Response/DTO/Coordinates.php +++ b/src/Response/DTO/Coordinates.php @@ -8,15 +8,11 @@ class Coordinates { - public float $latitude; - public float $longitude; - - public function __construct(float $latitude, float $longitude) - { + public function __construct( + public float $latitude, + public float $longitude, + ) { Assert::range($latitude, -90, 90); Assert::range($longitude, -180, 180); - - $this->latitude = $latitude; - $this->longitude = $longitude; } } diff --git a/src/Response/DTO/Error.php b/src/Response/DTO/Error.php index fd7f77f..9c65a3e 100644 --- a/src/Response/DTO/Error.php +++ b/src/Response/DTO/Error.php @@ -15,8 +15,12 @@ class Error implements DenormalizableInterface public array $validations = []; public int $code; - public function denormalize(DenormalizerInterface $denormalizer, $data, $format = null, array $context = []): void - { + public function denormalize( + DenormalizerInterface $denormalizer, + $data, + $format = null, + array $context = [], + ): void { $this->validations = empty($data['Validations']) ? [] : $denormalizer->denormalize( diff --git a/src/Response/DTO/ParcelShop.php b/src/Response/DTO/ParcelShop.php index 229ff50..c99f979 100644 --- a/src/Response/DTO/ParcelShop.php +++ b/src/Response/DTO/ParcelShop.php @@ -52,7 +52,7 @@ static function (WorkingHours $a, WorkingHours $b): int { if (null === $b->type) { return 1; } - if ($a->type->is($b->type)) { + if ($a->type === $b->type) { return $a->from <=> $b->from; } diff --git a/src/Response/DTO/Place.php b/src/Response/DTO/Place.php index 7814d76..69b01c8 100644 --- a/src/Response/DTO/Place.php +++ b/src/Response/DTO/Place.php @@ -6,18 +6,12 @@ class Place { - public string $zipCode; - public string $name; - public string $zipcodeName; - public ?bool $standardAvailable; - public ?bool $cargoAvailable; - - public function __construct(string $zipCode, string $name, string $zipcodeName, ?bool $standardAvailable = null, ?bool $cargoAvailable = null) - { - $this->zipCode = $zipCode; - $this->name = $name; - $this->zipcodeName = $zipcodeName; - $this->standardAvailable = $standardAvailable; - $this->cargoAvailable = $cargoAvailable; + public function __construct( + public string $zipCode, + public string $name, + public string $zipcodeName, + public ?bool $standardAvailable = null, + public ?bool $cargoAvailable = null, + ) { } } diff --git a/src/Response/ResponsePartialDenormalizeTrait.php b/src/Response/ResponsePartialDenormalizeTrait.php index 30d3cbc..40647a5 100644 --- a/src/Response/ResponsePartialDenormalizeTrait.php +++ b/src/Response/ResponsePartialDenormalizeTrait.php @@ -13,14 +13,14 @@ trait ResponsePartialDenormalizeTrait { public function denormalizeStatus(array $data): StatusResult { - return StatusResult::get($data['status']); + return StatusResult::from($data['status']); } public function denormalizeError( DenormalizerInterface $denormalizer, $data, $format = null, - array $context = [] + array $context = [], ): ?Error { return empty($data['error']) ? null : $denormalizer->denormalize( @@ -38,7 +38,7 @@ public function denormalizeValidation( DenormalizerInterface $denormalizer, $data, $format = null, - array $context = [] + array $context = [], ): array { return empty($data['validations']) ? [] diff --git a/src/Serializer/Normalizer/EnumNormalizer.php b/src/Serializer/Normalizer/EnumNormalizer.php index 7de2cd2..4dc1adf 100644 --- a/src/Serializer/Normalizer/EnumNormalizer.php +++ b/src/Serializer/Normalizer/EnumNormalizer.php @@ -4,34 +4,30 @@ namespace Answear\OverseasBundle\Serializer\Normalizer; -use MabeEnum\Enum; use Symfony\Component\Serializer\Exception\InvalidArgumentException; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; class EnumNormalizer implements NormalizerInterface, DenormalizerInterface { - /** - * @return array|string|int|float|bool|\ArrayObject|null - */ - public function normalize($object, $format = null, array $context = []) + public function normalize($object, $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null { - if (!$object instanceof Enum) { - throw new InvalidArgumentException(sprintf('The object must implement "%s".', Enum::class)); + if (!$object instanceof \BackedEnum) { + throw new InvalidArgumentException(sprintf('The object must implement "%s".', \BackedEnum::class)); } - return $object->getValue(); + return $object->value; } public function supportsNormalization($data, $format = null, array $context = []): bool { - return $data instanceof Enum; + return $data instanceof \BackedEnum; } public function supportsDenormalization($data, $type, $format = null, array $context = []): bool { try { - $type::get($data); + $type::from($data); } catch (\Throwable $t) { return false; } @@ -40,13 +36,11 @@ public function supportsDenormalization($data, $type, $format = null, array $con } /** - * @return array|string|int|float|bool|\ArrayObject|null - * - * @see Enum for $type + * @see \BackedEnum for $type */ - public function denormalize($data, $type, $format = null, array $context = []) + public function denormalize($data, $type, $format = null, array $context = []): \BackedEnum { - return $type::get($data); + return $type::from($data); } public function hasCacheableSupportsMethod(): bool @@ -57,7 +51,7 @@ public function hasCacheableSupportsMethod(): bool public function getSupportedTypes(?string $format): array { return [ - Enum::class => true, + \BackedEnum::class => true, ]; } } diff --git a/src/Service/ParcelShopsService.php b/src/Service/ParcelShopsService.php index b1ca216..6256e85 100644 --- a/src/Service/ParcelShopsService.php +++ b/src/Service/ParcelShopsService.php @@ -15,13 +15,10 @@ class ParcelShopsService { - 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, + ) { } /** @@ -37,7 +34,7 @@ public function get(): array /** @var ParcelShopsResponse $parcelShopsResult */ $parcelShopsResult = $this->serializer->decodeResponse(ParcelShopsResponse::class, $response); - if (!$parcelShopsResult->getStatus()->is(StatusResult::ok())) { + if (StatusResult::Ok !== $parcelShopsResult->getStatus()) { throw new BadRequestException($parcelShopsResult); } diff --git a/src/Service/PlacesService.php b/src/Service/PlacesService.php index c4409d4..ee5a6bf 100644 --- a/src/Service/PlacesService.php +++ b/src/Service/PlacesService.php @@ -15,13 +15,10 @@ class PlacesService { - 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, + ) { } /** @@ -37,7 +34,7 @@ public function get(?string $zipCode = null, ?string $name = null, ?bool $approx /** @var PlacesResponse $placesResponse */ $placesResponse = $this->serializer->decodeResponse(PlacesResponse::class, $response); - if (!$placesResponse->getStatus()->is(StatusResult::ok())) { + if (StatusResult::Ok !== $placesResponse->getStatus()) { throw new BadRequestException($placesResponse); } diff --git a/tests/Acceptance/DependencyInjection/ConfigurationTest.php b/tests/Acceptance/DependencyInjection/ConfigurationTest.php index 79af0c6..fddf745 100644 --- a/tests/Acceptance/DependencyInjection/ConfigurationTest.php +++ b/tests/Acceptance/DependencyInjection/ConfigurationTest.php @@ -8,6 +8,8 @@ use Answear\OverseasBundle\DependencyInjection\AnswearOverseasExtension; use Answear\OverseasBundle\DependencyInjection\Configuration; use Matthias\SymfonyConfigTest\PhpUnit\ConfigurationTestCaseTrait; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; @@ -16,10 +18,8 @@ class ConfigurationTest extends TestCase { use ConfigurationTestCaseTrait; - /** - * @test - * @dataProvider provideValidConfig - */ + #[Test] + #[DataProvider('provideValidConfig')] public function validTest(array $configs): void { $this->assertConfigurationIsValid($configs); @@ -35,10 +35,8 @@ public function validTest(array $configs): void self::assertSame((string) $configs[0]['apiKey'], $configProviderDefinition->getArgument(1)); } - /** - * @test - * @dataProvider provideInvalidConfig - */ + #[Test] + #[DataProvider('provideInvalidConfig')] public function invalid(array $config, ?string $expectedMessage = null): void { $this->assertConfigurationIsInvalid( @@ -47,10 +45,8 @@ public function invalid(array $config, ?string $expectedMessage = null): void ); } - /** - * @test - * @dataProvider provideMoreInvalidConfig - */ + #[Test] + #[DataProvider('provideMoreInvalidConfig')] public function moreInvalidTest(array $configs, \Throwable $expectedException): void { $this->expectException(get_class($expectedException)); @@ -64,7 +60,7 @@ public function moreInvalidTest(array $configs, \Throwable $expectedException): $extension->load($configs, $builder); } - public function provideInvalidConfig(): iterable + public static function provideInvalidConfig(): iterable { yield [ [ @@ -102,7 +98,7 @@ public function provideInvalidConfig(): iterable ]; } - public function provideMoreInvalidConfig(): iterable + public static function provideMoreInvalidConfig(): iterable { yield [ [ @@ -116,7 +112,7 @@ public function provideMoreInvalidConfig(): iterable ]; } - public function provideValidConfig(): iterable + public static function provideValidConfig(): iterable { yield [ [ diff --git a/tests/Integration/Service/ParcelShopsTest.php b/tests/Integration/Service/ParcelShopsTest.php index c7f0587..5969247 100644 --- a/tests/Integration/Service/ParcelShopsTest.php +++ b/tests/Integration/Service/ParcelShopsTest.php @@ -18,6 +18,7 @@ use Answear\OverseasBundle\Tests\MockGuzzleTrait; use Answear\OverseasBundle\Tests\Util\FileTestUtil; use GuzzleHttp\Psr7\Response; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; @@ -36,9 +37,7 @@ public function setUp(): void $this->serializer = new Serializer(); } - /** - * @test - */ + #[Test] public function successfulGetOffices(): void { $this->client = $this->getClient(); @@ -50,9 +49,7 @@ public function successfulGetOffices(): void $this->assertOfficeSame($service->get()); } - /** - * @test - */ + #[Test] public function badRequestTest(): void { $this->client = $this->getClient(false); @@ -65,8 +62,8 @@ public function badRequestTest(): void $service->get(); } catch (BadRequestException $exception) { self::assertSame('Error occurs.', $exception->getMessage()); - self::assertTrue($exception->getResponse()->getStatus()->is(StatusResult::error())); - self::assertInstanceOf(Error::class, $exception->getResponse()->getError()); + self::assertSame($exception->response->getStatus(), StatusResult::Error); + self::assertInstanceOf(Error::class, $exception->response->getError()); return; } @@ -79,7 +76,7 @@ private function getClient(?bool $withLogger = true): Client return new Client( new RequestTransformer( $this->serializer, - new ConfigProvider(EnvironmentEnum::TEST, 'api-key') + new ConfigProvider(EnvironmentEnum::Test->value, 'api-key') ), new OverseasLogger($withLogger ? $this->getLogger() : new NullLogger()), $this->setupGuzzleClient() @@ -104,7 +101,7 @@ private function assertOfficeSame(array $parcelShops): void $actualWorkingHours = []; foreach ($workingHours as $workingHour) { $actualWorkingHours[] = [ - 'type' => $workingHour->type->getValue(), + 'type' => $workingHour->type->value, 'typeName' => $workingHour->typeName, 'from' => $workingHour->from, 'until' => $workingHour->until, diff --git a/tests/Integration/Service/PlacesTest.php b/tests/Integration/Service/PlacesTest.php index 9dea1d3..16bf8ea 100644 --- a/tests/Integration/Service/PlacesTest.php +++ b/tests/Integration/Service/PlacesTest.php @@ -18,6 +18,8 @@ use Answear\OverseasBundle\Tests\MockGuzzleTrait; use Answear\OverseasBundle\Tests\Util\FileTestUtil; use GuzzleHttp\Psr7\Response; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; @@ -36,10 +38,8 @@ public function setUp(): void $this->serializer = new Serializer(); } - /** - * @test - * @dataProvider providePlacesParams - */ + #[Test] + #[DataProvider('providePlacesParams')] public function successfulPlaces(?string $zipCode = null, ?string $name = null, ?bool $approx = null): void { $this->client = $this->getClient( @@ -58,7 +58,7 @@ public function successfulPlaces(?string $zipCode = null, ?string $name = null, $this->assertPlacesSame($service->get($zipCode, $name, $approx)); } - public function providePlacesParams(): iterable + public static function providePlacesParams(): iterable { yield []; @@ -69,9 +69,7 @@ public function providePlacesParams(): iterable yield ['23764', 'name', true]; } - /** - * @test - */ + #[Test] public function badRequestTest(): void { $this->client = $this->getClient(false); @@ -84,8 +82,8 @@ public function badRequestTest(): void $service->get(); } catch (BadRequestException $exception) { self::assertSame('Error occurs.', $exception->getMessage()); - self::assertTrue($exception->getResponse()->getStatus()->is(StatusResult::error())); - self::assertInstanceOf(Error::class, $exception->getResponse()->getError()); + self::assertSame($exception->response->getStatus(), StatusResult::Error); + self::assertInstanceOf(Error::class, $exception->response->getError()); return; } @@ -93,9 +91,7 @@ public function badRequestTest(): void $this->fail('BadRequestException expected.'); } - /** - * @test - */ + #[Test] public function invalidZipcodeTest(): void { $this->client = $this->getClient(false); @@ -113,7 +109,7 @@ private function getClient(?bool $withLogger = true, array $uriParams = []): Cli return new Client( new RequestTransformer( $this->serializer, - new ConfigProvider(EnvironmentEnum::TEST, 'api-key') + new ConfigProvider(EnvironmentEnum::Test->value, 'api-key') ), new OverseasLogger($withLogger ? $this->getLogger($uriParams) : new NullLogger()), $this->setupGuzzleClient() diff --git a/tests/MockGuzzleTrait.php b/tests/MockGuzzleTrait.php index ea6de4c..03b45c3 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/Response/DTO/ParcelShopTest.php b/tests/Unit/Response/DTO/ParcelShopTest.php index fee6cad..1079e68 100644 --- a/tests/Unit/Response/DTO/ParcelShopTest.php +++ b/tests/Unit/Response/DTO/ParcelShopTest.php @@ -7,14 +7,14 @@ use Answear\OverseasBundle\Enum\DayType; use Answear\OverseasBundle\Response\DTO\ParcelShop; use Answear\OverseasBundle\Response\DTO\WorkingHours; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; class ParcelShopTest extends TestCase { - /** - * @dataProvider provideWorkingHoursSortedCorrectly - * @test - */ + #[Test] + #[DataProvider('provideWorkingHoursSortedCorrectly')] public function workingHoursSortedCorrectly(array $workingHours, array $expected): void { $shop = new ParcelShop(); @@ -22,17 +22,17 @@ public function workingHoursSortedCorrectly(array $workingHours, array $expected self::assertSame($expected, $shop->getSortedWorkingHours()); } - public function provideWorkingHoursSortedCorrectly(): iterable + public static function provideWorkingHoursSortedCorrectly(): iterable { $nulledType = new WorkingHours(); - $workingDays = $this->createWorkingHours(DayType::workingDay()); - $monday = $this->createWorkingHours(DayType::monday()); - $tuesday = $this->createWorkingHours(DayType::tuesday()); - $wednesday = $this->createWorkingHours(DayType::wednesday()); - $thursday = $this->createWorkingHours(DayType::thursday()); - $friday = $this->createWorkingHours(DayType::friday()); - $saturday = $this->createWorkingHours(DayType::saturday()); - $sunday = $this->createWorkingHours(DayType::sunday()); + $workingDays = self::createWorkingHours(DayType::WorkingDay); + $monday = self::createWorkingHours(DayType::Monday); + $tuesday = self::createWorkingHours(DayType::Tuesday); + $wednesday = self::createWorkingHours(DayType::Wednesday); + $thursday = self::createWorkingHours(DayType::Thursday); + $friday = self::createWorkingHours(DayType::Friday); + $saturday = self::createWorkingHours(DayType::Saturday); + $sunday = self::createWorkingHours(DayType::Sunday); yield 'with working days, already sorted' => [ [$workingDays, $saturday, $sunday], @@ -55,15 +55,15 @@ public function provideWorkingHoursSortedCorrectly(): iterable [$nulledType, $workingDays, $saturday, $sunday], ]; - $saturdayFirstPart = $this->createWorkingHours(DayType::saturday(), '09:00', '11:00'); - $saturdaySecondPart = $this->createWorkingHours(DayType::saturday(), '14:00', '16:00'); + $saturdayFirstPart = self::createWorkingHours(DayType::Saturday, '09:00', '11:00'); + $saturdaySecondPart = self::createWorkingHours(DayType::Saturday, '14:00', '16:00'); yield 'day parts' => [ [$saturdaySecondPart, $workingDays, $saturdayFirstPart, $sunday], [$workingDays, $saturdayFirstPart, $saturdaySecondPart, $sunday], ]; } - private function createWorkingHours(DayType $dayType, string $from = '09:00', string $until = '17:00'): WorkingHours + private static function createWorkingHours(DayType $dayType, string $from = '09:00', string $until = '17:00'): WorkingHours { $workingHours = new WorkingHours(); $workingHours->setType($dayType); From 7ff467b392a77d170084d5b18cdea709ec619f6a Mon Sep 17 00:00:00 2001 From: Wiktor Ramut Date: Wed, 2 Oct 2024 14:34:12 +0200 Subject: [PATCH 2/2] Review fixes --- src/Enum/DayType.php | 7 +------ src/Enum/EnvironmentEnum.php | 7 +------ src/Enum/StatusResult.php | 7 +------ 3 files changed, 3 insertions(+), 18 deletions(-) diff --git a/src/Enum/DayType.php b/src/Enum/DayType.php index d770542..02729cf 100644 --- a/src/Enum/DayType.php +++ b/src/Enum/DayType.php @@ -4,7 +4,7 @@ namespace Answear\OverseasBundle\Enum; -enum DayType: int implements \JsonSerializable +enum DayType: int { case WorkingDay = 1; case Monday = 7; @@ -16,11 +16,6 @@ enum DayType: int implements \JsonSerializable case Saturday2 = 12; case Sunday = 3; - public function jsonSerialize(): int - { - return $this->value; - } - public function getOrdinal(): int { return match ($this) { diff --git a/src/Enum/EnvironmentEnum.php b/src/Enum/EnvironmentEnum.php index b63e93f..f947236 100644 --- a/src/Enum/EnvironmentEnum.php +++ b/src/Enum/EnvironmentEnum.php @@ -4,16 +4,11 @@ namespace Answear\OverseasBundle\Enum; -enum EnvironmentEnum: string implements \JsonSerializable +enum EnvironmentEnum: string { case Prod = 'prod'; case Test = 'test'; - public function jsonSerialize(): string - { - return $this->value; - } - /** * @return string[] */ diff --git a/src/Enum/StatusResult.php b/src/Enum/StatusResult.php index b9c3385..63e88ec 100644 --- a/src/Enum/StatusResult.php +++ b/src/Enum/StatusResult.php @@ -4,14 +4,9 @@ namespace Answear\OverseasBundle\Enum; -enum StatusResult: int implements \JsonSerializable +enum StatusResult: int { case Ok = 0; case Error = 1; case ValidationFailed = 2; - - public function jsonSerialize(): int - { - return $this->value; - } }