Skip to content

Commit

Permalink
Fix ExchangeRate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomanhez committed Mar 5, 2020
1 parent 6c7b549 commit 16f3952
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 51 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 @@ -16,6 +16,6 @@ Feature: Filtering exchange rates by a currency
When I browse exchange rates of the store
And I choose "Euro" as a currency filter
And I filter
Then I should see 2 exchange rates on the list
Then I should see 2 filtered exchange rates on the list
And I should see an exchange rate between "Euro" and "British Pound" on the list
And I should also see an exchange rate between "Polish Zloty" and "Euro" on the list
2 changes: 2 additions & 0 deletions src/Sylius/Behat/Client/ApiClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function update(): void;

public function delete(string $resource, string $id): void;

public function filter(string $resource, string $fieldType, string $fieldValue): void;

public function countCollectionItems(): int;

public function getCollection(): array;
Expand Down
15 changes: 15 additions & 0 deletions src/Sylius/Behat/Client/ApiPlatformClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ public function delete(string $resource, string $id): void
$this->request('DELETE', sprintf('/new-api/%s/%s', $resource, $id), []);
}

public function filter(string $resource, string $fieldType, string $fieldValue): void
{
$this->client->request(
'GET', sprintf(
'/new-api/%s?%s=%s',
$resource,
$fieldType,
$fieldValue
),
[],
[],
['HTTP_ACCEPT' => 'application/ld+json']
);
}

public function countCollectionItems(): int
{
return (int) $this->getResponseContentValue('hydra:totalItems');
Expand Down
80 changes: 33 additions & 47 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 Down Expand Up @@ -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 Down Expand Up @@ -137,35 +137,33 @@ public function iDeleteTheExchangeRateBetweenAnd(CurrencyInterface $sourceCurren
}

/**
* @When I choose :name as a currency filter
* @When I choose :currency as a currency filter
*/
public function iChooseCurrencyAsACurrencyFilter(string $name): void
public function iChooseCurrencyAsACurrencyFilter(CurrencyInterface $currency): void
{
$this->client->buildUpdateRequest('rating', $name);//TODO
$exchangeRates = [];
$this->client->filter('exchange_rates', 'sourceCurrency.code', $currency->getCode());
$exchangeRates = array_merge($exchangeRates, $this->client->getCollection());
$this->client->filter('exchange_rates', 'targetCurrency.code', $currency->getCode());
$exchangeRates = array_merge($exchangeRates, $this->client->getCollection());

$this->sharedStorage->set('filtered_exchange_rates', $exchangeRates);
}

/**
* @When I filter
*/
public function iFilter(): void
{
//TODO
//Intentionally left blank to fulfill context expectation
}

/**
* @When I check (also) the exchange rate between :sourceCurrencyName and :targetCurrencyName
* @Then I should see :count filtered exchange rates on the list
*/
public function iCheckTheExchangeRateBetweenAnd(string $sourceCurrencyName, string $targetCurrencyName): void
public function iShouldSeeFilteredExchangeRatesOnTheList(int $count = 0): 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
Assert::same(count($this->sharedStorage->get('filtered_exchange_rates')), $count);
}

/**
Expand All @@ -180,7 +178,7 @@ public function iShouldSeeExchangeRatesOnTheList(int $count = 0): void
* @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);
Expand Down Expand Up @@ -219,7 +217,7 @@ public function iShouldSeeTheExchangeRateBetweenAndInTheList(
/**
* @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 Down Expand Up @@ -279,33 +277,15 @@ public function thisExchangeRateShouldHaveARatioOf(ExchangeRateInterface $exchan
*/
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 +320,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('The %s field with value \'/new-api/currencies/EUR\' exists', $currencyType)
);
}

private function getExchangeRateBetweenCurrencies(string $sourceCode, string $targetCode): ExchangeRateInterface
Expand All @@ -368,8 +354,8 @@ private function getExchangeRateFromResponse(
foreach ($this->client->getCollection() 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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ public function iDeleteThem(): void

/**
* @Then I should see :count exchange rates on the list
* @Then I should see :count filtered exchange rates on the list
*/
public function iShouldSeeExchangeRatesOnTheList($count = 0)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Sylius/Bundle/ApiBundle/Resources/config/filters.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
<service id="app.exchange_rate_resource.search_filter" parent="ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter" public="false">
<tag name="api_platform.filter" id="exchange_rate.search_currency"/>
<argument type="collection">
<argument key="sourceCurrency.code">partial</argument>
<argument key="targetCurrency.code">partial</argument>
<argument key="sourceCurrency.code">start</argument>
<argument key="targetCurrency.code">start</argument>
</argument>
</service>
</services>
Expand Down

0 comments on commit 16f3952

Please sign in to comment.