Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7e2a326

Browse files
committed
Fix tests
1 parent d4df414 commit 7e2a326

16 files changed

Lines changed: 483 additions & 165 deletions

File tree

features/cart/shopping_cart/clearing_cart.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ Feature: Clearing cart
1313
Scenario: Clearing cart
1414
Given I see the summary of my cart
1515
When I clear my cart
16-
Then my cart should be empty
16+
Then my cart should be cleared

src/Sylius/Behat/Client/ApiPlatformClient.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,10 @@ public function buildCreateRequest(): void
139139

140140
public function buildUpdateRequest(string $id): void
141141
{
142+
$this->show($id);
143+
142144
$this->request = Request::update($this->resource, $id, $this->getToken());
143-
if ($this->sharedStorage->has('token')) {
144-
$this->show($id);
145-
$this->request->setContent(json_decode($this->client->getResponse()->getContent(), true));
146-
}
145+
$this->request->setContent(json_decode($this->client->getResponse()->getContent(), true));
147146
}
148147

149148
public function buildUploadRequest(): void

src/Sylius/Behat/Client/ResponseChecker.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,25 @@ public function getError(Response $response): string
5959

6060
public function isCreationSuccessful(Response $response): bool
6161
{
62-
return $response->getStatusCode() === Response::HTTP_CREATED || $response->getStatusCode() === Response::HTTP_OK;
62+
return $response->getStatusCode() === Response::HTTP_CREATED;
6363
}
6464

6565
public function isDeletionSuccessful(Response $response): bool
6666
{
6767
return $response->getStatusCode() === Response::HTTP_NO_CONTENT;
6868
}
6969

70+
7071
public function hasAccessDenied(Response $response): bool
7172
{
7273
return
7374
$response->getMessage() === 'JWT Token not found' &&
74-
$response->getStatusCode() === Response::HTTP_UNAUTHORIZED
75-
;
75+
$response->getStatusCode() === Response::HTTP_UNAUTHORIZED;
76+
}
77+
78+
public function isShowSuccessful(Response $response): bool
79+
{
80+
return $response->getStatusCode() === Response::HTTP_OK;
7681
}
7782

7883
public function isUpdateSuccessful(Response $response): bool

src/Sylius/Behat/Client/ResponseCheckerInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public function isCreationSuccessful(Response $response): bool;
3333

3434
public function isUpdateSuccessful(Response $response): bool;
3535

36+
public function isShowSuccessful(Response $response): bool;
37+
3638
public function isDeletionSuccessful(Response $response): bool;
3739

3840
public function hasAccessDenied(Response $response): bool;

src/Sylius/Behat/Context/Api/Shop/CartContext.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ public function __construct(
4545
$this->sharedStorage = $sharedStorage;
4646
}
4747

48+
/**
49+
* @When /^I clear my (cart)$/
50+
*/
51+
public function iClearMyCart(string $tokenValue): void
52+
{
53+
$this->cartsClient->delete($tokenValue);
54+
}
55+
4856
/**
4957
* @When /^I see the summary of my (cart)$/
5058
*/
@@ -56,7 +64,7 @@ public function iSeeTheSummaryOfMyCart(string $tokenValue): void
5664
/**
5765
* @When /^I (?:add|added) (this product) to the (cart)$/
5866
*/
59-
public function iAddThisProductToTheCart(ProductInterface $product, $tokenValue): void
67+
public function iAddThisProductToTheCart(ProductInterface $product, string $tokenValue): void
6068
{
6169
$this->putProductToCart($product, $tokenValue);
6270
}
@@ -70,24 +78,29 @@ public function iAddOfThemToMyCart(int $quantity, ProductInterface $product, str
7078
}
7179

7280
/**
73-
* @Then /^my (cart) should be empty$/
81+
* @Then my cart should be cleared
7482
*/
75-
public function myCartShouldBeEmpty(string $tokenValue): void
83+
public function myCartShouldBeCleared(): void
7684
{
77-
$response = $this->cartsClient->show($tokenValue);
85+
$response = $this->cartsClient->getLastResponse();
7886

7987
Assert::true(
80-
$this->responseChecker->isCreationSuccessful($response),
88+
$this->responseChecker->isDeletionSuccessful($response),
8189
SprintfResponseEscaper::provideMessageWithEscapedResponseContent('Cart has not been created.', $response)
8290
);
8391
}
8492

8593
/**
86-
* @When /^I clear my (cart)$/
94+
* @Then /^my (cart) should be empty$/
8795
*/
88-
public function iClearMyCart(string $tokenValue): void
96+
public function myCartShouldBeEmpty(string $tokenValue): void
8997
{
90-
$this->cartsClient->delete($tokenValue);
98+
$response = $this->cartsClient->show($tokenValue);
99+
100+
Assert::true(
101+
$this->responseChecker->isShowSuccessful($response),
102+
SprintfResponseEscaper::provideMessageWithEscapedResponseContent('Cart has not been created.', $response)
103+
);
91104
}
92105

