Skip to content

Commit

Permalink
[API] cover viewing order by customer
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKasp committed Sep 30, 2020
1 parent 0aaba4d commit ba603e0
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,11 @@ Feature: Seeing customer's orders placed as guest
And the store ships everywhere for free
And the store allows paying offline
And the guest customer placed order with "PHP T-Shirt" product for "john@snow.com" and "United States" based billing address with "Free" shipping method and "Offline" payment
And the another guest customer placed order with "PHP T-Shirt" product for "ned@stark.com" and "United States" based billing address with "Free" shipping method and "Offline" payment

@ui
@ui @api
Scenario: Not being able to hijack another customer's orders
Given I have product "PHP T-Shirt" in the cart
When I complete addressing step with email "john@snow.com" and "United States" based billing address
And I decide to change my address
And I specify the email as "ned@stark.com"
And I complete the addressing step
And I select "Free" shipping method
And I complete the shipping step
And I select "Offline" payment method
And I complete the payment step
And I confirm my order
And I register with previously used "ned@stark.com" email and "lannistersAreDumb" password
Given the customer "ned@stark.com" created account with password "lannistersAreDumb"
And I log in as "ned@stark.com" with "lannistersAreDumb" password
And I browse my orders
Then I should see a single order in the list
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Feature: Viewing orders on my account page
And the customer bought a single "Green Arrow"
And the customer "Oliver Queen" addressed it to "Seaside Fwy", "90802" "Los Angeles" in the "United States" with identical billing address

@ui
@ui @api
Scenario: Viewing orders
When I browse my orders
Then I should see a single order in the list
Expand Down
41 changes: 39 additions & 2 deletions src/Sylius/Behat/Context/Api/Shop/AccountContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Sylius\Behat\Client\ResponseCheckerInterface;
use Sylius\Behat\Context\Setup\ShopSecurityContext;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Symfony\Component\HttpFoundation\Response;
use Webmozart\Assert\Assert;
Expand All @@ -36,6 +37,12 @@ final class AccountContext implements Context
/** @var ResponseCheckerInterface */
private $responseChecker;

/** @var RegistrationContext */
private $registrationContext;

/** @var LoginContext */
private $loginContext;

/** @var ShopSecurityContext */
private $shopApiSecurityContext;

Expand All @@ -44,12 +51,16 @@ public function __construct(
ApiClientInterface $orderShopClient,
SharedStorageInterface $sharedStorage,
ResponseCheckerInterface $responseChecker,
RegistrationContext $registrationContext,
LoginContext $loginContext,
ShopSecurityContext $shopApiSecurityContext
) {
$this->accountClient = $accountClient;
$this->orderShopClient = $orderShopClient;
$this->sharedStorage = $sharedStorage;
$this->responseChecker = $responseChecker;
$this->registrationContext = $registrationContext;
$this->loginContext = $loginContext;
$this->shopApiSecurityContext = $shopApiSecurityContext;
}

Expand Down Expand Up @@ -202,12 +213,38 @@ public function iBrowseMyOrders(): void
$this->orderShopClient->index();
}

/**
* @When I register with previously used :email email and :password password
*/
public function iRegisterWithPreviouslyUsedEmailAndPassword(string $email, string $password): void
{
$this->registrationContext->iWantToRegisterNewAccount();
$this->registrationContext->iSpecifyTheEmailAs($email);
$this->registrationContext->iSpecifyThePasswordAs($password);
$this->registrationContext->iRegisterThisAccount();

$this->loginContext->iLogInAsWithPassword($email, $password);
}

/**
* @Then I should see a single order in the list
*/
public function iShouldSeeASingleOrderInTheList()
public function iShouldSeeASingleOrderInTheList(): void
{
Assert::same($this->responseChecker->countCollectionItems($this->orderShopClient->getLastResponse()), 1);
Assert::same($this->responseChecker->countCollectionItems($this->orderShopClient->index()), 1);
}

/**
* @Then this order should have :order number
*/
public function thisOrderShouldHaveNumber(OrderInterface $order): void
{
Assert::true(
$this->responseChecker->hasItemWithValue($this->orderShopClient->getLastResponse(),
'number',
$order->getNumber()
)
);
}

private function isViolationWithMessageInResponse(Response $response, string $message): bool
Expand Down
20 changes: 20 additions & 0 deletions src/Sylius/Behat/Context/Setup/OrderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,26 @@ public function theGuestCustomerPlacedOrderWithForAndBasedShippingAddress(
$this->objectManager->flush();
}

