Skip to content

Commit

Permalink
Add fixes and ExchangeRate transform
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomanhez committed Mar 11, 2020
1 parent 21c56e5 commit b30cfc6
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/Sylius/Behat/Client/ApiClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function buildUpdateRequest(string $resource, string $id): void;

public function buildFilter(array $filters): void;

/** @param string|int */
/** @param string|int $value */
public function addRequestData(string $key, $value): void;

public function addCompoundRequestData(array $data): void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,21 @@
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Currency\Model\CurrencyInterface;
use Sylius\Component\Currency\Model\ExchangeRateInterface;
use Sylius\Component\Currency\Repository\ExchangeRateRepositoryInterface;
use Webmozart\Assert\Assert;

final class ManagingExchangeRatesContext implements Context
{
/** @var ApiClientInterface */
private $client;

/** @var ExchangeRateRepositoryInterface */
private $exchangeRateRepository;

/** @var SharedStorageInterface */
private $sharedStorage;

public function __construct(
ApiClientInterface $client,
ExchangeRateRepositoryInterface $exchangeRateRepository,
SharedStorageInterface $sharedStorage
) {
$this->client = $client;
$this->exchangeRateRepository = $exchangeRateRepository;
$this->sharedStorage = $sharedStorage;
}

Expand Down Expand Up @@ -121,13 +115,10 @@ public function iSaveMyChanges(): void
}

/**
* @When I delete the exchange rate between :sourceCurrency and :targetCurrency
* @When /^I delete the (exchange rate between "[^"]+" and "[^"]+")$/
*/
public function iDeleteTheExchangeRateBetweenAnd(CurrencyInterface $sourceCurrency, CurrencyInterface $targetCurrency) : void
public function iDeleteTheExchangeRateBetweenAnd(ExchangeRateInterface $exchangeRate) : void
{
/** @var ExchangeRateInterface */
$exchangeRate = $this->getExchangeRateBetweenCurrencies($sourceCurrency->getCode(), $targetCurrency->getCode());

$this->client->delete('exchange_rates', $exchangeRate->getId());
}

Expand Down Expand Up @@ -223,7 +214,7 @@ public function thisExchangeRateShouldNoLongerBeOnTheList(ExchangeRateInterface
$exchangeRate->getTargetCurrency()
),
sprintf(
'Exchange rate with ratio %s between %s and %s still exists',
'Exchange rate with ratio %s between %s and %s still exists, but it should not.',
$exchangeRate->getRatio(),
$exchangeRate->getSourceCurrency()->getName(),
$exchangeRate->getTargetCurrency()->getName()
Expand Down Expand Up @@ -318,16 +309,6 @@ private function assertIfNotBeAbleToEditItCurrency(string $currencyType): void
);
}

private function getExchangeRateBetweenCurrencies(string $sourceCode, string $targetCode): ExchangeRateInterface
{
/** @var ExchangeRateInterface|null */
$exchangeRate = $this->exchangeRateRepository->findOneWithCurrencyPair($sourceCode, $targetCode);

Assert::notNull($exchangeRate);

return $exchangeRate;
}

private function getExchangeRateFromResponse(
CurrencyInterface $sourceCurrency,
CurrencyInterface $targetCurrency
Expand Down
71 changes: 71 additions & 0 deletions src/Sylius/Behat/Context/Transform/ExchangeRateContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Behat\Context\Transform;

use Behat\Behat\Context\Context;
use Sylius\Component\Currency\Converter\CurrencyNameConverterInterface;
use Sylius\Component\Currency\Model\ExchangeRateInterface;
use Sylius\Component\Currency\Repository\ExchangeRateRepositoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Webmozart\Assert\Assert;

final class ExchangeRateContext implements Context
{
/** @var CurrencyNameConverterInterface */
private $currencyNameConverter;

/** @var RepositoryInterface */
private $currencyRepository;

/** @var ExchangeRateRepositoryInterface */
private $exchangeRateRepository;

public function __construct(
CurrencyNameConverterInterface $currencyNameConverter,
RepositoryInterface $currencyRepository,
ExchangeRateRepositoryInterface $exchangeRateRepository
) {
$this->currencyNameConverter = $currencyNameConverter;
$this->currencyRepository = $currencyRepository;
$this->exchangeRateRepository = $exchangeRateRepository;
}

/**
* @Transform /^exchange rate between "([^"]+)" and "([^"]+)"$/
*/
public function getExchangeRateByCurrencies(
string $sourceCurrencyName,
string $targetCurrencyName
): ExchangeRateInterface {
$sourceCurrencyCode = $this->currencyNameConverter->convertToCode($sourceCurrencyName);
$targetCurrencyCode = $this->currencyNameConverter->convertToCode($targetCurrencyName);

/** @var ExchangeRateInterface|null */
$exchangeRate = $this
->exchangeRateRepository
->findOneWithCurrencyPair($sourceCurrencyCode, $targetCurrencyCode)
;

Assert::notNull(
$exchangeRate,
sprintf(
'ExchangeRate for %s and %s currencies does not exist.',
$sourceCurrencyName,
$targetCurrencyName
)
);

return $exchangeRate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

<service id="sylius.behat.context.api.admin.managing_exchange_rates" class="Sylius\Behat\Context\Api\Admin\ManagingExchangeRatesContext">
<argument type="service" id="Sylius\Behat\Client\ApiClientInterface" />
<argument type="service" id="sylius.repository.exchange_rate" />
<argument type="service" id="sylius.behat.shared_storage" />
</service>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
<service id="sylius.behat.context.transform.date_time" class="Sylius\Behat\Context\Transform\DateTimeContext">
</service>

<service id="sylius.behat.context.transform.exchange_rate" class="Sylius\Behat\Context\Transform\ExchangeRateContext">
<argument type="service" id="sylius.currency_name_converter" />
<argument type="service" id="sylius.repository.currency" />
<argument type="service" id="sylius.repository.exchange_rate" />
</service>

<service id="sylius.behat.context.transform.lexical" class="Sylius\Behat\Context\Transform\LexicalContext">
</service>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ default:
- sylius.behat.context.hook.doctrine_orm

- sylius.behat.context.transform.currency
- sylius.behat.context.transform.exchange_rate
- sylius.behat.context.transform.lexical
- sylius.behat.context.transform.shared_storage

Expand Down

0 comments on commit b30cfc6

Please sign in to comment.