Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace sylius calendar #15499

Merged
merged 1 commit into from Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -126,7 +126,7 @@
</service>

<service id="Sylius\Bundle\ApiBundle\Serializer\CustomerDenormalizer">
<argument type="service" id="Sylius\Calendar\Provider\DateTimeProviderInterface" />
<argument type="service" id="clock" />
<tag name="serializer.normalizer" priority="64" />
</service>
</services>
Expand Down
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