/**
* @Given /^the another guest customer placed order with ("[^"]+" product) for "([^"]+)" and ("[^"]+" based billing address) with ("[^"]+" shipping method) and ("[^"]+" payment)$/
*/
public function theAnotherGuestCustomerPlacedOrderWithForAndBasedShippingAddress(
ProductInterface $product,
string $email,
AddressInterface $address,
ShippingMethodInterface $shippingMethod,
PaymentMethodInterface $paymentMethod
) {
$customer = $this->createCustomer($email);

$this->customerRepository->add($customer);

$this->sharedStorage->set('customer', $customer);

$this->placeOrder($product, $shippingMethod, $address, $paymentMethod, $customer, 2);
$this->objectManager->flush();
}

/**
* @Given a customer :customer added something to cart
*/
Expand Down
14 changes: 14 additions & 0 deletions src/Sylius/Behat/Context/Setup/UserContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ public function thereIsUserIdentifiedBy($email, $password = 'sylius')
$this->userRepository->add($user);
}

/**
* @Given the customer :customerEmail created account with password :password
*/
public function theCustomerCreatedAccountWithPassword($email, $password = 'sylius')
{
/** @var ShopUserInterface $user */
$user = $this->userFactory->create(['email' => $email, 'password' => $password, 'enabled' => true]);

$user->setCustomer($this->sharedStorage->get('customer'));
$this->sharedStorage->set('user', $user);

$this->userRepository->add($user);
}

/**
* @Given the account of :email was deleted
* @Given my account :email was deleted
Expand Down
11 changes: 11 additions & 0 deletions src/Sylius/Behat/Context/Ui/Shop/AccountContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,17 @@ public function iViewingTheSummaryOfMyLastOrder()
$this->orderIndexPage->openLastOrderPage();
}

/**
* @When I log in as :email with :password password
*/
public function iLogInAsWithPassword(string $email, string $password): void
{
$this->loginPage->open();
$this->loginPage->specifyUsername($email);
$this->loginPage->specifyPassword($password);
$this->loginPage->logIn();
}

/**
* @Then it should has number :orderNumber
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
<argument type="service" id="sylius.behat.api_platform_client.shop.order" />
<argument type="service" id="sylius.behat.shared_storage" />
<argument type="service" id="Sylius\Behat\Client\ResponseCheckerInterface" />
<argument type="service" id="sylius.behat.context.api.shop.registration" />
<argument type="service" id="sylius.behat.context.api.shop.login" />
<argument type="service" id="sylius.behat.context.setup.shop_api_security" />
</service>
</services>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ default:
- sylius.behat.context.hook.doctrine_orm

- sylius.behat.context.transform.address
- sylius.behat.context.transform.channel
- sylius.behat.context.transform.country
- sylius.behat.context.transform.customer
- sylius.behat.context.transform.lexical
- sylius.behat.context.transform.order
- sylius.behat.context.transform.payment
- sylius.behat.context.transform.product
- sylius.behat.context.transform.shared_storage
- sylius.behat.context.transform.shipping_method
- sylius.behat.context.transform.user

- sylius.behat.context.setup.cart
- sylius.behat.context.setup.channel
- sylius.behat.context.setup.currency
- sylius.behat.context.setup.customer
Expand All @@ -26,6 +31,8 @@ default:
- sylius.behat.context.setup.user

- sylius.behat.context.api.shop.account
- sylius.behat.context.api.shop.checkout
- sylius.behat.context.api.shop.login

filters:
tags: "@customer_account && @api"
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ default:
- sylius.behat.context.ui.shop.cart
- sylius.behat.context.ui.shop.checkout
- sylius.behat.context.ui.shop.checkout.addressing
- sylius.behat.context.ui.shop.checkout.shipping
- sylius.behat.context.ui.shop.checkout.payment
- sylius.behat.context.ui.shop.checkout.complete
- sylius.behat.context.ui.shop.checkout.payment
- sylius.behat.context.ui.shop.checkout.shipping
- sylius.behat.context.ui.shop.currency

filters:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ public function getCollection(string $resourceClass, string $operationName = nul
return $this->orderRepository->findAll();
}

if (
$user instanceof ShopUserInterface
) {
if ($user instanceof ShopUserInterface) {
/** @var CustomerInterface $customer */
$customer = $user->getCustomer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@
</attribute>
</collectionOperation>

<collectionOperation name="test">
<attribute name="method">GET</attribute>
<attribute name="path">/orders</attribute>
</collectionOperation>

<collectionOperation name="shop_get">
<attribute name="method">GET</attribute>
<attribute name="path">/shop/orders</attribute>
Expand Down

0 comments on commit ba603e0

Please sign in to comment.