Skip to content

Commit

Permalink
chore: normalize SubscriptionItemPriceChangeDetails
Browse files Browse the repository at this point in the history
wip: serializer

wip: drop ambigiuous test

Update composer.lock

Update TestCase.php

move Data converter to root

wip: rename normalizer

wip: time normalizer

wip: code refactoring

wip: add tests
  • Loading branch information
imdhemy committed Nov 10, 2024
1 parent 582ebb5 commit 5bfb97e
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 151 deletions.
28 changes: 14 additions & 14 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

declare(strict_types=1);

namespace Imdhemy\GooglePlay\Normalizer;

use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;

final readonly class Normalizer
{
public function __construct(private SerializerInterface $serializer)
{
}

public static function create(): self
{
return new self(new Serializer([
new TimeNormalizer(),
new ObjectNormalizer(),
], [new JsonEncoder()]));
}

/**
* Deserializes data into the given type.
*
* @template TObject of object
* @template TType of string|class-string<TObject>
*
* @param TType $type
*
* @psalm-return (TType is class-string<TObject> ? TObject : mixed)
*
* @phpstan-return ($type is class-string<TObject> ? TObject : mixed)
*
* @psalm-suppress MixedReturnStatement
*/
public function normalize(array $data, string $type): mixed
{
$json = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR);

return $this->serializer->deserialize($json, $type, 'json');
}
}
32 changes: 32 additions & 0 deletions src/Normalizer/TimeNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Imdhemy\GooglePlay\Normalizer;

use Imdhemy\GooglePlay\ValueObjects\Time;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

final class TimeNormalizer implements DenormalizerInterface
{
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): Time
{
assert(is_string($data));

return new Time($data);
}

public function supportsDenormalization(
mixed $data,
string $type,
?string $format = null,
array $context = [],
): bool {
return Time::class === $type && is_string($data);
}

public function getSupportedTypes(?string $format): array
{
return [
Time::class => true,
];
}
}
34 changes: 0 additions & 34 deletions src/Serializer.php

This file was deleted.

32 changes: 0 additions & 32 deletions src/Serializer/DataConverter.php

This file was deleted.

22 changes: 0 additions & 22 deletions src/Serializer/DataConverterInterface.php

This file was deleted.

1 change: 1 addition & 0 deletions src/ValueObjects/SubscriptionItemPriceChangeDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function __construct(
public Money $newPrice,
public string $priceChangeMode,
public string $priceChangeState,
public Time $expectedNewPriceChargeTime,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

declare(strict_types=1);

namespace Tests\Serializer;
namespace Tests\Normalizer;

use Imdhemy\GooglePlay\Serializer\DataConverter;
use Imdhemy\GooglePlay\Normalizer\Normalizer;
use Tests\TestCase;

final class DataConverterTest extends TestCase
final class NormalizerTest extends TestCase
{
/** @test */
public function convert(): void
{
$data = ['name' => $this->faker->name()];

$instance = DataConverter::create()->convert($data, MyValueObject::class);
$instance = Normalizer::create()->normalize($data, MyValueObject::class);

$this->assertInstanceOf(MyValueObject::class, $instance);
$this->assertEquals($data['name'], $instance->name);
Expand Down
54 changes: 54 additions & 0 deletions tests/Normalizer/TimeNormalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Tests\Normalizer;

use Imdhemy\GooglePlay\Normalizer\TimeNormalizer;
use Imdhemy\GooglePlay\ValueObjects\Time;
use Tests\TestCase;

final class TimeNormalizerTest extends TestCase
{
/** @test */
public function denormalize(): void
{
$zulu = '2021-09-01T00:00:00Z';
$sut = new TimeNormalizer();

$time = $sut->denormalize($zulu, Time::class);

$this->assertEquals($zulu, $time->originalValue);
}

/**
* @test
*
* @dataProvider provide_data_for_supports_denormalization
*/
public function supports_denormalization(mixed $data, string $type, bool $expected): void
{
$sut = new TimeNormalizer();

$actual = $sut->supportsDenormalization($data, $type);

$this->assertEquals($expected, $actual);
}

public static function provide_data_for_supports_denormalization(): array
{
return [
'valid' => ['data' => '2021-09-01T00:00:00Z', 'type' => Time::class, 'expected' => true],
'different_type' => ['data' => '2021-09-01T00:00:00Z', 'type' => 'string', 'expected' => false],
'different_data' => ['data' => 123, 'type' => Time::class, 'expected' => false],
];
}

/** @test */
public function get_supported_types(): void
{
$sut = new TimeNormalizer();

$actual = $sut->getSupportedTypes(null);

$this->assertEquals([Time::class => true], $actual);
}
}
6 changes: 1 addition & 5 deletions tests/Purchase/Subscription/SubscriptionPurchaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Tests\Purchase\Subscription;

use Imdhemy\GooglePlay\Purchase\Subscription\SubscriptionPurchase;
use Imdhemy\GooglePlay\Serializer;
use Tests\TestCase;

final class SubscriptionPurchaseTest extends TestCase
Expand All @@ -18,10 +17,7 @@ public function create(): void
'regionCode' => $this->faker->countryCode(),
];

$actual = Serializer::create()->deserialize(
$this->jsonEncode($data),
SubscriptionPurchase::class
);
$actual = $this->normalizer->normalize($data, SubscriptionPurchase::class);

$this->assertSame($data['kind'], $actual->kind);
$this->assertSame($data['regionCode'], $actual->regionCode);
Expand Down
Loading

0 comments on commit 5bfb97e

Please sign in to comment.