Skip to content

Commit

Permalink
[API][Behat] Minor contexts refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
GSadee committed Mar 13, 2020
1 parent 30399dd commit a7e26e7
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 80 deletions.
14 changes: 7 additions & 7 deletions src/Sylius/Behat/Context/Api/Admin/ManagingCurrenciesContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ public function iShouldSeeCurrenciesInTheList(int $count): void
*/
public function currencyShouldAppearInTheStore(string $currencyName): void
{
$this->client->index();
Assert::true(
$this->responseChecker->hasItemWithValue($this->client->getLastResponse(), 'name', $currencyName),
$this->responseChecker->hasItemWithValue($this->client->index(), 'name', $currencyName),
sprintf('There is no currency with name "%s"', $currencyName)
);
}
Expand All @@ -93,10 +92,10 @@ public function currencyShouldAppearInTheStore(string $currencyName): void
*/
public function thereShouldStillBeOnlyOneCurrencyWithCode(string $code): void
{
$this->client->index();
Assert::eq(1, $this->responseChecker->countCollectionItems($this->client->getLastResponse()));
$response = $this->client->index();
Assert::same($this->responseChecker->countCollectionItems($response), 1);
Assert::true(
$this->responseChecker->hasItemWithValue($this->client->getLastResponse(), 'code', $code),
$this->responseChecker->hasItemWithValue($response, 'code', $code),
sprintf('There is no currency with code "%s"', $code)
);
}
Expand All @@ -106,11 +105,12 @@ public function thereShouldStillBeOnlyOneCurrencyWithCode(string $code): void
*/
public function iShouldBeNotifiedThatCurrencyCodeMustBeUnique(): void
{
$response = $this->client->getLastResponse();
Assert::false(
$this->responseChecker->isCreationSuccessful($this->client->getLastResponse()),
$this->responseChecker->isCreationSuccessful($response),
'Currency has been created successfully, but it should not'
);
Assert::same($this->responseChecker->getError($this->client->getLastResponse()), 'code: Currency code must be unique.');
Assert::same($this->responseChecker->getError($response), 'code: Currency code must be unique.');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ public function iDeleteTheCustomerGroup(CustomerGroupInterface $customerGroup):
*/
public function theCustomerGroupShouldAppearInTheStore(CustomerGroupInterface $customerGroup): void
{
$this->client->index();
Assert::true(
$this->responseChecker->hasItemWithValue($this->client->getLastResponse(), 'code', $customerGroup->getCode()),
$this->responseChecker->hasItemWithValue($this->client->index(), 'code', $customerGroup->getCode()),
sprintf('Customer group with code %s does not exist', $customerGroup->getCode())
);
}
Expand All @@ -129,9 +128,8 @@ public function theCustomerGroupShouldAppearInTheStore(CustomerGroupInterface $c
*/
public function thisCustomerGroupWithNameShouldAppearInTheStore(string $name): void
{
$this->client->index();
Assert::true(
$this->responseChecker->hasItemWithValue($this->client->getLastResponse(), 'name', $name),
$this->responseChecker->hasItemWithValue($this->client->index(), 'name', $name),
sprintf('Customer group with name %s does not exist', $name)
);
}
Expand All @@ -142,18 +140,16 @@ public function thisCustomerGroupWithNameShouldAppearInTheStore(string $name): v
*/
public function iShouldSeeCustomerGroupsInTheList(int $amountOfCustomerGroups = 1): void
{
$this->client->index();
Assert::same($this->responseChecker->countCollectionItems($this->client->getLastResponse()), $amountOfCustomerGroups);
Assert::same($this->responseChecker->countCollectionItems($this->client->index()), $amountOfCustomerGroups);
}

/**
* @Then /^(this customer group) should still be named "([^"]+)"$/
*/
public function thisCustomerGroupShouldStillBeNamed(CustomerGroupInterface $customerGroup, string $name): void
{
$this->client->show($customerGroup->getCode());
Assert::true(
$this->responseChecker->hasValue($this->client->getLastResponse(), 'name', $name),
$this->responseChecker->hasValue($this->client->show($customerGroup->getCode()), 'name', $name),
'Customer groups name is not ' . $name
);
}
Expand Down Expand Up @@ -194,10 +190,9 @@ public function iShouldBeInformedThatThisFormContainsErrors(): void
public function iShouldNotBeAbleToEditItsCode(): void
{
$this->client->updateRequestData(['code' => 'NEW_CODE']);
$this->client->update();

Assert::false(
$this->responseChecker->hasValue($this->client->getLastResponse(), 'code', 'NEW_CODE'),
$this->responseChecker->hasValue($this->client->update(), 'code', 'NEW_CODE'),
'The code field with value NEW_CODE exist'
);
}
Expand Down Expand Up @@ -246,8 +241,6 @@ public function iShouldBeNotifiedThatItHasBeenSuccessfullyDeleted(): void

private function isItemOnIndex(string $property, string $value): bool
{
$this->client->index();

return $this->responseChecker->hasItemWithValue($this->client->getLastResponse(), $property, $value);
return $this->responseChecker->hasItemWithValue($this->client->index(), $property, $value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ public function iShouldSeeExchangeRatesOnTheList(int $count): void
*/
public function iShouldSeeASingleExchangeRateInTheList(): void
{
$this->client->index();
Assert::same($this->responseChecker->countCollectionItems($this->client->getLastResponse()), 1);
Assert::same($this->responseChecker->countCollectionItems($this->client->index()), 1);
}

/**
Expand Down Expand Up @@ -200,10 +199,8 @@ public function iShouldSeeTheExchangeRateBetweenAndInTheList(
*/
public function itShouldHaveARatioOf(float $ratio): void
{
$this->client->index();

Assert::true(
$this->responseChecker->hasItemWithValue($this->client->getLastResponse(), 'ratio', (float) $ratio),
$this->responseChecker->hasItemWithValue($this->client->index(), 'ratio', (float) $ratio),
sprintf('ExchangeRate with ratio %s does not exist', $ratio)
);
}
Expand Down Expand Up @@ -353,10 +350,9 @@ private function assertIfNotBeAbleToEditItCurrency(string $currencyType): void
$this->client->addRequestData($currencyType, '/new-api/currencies/EUR');
$this->client->update();

$this->client->index();
Assert::false(
$this->responseChecker->hasItemOnPositionWithValue(
$this->client->getLastResponse(), 0, $currencyType, '/new-api/currencies/EUR'
$this->client->index(), 0, $currencyType, '/new-api/currencies/EUR'
),
sprintf('It was possible to change %s', $currencyType)
);
Expand All @@ -366,10 +362,8 @@ private function getExchangeRateFromResponse(
CurrencyInterface $sourceCurrency,
CurrencyInterface $targetCurrency
): ?array {
$this->client->index();

/** @var array $item */
foreach ($this->responseChecker->getCollection($this->client->getLastResponse()) as $item)
foreach ($this->responseChecker->getCollection($this->client->index()) as $item)
{
if (
$item['sourceCurrency'] === '/new-api/currencies/' . $sourceCurrency->getCode() &&
Expand Down
19 changes: 12 additions & 7 deletions src/Sylius/Behat/Context/Api/Admin/ManagingPaymentsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,11 @@ public function iShouldSeeThePaymentOfOrderAs(OrderInterface $order, string $pay
$payment = $order->getLastPayment();
Assert::notNull($payment);

$this->client->show((string) $payment->getId());
Assert::true($this->responseChecker->hasValue(
$this->client->getLastResponse(), 'state', StringInflector::nameToLowercaseCode($paymentState))
);
$this->client->show((string) $payment->getId()),
'state',
StringInflector::nameToLowercaseCode($paymentState)
));
}

/**
Expand All @@ -172,8 +173,10 @@ public function iShouldSeeThePaymentOfOrderAs(OrderInterface $order, string $pay
public function iShouldSeeThePaymentOfTheOrder(OrderInterface $order): void
{
Assert::true($this->responseChecker->hasItemWithValue(
$this->client->getLastResponse(), 'order', $this->iriConverter->getIriFromItem($order))
);
$this->client->getLastResponse(),
'order',
$this->iriConverter->getIriFromItem($order)
));
}

/**
Expand All @@ -182,7 +185,9 @@ public function iShouldSeeThePaymentOfTheOrder(OrderInterface $order): void
public function iShouldNotSeeThePaymentOfTheOrder(OrderInterface $order): void
{
Assert::false($this->responseChecker->hasItemWithValue(
$this->client->getLastResponse(), 'order', $this->iriConverter->getIriFromItem($order))
);
$this->client->getLastResponse(),
'order',
$this->iriConverter->getIriFromItem($order)
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,8 @@ public function theProductOptionShouldAppearInTheRegistry(ProductOptionInterface
{
$this->sharedStorage->set('product_option', $productOption);

$this->client->index();
Assert::true(
$this->responseChecker->hasItemWithValue($this->client->getLastResponse(), 'name', $productOption->getName()),
$this->responseChecker->hasItemWithValue($this->client->index(), 'name', $productOption->getName()),
sprintf('Product option should have name "%s", but it does not.', $productOption->getName())
);
}
Expand Down Expand Up @@ -198,9 +197,8 @@ public function theLastProductOptionInTheListShouldHave(string $field, string $v
*/
public function theProductOptionWithElementValueShouldNotBeAdded(string $element, string $value): void
{
$this->client->index();
Assert::false(
$this->responseChecker->hasItemWithValue($this->client->getLastResponse(), $element, $value),
$this->responseChecker->hasItemWithValue($this->client->index(), $element, $value),
sprintf('Product option should not have %s "%s", but it does,', $element, $value)
);
}
Expand All @@ -210,15 +208,11 @@ public function theProductOptionWithElementValueShouldNotBeAdded(string $element
*/
public function thereShouldStillBeOnlyOneProductOptionWith(string $element, string $value): void
{
$this->client->index();
$itemsCount = $this->responseChecker->countCollectionItems($this->client->getLastResponse());
$response = $this->client->index();
$itemsCount = $this->responseChecker->countCollectionItems($response);

Assert::same(
1,
$this->responseChecker->countCollectionItems($this->client->getLastResponse()),
sprintf('Expected 1 product options, but got %d', $itemsCount)
);
Assert::true($this->responseChecker->hasItemWithValue($this->client->getLastResponse(), $element, $value));
Assert::same($itemsCount, 1, sprintf('Expected 1 product options, but got %d', $itemsCount));
Assert::true($this->responseChecker->hasItemWithValue($response, $element, $value));
}

/**
Expand All @@ -227,8 +221,7 @@ public function thereShouldStillBeOnlyOneProductOptionWith(string $element, stri
*/
public function thisProductOptionNameShouldBe(ProductOptionInterface $productOption, string $name): void
{
$this->client->show($productOption->getCode());
Assert::true($this->responseChecker->hasValue($this->client->getLastResponse(), 'name', $name));
Assert::true($this->responseChecker->hasValue($this->client->show($productOption->getCode()), 'name', $name));
}

/**
Expand All @@ -239,11 +232,12 @@ public function productOptionShouldHaveTheOptionValue(
ProductOptionInterface $productOption,
string $optionValueName
): void {
$this->client->subResourceIndex('values', $productOption->getCode());

Assert::true(
$this->responseChecker->hasItemWithTranslation($this->client->getLastResponse(), 'en_US', 'value', $optionValueName)
);
Assert::true($this->responseChecker->hasItemWithTranslation(
$this->client->subResourceIndex('values', $productOption->getCode()),
'en_US',
'value',
$optionValueName
));
}

/**
Expand All @@ -252,22 +246,22 @@ public function productOptionShouldHaveTheOptionValue(
public function iShouldNotBeAbleToEditItsCode(): void
{
$this->client->updateRequestData(['code' => 'NEW_CODE']);
$this->client->update();

Assert::false($this->responseChecker->hasValue($this->client->getLastResponse(), 'code', 'NEW_CODE'));
Assert::false($this->responseChecker->hasValue($this->client->update(), 'code', 'NEW_CODE'));
}

/**
* @Then I should be notified that product option with this code already exists
*/
public function iShouldBeNotifiedThatProductOptionWithThisCodeAlreadyExists(): void
{
$response = $this->client->getLastResponse();
Assert::false(
$this->responseChecker->isCreationSuccessful($this->client->getLastResponse()),
$this->responseChecker->isCreationSuccessful($response),
'Product option has been created successfully, but it should not'
);
Assert::same(
$this->responseChecker->getError($this->client->getLastResponse()),
$this->responseChecker->getError($response),
'code: The option with given code already exists.'
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,14 @@ public function iShouldBeNotifiedThatItHasBeenSuccessfullyDeleted(): void

private function isItemOnIndex(string $property, string $value): bool
{
$this->client->index();

return $this->responseChecker->hasItemWithValue($this->client->getLastResponse(), $property, $value);
return $this->responseChecker->hasItemWithValue($this->client->index(), $property, $value);
}

/** @param string|int $value */
private function assertIfReviewHasElementWithValue(ReviewInterface $productReview, string $element, $value): void
{
$this->client->show((string) $productReview->getId());
Assert::true(
$this->responseChecker->hasValue($this->client->getLastResponse(), $element, $value),
$this->responseChecker->hasValue($this->client->show((string) $productReview->getId()), $element, $value),
sprintf('Product review %s is not %s', $element, $value)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ public function iShouldBeNotifiedThatElementIsRequired(string $element): void
*/
public function iShouldSeeShippingCategoriesInTheList(int $count = 1): void
{
$this->client->index();
Assert::same($this->responseChecker->countCollectionItems($this->client->getLastResponse()), $count);
Assert::same($this->responseChecker->countCollectionItems($this->client->index()), $count);
}

/**
Expand Down Expand Up @@ -196,10 +195,9 @@ public function thisShippingCategoryShouldNoLongerExistInTheRegistry(
public function iShouldNotBeAbleToEditItsCode(): void
{
$this->client->addRequestData('code', 'NEW_CODE');
$this->client->update();

Assert::false(
$this->responseChecker->hasValue($this->client->getLastResponse(), 'code', 'NEW_CODE'),
$this->responseChecker->hasValue($this->client->update(), 'code', 'NEW_CODE'),
'The shipping category code should not be changed to "NEW_CODE", but it is'
);
}
Expand All @@ -209,9 +207,8 @@ public function iShouldNotBeAbleToEditItsCode(): void
*/
public function thereShouldStillBeOnlyOneShippingCategoryWith(string $code): void
{
$this->client->index();
Assert::same(
count($this->responseChecker->getCollectionItemsWithValue($this->client->getLastResponse(), 'code', $code)),
count($this->responseChecker->getCollectionItemsWithValue($this->client->index(), 'code', $code)),
1
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,9 @@ public function theTaxCategoryShouldAppearInTheRegistry(string $taxCategoryName)
public function iShouldNotBeAbleToEditItsCode(): void
{
$this->client->addRequestData('code', 'NEW_CODE');
$this->client->update();

Assert::false(
$this->responseChecker->hasValue($this->client->getLastResponse(), 'code', 'NEW_CODE'),
$this->responseChecker->hasValue($this->client->update(), 'code', 'NEW_CODE'),
'The code field with value NEW_CODE exist'
);
}
Expand All @@ -167,9 +166,8 @@ public function iShouldNotBeAbleToEditItsCode(): void
*/
public function thisTaxCategoryNameShouldBe(TaxCategoryInterface $taxCategory, string $taxCategoryName): void
{
$this->client->show($taxCategory->getCode());
Assert::true(
$this->responseChecker->hasValue($this->client->getLastResponse(), 'name', $taxCategoryName),
$this->responseChecker->hasValue($this->client->show($taxCategory->getCode()), 'name', $taxCategoryName),
sprintf('Tax category name is not %s', $taxCategoryName)
);
}
Expand All @@ -190,9 +188,8 @@ public function iShouldBeNotifiedThatTaxCategoryWithThisCodeAlreadyExists(): voi
*/
public function thereShouldStillBeOnlyOneTaxCategoryWith(string $element, string $value): void
{
$this->client->index();
Assert::same(
count($this->responseChecker->getCollectionItemsWithValue($this->client->getLastResponse(), $element, $value)),
count($this->responseChecker->getCollectionItemsWithValue($this->client->index(), $element, $value)),
1
);
}
Expand Down Expand Up @@ -259,8 +256,6 @@ public function iShouldBeNotifiedThatItHasBeenSuccessfullyDeleted(): void

private function isItemOnIndex(string $property, string $value): bool
{
$this->client->index();

return $this->responseChecker->hasItemWithValue($this->client->getLastResponse(), $property, $value);
return $this->responseChecker->hasItemWithValue($this->client->index(), $property, $value);
}
}

0 comments on commit a7e26e7

Please sign in to comment.