-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fixes and ExchangeRate transform
- Loading branch information
Showing
6 changed files
with
82 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/Sylius/Behat/Context/Transform/ExchangeRateContext.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters