Skip to content

Commit

Permalink
Moving DatetimeCreation to request class
Browse files Browse the repository at this point in the history
  • Loading branch information
mamazu committed Sep 21, 2019
1 parent f010636 commit aebff63
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 29 deletions.
22 changes: 4 additions & 18 deletions spec/Command/Customer/UpdateCustomerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

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 @@ -42,23 +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()->shouldHaveType(DateTimeImmutable::class);
}

function it_has_no_birthday_if_not_provided(): void
{
$this->beConstructedWith('Sherlock',
'Holmes',
'sherlock@holmes.com',
null,
'm',
'091231512512',
true
);

$this->birthday()->shouldReturn(null);
$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
11 changes: 4 additions & 7 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 Down Expand Up @@ -57,11 +58,7 @@ public function email(): string

public function birthday(): ?\DateTimeImmutable
{
if ($this->birthday === null) {
return null;
}

return new \DateTimeImmutable($this->birthday);
return $this->birthday;
}

public function gender(): ?string
Expand Down
6 changes: 5 additions & 1 deletion src/Request/Customer/UpdateCustomerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sylius\ShopApiPlugin\Request\Customer;

use DateTimeImmutable;
use Sylius\ShopApiPlugin\Command\CommandInterface;
use Sylius\ShopApiPlugin\Command\Customer\UpdateCustomer;
use Sylius\ShopApiPlugin\Request\RequestInterface;
Expand All @@ -20,7 +21,7 @@ class UpdateCustomerRequest implements RequestInterface
/** @var string|null */
protected $email;

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

/** @var string */
Expand All @@ -38,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

0 comments on commit aebff63

Please sign in to comment.