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

Fixing the customer birthday #548

Merged
merged 2 commits into from
Sep 21, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions spec/Command/Customer/UpdateCustomerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions spec/Handler/Customer/UpdateCustomerHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,14 +24,16 @@ 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');

$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();
Expand All @@ -42,7 +45,7 @@ function it_updates_customer(
'Sherlock',
'Holmes',
'sherlock@holmes.com',
'2017-11-01',
$birthday,
'm',
'091231512512',
true
Expand Down
7 changes: 4 additions & 3 deletions src/Command/Customer/UpdateCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sylius\ShopApiPlugin\Command\Customer;

use DateTimeImmutable;
use Sylius\ShopApiPlugin\Command\CommandInterface;

class UpdateCustomer implements CommandInterface
Expand All @@ -17,7 +18,7 @@ class UpdateCustomer implements CommandInterface
/** @var string */
protected $email;

/** @var string|null */
/** @var DateTimeImmutable|null */
protected $birthday;

/** @var string */
Expand All @@ -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;
Expand All @@ -55,7 +56,7 @@ public function email(): string
return $this->email;
}

public function birthday(): ?string
public function birthday(): ?\DateTimeImmutable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function birthday(): ?\DateTimeImmutable
public function birthday(): ?DateTimeImmutable

as it is already imported

{
return $this->birthday;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Handler/Customer/UpdateCustomerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
lchrusciel marked this conversation as resolved.
Show resolved Hide resolved
$customer->setPhoneNumber($command->phoneNumber());
$customer->setSubscribedToNewsletter($command->subscribedToNewsletter());

Expand Down
7 changes: 5 additions & 2 deletions src/Request/Customer/UpdateCustomerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,7 +21,7 @@ class UpdateCustomerRequest implements RequestInterface
/** @var string|null */
protected $email;

/** @var DateTimeInterface|null */
/** @var DateTimeImmutable|null */
protected $birthday;

/** @var string */
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion tests/Request/UpdateCustomerRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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))
Expand Down