forked from PrestaShop/PrestaShop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Currency repository interface + basic implementation
Also added Currency data repository interface for mocking purpose
- Loading branch information
1 parent
5928d69
commit 1509fd3
Showing
7 changed files
with
326 additions
and
4 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
45 changes: 45 additions & 0 deletions
45
src/Core/Localization/Currency/DataRepositoryInterface.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,45 @@ | ||
<?php | ||
/** | ||
* 2007-2018 PrestaShop | ||
* | ||
* NOTICE OF LICENSE | ||
* | ||
* This source file is subject to the Open Software License (OSL 3.0) | ||
* that is bundled with this package in the file LICENSE.txt. | ||
* It is also available through the world-wide-web at this URL: | ||
* https://opensource.org/licenses/OSL-3.0 | ||
* If you did not receive a copy of the license and are unable to | ||
* obtain it through the world-wide-web, please send an email | ||
* to [email protected] so we can send you a copy immediately. | ||
* | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer | ||
* versions in the future. If you wish to customize PrestaShop for your | ||
* needs please refer to http://www.prestashop.com for more information. | ||
* | ||
* @author PrestaShop SA <[email protected]> | ||
* @copyright 2007-2018 PrestaShop SA | ||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) | ||
* International Registered Trademark & Property of PrestaShop SA | ||
*/ | ||
|
||
namespace PrestaShop\PrestaShop\Core\Localization\Currency; | ||
|
||
/** | ||
* Currency data repository interface | ||
* | ||
* Describes the behavior of currency DataRepository classes | ||
*/ | ||
interface DataRepositoryInterface | ||
{ | ||
/** | ||
* Get complete currency data by currency code | ||
* | ||
* @param string $currencyCode | ||
* | ||
* @return array | ||
* The currency data | ||
*/ | ||
public function getDataByCurrencyCode($currencyCode); | ||
} |
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,79 @@ | ||
<?php | ||
/** | ||
* 2007-2018 PrestaShop | ||
* | ||
* NOTICE OF LICENSE | ||
* | ||
* This source file is subject to the Open Software License (OSL 3.0) | ||
* that is bundled with this package in the file LICENSE.txt. | ||
* It is also available through the world-wide-web at this URL: | ||
* https://opensource.org/licenses/OSL-3.0 | ||
* If you did not receive a copy of the license and are unable to | ||
* obtain it through the world-wide-web, please send an email | ||
* to [email protected] so we can send you a copy immediately. | ||
* | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer | ||
* versions in the future. If you wish to customize PrestaShop for your | ||
* needs please refer to http://www.prestashop.com for more information. | ||
* | ||
* @author PrestaShop SA <[email protected]> | ||
* @copyright 2007-2018 PrestaShop SA | ||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) | ||
* International Registered Trademark & Property of PrestaShop SA | ||
*/ | ||
|
||
namespace PrestaShop\PrestaShop\Core\Localization\Currency; | ||
|
||
use PrestaShop\PrestaShop\Core\Localization\Currency\RepositoryInterface as CurrencyRepositoryInterface; | ||
use PrestaShop\PrestaShop\Core\Localization\Currency\DataRepositoryInterface as CurrencyDataRepositoryInterface; | ||
|
||
/** | ||
* Currency repository class | ||
* | ||
* Used to get Currency instances (by currency code for example) | ||
*/ | ||
class Repository implements CurrencyRepositoryInterface | ||
{ | ||
/** | ||
* Available currencies, indexed by ISO code. | ||
* Lazy loaded. | ||
* | ||
* @var Currency[] | ||
*/ | ||
protected $currencies; | ||
|
||
/** | ||
* @var CurrencyDataRepositoryInterface | ||
*/ | ||
protected $dataRepository; | ||
|
||
public function __construct(CurrencyDataRepositoryInterface $dataRepository) | ||
{ | ||
$this->dataRepository = $dataRepository; | ||
} | ||
|
||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getCurrency($currencyCode) | ||
{ | ||
if (!isset($this->currencies[$currencyCode])) { | ||
$data = $this->dataRepository->getDataByCurrencyCode($currencyCode); | ||
|
||
$this->currencies[$currencyCode] = new Currency( | ||
$data['isActive'], | ||
$data['conversionRate'], | ||
$data['isoCode'], | ||
$data['numericIsoCode'], | ||
$data['symbols'], | ||
$data['precision'], | ||
$data['names'] | ||
); | ||
} | ||
|
||
return $this->currencies[$currencyCode]; | ||
} | ||
} |
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,47 @@ | ||
<?php | ||
/** | ||
* 2007-2018 PrestaShop | ||
* | ||
* NOTICE OF LICENSE | ||
* | ||
* This source file is subject to the Open Software License (OSL 3.0) | ||
* that is bundled with this package in the file LICENSE.txt. | ||
* It is also available through the world-wide-web at this URL: | ||
* https://opensource.org/licenses/OSL-3.0 | ||
* If you did not receive a copy of the license and are unable to | ||
* obtain it through the world-wide-web, please send an email | ||
* to [email protected] so we can send you a copy immediately. | ||
* | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer | ||
* versions in the future. If you wish to customize PrestaShop for your | ||
* needs please refer to http://www.prestashop.com for more information. | ||
* | ||
* @author PrestaShop SA <[email protected]> | ||
* @copyright 2007-2018 PrestaShop SA | ||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) | ||
* International Registered Trademark & Property of PrestaShop SA | ||
*/ | ||
|
||
namespace PrestaShop\PrestaShop\Core\Localization\Currency; | ||
|
||
/** | ||
* Currency repository interface | ||
* | ||
* Describes the behavior of Currency Repository classes | ||
*/ | ||
interface RepositoryInterface | ||
{ | ||
/** | ||
* Get a Currency instance by ISO code. | ||
* | ||
* @param $currencyCode | ||
* Wanted currency's ISO code | ||
* Must be an alphabetic ISO 4217 currency code | ||
* | ||
* @return Currency | ||
* The wanted Currency instance | ||
*/ | ||
public function getCurrency($currencyCode); | ||
} |
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
151 changes: 151 additions & 0 deletions
151
tests/Unit/Core/Localization/Currency/RepositoryTest.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,151 @@ | ||
<?php | ||
/** | ||
* 2007-2018 PrestaShop | ||
* | ||
* NOTICE OF LICENSE | ||
* | ||
* This source file is subject to the Open Software License (OSL 3.0) | ||
* that is bundled with this package in the file LICENSE.txt. | ||
* It is also available through the world-wide-web at this URL: | ||
* https://opensource.org/licenses/OSL-3.0 | ||
* If you did not receive a copy of the license and are unable to | ||
* obtain it through the world-wide-web, please send an email | ||
* to [email protected] so we can send you a copy immediately. | ||
* | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer | ||
* versions in the future. If you wish to customize PrestaShop for your | ||
* needs please refer to http://www.prestashop.com for more information. | ||
* | ||
* @author PrestaShop SA <[email protected]> | ||
* @copyright 2007-2018 PrestaShop SA | ||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) | ||
* International Registered Trademark & Property of PrestaShop SA | ||
*/ | ||
|
||
namespace Tests\Unit\Core\Localization\Currency; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use PrestaShop\PrestaShop\Core\Localization\Currency\DataRepositoryInterface as CurrencyDataRepositoryInterface; | ||
use PrestaShop\PrestaShop\Core\Localization\Currency\Repository as CurrencyRepository; | ||
use PrestaShop\PrestaShop\Core\Localization\Exception\LocalizationException; | ||
|
||
class RepositoryTest extends TestCase | ||
{ | ||
/** | ||
* @var CurrencyRepository | ||
*/ | ||
protected $currencyRepository; | ||
|
||
protected function setUp() | ||
{ | ||
$euroData = [ | ||
'isActive' => true, | ||
'conversionRate' => 1, | ||
'isoCode' => 'EUR', | ||
'numericIsoCode' => 978, | ||
'symbols' => ['fr-FR' => '€', 'en-US' => '€'], | ||
'precision' => 2, | ||
'names' => ['fr-FR' => 'euro', 'en-US' => 'euro'], | ||
]; | ||
$peaceData = [ | ||
'isActive' => true, | ||
'conversionRate' => 1, | ||
'isoCode' => 'PCE', | ||
'numericIsoCode' => 999, | ||
'symbols' => ['fr-FR' => '☮', 'en-US' => '☮'], | ||
'precision' => 2, | ||
'names' => ['fr-FR' => 'paix', 'en-US' => 'peace'], | ||
]; | ||
|
||
$getDataByCurrencyCode = function ($isoCode) use ($euroData, $peaceData) { | ||
if ($isoCode == 'EUR') { | ||
return $euroData; | ||
} | ||
|
||
if ($isoCode == 'PCE') { | ||
return $peaceData; | ||
} | ||
|
||
throw new LocalizationException('Unknown currency code'); | ||
}; | ||
|
||
$dataRepo = $this->createMock(CurrencyDataRepositoryInterface::class); | ||
$dataRepo | ||
->method('getDataByCurrencyCode') | ||
->willReturnCallback($getDataByCurrencyCode); | ||
|
||
/** @var $dataRepo CurrencyDataRepositoryInterface */ | ||
$this->currencyRepository = new CurrencyRepository($dataRepo); | ||
} | ||
|
||
/** | ||
* Given a valid currency code | ||
* When asking the currency repository for the corresponding Currency | ||
* Then the expected Currency instance should be returned | ||
* | ||
* @param string $currencyCode | ||
* Alphabetic ISO 4217 currency code passed to retreive the wanted Currency instance | ||
* | ||
* @param array $expectedNames | ||
* Expected currency names, indexed by locale code | ||
* | ||
* @param array $expectedSymbols | ||
* Expected currency symbols, indexed by locale code | ||
* | ||
* @throws \PrestaShop\PrestaShop\Core\Localization\Exception\LocalizationException | ||
* @dataProvider provideValidCurrencyCodes | ||
*/ | ||
public function testGetCurrency($currencyCode, $expectedNames, $expectedSymbols) | ||
{ | ||
$currency = $this->currencyRepository->getCurrency($currencyCode); | ||
foreach ($expectedNames as $localeCode => $name) { | ||
$this->assertSame($name, $currency->getName($localeCode)); | ||
} | ||
|
||
foreach ($expectedSymbols as $localeCode => $symbol) { | ||
$this->assertSame($symbol, $currency->getSymbol($localeCode)); | ||
} | ||
} | ||
|
||
/** | ||
* Provide valid currency codes and the expected results | ||
* | ||
* Each data set item is structured as following : | ||
* 'Data set identifier' => [ | ||
* '<Currency ISO code to pass>', | ||
* [<Expected names to receive>], | ||
* [<Expected symbols to receive>] | ||
* ] | ||
* | ||
* @return array | ||
*/ | ||
public function provideValidCurrencyCodes() | ||
{ | ||
return [ | ||
'French euro' => [ | ||
'EUR', | ||
['fr-FR' => 'euro', 'en-US' => 'euro'], | ||
['fr-FR' => '€', 'en-US' => '€'], | ||
], | ||
'Peace money' => [ | ||
'PCE', | ||
['fr-FR' => 'paix', 'en-US' => 'peace'], | ||
['fr-FR' => '☮', 'en-US' => '☮'], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Given an unknown or invalid currency code | ||
* When asking the currency repository for the corresponding Currency | ||
* Then an exception should be raised | ||
* | ||
* @expectedException \PrestaShop\PrestaShop\Core\Localization\Exception\LocalizationException | ||
*/ | ||
public function testGetCurrencyWithUnknownCode() | ||
{ | ||
$this->currencyRepository->getCurrency('foobar'); | ||
} | ||
} |