Skip to content

Commit

Permalink
[ApiBundle] Replace the sylius/calendar with the symfony/clock at Cus…
Browse files Browse the repository at this point in the history
…tomerDenormalizer class
  • Loading branch information
Wojdylak committed Nov 5, 2023
1 parent 17de1b8 commit 1f6f340
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
Expand Up @@ -13,8 +13,8 @@

namespace Sylius\Bundle\ApiBundle\Serializer;

use Sylius\Calendar\Provider\DateTimeProviderInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
Expand All @@ -26,7 +26,7 @@ final class CustomerDenormalizer implements ContextAwareDenormalizerInterface, D

private const ALREADY_CALLED = 'sylius_customer_denormalizer_already_called';

public function __construct(private DateTimeProviderInterface $calendar)
public function __construct(private ClockInterface $clock)
{
}

Expand All @@ -46,7 +46,7 @@ public function denormalize(mixed $data, string $type, string $format = null, ar

$user = $data['user'] ?? null;
if (null !== $user && array_key_exists('verified', $user)) {
$data['user']['verified'] = true === $user['verified'] ? $this->calendar->now()->format(\DateTimeInterface::RFC3339) : null;
$data['user']['verified'] = true === $user['verified'] ? $this->clock->now()->format(\DateTimeInterface::RFC3339) : null;
}

return $this->denormalizer->denormalize($data, $type, $format, $context);
Expand Down
Expand Up @@ -14,17 +14,17 @@
namespace spec\Sylius\Bundle\ApiBundle\Serializer;

use PhpSpec\ObjectBehavior;
use Sylius\Calendar\Provider\DateTimeProviderInterface;
use Sylius\Component\Customer\Model\CustomerInterface;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

final class CustomerDenormalizerSpec extends ObjectBehavior
{
private const ALREADY_CALLED = 'sylius_customer_denormalizer_already_called';

function let(DateTimeProviderInterface $calendar): void
function let(ClockInterface $clock): void
{
$this->beConstructedWith($calendar);
$this->beConstructedWith($clock);
}

function it_does_not_support_denormalization_when_the_denormalizer_has_already_been_called(): void
Expand All @@ -44,39 +44,39 @@ function it_does_not_support_denormalization_when_type_is_not_a_customer(): void

function it_does_nothing_if_user_verified_is_not_set(
DenormalizerInterface $denormalizer,
DateTimeProviderInterface $calendar,
ClockInterface $clock,
CustomerInterface $customer,
): void {
$this->setDenormalizer($denormalizer);
$denormalizer->denormalize([], CustomerInterface::class, null, [self::ALREADY_CALLED => true])->willReturn($customer);

$this->denormalize([], CustomerInterface::class)->shouldReturn($customer);

$calendar->now()->shouldNotHaveBeenCalled();
$clock->now()->shouldNotHaveBeenCalled();
}

public function it_changes_user_verified_from_false_to_null(
DenormalizerInterface $denormalizer,
DateTimeProviderInterface $calendar,
ClockInterface $clock,
CustomerInterface $customer,
): void {
$this->setDenormalizer($denormalizer);
$denormalizer->denormalize(['user' => ['verified' => null]], CustomerInterface::class, null, [self::ALREADY_CALLED => true])->willReturn($customer);

$this->denormalize(['user' => ['verified' => false]], CustomerInterface::class)->shouldReturn($customer);

$calendar->now()->shouldNotHaveBeenCalled();
$clock->now()->shouldNotHaveBeenCalled();
}

public function it_changes_user_verified_from_true_to_datetime(
DenormalizerInterface $denormalizer,
DateTimeProviderInterface $calendar,
ClockInterface $clock,
CustomerInterface $customer,
): void {
$this->setDenormalizer($denormalizer);

$dateTime = new \DateTime('2021-01-01T00:00:00+00:00');
$calendar->now()->willReturn($dateTime);
$dateTime = new \DateTimeImmutable('2021-01-01T00:00:00+00:00');
$clock->now()->willReturn($dateTime);
$denormalizer->denormalize(['user' => ['verified' => '2021-01-01T00:00:00+00:00']], CustomerInterface::class, null, [self::ALREADY_CALLED => true])->willReturn($customer);

$this->denormalize(['user' => ['verified' => true]], CustomerInterface::class)->shouldReturn($customer);
Expand Down

0 comments on commit 1f6f340

Please sign in to comment.