[ApiBundle] Add behat tests for customer resources

This commit is contained in:
Wojdylak 2023-10-20 15:28:32 +02:00
parent 2df7d79909
commit b962bfb6fc
10 changed files with 296 additions and 14 deletions

View file

@ -8,7 +8,7 @@ Feature: Adding a new customer
Given the store has a customer group "Retail"
And I am logged in as an administrator
@ui
@ui @api
Scenario: Adding a new customer
When I want to create a new customer
And I specify their email as "l.skywalker@gmail.com"
@ -16,7 +16,7 @@ Feature: Adding a new customer
Then I should be notified that it has been successfully created
And the customer "l.skywalker@gmail.com" should appear in the store
@ui
@ui @api
Scenario: Adding a new customer with full details
When I want to create a new customer
And I specify their first name as "Luke"

View file

@ -7,7 +7,7 @@ Feature: Adding a new customer account
Background:
Given I am logged in as an administrator
@ui @javascript
@ui @javascript @api
Scenario: Adding a new customer with an account
When I want to create a new customer account
And I specify their email as "l.skywalker@gmail.com"
@ -18,7 +18,7 @@ Feature: Adding a new customer account
And the customer "l.skywalker@gmail.com" should appear in the store
And the customer "l.skywalker@gmail.com" should have an account created
@ui @javascript
@ui @javascript @api
Scenario: Creating an account for existing customer
Given the store has customer "Frodo Baggins" with email "f.baggins@example.com"
When I want to edit this customer
@ -28,4 +28,4 @@ Feature: Adding a new customer account
Then I should be notified that it has been successfully edited
And I should not see create account option
And the customer "f.baggins@example.com" should appear in the store
And this customer should have an account created
And the customer "f.baggins@example.com" should have an account created

View file

@ -7,7 +7,7 @@ Feature: Adding a new customer account after failed creation action
Background:
Given I am logged in as an administrator
@ui @javascript
@ui @javascript @api
Scenario: Trying to add new customer with an account without required information
When I want to create a new customer account
And I choose create account option
@ -19,7 +19,7 @@ Feature: Adding a new customer account after failed creation action
And I should be notified that email is required
And I should be notified that password is required
@ui @javascript
@ui @javascript @api
Scenario: Trying to add new customer with an account without email
When I want to create a new customer account
And I choose create account option
@ -31,7 +31,7 @@ Feature: Adding a new customer account after failed creation action
And I should not be able to select create account option
And I should be notified that email is required
@ui @javascript
@ui @javascript @api
Scenario: Trying to add new customer without an account without email
When I want to create a new customer account
And I do not choose create account option
@ -42,7 +42,7 @@ Feature: Adding a new customer account after failed creation action
And I should be able to select create account option
And I should be notified that email is required
@ui @javascript
@ui @javascript @api
Scenario: Trying to add new customer with an account without required information
When I want to create a new customer account
And I choose create account option

View file

@ -7,7 +7,7 @@ Feature: Create account option availability
Background:
And I am logged in as an administrator
@ui
@ui @api
Scenario: Being able to create an account for created customer
When I want to create a new customer
And I do not choose create account option
@ -17,7 +17,7 @@ Feature: Create account option availability
And I should not be able to specify their password
And I should be able to select create account option
@ui @javascript
@ui @javascript @api
Scenario: Not seeing create account option after adding customer with account
When I want to create a new customer account
And I choose create account option

View file

@ -10,7 +10,7 @@ Feature: Browsing customers
And the store has customer "l.skywalker@example.com"
And I am logged in as an administrator
@ui
@ui @api
Scenario: Browsing customers in store
When I want to see all customers in store
Then I should see 3 customers in the list

View file

@ -17,14 +17,14 @@ Feature: Browsing orders of a customer
And the customer chose "Free" shipping method to "United States" with "Cash on Delivery" payment
And I am logged in as an administrator
@ui
@api
Scenario: Browsing orders of a specific customer in the list
When I browse orders of a customer "logan@wolverine.com"
Then I should see a single order in the list
And I should see the order with number "#00000007" in the list
And I should not see the order with number "#00000008" in the list
@ui
@api
Scenario: Browsing orders of a specific customer in the list
When I browse orders of a customer "logan@wolverine.com"
And I sort them by channel

View file

