Skip to content

Commit

Permalink
Ability to add new currencies without replacing default ones (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas7777 authored Mar 21, 2020
1 parent 51c2d24 commit 953aa05
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ vendor
composer.phar
composer.lock
phpunit.xml
bin/
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## 0.10.0 - 2020-03-21
### Added
- Added `\Maba\Component\Monetary\Information\MoneyInformationProvider::addAvailableCurrencies` method to add additional currencies and precisions
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@
"Maba\\Component\\Monetary\\": "src",
"Maba\\Component\\Monetary\\Tests\\": "test"
}
},
"config": {
"bin-dir": "bin"
}
}
12 changes: 10 additions & 2 deletions src/Information/MoneyInformationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MoneyInformationProvider implements MoneyInformationProviderInterface
{
const DEFAULT_PRECISION = 2;

static protected $defaultCurrencyPrecisions = array (
static protected $defaultCurrencyPrecisions = array(
'BHD' => 3,
'BIF' => 0,
'BYR' => 0,
Expand Down Expand Up @@ -238,4 +238,12 @@ public function getSupportedCurrencies()
return $this->availableCurrencies;
}

}
public function addAvailableCurrencies(array $availableCurrencies, array $currencyPrecisions = null)
{
$this->currencyPrecisions = $currencyPrecisions !== null
? array_replace($this->currencyPrecisions, $currencyPrecisions)
: $this->currencyPrecisions
;
$this->availableCurrencies = array_unique(array_merge($this->availableCurrencies, $availableCurrencies));
}
}
93 changes: 93 additions & 0 deletions test/Information/MoneyInformationProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Maba\Component\Monetary\Tests\Information;

use PHPUnit_Framework_TestCase;
use Maba\Component\Monetary\Information\MoneyInformationProvider;

class MoneyInformationProviderTest extends PHPUnit_Framework_TestCase
{
private $currencyPrecisions;
private $availableCurrencies;

public function setUp()
{
$this->currencyPrecisions = array(
'BHD' => 3,
'BIF' => 0,
'BYR' => 0,
'CLF' => 4,
'CLP' => 0,
) ;
$this->availableCurrencies = array(
'BHD',
'BIF',
'BYR',
'CLF',
'CLP',
);
}

/**
* @param array $availableCurrencies
* @param array $expected
*
* @dataProvider dataProviderGetSupportedCurrenciesAfterAddingNewOnes
*/
public function testGetSupportedCurrenciesAfterAddingNewOnes(array $availableCurrencies, array $expected)
{
$moneyInformationProvider = new MoneyInformationProvider($this->currencyPrecisions,$this->availableCurrencies);
$moneyInformationProvider->addAvailableCurrencies($availableCurrencies);

$this->assertSame($expected, $moneyInformationProvider->getSupportedCurrencies());
}

public function dataProviderGetSupportedCurrenciesAfterAddingNewOnes()
{
return array(
'adding existing currency' => array(
array('BHD', 'BIF'),
array('BHD', 'BIF', 'BYR', 'CLF', 'CLP'),
),
'adding new currency' => array(
array('XAU'),
array('BHD', 'BIF', 'BYR', 'CLF', 'CLP', 'XAU'),
),
);
}

/**
* @param string $currency
* @param array|null $precision
* @param string $expected
*
* @dataProvider dataProviderGetDefaultCurrency
*/
public function testGetDefaultPrecision($currency, $expected, array $precision = null)
{
$moneyInformationProvider = new MoneyInformationProvider($this->currencyPrecisions, $this->availableCurrencies);
$moneyInformationProvider->addAvailableCurrencies(array($currency), $precision);

$this->assertSame($expected, $moneyInformationProvider->getDefaultPrecision($currency));
}

public function dataProviderGetDefaultCurrency()
{
return array(
'adding existing currency with different precision' => array(
'BHD',
4,
array('BHD' => 4),
),
'adding new currency with precision' => array(
'XAU',
6,
array('XAU' => 6),
),
'adding new currency without precision' => array(
'XAU',
MoneyInformationProvider::DEFAULT_PRECISION,
),
);
}
}

0 comments on commit 953aa05

Please sign in to comment.