Skip to content

Commit

Permalink
Fix test for ExchangeRate
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomanhez committed Mar 9, 2020
1 parent dc981b8 commit 1bfbb84
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Feature: Deleting multiple exchange rates
And the exchange rate of "Polish Zloty" to "Euro" is 0.22
And I am logged in as an administrator

@ui @api
@ui
Scenario: Deleting multiple exchange rates at once
When I browse exchange rates
And I check the exchange rate between "Euro" and "British Pound"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Feature: Filtering exchange rates by a currency
And the exchange rate of "Polish Zloty" to "Euro" is 0.22
And I am logged in as an administrator

@ui @api
@ui
Scenario: Filtering exchange rates by a chosen currency
When I browse exchange rates of the store
And I choose "Euro" as a currency filter
Expand Down
2 changes: 1 addition & 1 deletion src/Sylius/Behat/Client/ApiClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function applyTransition(string $resource, string $id, string $transition

public function countCollectionItems(): int;

public function getCollection(): array;
public function getCollectionItems(): array;

public function getCollectionItemsWithValue(string $key, string $value): array;

Expand Down
12 changes: 6 additions & 6 deletions src/Sylius/Behat/Client/ApiPlatformClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function subResourceIndex(string $resource, string $subResource, string $

public function buildCreateRequest(string $resource): void
{
$this->request['url'] = '/new-api/'.$resource;
$this->request['url'] = '/new-api/' . $resource;
}

public function buildUpdateRequest(string $resource, string $id): void
Expand Down Expand Up @@ -113,14 +113,14 @@ public function countCollectionItems(): int
return (int) $this->getResponseContentValue('hydra:totalItems');
}

public function getCollection(): array
public function getCollectionItems(): array
{
return $this->getResponseContentValue('hydra:member');
}

public function getCollectionItemsWithValue(string $key, string $value): array
{
$items = array_filter($this->getCollection(), function (array $item) use ($key, $value): bool {
$items = array_filter($this->getCollectionItems(), function (array $item) use ($key, $value): bool {
return $item[$key] === $value;
});

Expand Down Expand Up @@ -156,7 +156,7 @@ public function responseHasValue(string $key, $value): bool
/** @param string|float $value */
public function hasItemWithValue(string $key, $value): bool
{
foreach ($this->getCollection() as $resource) {
foreach ($this->getCollectionItems() as $resource) {
if ($resource[$key] === $value) {
return true;
}
Expand All @@ -167,12 +167,12 @@ public function hasItemWithValue(string $key, $value): bool

public function hasItemOnPositionWithValue(int $position, string $key, string $value): bool
{
return $this->getCollection()[$position][$key] === $value;
return $this->getCollectionItems()[$position][$key] === $value;
}

public function hasItemWithTranslation(string $locale, string $key, string $translation): bool
{
foreach ($this->getCollection() as $resource) {
foreach ($this->getCollectionItems() as $resource) {
if (
isset($resource['translations']) &&
isset($resource['translations'][$locale]) &&
Expand Down
119 changes: 39 additions & 80 deletions src/Sylius/Behat/Context/Api/Admin/ManagingExchangeRatesContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function iWantToAddNewExchangeRate(): void
*/
public function iWantToEditThisExchangeRate(ExchangeRateInterface $exchangeRate): void
{
$this->sharedStorage->set('exchangeRateId', $exchangeRate->getId());
$this->sharedStorage->set('exchange_rate_id', $exchangeRate->getId());
}

/**
Expand All @@ -65,7 +65,7 @@ public function iAmEditingThisExchangeRate(ExchangeRateInterface $exchangeRate):
}

/**
* @Given I am browsing exchange rates of the store
* @When I am browsing exchange rates of the store
* @When I browse exchange rates
* @When I browse exchange rates of the store
*/
Expand All @@ -75,12 +75,12 @@ public function iBrowseExchangeRatesOfTheStore(): void
}

/**
* @When /^I specify its ratio as (-?[0-9\.]+)$/
* @When I specify its ratio as :ratio
* @When I don't specify its ratio
*/
public function iSpecifyItsRatioAs(?float $ratio = null): void
public function iSpecifyItsRatioAs(?string $ratio = null): void
{
if($ratio !== null) {
if ($ratio !== null) {
$this->client->addRequestData('ratio', $ratio);
}
}
Expand All @@ -90,15 +90,15 @@ public function iSpecifyItsRatioAs(?float $ratio = null): void
*/
public function iChooseAsTheSourceCurrency(string $currencyCode): void
{
$this->client->addRequestData('sourceCurrency', '/new-api/currencies/'.$currencyCode);
$this->client->addRequestData('sourceCurrency', '/new-api/currencies/' . $currencyCode);
}

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

/**
Expand All @@ -114,7 +114,7 @@ public function iAddIt(): void
*/
public function iChangeRatioTo(string $ratio): void
{
$this->client->addRequestData('ratio', $ratio);
$this->client->updateRequestData(['ratio' => $ratio]);
}

/**
Expand All @@ -136,54 +136,22 @@ public function iDeleteTheExchangeRateBetweenAnd(CurrencyInterface $sourceCurren
$this->client->delete('exchange_rates', $exchangeRate->getId());
}

/**
* @When I choose :name as a currency filter
*/
public function iChooseCurrencyAsACurrencyFilter(string $name): void
{
$this->client->buildUpdateRequest('rating', $name);//TODO
}

/**
* @When I filter
*/
public function iFilter(): void
{
//TODO
}

/**
* @When I check (also) the exchange rate between :sourceCurrencyName and :targetCurrencyName
*/
public function iCheckTheExchangeRateBetweenAnd(string $sourceCurrencyName, string $targetCurrencyName): void
{
// Intentionally left blank to fulfill after handling bulk delete
}

/**
* @When I delete them
*/
public function iDeleteThem(): void
{
// Intentionally left blank to fulfill after handling bulk delete
}

/**
* @Then I should see :count exchange rates on the list
*/
public function iShouldSeeExchangeRatesOnTheList(int $count = 0): void
public function iShouldSeeExchangeRatesOnTheList(int $count): void
{
Assert::same($this->client->countCollectionItems(), $count);
Assert::count($this->client->getCollectionItems(), $count);
}

/**
* @Then I should see a single exchange rate in the list
* @Then I should( still) see one exchange rate on the list
*/
public function iShouldSeeOneExchangeRateOnTheList(): void
public function iShouldSeeASingleExchangeRateInTheList(): void
{
$this->client->index('exchange_rates');
Assert::same(count($this->client->getCollection()), 1);
Assert::count($this->client->getCollectionItems(), 1);
}

/**
Expand All @@ -195,7 +163,7 @@ public function theExchangeRateWithRatioBetweenAndShouldAppearInTheStore(
CurrencyInterface $targetCurrency
): void {
Assert::true(
$this->hasExchangeRateFromResponse($ratio, $sourceCurrency, $targetCurrency),
$this->responseHasExchangeRate($ratio, $sourceCurrency, $targetCurrency),
sprintf(
'Exchange rate with ratio %s between %s and %s does not exist',
$ratio,
Expand All @@ -213,13 +181,16 @@ public function iShouldSeeTheExchangeRateBetweenAndInTheList(
CurrencyInterface $sourceCurrency,
CurrencyInterface $targetCurrency
): void {
Assert::notNull($this->getExchangeRateFromResponse($sourceCurrency, $targetCurrency));
Assert::notNull(
$this->getExchangeRateFromResponse($sourceCurrency, $targetCurrency),
sprintf('Exchange rate for %s and %s currencies does not exist', $sourceCurrency, $targetCurrency)
);
}

/**
* @Then it should have a ratio of :ratio
*/
public function itShouldHaveRatioOf(float $ratio): void
public function itShouldHaveARatioOf(float $ratio): void
{
$this->client->index('exchange_rates');

Expand All @@ -235,7 +206,7 @@ public function itShouldHaveRatioOf(float $ratio): void
public function thisExchangeRateShouldNoLongerBeOnTheList(ExchangeRateInterface $exchangeRate): void
{
Assert::false(
$this->hasExchangeRateFromResponse(
$this->responseHasExchangeRate(
$exchangeRate->getRatio(),
$exchangeRate->getSourceCurrency(),
$exchangeRate->getTargetCurrency()
Expand Down Expand Up @@ -264,48 +235,30 @@ public function theExchangeRateBetweenAndShouldNotBeAdded(
/**
* @Then /^(this exchange rate) should have a ratio of ([0-9\.]+)$/
*/
public function thisExchangeRateShouldHaveARatioOf(ExchangeRateInterface $exchangeRate, string $ratio): void
public function thisExchangeRateShouldHaveARatioOf(ExchangeRateInterface $exchangeRate, float $ratio): void
{
$exchangeRate = $this->getExchangeRateFromResponse(
$exchangeRate->getSourceCurrency(),
$exchangeRate->getTargetCurrency()
);

$exchangeRate['ratio'] = $ratio;
Assert::same($exchangeRate['ratio'], $ratio);
}

/**
* @Then I should not be able to edit its source currency
*/
public function iShouldNotBeAbleToEditItsSourceCurrency(): void
{
$this->client->buildUpdateRequest('exchange_rates', $this->sharedStorage->get('exchangeRateId'));

$this->client->addRequestData('sourceCurrency', '/new-api/currencies/EUR');
$this->client->update();

$this->client->index('exchange_rates');
Assert::false(
$this->client->hasItemOnPositionWithValue(0,'sourceCurrency', '/new-api/currencies/EUR'),
'The sourceCurrency field with value \'/new-api/currencies/EUR\' exists'
);
$this->assertIfNotBeAbleToEditItCurrency('sourceCurrency');
}

/**
* @Then I should not be able to edit its target currency
*/
public function iShouldNotBeAbleToEditItsTargetCurrency(): void
{
$this->client->buildUpdateRequest('exchange_rates', $this->sharedStorage->get('exchangeRateId'));

$this->client->addRequestData('targetCurrency', '/new-api/currencies/EUR');
$this->client->update();

$this->client->index('exchange_rates');
Assert::false(
$this->client->hasItemOnPositionWithValue(0, 'targetCurrency', '/new-api/currencies/EUR'),
'The targetCurrency field with value \'/new-api/currencies/EUR\' exists'
);
$this->assertIfNotBeAbleToEditItCurrency('targetCurrency');
}

/**
Expand Down Expand Up @@ -340,12 +293,18 @@ public function iShouldBeNotifiedThatTheCurrencyPairMustBeUnique(): void
Assert::contains($this->client->getError(), 'The currency pair must be unique.');
}

/**
* @Then I should be notified that they have been successfully deleted
*/
public function iShouldBeNotifiedThatTheyHaveBeenSuccessfullyDeleted(): void
private function assertIfNotBeAbleToEditItCurrency(string $currencyType): void
{
// Intentionally left blank to fulfill after handling bulk delete
$this->client->buildUpdateRequest('exchange_rates', $this->sharedStorage->get('exchange_rate_id'));

$this->client->addRequestData($currencyType, '/new-api/currencies/EUR');
$this->client->update();

$this->client->index('exchange_rates');
Assert::false(
$this->client->hasItemOnPositionWithValue(0, $currencyType, '/new-api/currencies/EUR'),
sprintf('It was possible to change %s', $currencyType)
);
}

private function getExchangeRateBetweenCurrencies(string $sourceCode, string $targetCode): ExchangeRateInterface
Expand All @@ -365,11 +324,11 @@ private function getExchangeRateFromResponse(
$this->client->index('exchange_rates');

/** @var array $item */
foreach ($this->client->getCollection() as $item)
foreach ($this->client->getCollectionItems() as $item)
{
if (
$item['sourceCurrency'] === '/new-api/currencies/'.$sourceCurrency->getCode() &&
$item['targetCurrency'] === '/new-api/currencies/'.$targetCurrency->getCode()
$item['sourceCurrency'] === '/new-api/currencies/' . $sourceCurrency->getCode() &&
$item['targetCurrency'] === '/new-api/currencies/' . $targetCurrency->getCode()
) {
return $item;
}
Expand All @@ -378,11 +337,11 @@ private function getExchangeRateFromResponse(
return null;
}

private function hasExchangeRateFromResponse(
private function responseHasExchangeRate(
float $ratio,
CurrencyInterface $sourceCurrency,
CurrencyInterface $targetCurrency
): bool {
return $this->getExchangeRateFromResponse($sourceCurrency, $targetCurrency)['ratio'] === $ratio ? true : false;
return $this->getExchangeRateFromResponse($sourceCurrency, $targetCurrency)['ratio'] === $ratio;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ default:
- sylius.behat.context.transform.lexical
- sylius.behat.context.transform.shared_storage

- sylius.behat.context.setup.admin_security
- sylius.behat.context.setup.admin_api_security
- sylius.behat.context.setup.currency
- sylius.behat.context.setup.exchange_rate

Expand All @@ -20,3 +20,4 @@ default:

filters:
tags: "@managing_exchange_rates && @api"
javascript: false
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
</attribute>
</attribute>
<collectionOperations>
<collectionOperation name="get">
<attribute name="filters">
<attribute>exchange_rate.search_currency</attribute>
</attribute>
</collectionOperation>
<collectionOperation name="get" />
<collectionOperation name="post">
<attribute name="denormalization_context">
<attribute name="groups">exchange_rate:write</attribute>
Expand Down

0 comments on commit 1bfbb84

Please sign in to comment.