Skip to content

Commit

Permalink
use api route prefix in Behat tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Prometee committed May 4, 2022
1 parent 50be917 commit f239bf7
Show file tree
Hide file tree
Showing 17 changed files with 85 additions and 125 deletions.
25 changes: 5 additions & 20 deletions src/Sylius/Behat/Client/ApiPlatformClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,15 @@

final class ApiPlatformClient implements ApiClientInterface
{
private AbstractBrowser $client;

private SharedStorageInterface $sharedStorage;

private RequestFactoryInterface $requestFactory;

private string $authorizationHeader;

private ?string $section;

private ?RequestInterface $request = null;

public function __construct(
AbstractBrowser $client,
SharedStorageInterface $sharedStorage,
RequestFactoryInterface $requestFactory,
string $authorizationHeader,
?string $section = null
private AbstractBrowser $client,
private SharedStorageInterface $sharedStorage,
private RequestFactoryInterface $requestFactory,
private string $authorizationHeader,
private ?string $section = null
) {
$this->client = $client;
$this->sharedStorage = $sharedStorage;
$this->requestFactory = $requestFactory;
$this->authorizationHeader = $authorizationHeader;
$this->section = $section;
}

public function index(string $resource): Response
Expand Down
19 changes: 7 additions & 12 deletions src/Sylius/Behat/Client/ApiPlatformSecurityClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,19 @@

final class ApiPlatformSecurityClient implements ApiSecurityClientInterface
{
private AbstractBrowser $client;

private string $section;

private SharedStorageInterface $sharedStorage;

private array $request = [];

public function __construct(AbstractBrowser $client, string $section, SharedStorageInterface $sharedStorage)
{
$this->client = $client;
$this->section = $section;
$this->sharedStorage = $sharedStorage;
public function __construct(
private AbstractBrowser $client,
private string $apiRoute,
private string $section,
private SharedStorageInterface $sharedStorage
) {
}

public function prepareLoginRequest(): void
{
$this->request['url'] = sprintf('/api/v2/%s/authentication-token', $this->section);
$this->request['url'] = sprintf('%s/%s/authentication-token', $this->apiRoute, $this->section);
$this->request['method'] = 'POST';
}

Expand Down
37 changes: 19 additions & 18 deletions src/Sylius/Behat/Context/Api/Admin/ManagingExchangeRatesContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,12 @@

final class ManagingExchangeRatesContext implements Context
{
private ApiClientInterface $client;

private ResponseCheckerInterface $responseChecker;

private SharedStorageInterface $sharedStorage;

public function __construct(
ApiClientInterface $client,
ResponseCheckerInterface $responseChecker,
SharedStorageInterface $sharedStorage
private ApiClientInterface $client,
private ResponseCheckerInterface $responseChecker,
private SharedStorageInterface $sharedStorage,
private string $apiRoute
) {
$this->client = $client;
$this->responseChecker = $responseChecker;
$this->sharedStorage = $sharedStorage;
}

/**
Expand Down Expand Up @@ -85,15 +77,21 @@ public function iSpecifyItsRatioAs(?string $ratio = null): void
*/
public function iChooseAsTheSourceCurrency(string $currencyCode): void
{
$this->client->addRequestData('sourceCurrency', '/api/v2/admin/currencies/' . $currencyCode);
$this->client->addRequestData(
'sourceCurrency',
sprintf('%s/admin/currencies/%s', $this->apiRoute, $currencyCode)
);
}

/**
* @When I choose :currencyCode as the target currency
*/
public function iChooseAsTheTargetCurrency(string $currencyCode): void
{
$this->client->addRequestData('targetCurrency', '/api/v2/admin/currencies/' . $currencyCode);
$this->client->addRequestData(
'targetCurrency',
sprintf('%s/admin/currencies/%s', $this->apiRoute, $currencyCode)
);
}

/**
Expand Down Expand Up @@ -347,15 +345,18 @@ private function assertIfNotBeAbleToEditItCurrency(string $currencyType): void
{
$this->client->buildUpdateRequest(Resources::EXCHANGE_RATES, $this->sharedStorage->get('exchange_rate_id'));

$this->client->addRequestData($currencyType, '/api/v2/admin/currencies/EUR');
$this->client->addRequestData(
$currencyType,
sprintf('%s/admin/currencies/EUR', $this->apiRoute)
);
$this->client->update();

Assert::false(
$this->responseChecker->hasItemOnPositionWithValue(
$this->client->index(Resources::EXCHANGE_RATES),
0,
$currencyType,
'/api/v2/admin/currencies/EUR'
sprintf('%s/admin/currencies/EUR', $this->apiRoute)
),
sprintf('It was possible to change %s', $currencyType)
);
Expand All @@ -368,8 +369,8 @@ private function getExchangeRateFromResponse(
/** @var array $item */
foreach ($this->responseChecker->getCollection($this->client->index(Resources::EXCHANGE_RATES)) as $item) {
if (
$item['sourceCurrency'] === '/api/v2/admin/currencies/' . $sourceCurrency->getCode() &&
$item['targetCurrency'] === '/api/v2/admin/currencies/' . $targetCurrency->getCode()
$item['sourceCurrency'] === sprintf('%s/admin/currencies/%s', $this->apiRoute, $sourceCurrency->getCode()) &&
$item['targetCurrency'] === sprintf('%s/admin/currencies/%s', $this->apiRoute, $targetCurrency->getCode())
) {
return $item;
}
Expand Down
18 changes: 5 additions & 13 deletions src/Sylius/Behat/Context/Api/Admin/ManagingPaymentsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,12 @@

final class ManagingPaymentsContext implements Context
{
private ApiClientInterface $client;

private ResponseCheckerInterface $responseChecker;

private IriConverterInterface $iriConverter;

public function __construct(
ApiClientInterface $client,
ResponseCheckerInterface $responseChecker,
IriConverterInterface $iriConverter
private ApiClientInterface $client,
private ResponseCheckerInterface $responseChecker,
private IriConverterInterface $iriConverter,
private string $apiRoute
) {
$this->client = $client;
$this->responseChecker = $responseChecker;
$this->iriConverter = $iriConverter;
}

/**
Expand Down Expand Up @@ -142,7 +134,7 @@ public function iShouldSeePaymentForTheOrderInTheList(OrderInterface $order, int
$this->client->getLastResponse(),
$position - 1,
'order',
sprintf('/api/v2/admin/orders/%s', $order->getTokenValue())
sprintf('%s/admin/orders/%s', $this->apiRoute, $order->getTokenValue())
));
}

Expand Down
23 changes: 6 additions & 17 deletions src/Sylius/Behat/Context/Api/Admin/ManagingProductsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,13 @@ final class ManagingProductsContext implements Context
{
public const SORT_TYPES = ['ascending' => 'asc', 'descending' => 'desc'];

private ApiClientInterface $client;

private ResponseCheckerInterface $responseChecker;

private IriConverterInterface $iriConverter;

private SharedStorageInterface $sharedStorage;

public function __construct(
ApiClientInterface $client,
ResponseCheckerInterface $responseChecker,
IriConverterInterface $iriConverter,
SharedStorageInterface $sharedStorage
private ApiClientInterface $client,
private ResponseCheckerInterface $responseChecker,
private IriConverterInterface $iriConverter,
private SharedStorageInterface $sharedStorage,
private string $apiRoute
) {
$this->client = $client;
$this->responseChecker = $responseChecker;
$this->iriConverter = $iriConverter;
$this->sharedStorage = $sharedStorage;
}

/**
Expand Down Expand Up @@ -373,7 +362,7 @@ public function iShouldNotBeAbleToEditItsCode(): void
$this->client->getLastResponse(),
0,
'code',
'/api/v2/admin/products/_NEW'
sprintf('%s/admin/products/_NEW', $this->apiRoute)
),
sprintf('It was possible to change %s', '_NEW')
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public function __construct(
private ApiClientInterface $client,
private ResponseCheckerInterface $responseChecker,
private IriConverterInterface $iriConverter,
private SharedStorageInterface $sharedStorage
private SharedStorageInterface $sharedStorage,
private string $apiRoute
) {
}

Expand Down Expand Up @@ -116,7 +117,7 @@ public function iTryToShipShipmentOfOrder(OrderInterface $order): void
$shipment = $order->getShipments()->first();

$this->client->customAction(
sprintf('/api/v2/admin/shipments/%s/ship', (string) $shipment->getId()),
sprintf('%s/admin/shipments/%s/ship', $this->apiRoute,(string) $shipment->getId()),
HttpRequest::METHOD_PATCH
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Sylius/Behat/Context/Api/Shop/CartContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ public function thisItemShouldHaveOptionValue(ProductInterface $product, string
private function pickupCart(?string $localeCode = null): string
{
$request = $this->requestFactory->custom(
'/api/v2/shop/orders',
'/shop/orders',
HttpRequest::METHOD_POST,
$localeCode ? ['HTTP_ACCEPT_LANGUAGE' => $localeCode] : []
);
Expand Down
5 changes: 3 additions & 2 deletions src/Sylius/Behat/Context/Api/Shop/ChannelContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ final class ChannelContext implements Context
public function __construct(
private ApiClientInterface $client,
private ResponseCheckerInterface $responseChecker,
private SharedStorageInterface $sharedStorage
private SharedStorageInterface $sharedStorage,
private string $apiRoute
) {
}

Expand All @@ -53,7 +54,7 @@ public function iShouldShopUsingTheCurrency(string $currencyCode): void
$this->client->getLastResponse(),
'baseCurrency'
),
sprintf('/api/v2/shop/currencies/%s', $currencyCode)
sprintf('%s/shop/currencies/%s', $this->apiRoute, $currencyCode)
);
}
}
2 changes: 1 addition & 1 deletion src/Sylius/Behat/Context/Api/Shop/CustomerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ private function isViolationWithMessageInResponse(Response $response, string $me
private function verifyAccount(string $token): void
{
$request = $this->requestFactory->custom(
\sprintf('/api/v2/shop/account-verification-requests/%s', $token),
\sprintf('/shop/account-verification-requests/%s', $token),
HttpRequest::METHOD_PATCH,
);

Expand Down
15 changes: 5 additions & 10 deletions src/Sylius/Behat/Context/Api/Shop/HomepageContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,11 @@

final class HomepageContext implements Context
{
private ApiClientInterface $client;

private ResponseCheckerInterface $responseChecker;

public function __construct(
ApiClientInterface $client,
ResponseCheckerInterface $responseChecker
private ApiClientInterface $client,
private ResponseCheckerInterface $responseChecker,
private string $apiRoute
) {
$this->client = $client;
$this->responseChecker = $responseChecker;
}

/**
Expand All @@ -39,7 +34,7 @@ public function __construct(
public function iCheckLatestProducts(): void
{
$this->client->customAction(
'api/v2/shop/products?itemsPerPage=3&order[createdAt]=desc',
sprintf('%s/shop/products?itemsPerPage=3&order[createdAt]=desc', $this->apiRoute),
HttpRequest::METHOD_GET
);
}
Expand All @@ -49,7 +44,7 @@ public function iCheckLatestProducts(): void
*/
public function iCheckAvailableTaxons(): void
{
$this->client->customAction('api/v2/shop/taxons', HttpRequest::METHOD_GET);
$this->client->customAction(sprintf('%s/shop/taxons', $this->apiRoute), HttpRequest::METHOD_GET);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Sylius/Behat/Context/Api/Shop/LoginContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(
private AbstractBrowser $shopAuthenticationTokenClient,
private ResponseCheckerInterface $responseChecker,
private SharedStorageInterface $sharedStorage,
private RequestFactoryInterface $requestFactory,
private RequestFactoryInterface $requestFactory
) {
}

Expand All @@ -58,7 +58,7 @@ public function iLogInWithTheEmail(string $email): void
{
$this->shopAuthenticationTokenClient->request(
'POST',
'/api/v2/shop/authentication-token',
'/shop/authentication-token',
[],
[],
['CONTENT_TYPE' => 'application/json', 'HTTP_ACCEPT' => 'application/json'],
Expand Down Expand Up @@ -104,7 +104,7 @@ public function iResetPasswordForEmailInLocale(string $email, LocaleInterface $l
public function iFollowLinkOnMyEmailToResetPassword(ShopUserInterface $user): void
{
$this->request = $this->requestFactory->custom(
sprintf('api/v2/shop/reset-password-requests/%s', $user->getPasswordResetToken()),
sprintf('/shop/reset-password-requests/%s', $user->getPasswordResetToken()),
HttpRequest::METHOD_PATCH
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Sylius/Behat/Context/Api/Shop/OrderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function iChangeMyPaymentMethodTo(PaymentMethodInterface $paymentMethod):

$request = $this->requestFactory->custom(
sprintf(
'/api/v2/shop/account/orders/%s/payments/%s',
'/shop/account/orders/%s/payments/%s',
$order->getTokenValue(),
(string) $order->getPayments()->first()->getId()
),
Expand Down Expand Up @@ -380,7 +380,7 @@ private function getAdjustmentsForOrder(): array
private function getAdjustmentsForOrderItem(string $itemId): array
{
$response = $this->shopClient->customAction(
sprintf('/api/v2/shop/orders/%s/items/%s/adjustments', $this->sharedStorage->get('cart_token'), $itemId),
sprintf('/shop/orders/%s/items/%s/adjustments', $this->sharedStorage->get('cart_token'), $itemId),
HttpRequest::METHOD_GET
);

Expand Down
5 changes: 3 additions & 2 deletions src/Sylius/Behat/Context/Api/Shop/ProductContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function __construct(
private IriConverterInterface $iriConverter,
private ChannelContextSetterInterface $channelContextSetter,
private RequestFactoryInterface $requestFactory,
private string $apiRoute
) {
}

Expand Down Expand Up @@ -76,7 +77,7 @@ public function iViewProductInTheLocale(ProductInterface $product, string $local
*/
public function iViewProductUsingSlug(ProductInterface $product): void
{
$this->client->showByIri('/api/v2/shop/products-by-slug/' . $product->getSlug());
$this->client->showByIri(sprintf('%s/shop/products-by-slug/%s', $this->apiRoute, $product->getSlug()));

$this->sharedStorage->set('product', $product);
}
Expand All @@ -88,7 +89,7 @@ public function iShouldBeRedirectedToProduct(ProductInterface $product): void
{
$response = $this->client->getLastResponse();

Assert::eq($response->headers->get('Location'), '/api/v2/shop/products/' . $product->getCode());
Assert::eq($response->headers->get('Location'), sprintf('%s/shop/products/%s', $this->apiRoute, $product->getCode()));
}

/**
Expand Down
Loading

0 comments on commit f239bf7

Please sign in to comment.