diff --git a/features/account/customer_account/customer_profile_validation.feature b/features/account/customer_account/customer_profile_validation.feature index 655257ee03e..07c94095478 100644 --- a/features/account/customer_account/customer_profile_validation.feature +++ b/features/account/customer_account/customer_profile_validation.feature @@ -10,7 +10,7 @@ Feature: Customer profile validation And there is a customer "Francis Underwood" identified by an email "francis@underwood.com" and a password "whitehouse" And I am logged in as "francis@underwood.com" - @ui + @ui @api Scenario: Trying to remove my first name When I want to modify my profile And I remove the first name @@ -18,7 +18,7 @@ Feature: Customer profile validation Then I should be notified that the first name is required And my name should still be "Francis Underwood" - @ui + @ui @api Scenario: Trying to remove my last name When I want to modify my profile And I remove the last name @@ -26,7 +26,7 @@ Feature: Customer profile validation Then I should be notified that the last name is required And my name should still be "Francis Underwood" - @ui + @ui @api Scenario: Trying to remove my email When I want to modify my profile And I remove the customer email @@ -34,7 +34,7 @@ Feature: Customer profile validation Then I should be notified that the email is required And my email should still be "francis@underwood.com" - @ui + @ui @api Scenario: Trying to change my email to an existing value When I want to modify my profile And I specify the customer email as "claire@underwood.com" @@ -42,7 +42,7 @@ Feature: Customer profile validation Then I should be notified that the email is already used And my email should still be "francis@underwood.com" - @ui + @ui @api Scenario: Trying to change my email to an invalid value When I want to modify my profile And I specify the customer email as "francisunderwood" diff --git a/features/account/customer_account/editing_customer_profile.feature b/features/account/customer_account/editing_customer_profile.feature index 9884eb4c380..16c6606ed4f 100644 --- a/features/account/customer_account/editing_customer_profile.feature +++ b/features/account/customer_account/editing_customer_profile.feature @@ -9,7 +9,7 @@ Feature: Editing a customer profile And there is a customer "Francis Underwood" identified by an email "francis@underwood.com" and a password "whitehouse" And I am logged in as "francis@underwood.com" - @ui + @ui @api Scenario: Changing my first name and last name When I want to modify my profile And I specify the first name as "Will" @@ -18,7 +18,7 @@ Feature: Editing a customer profile Then I should be notified that it has been successfully edited And my name should be "Will Conway" - @ui + @ui @api Scenario: Changing my email When I want to modify my profile And I specify the customer email as "frank@underwood.com" diff --git a/src/Sylius/Behat/Context/Api/Shop/AccountContext.php b/src/Sylius/Behat/Context/Api/Shop/AccountContext.php new file mode 100644 index 00000000000..ebec0c3ea3b --- /dev/null +++ b/src/Sylius/Behat/Context/Api/Shop/AccountContext.php @@ -0,0 +1,195 @@ +accountClient = $accountClient; + $this->sharedStorage = $sharedStorage; + $this->responseChecker = $responseChecker; + } + + /** + * @When I want to modify my profile + */ + public function iWantToModifyMyProfile(): void + { + /** @var ShopUserInterface $shopUser */ + $shopUser = $this->sharedStorage->get('user'); + $customer = $shopUser->getCustomer(); + + $this->accountClient->buildUpdateRequest((string) $customer->getId()); + } + + /** + * @When I specify the first name as :firstName + * @When I remove the first name + */ + public function iSpecifyTheFirstName(string $firstName = ''): void + { + $this->accountClient->addRequestData('firstName', $firstName); + } + + /** + * @When I specify the last name as :lastName + * @When I remove the last name + */ + public function iSpecifyTheLastName(string $lastName = ''): void + { + $this->accountClient->addRequestData('lastName', $lastName); + } + + /** + * @When I specify the customer email as :email + * @When I remove the customer email + */ + public function iSpecifyCustomerTheEmail(string $email = ''): void + { + $this->accountClient->addRequestData('email', $email); + } + + /** + * @When I save my changes + * @When I try to save my changes + */ + public function iSaveMyChanges(): void + { + $this->accountClient->update(); + } + + /** + * @Then I should be notified that it has been successfully edited + */ + public function iShouldBeNotifiedThatItHasBeenSuccessfullyEdited(): void + { + Assert::true($this->responseChecker->isUpdateSuccessful($this->accountClient->getLastResponse())); + } + + /** + * @Then my email should be :email + * @Then my email should still be :email + */ + public function myEmailShouldBe(string $email): void + { + /** @var ShopUserInterface $shopUser */ + $shopUser = $this->sharedStorage->get('user'); + + $response = $this->accountClient->show((string) $shopUser->getCustomer()->getId()); + + Assert::true($this->responseChecker->hasValue($response, 'email', $email)); + } + + /** + * @Then my name should be :name + * @Then my name should still be :name + */ + public function myNameShouldBe(string $name): void + { + /** @var ShopUserInterface $shopUser */ + $shopUser = $this->sharedStorage->get('user'); + + $response = $this->accountClient->show((string) $shopUser->getCustomer()->getId()); + + Assert::true($this->responseChecker->hasValue($response, 'fullName', $name)); + } + + /** + * @Then I should be notified that the first name is required + */ + public function iShouldBeNotifiedThatFirstNameIsRequired(): void + { + $this->isViolationWithMessageInResponse( + $this->accountClient->getLastResponse(), + 'First name must be at least 2 characters long.' + ); + } + + /** + * @Then I should be notified that the last name is required + */ + public function iShouldBeNotifiedThatLastNameIsRequired(): void + { + $this->isViolationWithMessageInResponse( + $this->accountClient->getLastResponse(), + 'Last name must be at least 2 characters long.' + ); + } + + /** + * @Then I should be notified that the email is required + */ + public function iShouldBeNotifiedThatEmailIsRequired(): void + { + $this->isViolationWithMessageInResponse( + $this->accountClient->getLastResponse(), + 'Please enter your email.' + ); + } + + /** + * @Then I should be notified that the email is already used + */ + public function iShouldBeNotifiedThatTheEmailIsAlreadyUsed(): void + { + $this->isViolationWithMessageInResponse( + $this->accountClient->getLastResponse(), + 'This email is already used.' + ); + } + + /** + * @Then I should be notified that the email is invalid + */ + public function iShouldBeNotifiedThatElementIsInvalid(): void + { + $this->isViolationWithMessageInResponse( + $this->accountClient->getLastResponse(), + 'This email is invalid.' + ); + } + + private function isViolationWithMessageInResponse(Response $response, string $message): bool + { + $violations = $this->responseChecker->getResponseContent($response)['violations']; + foreach ($violations as $violation) { + if ($violation['message'] === $message) { + return true; + } + } + + return false; + } +} diff --git a/src/Sylius/Behat/Context/Setup/ShopSecurityContext.php b/src/Sylius/Behat/Context/Setup/ShopSecurityContext.php index 153b44fcd6b..e778c2422a1 100644 --- a/src/Sylius/Behat/Context/Setup/ShopSecurityContext.php +++ b/src/Sylius/Behat/Context/Setup/ShopSecurityContext.php @@ -49,19 +49,21 @@ public function __construct( /** * @Given I am logged in as :email */ - public function iAmLoggedInAs($email) + public function iAmLoggedInAs(string $email): void { $user = $this->userRepository->findOneByEmail($email); Assert::notNull($user); $this->securityService->logIn($user); + + $this->sharedStorage->set('user', $user); } /** * @Given I am a logged in customer * @Given the customer logged in */ - public function iAmLoggedInCustomer() + public function iAmLoggedInCustomer(): void { $user = $this->userFactory->create(['email' => 'sylius@example.com', 'password' => 'sylius', 'enabled' => true]); $this->userRepository->add($user); diff --git a/src/Sylius/Behat/Resources/config/services/api.xml b/src/Sylius/Behat/Resources/config/services/api.xml index e75c0c474c6..c04cea37a08 100644 --- a/src/Sylius/Behat/Resources/config/services/api.xml +++ b/src/Sylius/Behat/Resources/config/services/api.xml @@ -138,6 +138,11 @@ admin + + customers + shop + + diff --git a/src/Sylius/Behat/Resources/config/services/contexts/api/shop.xml b/src/Sylius/Behat/Resources/config/services/contexts/api/shop.xml index ff4471bfef4..0b08695cb35 100644 --- a/src/Sylius/Behat/Resources/config/services/contexts/api/shop.xml +++ b/src/Sylius/Behat/Resources/config/services/contexts/api/shop.xml @@ -70,5 +70,11 @@ + + + + + + diff --git a/src/Sylius/Behat/Resources/config/suites.yml b/src/Sylius/Behat/Resources/config/suites.yml index 951c7f60ae5..f0d088c3914 100644 --- a/src/Sylius/Behat/Resources/config/suites.yml +++ b/src/Sylius/Behat/Resources/config/suites.yml @@ -2,6 +2,7 @@ # (c) Paweł Jędrzejewski imports: + - suites/api/account/customer.yml - suites/api/account/customer_registration.yml - suites/api/account/login.yml - suites/api/addressing/managing_countries.yml diff --git a/src/Sylius/Behat/Resources/config/suites/api/account/customer.yml b/src/Sylius/Behat/Resources/config/suites/api/account/customer.yml new file mode 100644 index 00000000000..0c063bcd01d --- /dev/null +++ b/src/Sylius/Behat/Resources/config/suites/api/account/customer.yml @@ -0,0 +1,23 @@ +# This file is part of the Sylius package. +# (c) Paweł Jędrzejewski + +default: + suites: + api_customer_account: + contexts: + - sylius.behat.context.hook.doctrine_orm + + - sylius.behat.context.transform.country + - sylius.behat.context.transform.customer + - sylius.behat.context.transform.user + + - sylius.behat.context.setup.currency + - sylius.behat.context.setup.customer + - sylius.behat.context.setup.shop_security + - sylius.behat.context.setup.user + - sylius.behat.context.setup.channel + + - sylius.behat.context.api.shop.account + + filters: + tags: "@customer_account && @api" diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Customer.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Customer.xml index 0ebce543d21..2f09aea6fef 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Customer.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Customer.xml @@ -16,7 +16,8 @@ xsi:schemaLocation="https://api-platform.com/schema/metadata https://api-platform.com/schema/metadata/metadata-2.0.xsd" > - admin + + sylius @@ -29,13 +30,33 @@ GET + /admin/customers/{id} + + customer:read + + + + + GET + /shop/customers/{id} customer:read + + + PUT + /shop/customers/{id} + + customer:update + + - + + + + diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/Customer.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/Customer.xml index 099ef2c71de..f7efdc45277 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/Customer.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/Customer.xml @@ -22,6 +22,21 @@ customer:read + customer:update + + + + customer:read + customer:update + + + + customer:read + customer:update + + + + customer:read