diff --git a/spec/Command/Customer/UpdateCustomerSpec.php b/spec/Command/Customer/UpdateCustomerSpec.php index e22f6bc92..fa2be81f9 100644 --- a/spec/Command/Customer/UpdateCustomerSpec.php +++ b/spec/Command/Customer/UpdateCustomerSpec.php @@ -4,17 +4,18 @@ namespace spec\Sylius\ShopApiPlugin\Command\Customer; +use DateTimeImmutable; use PhpSpec\ObjectBehavior; final class UpdateCustomerSpec extends ObjectBehavior { - function let(): void + function let(DateTimeImmutable $birthday): void { $this->beConstructedWith( 'Sherlock', 'Holmes', 'sherlock@holmes.com', - '2017-11-01', + $birthday, 'm', '091231512512', true @@ -41,9 +42,9 @@ function it_has_gender(): void $this->gender()->shouldReturn('m'); } - function it_has_birthday(): void + function it_has_birthday(DateTimeImmutable $birthday): void { - $this->birthday()->shouldReturn('2017-11-01'); + $this->birthday()->shouldReturn($birthday); } function it_has_phone_number(): void diff --git a/spec/Handler/Customer/UpdateCustomerHandlerSpec.php b/spec/Handler/Customer/UpdateCustomerHandlerSpec.php index 8ed6424ad..e8a55fdb6 100644 --- a/spec/Handler/Customer/UpdateCustomerHandlerSpec.php +++ b/spec/Handler/Customer/UpdateCustomerHandlerSpec.php @@ -4,6 +4,7 @@ namespace spec\Sylius\ShopApiPlugin\Handler\Customer; +use DateTimeImmutable; use PhpSpec\ObjectBehavior; use Sylius\Component\Core\Model\CustomerInterface; use Sylius\Component\Resource\Repository\RepositoryInterface; @@ -23,6 +24,8 @@ function it_updates_customer( RepositoryInterface $customerRepository, CustomerInterface $customer ): void { + $birthday = new DateTimeImmutable('2019-02-10 10:22:00'); + $customerRepository->findOneBy(['email' => 'sherlock@holmes.com'])->willReturn($customer); $customer->getId()->willReturn('USER_ID'); @@ -30,7 +33,7 @@ function it_updates_customer( $customer->setFirstName('Sherlock')->shouldBeCalled(); $customer->setLastName('Holmes')->shouldBeCalled(); $customer->setEmail('sherlock@holmes.com')->shouldBeCalled(); - $customer->setBirthday(new \DateTimeImmutable('2017-11-01'))->shouldBeCalled(); + $customer->setBirthday($birthday)->shouldBeCalled(); $customer->setGender('m')->shouldBeCalled(); $customer->setPhoneNumber('091231512512')->shouldBeCalled(); $customer->setSubscribedToNewsletter(true)->shouldBeCalled(); @@ -42,7 +45,7 @@ function it_updates_customer( 'Sherlock', 'Holmes', 'sherlock@holmes.com', - '2017-11-01', + $birthday, 'm', '091231512512', true diff --git a/src/Command/Customer/UpdateCustomer.php b/src/Command/Customer/UpdateCustomer.php index 752449564..6f1737ba5 100644 --- a/src/Command/Customer/UpdateCustomer.php +++ b/src/Command/Customer/UpdateCustomer.php @@ -4,6 +4,7 @@ namespace Sylius\ShopApiPlugin\Command\Customer; +use DateTimeImmutable; use Sylius\ShopApiPlugin\Command\CommandInterface; class UpdateCustomer implements CommandInterface @@ -17,7 +18,7 @@ class UpdateCustomer implements CommandInterface /** @var string */ protected $email; - /** @var string|null */ + /** @var DateTimeImmutable|null */ protected $birthday; /** @var string */ @@ -29,7 +30,7 @@ class UpdateCustomer implements CommandInterface /** @var bool */ protected $subscribedToNewsletter; - public function __construct(string $firstName, string $lastName, string $email, ?string $birthday, string $gender, ?string $phoneNumber, ?bool $subscribedToNewsletter) + public function __construct(string $firstName, string $lastName, string $email, ?DateTimeImmutable $birthday, string $gender, ?string $phoneNumber, ?bool $subscribedToNewsletter) { $this->firstName = $firstName; $this->lastName = $lastName; @@ -55,7 +56,7 @@ public function email(): string return $this->email; } - public function birthday(): ?string + public function birthday(): ?\DateTimeImmutable { return $this->birthday; } diff --git a/src/Handler/Customer/UpdateCustomerHandler.php b/src/Handler/Customer/UpdateCustomerHandler.php index 53b81fd8f..94c565b7e 100644 --- a/src/Handler/Customer/UpdateCustomerHandler.php +++ b/src/Handler/Customer/UpdateCustomerHandler.php @@ -28,7 +28,7 @@ public function __invoke(UpdateCustomer $command): void $customer->setLastName($command->lastName()); $customer->setEmail($command->email()); $customer->setGender($command->gender()); - $customer->setBirthday(new \DateTimeImmutable($command->birthday())); + $customer->setBirthday($command->birthday()); $customer->setPhoneNumber($command->phoneNumber()); $customer->setSubscribedToNewsletter($command->subscribedToNewsletter()); diff --git a/src/Request/Customer/UpdateCustomerRequest.php b/src/Request/Customer/UpdateCustomerRequest.php index 8f61d7937..9f20224f1 100644 --- a/src/Request/Customer/UpdateCustomerRequest.php +++ b/src/Request/Customer/UpdateCustomerRequest.php @@ -4,7 +4,7 @@ namespace Sylius\ShopApiPlugin\Request\Customer; -use DateTimeInterface; +use DateTimeImmutable; use Sylius\ShopApiPlugin\Command\CommandInterface; use Sylius\ShopApiPlugin\Command\Customer\UpdateCustomer; use Sylius\ShopApiPlugin\Request\RequestInterface; @@ -21,7 +21,7 @@ class UpdateCustomerRequest implements RequestInterface /** @var string|null */ protected $email; - /** @var DateTimeInterface|null */ + /** @var DateTimeImmutable|null */ protected $birthday; /** @var string */ @@ -39,6 +39,9 @@ protected function __construct(Request $request) $this->lastName = $request->request->get('lastName'); $this->email = $request->request->get('email'); $this->birthday = $request->request->get('birthday'); + if ($this->birthday !== null) { + $this->birthday = new DateTimeImmutable($this->birthday); + } $this->gender = $request->request->get('gender'); $this->phoneNumber = $request->request->get('phoneNumber'); $this->subscribedToNewsletter = $request->request->getBoolean('subscribedToNewsletter') ?? false; diff --git a/tests/Request/UpdateCustomerRequestTest.php b/tests/Request/UpdateCustomerRequestTest.php index 6b085a0d5..f72daf4ff 100644 --- a/tests/Request/UpdateCustomerRequestTest.php +++ b/tests/Request/UpdateCustomerRequestTest.php @@ -4,6 +4,7 @@ namespace Tests\Sylius\ShopApiPlugin\Request; +use DateTimeImmutable; use PHPUnit\Framework\TestCase; use Sylius\ShopApiPlugin\Command\Customer\UpdateCustomer; use Sylius\ShopApiPlugin\Request\Customer\UpdateCustomerRequest; @@ -30,7 +31,7 @@ public function it_creates_update_customer_command() 'ivan', 'Mts', 'ivan.matas@locastic.com', - '2017-11-01', + new DateTimeImmutable('2017-11-01'), 'm', '125125112', true))