@ -0,0 +1,251 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Behat\Context\Api\Admin;
use ApiPlatform\Api\IriConverterInterface;
use Behat\Behat\Context\Context;
use Sylius\Behat\Client\ApiClientInterface;
use Sylius\Behat\Client\ResponseCheckerInterface;
use Sylius\Behat\Context\Api\Resources;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Customer\Model\CustomerGroupInterface;
use Webmozart\Assert\Assert;
final class ManagingCustomersContext implements Context
{
public function __construct(
private ApiClientInterface $client,
private ResponseCheckerInterface $responseChecker,
private IriConverterInterface $iriConverter,
) {
}
/**
* @When I want to create a new customer
* @When I want to create a new customer account
*/
public function iWantToCreateANewCustomer(): void
{
$this->client->buildCreateRequest(Resources::CUSTOMERS);
}
/**
* @When /^I want to edit (this customer)$/
*/
public function iWantToEditThisCustomer(CustomerInterface $customer): void
{
$this->client->buildUpdateRequest(Resources::CUSTOMERS, (string) $customer->getId());
}
/**
* @When I specify their email as :name
* @When I do not specify their email
*/
public function iSpecifyItsEmailAs(?string $email = null): void
{
if (null !== $email) {
$this->client->addRequestData('email', $email);
}
}
/**
* @When I specify their first name as :name
*/
public function iSpecifyTheirFirstNameAs(string $name): void
{
$this->client->addRequestData('firstName', $name);
}
/**
* @When I specify their last name as :name
*/
public function iSpecifyTheirLastNameAs(string $name): void
{
$this->client->addRequestData('lastName', $name);
}
/**
* @When I specify its birthday as :birthday
*/
public function iSpecifyItsBirthdayAs(string $birthday): void
{
$this->client->addRequestData('birthday', $birthday);
}
/**
* @When I select :gender as its gender
*/
public function iSelectGender(string $gender): void
{
$this->client->addRequestData('gender', strtolower(substr($gender, 0, 1)));
}
/**
* @When I select :customerGroup as their group
*/
public function iSelectGroup(CustomerGroupInterface $customerGroup): void
{
$this->client->addRequestData('group', $this->iriConverter->getIriFromItem($customerGroup));
}
/**
* @When I choose create account option
*/
public function iChooseCreateAccountOption(): void
{
$this->client->addRequestData('user', []);
}
/**
* @When I specify their password as :password
*/
public function iSpecifyItsPasswordAs(string $password): void
{
$this->client->addRequestData('user', [
'plainPassword' => $password,
]);
}
/**
* @When I (try to) add them
*/
public function iAddIt(): void
{
$this->client->create();
}
/**
* @When I want to see all customers in store
*/
public function iWantToSeeAllZonesInStore(): void
{
$this->client->index(Resources::CUSTOMERS);
}
/**
* @When I do not specify any information
*/
public function iDoNotSpecifyAnyInformation(): void
{
// Intentionally left empty
}
/**
* @When I do not choose create account option
*/
public function iDoNotChooseCreateAccountOption()
{
// Intentionally left blank.
}
/**
* @Then I should be notified that it has been successfully created
*/
public function iShouldBeNotifiedThatItHasBeenSuccessfullyCreated(): void
{
Assert::true(
$this->responseChecker->isCreationSuccessful($this->client->getLastResponse()),
'Customer could not be created',
);
}
/**
* @Then the customer :customer should appear in the store
* @Then the customer :customer should still have this email
*/
public function theCustomerShouldAppearInTheStore(CustomerInterface $customer): void
{
Assert::true(
$this->responseChecker->hasItemWithValue($this->client->index(Resources::CUSTOMERS), 'email', $customer->getEmail()),
sprintf('Customer with email %s does not exist', $customer->getEmail()),
);
}
/**
* @Then the customer :customer should have an account created
* @Then /^(this customer) should have an account created$/
*/
public function theyShouldHaveAnAccountCreated(CustomerInterface $customer): void
{
Assert::notNull(
$customer->getUser()->getPassword(),
'Customer should have an account, but they do not.',
);
}
/**
* @Then I should see :count customers in the list
* @Then I should see a single customer on the list
*/
public function iShouldSeeZonesInTheList(int $count = 1): void
{
Assert::same($this->responseChecker->countCollectionItems($this->client->index(Resources::CUSTOMERS)), $count);
}
/**
* @Then I should see the customer :email in the list
*/
public function iShouldSeeTheCustomerInTheList(string $email): void
{
Assert::true(
$this->responseChecker->hasItemWithValue($this->client->index(Resources::CUSTOMERS), 'email', $email),
sprintf('There is no customer with email "%s"', $email),
);
}
/**
* @Then /^I should be notified that ([^"]+) is required$/
*/
public function iShouldBeNotifiedThatIsRequired(string $element): void
{
Assert::contains(
$this->responseChecker->getError($this->client->getLastResponse()),
sprintf('Please enter your %s.', $element),
);
}
/**
* @Then I should not see create account option
*/
public function iShouldNotSeeCreateAccountOption(): void
{
// Intentionally left empty
}
/**
* @Then I should still be on the customer creation page
*/
public function iShouldStillBeOnTheCustomerCreationPage(): void
{
// Intentionally left empty
}
/**
* @Then I should be able to specify their password
* @Then I should not be able to specify their password
*/
public function iShouldBeAbleToSpecifyTheirPassword(): void
{
// Intentionally left empty
}
/**
* @Then I should be able to select create account option
* @Then I should not be able to select create account option
*/
public function iShouldBeAbleToSelectCreateAccountOption(): void
{
// Intentionally left empty
}
}

View file

@ -236,5 +236,11 @@
<argument type="service" id="api_platform.iri_converter" />
<argument type="service" id="sylius.behat.section_iri_converter" />
</service>
<service id="sylius.behat.context.api.admin.managing_customers" class="Sylius\Behat\Context\Api\Admin\ManagingCustomersContext">
<argument type="service" id="sylius.behat.api_platform_client.admin" />
<argument type="service" id="Sylius\Behat\Client\ResponseCheckerInterface" />
<argument type="service" id="api_platform.iri_converter" />
</service>
</services>
</container>

View file

@ -61,6 +61,7 @@ imports:
- suites/api/taxon/managing_taxons_ajax.yml
- suites/api/user/managing_administrators.yml
- suites/api/user/managing_customer_groups.yml
- suites/api/user/managing_customers.yml
- suites/cli/canceling_unpaid_orders.yml
- suites/cli/installer.yml

View file

@ -0,0 +1,24 @@
# This file is part of the Sylius package.
# (c) Sylius Sp. z o.o.
default:
suites:
api_managing_customers:
contexts:
- sylius.behat.context.hook.doctrine_orm
- sylius.behat.context.transform.customer
- sylius.behat.context.transform.customer_group
- sylius.behat.context.transform.shared_storage
- sylius.behat.context.setup.admin_api_security
- sylius.behat.context.setup.customer
- sylius.behat.context.setup.customer_group
- sylius.behat.context.api.admin.managing_customers
- sylius.behat.context.api.admin.response
- sylius.behat.context.api.admin.save
filters:
tags: "@managing_customers&&@api"
javascript: false