Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API][Order] Fix possibility to limit orders by refactoring from data provider to extension #12565

Merged
merged 4 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions UPGRADE-1.9.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# UPGRADE FROM `v1.9.3` TO `v1.9.4`

1. `Sylius\Bundle\ApiBundle\DataProvider\OrderCollectionDataProvider` has been removed and the same logic
is now implemented in `Sylius\Bundle\ApiBundle\Doctrine\QueryCollectionExtension\OrdersByLoggedInUserExtension`

# UPGRADE FROM `v1.9.2` TO `v1.9.3`

1. The endpoint `GET api/v2/order-items/{id}/adjustments` has been changed to `GET api/v2/admin/order-items/{id}/adjustments`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ Feature: Seeing customer's orders placed as guest
@ui @api
Scenario: Not being able to hijack another customer's orders
Given I registered with previously used "ned@stark.com" email and "lannistersAreDumb" password
And I log in as "ned@stark.com" with "lannistersAreDumb" password
When 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
@@ -0,0 +1,24 @@
@managing_orders
Feature: Limiting the number of orders
In order to display a specific number of orders
As an Administrator
I want to be able to limit the number of orders on the list

Background:
Given the store operates on a single channel in "United States"
And the store has a product "Blue ElePHPant"
And the store ships everywhere for free
And the store allows paying offline
And there is a customer "john@doe.com" that placed an order "#00000022"
And the customer bought a single "Blue ElePHPant"
And the customer chose "Free" shipping method to "United States" with "Offline" payment
And this customer placed another order "#00000023"
And the customer bought a single "Blue ElePHPant"
And the customer chose "Free" shipping method to "United States" with "Offline" payment
And I am logged in as an administrator

@api
Scenario: Limiting the number of orders
When I browse orders
And I limit number of items to 1
Then I should see a single order in the list
17 changes: 17 additions & 0 deletions src/Sylius/Behat/Context/Api/Admin/ManagingOrdersContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ public function iShipThisOrder(OrderInterface $order): void
);
}

/**
* @When I limit number of items to :limit
*/
public function iLimitNumberOfItemsTo(int $limit): void
{
$this->client->addFilter('itemsPerPage', $limit);
$this->client->filter();
}

/**
* @Then I should see a single order from customer :customer
*/
Expand All @@ -131,6 +140,14 @@ public function iShouldSeeASingleOrderFromCustomer(CustomerInterface $customer):
);
}

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

/**
* @Then I should be notified that it has been successfully updated
*/
Expand Down
9 changes: 7 additions & 2 deletions src/Sylius/Behat/Context/Setup/OrderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,16 @@ public function __construct(
* @Given a customer :customer placed an order :orderNumber
* @Given the customer :customer has already placed an order :orderNumber
* @Given there is a customer :customer that placed an order :orderNumber in channel :channel
* @Given /^(this customer) placed (another order "[^"]+")$/
*/
public function thereIsCustomerThatPlacedOrder(CustomerInterface $customer, $orderNumber = null, $channel = null)
{
public function thereIsCustomerThatPlacedOrder(
CustomerInterface $customer,
string $orderNumber = null,
ChannelInterface $channel = null
): void {
$order = $this->createOrder($customer, $orderNumber, $channel);

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

$this->orderRepository->add($order);
Expand Down
1 change: 1 addition & 0 deletions src/Sylius/Behat/Context/Transform/OrderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function getOrderByCustomer(string $email): OrderInterface
/**
* @Transform :orderNumber
* @Transform /^an order "([^"]+)"$/
* @Transform /^another order "([^"]+)"$/
* @Transform /^the order "([^"]+)"$/
* @Transform /^the "([^"]+)" order$/
*/
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* 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\Bundle\ApiBundle\Doctrine\QueryCollectionExtension;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\ContextAwareQueryCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use Doctrine\ORM\QueryBuilder;
use Sylius\Bundle\ApiBundle\Context\UserContextInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ShopUserInterface;

/** @experimental */
final class OrdersByLoggedInUserExtension implements ContextAwareQueryCollectionExtensionInterface
{
/** @var UserContextInterface */
private $userContext;

public function __construct(UserContextInterface $userContext)
{
$this->userContext = $userContext;
}

public function applyToCollection(
QueryBuilder $queryBuilder,
QueryNameGeneratorInterface $queryNameGenerator,
string $resourceClass,
string $operationName = null,
array $context = []
): void {
if (!is_a($resourceClass, OrderInterface::class, true)) {
return;
}

$rootAlias = $queryBuilder->getRootAliases()[0];
$queryBuilder
->andWhere(sprintf('%s.state != :state', $rootAlias))
->setParameter('state', OrderInterface::STATE_CART)
;

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

$queryBuilder
->andWhere(sprintf('%s.customer = :customer', $rootAlias))
->setParameter('customer', $customer)
;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,5 @@
<service id="sylius.api.context.user" class="Sylius\Bundle\ApiBundle\Context\TokenBasedUserContext">
<argument type="service" id="security.token_storage" />
</service>

<service
id="sylius.api.collection_data_provider.orders"
class="Sylius\Bundle\ApiBundle\DataProvider\OrderCollectionDataProvider"
>
<argument type="service" id="sylius.api.context.user" />
<argument type="service" id="sylius.repository.order" />
<tag name="api_platform.collection_data_provider" priority="10" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
<tag name="api_platform.doctrine.orm.query_extension.collection" />
</service>

<service
id="sylius.api.doctrine.query_collection_extension.orders_by_logged_in_user"
class="Sylius\Bundle\ApiBundle\Doctrine\QueryCollectionExtension\OrdersByLoggedInUserExtension"
public="true"
>
<argument type="service" id="sylius.api.context.user" />
<tag name="api_platform.doctrine.orm.query_extension.collection" />
</service>

<service
id="sylius.api.doctrine.query_item_extension.get_order"
class="Sylius\Bundle\ApiBundle\Doctrine\QueryItemExtension\OrderGetMethodItemExtension"
Expand Down

This file was deleted.