93106
/**

src/Sylius/Behat/Context/Transform/CartContext.php renamed to src/Sylius/Behat/Context/Transform/CartTokenContext.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use Sylius\Behat\Service\SharedStorageInterface;
2121
use Symfony\Component\HttpFoundation\Response;
2222

23-
final class CartContext implements Context
23+
final class CartTokenContext implements Context
2424
{
2525
/** @var ApiClientInterface */
2626
private $cartsClient;
@@ -44,16 +44,17 @@ public function __construct(
4444
/**
4545
* @Transform /^(cart)$/
4646
*/
47-
public function provideCart(string $cart): string
47+
public function provideCartToken(): string
4848
{
4949
if ($this->sharedStorage->has('cart_token')) {
5050
$tokenValue = $this->sharedStorage->get('cart_token');
5151

5252
$response = $this->cartsClient->show($tokenValue);
53-
if ($response->getStatusCode() === Response::HTTP_OK || $response->getStatusCode() === Response::HTTP_CREATED) {
54-
return $this->sharedStorage->get('cart_token');
53+
if ($response->getStatusCode() === Response::HTTP_OK) {
54+
return $tokenValue;
5555
}
5656
}
57+
5758
$response = $this->cartsClient->create(Request::create('orders'));
5859

5960
$tokenValue = $this->responseChecker->getValue($response, 'tokenValue');

src/Sylius/Behat/Context/Ui/Shop/CartContext.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function iUpdateMyCart()
6868

6969
/**
7070
* @Then my cart should be empty
71+
* @Then my cart should be cleared
7172
* @Then cart should be empty with no value
7273
*/
7374
public function iShouldBeNotifiedThatMyCartIsEmpty()

src/Sylius/Behat/Resources/config/services/contexts/transform.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@
148148
<argument type="service" id="sylius.behat.shared_storage" />
149149
</service>
150150

151-
<service id="sylius.behat.context.transform.cart" class="Sylius\Behat\Context\Transform\CartContext">
152-
<argument type="service" id="sylius.behat.api_platform_client.product" />
151+
<service id="sylius.behat.context.transform.cart" class="Sylius\Behat\Context\Transform\CartTokenContext">
152+
<argument type="service" id="sylius.behat.api_platform_client.cart" />
153153
<argument type="service" id="Sylius\Behat\Client\ResponseCheckerInterface" />
154154
<argument type="service" id="sylius.behat.shared_storage" />
155155
</service>
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Paweł Jędrzejewski
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Bundle\ApiBundle\Doctrine\QueryItemExtension;
15+
16+
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
17+
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
18+
use Doctrine\ORM\QueryBuilder;
19+
use Sylius\Bundle\ApiBundle\Context\UserContextInterface;
20+
use Sylius\Component\Core\Model\AdminUserInterface;
21+
use Sylius\Component\Core\Model\OrderInterface;
22+
use Sylius\Component\Core\Model\ShopUserInterface;
23+
use Symfony\Component\HttpFoundation\Request;
24+
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
25+
use Symfony\Component\Security\Core\User\UserInterface;
26+
27+
final class OrderDeleteMethodExtension implements QueryItemExtensionInterface
28+
{
29+
/** @var UserContextInterface */
30+
private $userContext;
31+
32+
public function __construct(UserContextInterface $userContext)
33+
{
34+
$this->userContext = $userContext;
35+
}
36+
37+
public function applyToItem(
38+
QueryBuilder $queryBuilder,
39+
QueryNameGeneratorInterface $queryNameGenerator,
40+
string $resourceClass,
41+
array $identifiers,
42+
string $operationName = null,
43+
array $context = []
44+
) {
45+
$operationName = strtoupper($operationName);
46+
47+
if (!is_a($resourceClass, OrderInterface::class, true)) {
48+
return;
49+
}
50+
51+
if ($operationName !== Request::METHOD_DELETE) {
52+
return;
53+
}
54+
55+
$rootAlias = $queryBuilder->getRootAliases()[0];
56+
$user = $this->userContext->getUser();
57+
58+
$this->applyToItemForDeleteMethod($user, $queryBuilder, $operationName, $rootAlias);
59+
}
60+
61+
private function applyToItemForDeleteMethod(
62+
?UserInterface $user,
63+
QueryBuilder $queryBuilder,
64+
string $operationName,
65+
string $rootAlias
66+
): void {
67+
if ($user === null) {
68+
$queryBuilder
69+
->andWhere(sprintf('%s.customer IS NULL', $rootAlias))
70+
->andWhere(sprintf('%s.state = :state', $rootAlias))
71+
->setParameter('state', OrderInterface::STATE_CART)
72+
;
73+
74+
return;
75+
}
76+
77+
if ($user instanceof ShopUserInterface && in_array('ROLE_API_ACCESS', $user->getRoles(), true)) {
78+
$queryBuilder
79+
->andWhere(sprintf('%s.customer = :customer', $rootAlias))
80+
->setParameter('customer', $user->getCustomer()->getId())
81+
->andWhere(sprintf('%s.state = :state', $rootAlias))
82+
->setParameter('state', OrderInterface::STATE_CART)
83+
;
84+
85+
return;
86+
}
87+
88+
if ($user instanceof AdminUserInterface && in_array('ROLE_API_ACCESS', $user->getRoles(), true)) {
89+
$queryBuilder
90+
->andWhere(sprintf('%s.state = :state', $rootAlias))
91+
->setParameter('state', OrderInterface::STATE_CART)
92+
;
93+
94+
return;
95+
}
96+
97+
throw new AccessDeniedHttpException('Requested method is not allowed.');
98+
}
99+
}

src/Sylius/Bundle/ApiBundle/Doctrine/QueryItemExtension/OrderExtension.php

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)