diff --git a/UPGRADE.md b/UPGRADE.md index 049abdf59..bdec96714 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -48,6 +48,12 @@ * The argument in constructor of `PickupCartRequest` class has been changed from `Request $request `to `string channelCode`. +* The address-book create route has been changed : + + | Old Route | New route | + |:--------------------------------------|:---------------------------------------| + | `address-book` | `address-book/` | + # UPGRADE FROM 1.0.0-beta.17 to 1.0.0-beta.18 * Customer registration payload changed: diff --git a/spec/Command/AddressBook/CreateAddressSpec.php b/spec/Command/AddressBook/CreateAddressSpec.php deleted file mode 100644 index ecdb797ea..000000000 --- a/spec/Command/AddressBook/CreateAddressSpec.php +++ /dev/null @@ -1,37 +0,0 @@ -beConstructedWith(Address::createFromArray([ - 'firstName' => 'Sherlock', - 'lastName' => 'Holmes', - 'city' => 'London', - 'street' => 'Baker Street 221b', - 'countryCode' => 'GB', - 'postcode' => 'NWB', - 'provinceName' => 'Greater London', - 'provinceCode' => 'GB-GL', - 'phoneNumber' => '0912538092', - 'company' => 'Sherlock ltd.', - ]), 'user@email.com'); - } - - function it_has_address(): void - { - $this->address()->shouldBeAnInstanceOf(Address::class); - } - - function it_has_user_email(): void - { - $this->userEmail()->shouldReturn('user@email.com'); - } -} diff --git a/spec/Command/AddressBook/RemoveAddressSpec.php b/spec/Command/AddressBook/RemoveAddressSpec.php deleted file mode 100644 index e62d71c41..000000000 --- a/spec/Command/AddressBook/RemoveAddressSpec.php +++ /dev/null @@ -1,25 +0,0 @@ -beConstructedWith('ADDRESS_ID', 'user@email.com'); - } - - function it_has_id(): void - { - $this->id()->shouldReturn('ADDRESS_ID'); - } - - function it_has_user_email(): void - { - $this->userEmail()->shouldReturn('user@email.com'); - } -} diff --git a/spec/Command/AddressBook/UpdateAddressSpec.php b/spec/Command/AddressBook/UpdateAddressSpec.php deleted file mode 100644 index a423aa4bd..000000000 --- a/spec/Command/AddressBook/UpdateAddressSpec.php +++ /dev/null @@ -1,62 +0,0 @@ -shouldHaveType(UpdateAddress::class); - } - - function let(): void - { - $this->beConstructedWith(Address::createFromArray([ - 'id' => 'ADDRESS_ID', - 'firstName' => 'Sherlock', - 'lastName' => 'Holmes', - 'city' => 'London', - 'street' => 'Baker Street 221b', - 'countryCode' => 'GB', - 'postcode' => 'NWB', - 'provinceName' => 'Greater London', - 'provinceCode' => 'GB-GL', - 'phoneNumber' => '0912538092', - 'company' => 'Sherlock ltd.', - ]), 'user@email.com', 'ADDRESS_ID'); - } - - function it_has_address_id(): void - { - $this->id()->shouldReturn('ADDRESS_ID'); - } - - function it_has_an_address(): void - { - $address = Address::createFromArray([ - 'id' => 'ADDRESS_ID', - 'firstName' => 'Sherlock', - 'lastName' => 'Holmes', - 'city' => 'London', - 'street' => 'Baker Street 221b', - 'countryCode' => 'GB', - 'postcode' => 'NWB', - 'provinceName' => 'Greater London', - 'provinceCode' => 'GB-GL', - 'phoneNumber' => '0912538092', - 'company' => 'Sherlock ltd.', - ]); - $this->address()->shouldBeLike($address); - } - - function it_has_user_email(): void - { - $this->userEmail()->shouldReturn('user@email.com'); - } -} diff --git a/spec/Factory/AddressBook/AddressBookViewFactorySpec.php b/spec/Factory/AddressBook/AddressBookViewFactorySpec.php deleted file mode 100644 index 72623c5e7..000000000 --- a/spec/Factory/AddressBook/AddressBookViewFactorySpec.php +++ /dev/null @@ -1,57 +0,0 @@ -beConstructedWith(AddressBookView::class); - } - - function it_is_initializable(): void - { - $this->shouldHaveType(AddressBookViewFactory::class); - } - - function it_creates_address_book_view(AddressInterface $address, CustomerInterface $customer): void - { - $address->getId()->willReturn('ADDRESS_ID'); - $address->getFirstName()->willReturn('Sherlock'); - $address->getLastName()->willReturn('Holmes'); - $address->getCountryCode()->willReturn('GB'); - $address->getCity()->willReturn('London'); - $address->getStreet()->willReturn('Baker Street 221b'); - $address->getPostcode()->willReturn('NMW'); - $address->getProvinceName()->willReturn('Greater London'); - $address->getProvinceCode()->willReturn('GB-GL'); - $address->getPhoneNumber()->willReturn('0912538092'); - $address->getCompany()->willReturn('Sherlock ltd.'); - - $customer->getDefaultAddress()->willReturn($address); - - $addressBookView = new AddressBookView(); - $addressBookView->id = 'ADDRESS_ID'; - $addressBookView->firstName = 'Sherlock'; - $addressBookView->lastName = 'Holmes'; - $addressBookView->countryCode = 'GB'; - $addressBookView->city = 'London'; - $addressBookView->street = 'Baker Street 221b'; - $addressBookView->postcode = 'NMW'; - $addressBookView->provinceName = 'Greater London'; - $addressBookView->provinceCode = 'GB-GL'; - $addressBookView->phoneNumber = '0912538092'; - $addressBookView->company = 'Sherlock ltd.'; - $addressBookView->default = true; - - $this->create($address, $customer)->shouldBeLike($addressBookView); - } -} diff --git a/spec/Handler/AddressBook/CreateAddressHandlerSpec.php b/spec/Handler/AddressBook/CreateAddressHandlerSpec.php deleted file mode 100644 index ffbe8cef9..000000000 --- a/spec/Handler/AddressBook/CreateAddressHandlerSpec.php +++ /dev/null @@ -1,56 +0,0 @@ -beConstructedWith( - $addressRepository, - $customerRepository, - $addressMapper - ); - } - - function it_creates_new_address_for_current_user( - CustomerInterface $customer, - RepositoryInterface $addressRepository, - RepositoryInterface $customerRepository, - AddressMapperInterface $addressMapper, - AddressInterface $address - ): void { - $addressData = Address::createFromArray([ - 'firstName' => 'Sherlock', - 'lastName' => 'Holmes', - 'city' => 'London', - 'street' => 'Baker Street 221b', - 'countryCode' => 'GB', - 'postcode' => 'NWB', - 'company' => 'Sherlock ltd.', - 'phoneNumber' => '0912538092', - ]); - - $customerRepository->findOneBy(['email' => 'user@email.com'])->willReturn($customer); - - $addressMapper->map($addressData)->willReturn($address); - - $customer->addAddress($address)->shouldBeCalled(); - $addressRepository->add($address)->shouldBeCalled(); - - $this(new CreateAddress($addressData, 'user@email.com')); - } -} diff --git a/spec/Handler/AddressBook/RemoveAddressHandlerSpec.php b/spec/Handler/AddressBook/RemoveAddressHandlerSpec.php deleted file mode 100644 index f2c6aa41f..000000000 --- a/spec/Handler/AddressBook/RemoveAddressHandlerSpec.php +++ /dev/null @@ -1,35 +0,0 @@ -beConstructedWith( - $addressRepository, - $orderRepository - ); - } - - function it_removes_address_from_address_book( - AddressRepositoryInterface $addressRepository, - AddressInterface $address - ): void { - $addressRepository->findOneBy(['id' => 'ADDRESS_ID'])->willReturn($address); - - $addressRepository->remove($address)->shouldBeCalled(); - - $this(new RemoveAddress('ADDRESS_ID', 'user@email.com')); - } -} diff --git a/spec/Handler/AddressBook/UpdateAddressBookAddressHandlerSpec.php b/spec/Handler/AddressBook/UpdateAddressBookAddressHandlerSpec.php deleted file mode 100644 index ccc5b7f16..000000000 --- a/spec/Handler/AddressBook/UpdateAddressBookAddressHandlerSpec.php +++ /dev/null @@ -1,135 +0,0 @@ -beConstructedWith( - $addressRepository, - $shopUserRepository, - $addressMapper - ); - } - - function it_updates_address( - ShopUserInterface $shopUser, - CustomerInterface $customer, - RepositoryInterface $addressRepository, - RepositoryInterface $shopUserRepository, - AddressMapperInterface $addressMapper, - AddressInterface $address - ): void { - $shopUserRepository->findOneBy(['username' => 'user@email.com'])->willReturn($shopUser); - $addressRepository->findOneBy(['id' => 'ADDRESS_ID'])->willReturn($address); - - $address->getCustomer()->willReturn($customer); - $shopUser->getCustomer()->willReturn($customer); - - $customer->getId()->willReturn('USER_ID'); - $shopUser->getId()->willReturn('USER_ID'); - - $addressData = Address::createFromArray([ - 'id' => 'ADDRESS_ID', - 'firstName' => 'Sherlock', - 'lastName' => 'Holmes', - 'city' => 'London', - 'street' => 'Baker Street 221b', - 'countryCode' => 'GB', - 'postcode' => 'NWB', - 'provinceCode' => 'GB-GL', - 'company' => 'Sherlock ltd.', - 'phoneNumber' => '0912538092', - ]); - $addressMapper->mapExisting($address, $addressData)->willReturn($address); - - $addressRepository->add($address)->shouldBeCalled(); - - $this(new UpdateAddress($addressData, 'user@email.com', 'ADDRESS_ID')); - } - - function it_throws_an_exception_if_current_user_is_not_address_owner( - ShopUserInterface $shopUser, - CustomerInterface $customer, - CustomerInterface $otherCustomer, - RepositoryInterface $addressRepository, - RepositoryInterface $shopUserRepository, - AddressMapperInterface $addressMapper, - AddressInterface $address - ): void { - $shopUserRepository->findOneBy(['username' => 'user@email.com'])->willReturn($shopUser); - $addressRepository->findOneBy(['id' => 'ADDRESS_ID'])->willReturn($address); - - $address->getCustomer()->willReturn($customer); - $shopUser->getCustomer()->willReturn($otherCustomer); - - $addressMapper->mapExisting(Argument::any())->shouldNotBeCalled(); - - $addressRepository->add($address)->shouldNotBeCalled(); - - $this->shouldThrow(\InvalidArgumentException::class)->during('__invoke', [new UpdateAddress(Address::createFromArray([ - 'id' => 'ADDRESS_ID', - 'firstName' => 'Sherlock', - 'lastName' => 'Holmes', - 'city' => 'London', - 'street' => 'Baker Street 221b', - 'countryCode' => 'GB', - 'postcode' => 'NWB', - 'phoneNumber' => '0912538092', - 'company' => 'Sherlock ltd.', - ]), 'user@email.com', 'ADDRESS_ID')]); - } - - function it_throws_an_exception_if_the_address_is_invalid( - ShopUserInterface $shopUser, - CustomerInterface $customer, - RepositoryInterface $addressRepository, - RepositoryInterface $shopUserRepository, - AddressMapperInterface $addressMapper, - AddressInterface $address - ): void { - $shopUserRepository->findOneBy(['username' => 'user@email.com'])->willReturn($shopUser); - $addressRepository->findOneBy(['id' => 'ADDRESS_ID'])->willReturn($address); - - $address->getCustomer()->willReturn($customer); - $shopUser->getCustomer()->willReturn($customer); - - $customer->getId()->willReturn('USER_ID'); - $shopUser->getId()->willReturn('USER_ID'); - - $addressData = Address::createFromArray([ - 'id' => 'ADDRESS_ID', - 'firstName' => 'Sherlock', - 'lastName' => 'Holmes', - 'city' => 'London', - 'street' => 'Baker Street 221b', - 'countryCode' => 'GB', - 'postcode' => 'NWB', - 'provinceCode' => 'WRONG_PROVINCE_CODE', - 'phoneNumber' => '0912538092', - 'company' => 'Sherlock ltd.', - ]); - $addressMapper->mapExisting($address, $addressData)->willThrow(\InvalidArgumentException::class); - - $addressRepository->add($address)->shouldNotBeCalled(); - - $this->shouldThrow(\InvalidArgumentException::class) - ->during('__invoke', [new UpdateAddress($addressData, 'user@email.com', 'ADDRESS_ID')]); - } -} diff --git a/src/Command/AddressBook/CreateAddress.php b/src/Command/AddressBook/CreateAddress.php deleted file mode 100644 index ad3f6006e..000000000 --- a/src/Command/AddressBook/CreateAddress.php +++ /dev/null @@ -1,32 +0,0 @@ -address = $address; - $this->userEmail = $userEmail; - } - - public function address(): Address - { - return $this->address; - } - - public function userEmail(): string - { - return $this->userEmail; - } -} diff --git a/src/Command/AddressBook/RemoveAddress.php b/src/Command/AddressBook/RemoveAddress.php deleted file mode 100644 index efacaf8f8..000000000 --- a/src/Command/AddressBook/RemoveAddress.php +++ /dev/null @@ -1,30 +0,0 @@ -id = $id; - $this->userEmail = $userEmail; - } - - public function id() - { - return $this->id; - } - - public function userEmail(): string - { - return $this->userEmail; - } -} diff --git a/src/Command/AddressBook/UpdateAddress.php b/src/Command/AddressBook/UpdateAddress.php deleted file mode 100644 index b1fa337f5..000000000 --- a/src/Command/AddressBook/UpdateAddress.php +++ /dev/null @@ -1,41 +0,0 @@ -address = $address; - $this->userEmail = $userEmail; - $this->addressId = $addressId; - } - - public function id(): string - { - return $this->addressId; - } - - public function address(): Address - { - return $this->address; - } - - public function userEmail(): string - { - return $this->userEmail; - } -} diff --git a/src/Controller/AddressBook/CreateAddressAction.php b/src/Controller/AddressBook/CreateAddressAction.php deleted file mode 100644 index 35e7a5a0d..000000000 --- a/src/Controller/AddressBook/CreateAddressAction.php +++ /dev/null @@ -1,100 +0,0 @@ -viewHandler = $viewHandler; - $this->bus = $bus; - $this->validator = $validator; - $this->validationErrorViewFactory = $validationErrorViewFactory; - $this->addressViewFactory = $addressViewFactory; - $this->addressRepository = $addressRepository; - $this->loggedInUserProvider = $loggedInUserProvider; - } - - public function __invoke(Request $request): Response - { - $addressModel = Address::createFromRequest($request); - - $validationResults = $this->validator->validate($addressModel); - - if (0 !== count($validationResults)) { - return $this->viewHandler->handle( - View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST) - ); - } - - try { - /** @var ShopUserInterface $user */ - $user = $this->loggedInUserProvider->provide(); - } catch (TokenNotFoundException $exception) { - return $this->viewHandler->handle(View::create(null, Response::HTTP_UNAUTHORIZED)); - } - - if (($customer = $user->getCustomer()) !== null) { - $this->bus->dispatch(new CreateAddress($addressModel, $user->getEmail())); - - return $this->viewHandler->handle(View::create($this->getLastInsertedAddress($customer), Response::HTTP_CREATED)); - } - - return $this->viewHandler->handle(View::create(['message' => 'The user is not a customer'], Response::HTTP_BAD_REQUEST)); - } - - /** Returns the id that was inserted last in the address book */ - private function getLastInsertedAddress(Customer $customer): AddressBookView - { - $addresses = $this->addressRepository->findByCustomer($customer); - - return $this->addressViewFactory->create(end($addresses), $customer); - } -} diff --git a/src/Controller/AddressBook/RemoveAddressAction.php b/src/Controller/AddressBook/RemoveAddressAction.php deleted file mode 100644 index a240f4396..000000000 --- a/src/Controller/AddressBook/RemoveAddressAction.php +++ /dev/null @@ -1,77 +0,0 @@ -viewHandler = $viewHandler; - $this->validator = $validator; - $this->validationErrorViewFactory = $validationErrorViewFactory; - $this->bus = $bus; - $this->loggedInUserProvider = $loggedInUserProvider; - } - - public function __invoke(Request $request): Response - { - try { - /** @var ShopUserInterface $user */ - $user = $this->loggedInUserProvider->provide(); - } catch (TokenNotFoundException $exception) { - return $this->viewHandler->handle(View::create(null, Response::HTTP_UNAUTHORIZED)); - } - - $removeAddressRequest = new RemoveAddressRequest($request, $user->getEmail()); - - $validationResults = $this->validator->validate($removeAddressRequest); - - if (0 !== count($validationResults)) { - return $this->viewHandler->handle( - View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST) - ); - } - - if ($user->getCustomer() !== null) { - $this->bus->dispatch($removeAddressRequest->getCommand()); - - return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT)); - } - - return $this->viewHandler->handle(View::create(['message' => 'The user is not a customer'], Response::HTTP_BAD_REQUEST)); - } -} diff --git a/src/Controller/AddressBook/ShowAddressBookAction.php b/src/Controller/AddressBook/ShowAddressBookAction.php deleted file mode 100644 index fa3d45778..000000000 --- a/src/Controller/AddressBook/ShowAddressBookAction.php +++ /dev/null @@ -1,62 +0,0 @@ -viewHandler = $viewHandler; - $this->loggedInUserProvider = $loggedInUserProvider; - $this->addressBookViewFactory = $addressBookViewFactory; - } - - /** Returns the list of addresses that is stored in the user's address book */ - public function __invoke(): Response - { - try { - /** @var ShopUserInterface $user */ - $user = $this->loggedInUserProvider->provide(); - } catch (TokenNotFoundException $exception) { - return $this->viewHandler->handle(View::create(null, Response::HTTP_UNAUTHORIZED)); - } - - $customer = $user->getCustomer(); - - if ($customer instanceof Customer) { - $addressViews = $customer->getAddresses()->map( - function (AddressInterface $address) use ($customer) { - return $this->addressBookViewFactory->create($address, $customer); - } - ); - - return $this->viewHandler->handle(View::create($addressViews, Response::HTTP_OK)); - } - - return $this->viewHandler->handle(View::create(['message' => 'The user is not a customer'], Response::HTTP_BAD_REQUEST)); - } -} diff --git a/src/Controller/AddressBook/UpdateAddressAction.php b/src/Controller/AddressBook/UpdateAddressAction.php deleted file mode 100644 index fa5957cc8..000000000 --- a/src/Controller/AddressBook/UpdateAddressAction.php +++ /dev/null @@ -1,100 +0,0 @@ -viewHandler = $viewHandler; - $this->validator = $validator; - $this->bus = $bus; - $this->validationErrorViewFactory = $validationErrorViewFactory; - $this->addressBookViewFactory = $addressViewFactory; - $this->addressRepository = $addressRepository; - $this->loggedInUserProvider = $loggedInUserProvider; - } - - /** - * @param string|int $id - */ - public function __invoke(Request $request, $id): Response - { - $addressModel = Address::createFromRequest($request); - - $validationResults = $this->validator->validate($addressModel); - - if (0 !== count($validationResults)) { - return $this->viewHandler->handle( - View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST) - ); - } - - try { - /** @var ShopUserInterface $user */ - $user = $this->loggedInUserProvider->provide(); - } catch (TokenNotFoundException $exception) { - return $this->viewHandler->handle(View::create(null, Response::HTTP_UNAUTHORIZED)); - } - - if ($user->getCustomer() !== null) { - $this->bus->dispatch(new UpdateAddress($addressModel, $user->getEmail(), $id)); - - /** @var AddressInterface $updatedAddress */ - $updatedAddress = $this->addressRepository->findOneBy(['id' => $id]); - - return $this->viewHandler->handle(View::create( - $this->addressBookViewFactory->create($updatedAddress, $user->getCustomer()), - Response::HTTP_OK - )); - } - - return $this->viewHandler->handle(View::create(['message' => 'The user is not a customer'], Response::HTTP_BAD_REQUEST)); - } -} diff --git a/src/Factory/AddressBook/AddressBookViewFactory.php b/src/Factory/AddressBook/AddressBookViewFactory.php deleted file mode 100644 index 3a8e60990..000000000 --- a/src/Factory/AddressBook/AddressBookViewFactory.php +++ /dev/null @@ -1,42 +0,0 @@ -addressBookViewClass = $addressBookViewClass; - } - - /** {@inheritdoc} */ - public function create(AddressInterface $address, CustomerInterface $customer): AddressBookView - { - /** @var AddressBookView $addressBookView */ - $addressBookView = new $this->addressBookViewClass(); - - $addressBookView->id = $address->getId(); - $addressBookView->firstName = $address->getFirstName(); - $addressBookView->lastName = $address->getLastName(); - $addressBookView->countryCode = $address->getCountryCode(); - $addressBookView->city = $address->getCity(); - $addressBookView->street = $address->getStreet(); - $addressBookView->postcode = $address->getPostcode(); - $addressBookView->provinceName = $address->getProvinceName(); - $addressBookView->provinceCode = $address->getProvinceCode(); - $addressBookView->company = $address->getCompany(); - $addressBookView->phoneNumber = $address->getPhoneNumber(); - $addressBookView->default = $address === $customer->getDefaultAddress(); - - return $addressBookView; - } -} diff --git a/src/Factory/AddressBook/AddressBookViewFactoryInterface.php b/src/Factory/AddressBook/AddressBookViewFactoryInterface.php deleted file mode 100644 index f60887656..000000000 --- a/src/Factory/AddressBook/AddressBookViewFactoryInterface.php +++ /dev/null @@ -1,14 +0,0 @@ -addressRepository = $addressRepository; - $this->customerRepository = $customerRepository; - $this->addressMapper = $addressMapper; - } - - public function __invoke(CreateAddress $command): void - { - /** @var CustomerInterface|null $customer */ - $customer = $this->customerRepository->findOneBy(['email' => $command->userEmail()]); - Assert::notNull($customer); - - $address = $this->addressMapper->map($command->address()); - - $customer->addAddress($address); - $this->addressRepository->add($address); - } -} diff --git a/src/Handler/AddressBook/RemoveAddressHandler.php b/src/Handler/AddressBook/RemoveAddressHandler.php deleted file mode 100644 index e324c10c7..000000000 --- a/src/Handler/AddressBook/RemoveAddressHandler.php +++ /dev/null @@ -1,33 +0,0 @@ -addressRepository = $addressRepository; - $this->orderRepository = $orderRepository; - } - - public function __invoke(RemoveAddress $removeAddress): void - { - /** @var AddressInterface $address */ - $address = $this->addressRepository->findOneBy(['id' => $removeAddress->id()]); - - $this->addressRepository->remove($address); - } -} diff --git a/src/Handler/AddressBook/UpdateAddressBookAddressHandler.php b/src/Handler/AddressBook/UpdateAddressBookAddressHandler.php deleted file mode 100644 index 419bfa0bc..000000000 --- a/src/Handler/AddressBook/UpdateAddressBookAddressHandler.php +++ /dev/null @@ -1,56 +0,0 @@ -addressRepository = $addressRepository; - $this->shopUserRepository = $shopUserRepository; - $this->addressMapper = $addressMapper; - } - - public function __invoke(UpdateAddress $command): void - { - /** @var AddressInterface $address */ - $address = $this->addressRepository->findOneBy(['id' => $command->id()]); - Assert::notNull($address, 'Address does not exist'); - - /** @var ShopUserInterface $shopUser */ - $shopUser = $this->shopUserRepository->findOneBy(['username' => $command->userEmail()]); - - $this->assertCurrentUserIsOwner($address, $shopUser); - - $address = $this->addressMapper->mapExisting($address, $command->address()); - - $this->addressRepository->add($address); - } - - private function assertCurrentUserIsOwner(AddressInterface $address, ShopUserInterface $user) - { - Assert::notNull($address->getCustomer(), 'Address is not associated with any user'); - Assert::eq($address->getCustomer(), $user->getCustomer(), 'Current user is not owner of this address'); - } -} diff --git a/src/Request/AddressBook/RemoveAddressRequest.php b/src/Request/AddressBook/RemoveAddressRequest.php deleted file mode 100644 index 4d583fdf1..000000000 --- a/src/Request/AddressBook/RemoveAddressRequest.php +++ /dev/null @@ -1,28 +0,0 @@ -id = $request->attributes->get('id'); - $this->userEmail = $userEmail; - } - - public function getCommand(): RemoveAddress - { - return new RemoveAddress($this->id, $this->userEmail); - } -} diff --git a/src/Resources/config/app/config.yml b/src/Resources/config/app/config.yml index f13cbfe89..9da4ccade 100644 --- a/src/Resources/config/app/config.yml +++ b/src/Resources/config/app/config.yml @@ -10,6 +10,9 @@ jms_serializer: sylius-shop-api-placed-order: namespace_prefix: "Sylius\\ShopApiPlugin\\View\\Order" path : "@SyliusShopApiPlugin/Resources/config/serializer/Order" + sylius-addressing: + namespace_prefix: "Sylius\\Component\\Addressing" + path: "@SyliusShopApiPlugin/Resources/config/serializer/Address" framework: messenger: diff --git a/src/Resources/config/routing/address_book.yml b/src/Resources/config/routing/address_book.yml index da8b9224c..1555f254f 100644 --- a/src/Resources/config/routing/address_book.yml +++ b/src/Resources/config/routing/address_book.yml @@ -1,29 +1,53 @@ -sylius_shop_api_show_address_book: - path: /address-book - methods: [GET] +sylius_shop_api_address_book_create: + path: /address-book/ + controller: sylius.controller.address:createAction + methods: [POST] defaults: - _controller: sylius.shop_api_plugin.controller.address_book.show_address_book_action + _sylius: + factory: + method: createForCustomer + arguments: + - "expr:service('sylius.context.customer').getCustomer()" -sylius_shop_api_create_address_book_entry: - path: /address-book - methods: [POST] +sylius_shop_api_address_book_index: + path: /address-book/ + controller: sylius.controller.address:indexAction + methods: [GET] defaults: - _controller: sylius.shop_api_plugin.controller.address_book.create_address_action + _sylius: + paginate: false + repository: + method: findByCustomer + arguments: + - "expr:notFoundOnNull(service('sylius.context.customer').getCustomer())" -sylius_shop_api_remove_address_book_entry: +sylius_shop_api_address_book_delete: path: /address-book/{id} + controller: sylius.controller.address:deleteAction methods: [DELETE] defaults: - _controller: sylius.shop_api_plugin.controller.address_book.remove_address_action + _sylius: + repository: + method: findOneByCustomer + arguments: + - $id + - "expr:service('sylius.context.customer').getCustomer()" + csrf_protection: false + +sylius_shop_api_address_book_update: + path: /address-book/{id} + controller: sylius.controller.address:updateAction + methods: [PUT, PATCH] + defaults: + _sylius: + repository: + method: findOneByCustomer + arguments: + - $id + - "expr:service('sylius.context.customer').getCustomer()" sylius_shop_api_set_default_address: path: /address-book/{id}/default methods: [PATCH] defaults: _controller: sylius.shop_api_plugin.controller.address_book.set_default_address_action - -sylius_shop_api_update_address_book_entry: - path: /address-book/{id} - methods: [PUT] - defaults: - _controller: sylius.shop_api_plugin.controller.address_book.update_address_action diff --git a/src/Resources/config/serializer/Address/Model.Address.yml b/src/Resources/config/serializer/Address/Model.Address.yml new file mode 100644 index 000000000..f65324635 --- /dev/null +++ b/src/Resources/config/serializer/Address/Model.Address.yml @@ -0,0 +1,48 @@ +Sylius\Component\Addressing\Model\Address: + exclusion_policy: ALL + xml_root_name: address + properties: + id: + expose: true + type: integer + xml_attribute: true + firstName: + expose: true + type: string + groups: [Default, Detailed] + lastName: + expose: true + type: string + groups: [Default, Detailed] + street: + expose: true + type: string + groups: [Default, Detailed] + city: + expose: true + type: string + groups: [Default, Detailed] + postcode: + expose: true + type: string + groups: [Default, Detailed] + phoneNumber: + expose: true + type: string + groups: [Default, Detailed] + countryCode: + expose: true + type: string + groups: [Default, Detailed] + provinceCode: + expose: true + type: string + groups: [Default, Detailed] + provinceName: + expose: true + type: string + groups: [Default, Detailed] + company: + expose: true + type: string + groups: [Default, Detailed] diff --git a/src/Resources/config/services/actions/address_book.xml b/src/Resources/config/services/actions/address_book.xml index 5c98fce32..b6a657628 100644 --- a/src/Resources/config/services/actions/address_book.xml +++ b/src/Resources/config/services/actions/address_book.xml @@ -4,48 +4,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/Resources/config/services/handler/address_book.xml b/src/Resources/config/services/handler/address_book.xml index 309655cff..eda19aa03 100644 --- a/src/Resources/config/services/handler/address_book.xml +++ b/src/Resources/config/services/handler/address_book.xml @@ -2,21 +2,6 @@ - - - - - - - - - - - - - @@ -24,13 +9,5 @@ - - - - - - - diff --git a/src/View/AddressBook/AddressBookView.php b/src/View/AddressBook/AddressBookView.php deleted file mode 100644 index 98541e948..000000000 --- a/src/View/AddressBook/AddressBookView.php +++ /dev/null @@ -1,44 +0,0 @@ -get('sylius.repository.address'); /** @var AddressInterface $address */ - $address = $addressRepository->findOneBy(['street' => 'Kupreska 12']); + $address = $addressRepository->findOneBy(['street' => 'New street']); - Assert::assertSame($address->getCustomer(), $customer); + Assert::assertSame($address->getCustomer()->getId(), $customer->getId()); Assert::assertNotNull($address); Assert::assertTrue($customer->hasAddress($address)); } @@ -127,12 +128,12 @@ public function it_does_not_allow_user_to_add_new_address_to_address_book_withou JSON; $response = $this->createAddress($data); - $this->assertResponse($response, 'address_book/validation_create_address_book_with_wrong_province_response', Response::HTTP_INTERNAL_SERVER_ERROR); + $this->assertResponse($response, 'address_book/validation_create_address_book_with_wrong_province_response', Response::HTTP_BAD_REQUEST); } private function createAddress(string $data): Response { - $this->client->request('POST', '/shop-api/address-book', [], [], self::CONTENT_TYPE_HEADER, $data); + $this->client->request('POST', '/shop-api/address-book/', [], [], self::CONTENT_TYPE_HEADER, $data); return $this->client->getResponse(); } diff --git a/tests/Controller/AddressBook/AddressBookRemoveAddressApiTest.php b/tests/Controller/AddressBook/AddressBookRemoveAddressApiTest.php index 2bf20303b..8d2e7794d 100644 --- a/tests/Controller/AddressBook/AddressBookRemoveAddressApiTest.php +++ b/tests/Controller/AddressBook/AddressBookRemoveAddressApiTest.php @@ -39,13 +39,13 @@ public function it_deletes_address_from_address_book(): void /** * @test */ - public function it_returns_bad_request_exception_if_address_has_not_been_found(): void + public function it_returns_a_not_found_exception_if_address_has_not_been_found(): void { $this->loadFixturesFromFiles(['channel.yml', 'customer.yml', 'country.yml', 'address.yml']); $this->logInUser('oliver@queen.com', '123password'); $response = $this->removeAddress('-1'); - $this->assertResponseCode($response, Response::HTTP_BAD_REQUEST); + $this->assertResponseCode($response, Response::HTTP_NOT_FOUND); } /** @@ -62,7 +62,7 @@ public function it_validates_if_current_user_is_owner_of_address(): void $address = $addressRepository->findOneBy(['street' => 'Vukovarska']); $response = $this->removeAddress((string) $address->getId()); - $this->assertResponseCode($response, Response::HTTP_BAD_REQUEST); + $this->assertResponseCode($response, Response::HTTP_NOT_FOUND); } private function removeAddress(string $id): Response diff --git a/tests/Controller/AddressBook/AddressBookShowApiTest.php b/tests/Controller/AddressBook/AddressBookShowApiTest.php index d3b66d631..4fc210af6 100644 --- a/tests/Controller/AddressBook/AddressBookShowApiTest.php +++ b/tests/Controller/AddressBook/AddressBookShowApiTest.php @@ -27,17 +27,17 @@ public function it_shows_address_book(): void /** * @test */ - public function it_returns_an_unauthorized_exception_if_there_is_no_logged_in_user(): void + public function it_returns_a_not_found_exception_if_there_is_no_logged_in_user(): void { $this->loadFixturesFromFile('channel.yml'); $response = $this->showAddressBook(); - $this->assertResponseCode($response, Response::HTTP_UNAUTHORIZED); + $this->assertResponseCode($response, Response::HTTP_NOT_FOUND); } private function showAddressBook(): Response { - $this->client->request('GET', '/shop-api/address-book', [], [], self::CONTENT_TYPE_HEADER); + $this->client->request('GET', '/shop-api/address-book/', [], [], self::CONTENT_TYPE_HEADER); return $this->client->getResponse(); } diff --git a/tests/Controller/AddressBook/AddressBookUpdateAddressApiTest.php b/tests/Controller/AddressBook/AddressBookUpdateAddressApiTest.php index 487c73ce8..bba8fa3df 100644 --- a/tests/Controller/AddressBook/AddressBookUpdateAddressApiTest.php +++ b/tests/Controller/AddressBook/AddressBookUpdateAddressApiTest.php @@ -44,7 +44,7 @@ public function it_updates_address_in_address_book(): void JSON; $response = $this->updateAddress((string) $address->getId(), $data); - $this->assertResponse($response, 'address_book/update_address', Response::HTTP_OK); + Assert::assertSame($response->getStatusCode(), Response::HTTP_NO_CONTENT); /** @var AddressInterface $updatedAddress */ $updatedAddress = $addressRepository->findOneBy(['id' => $address->getId()]); diff --git a/tests/Responses/Expected/address_book/add_address_response.json b/tests/Responses/Expected/address_book/add_address_response.json index c8b8eac50..ff05f2a85 100644 --- a/tests/Responses/Expected/address_book/add_address_response.json +++ b/tests/Responses/Expected/address_book/add_address_response.json @@ -1,11 +1,38 @@ { "id": @integer@, - "firstName": "Jurica", - "lastName": "Separovic", - "phoneNumber": "091892212", - "city": "Split", - "street": "Kupreska 12", + "firstName": "New name", + "lastName": "New lastName", + "phoneNumber": "0918972132", "countryCode": "GB", - "postcode": "2433", - "default": false + "provinceCode": "GB-WLS", + "street": "New street", + "city": "New city", + "postcode": "2000", + "customer": { + "id": @integer@, + "email": "oliver@queen.com", + "emailCanonical": "oliver@queen.com", + "firstName": "Oliver", + "lastName": "Queen", + "gender": "m", + "group": { + "id": @integer@, + "code": "retail", + "name": "Retail" + }, + "user": { + "id": @integer@, + "username": "oliver@queen.com", + "usernameCanonical": "oliver@queen.com", + "roles": [ + "ROLE_USER" + ], + "enabled": true + }, + "_links": { + "self": { + "href": "@string@.contains('/api/v1/customers/')" + } + } + } } diff --git a/tests/Responses/Expected/address_book/show_address_book_response.json b/tests/Responses/Expected/address_book/show_address_book_response.json index 55e756503..d0081c2fc 100644 --- a/tests/Responses/Expected/address_book/show_address_book_response.json +++ b/tests/Responses/Expected/address_book/show_address_book_response.json @@ -3,39 +3,117 @@ "id": @integer@, "firstName": "Jeanie", "lastName": "Metz", + "phoneNumber": "349713", + "company": "Sylius", "countryCode": "GB", + "provinceCode": "GB-SCT", "street": "McGlynn Island", "city": "Klingside", "postcode": "33553", - "provinceCode": "GB-SCT", - "company": "Sylius", - "phoneNumber": "349713", - "default": false + "customer": { + "id": @integer@, + "email": "oliver@queen.com", + "emailCanonical": "oliver@queen.com", + "firstName": "Oliver", + "lastName": "Queen", + "gender": "m", + "group": { + "id": @integer@, + "code": "retail", + "name": "Retail" + }, + "user": { + "id": @integer@, + "username": "oliver@queen.com", + "usernameCanonical": "oliver@queen.com", + "roles": [ + "ROLE_USER" + ], + "enabled": true + }, + "_links": { + "self": { + "href": "@string@.contains('/api/v1/customers/')" + } + } + } }, { "id": @integer@, "firstName": "Jeanie", "lastName": "Metz", + "phoneNumber": "349713", + "company": "Locastic", "countryCode": "GB", + "provinceCode": "GB-SCT", "street": "Kupreska", "city": "Klingside", "postcode": "33553", - "provinceCode": "GB-SCT", - "company": "Locastic", - "phoneNumber": "349713", - "default": false + "customer": { + "id": @integer@, + "email": "oliver@queen.com", + "emailCanonical": "oliver@queen.com", + "firstName": "Oliver", + "lastName": "Queen", + "gender": "m", + "group": { + "id": @integer@, + "code": "retail", + "name": "Retail" + }, + "user": { + "id": @integer@, + "username": "oliver@queen.com", + "usernameCanonical": "oliver@queen.com", + "roles": [ + "ROLE_USER" + ], + "enabled": true + }, + "_links": { + "self": { + "href": "@string@.contains('/api/v1/customers/')" + } + } + } }, { "id": @integer@, "firstName": "Jeanie", "lastName": "Metz", + "phoneNumber": "349713", + "company": "Locastic", "countryCode": "GB", + "provinceCode": "GB-SCT", "street": "Lurline Springs", "city": "Klingside", "postcode": "33553", - "provinceCode": "GB-SCT", - "company": "Locastic", - "phoneNumber": "349713", - "default": false + "customer": { + "id": @integer@, + "email": "oliver@queen.com", + "emailCanonical": "oliver@queen.com", + "firstName": "Oliver", + "lastName": "Queen", + "gender": "m", + "group": { + "id": @integer@, + "code": "retail", + "name": "Retail" + }, + "user": { + "id": @integer@, + "username": "oliver@queen.com", + "usernameCanonical": "oliver@queen.com", + "roles": [ + "ROLE_USER" + ], + "enabled": true + }, + "_links": { + "self": { + "href": "@string@.contains('/api/v1/customers/')" + } + } + } } ] diff --git a/tests/Responses/Expected/address_book/update_address.json b/tests/Responses/Expected/address_book/update_address.json deleted file mode 100644 index bdae1442d..000000000 --- a/tests/Responses/Expected/address_book/update_address.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "id": "@integer@", - "firstName": "New name", - "lastName": "New lastName", - "countryCode": "GB", - "street": "New street", - "city": "New city", - "postcode": "2000", - "provinceName": "Wales", - "provinceCode": "GB-WLS", - "company": "Locastic", - "phoneNumber": "0918972132", - "default": false -} diff --git a/tests/Responses/Expected/address_book/validation_create_address_book_response.json b/tests/Responses/Expected/address_book/validation_create_address_book_response.json index 5e67524d1..35bf4ec9d 100644 --- a/tests/Responses/Expected/address_book/validation_create_address_book_response.json +++ b/tests/Responses/Expected/address_book/validation_create_address_book_response.json @@ -1,25 +1,40 @@ { "code": 400, - "message": "Validation failed", + "message": "Validation Failed", "errors": { - "firstName": [ - "@string@" - ], - "lastName": [ - "@string@" - ], - "countryCode": [ - "@string@", - "@string@" - ], - "street": [ - "@string@" - ], - "city": [ - "@string@" - ], - "postcode": [ - "@string@" - ] + "children": { + "firstName": { + "errors": [ + "Please enter first name." + ] + }, + "lastName": { + "errors": [ + "Please enter last name." + ] + }, + "phoneNumber": {}, + "company": {}, + "countryCode": { + "errors": [ + "Please select country." + ] + }, + "street": { + "errors": [ + "Please enter street." + ] + }, + "city": { + "errors": [ + "Please enter city." + ] + }, + "postcode": { + "errors": [ + "Please enter postcode." + ] + } + } } } diff --git a/tests/Responses/Expected/address_book/validation_create_address_book_with_wrong_country_response.json b/tests/Responses/Expected/address_book/validation_create_address_book_with_wrong_country_response.json index 1a45546f0..e3f0b81eb 100644 --- a/tests/Responses/Expected/address_book/validation_create_address_book_with_wrong_country_response.json +++ b/tests/Responses/Expected/address_book/validation_create_address_book_with_wrong_country_response.json @@ -1,9 +1,20 @@ { "code": 400, - "message": "Validation failed", + "message": "Validation Failed", "errors": { - "countryCode": [ - "The country does not exist" - ] + "children": { + "firstName": {}, + "lastName": {}, + "phoneNumber": {}, + "company": {}, + "countryCode": { + "errors": [ + "This value is not valid." + ] + }, + "street": {}, + "city": {}, + "postcode": {} + } } } diff --git a/tests/Responses/Expected/address_book/validation_create_address_book_with_wrong_province_response.json b/tests/Responses/Expected/address_book/validation_create_address_book_with_wrong_province_response.json index 0505a0e26..c14a8ffd8 100644 --- a/tests/Responses/Expected/address_book/validation_create_address_book_with_wrong_province_response.json +++ b/tests/Responses/Expected/address_book/validation_create_address_book_with_wrong_province_response.json @@ -1,4 +1,24 @@ { - "code": 500, - "message": "Province does not exist." + "code": 400, + "message": "Validation Failed", + "errors": { + "errors": [ + "Please select proper province." + ], + "children": { + "firstName": {}, + "lastName": {}, + "phoneNumber": {}, + "company": {}, + "countryCode": {}, + "street": {}, + "city": {}, + "postcode": {}, + "provinceCode": { + "errors": [ + "This value is not valid." + ] + } + } + } }