Skip to content

Commit

Permalink
Refactor tests (#296)
Browse files Browse the repository at this point in the history
Refactor tests and solve a few uncovered problems

* Refactor tests for calculators

* Refactor tests for currencies

* Applied fixes from StyleCI

* Refactor exception tests

* division precision for gmp

* Refactor exchange tests

* Refactor formatter tests

* Refactor parser tests

* Applied fixes from StyleCI

* Refactor converter tests

* Refactor currency pair tests

* Refactor money tests

* Refactor currency tests

* Refactor number tests

* Tweak phpspec configuration

Changelogs summary:

 - coduo/phpspec-data-provider-extension removed (installed version was 1.0.3)

* Applied fixes from StyleCI
  • Loading branch information
sagikazarmark authored Oct 19, 2016
1 parent 2707d45 commit c8bb277
Show file tree
Hide file tree
Showing 68 changed files with 1,324 additions and 715 deletions.
19 changes: 11 additions & 8 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
tests/ export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.php_cs export-ignore
.styleci.yml export-ignore
.travis.yml export-ignore
phpunit.xml.dist export-ignore
/spec/ export-ignore
/tests/ export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.php_cs export-ignore
/.styleci.yml export-ignore
/.travis.yml export-ignore
/phpspec.ci.yml export-ignore
/phpspec.yml.dist export-ignore
/phpunit.xml.dist export-ignore
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@

## Unreleased

### Added

- DecimalMoneyFormatter: returns locale-independent raw decimal string

### Changed

- **[BC break]** Replaced StringToUnitsParser with DecimalMoneyParser
- **[BC break]** Moved `Money\Exception\Exception` to `Money\Exception`
- **[BC break]** UnkownCurrencyException is now DomainException instead of RuntimeException

### Added

- DecimalMoneyFormatter: returns locale-independent raw decimal string

## 3.0.0-beta.3 - 2016-10-04

Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"cache/taggable-cache": "^0.4.0",
"phpspec/phpspec": "^2.5",
"henrikbjorn/phpspec-code-coverage": "^2.0.2",
"coduo/phpspec-data-provider-extension": "^1.0.3",
"moneyphp/iso-currencies": "dev-master",
"sllh/php-cs-fixer-styleci-bridge": "^2.1"
},
Expand All @@ -56,7 +55,7 @@
},
"scripts": {
"test": "vendor/bin/phpspec run && vendor/bin/phpunit",
"test-ci": "vendor/bin/phpspec run -c phpspec.yml.ci && vendor/bin/phpunit --coverage-text",
"test-ci": "vendor/bin/phpspec run -c phpspec.ci.yml && vendor/bin/phpunit --coverage-text",
"update-currencies": "cp vendor/moneyphp/iso-currencies/resources/current.php resources/currency.php"
},
"extra": {
Expand Down
1 change: 0 additions & 1 deletion phpspec.yml.ci → phpspec.ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ suites:
psr4_prefix: Money
formatter.name: pretty
extensions:
- Coduo\PhpSpec\DataProvider\DataProviderExtension
- PhpSpec\Extension\CodeCoverageExtension
code_coverage:
format: clover
Expand Down
2 changes: 0 additions & 2 deletions phpspec.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ suites:
namespace: Money
psr4_prefix: Money
formatter.name: pretty
extensions:
- Coduo\PhpSpec\DataProvider\DataProviderExtension
3 changes: 2 additions & 1 deletion spec/Calculator/BcMathCalculatorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace spec\Money\Calculator;

use Money\Calculator\BcMathCalculator;
use PhpSpec\ObjectBehavior;

class BcMathCalculatorSpec extends ObjectBehavior
Expand All @@ -10,6 +11,6 @@ class BcMathCalculatorSpec extends ObjectBehavior

function it_is_initializable()
{
$this->shouldHaveType('Money\Calculator\BcMathCalculator');
$this->shouldHaveType(BcMathCalculator::class);
}
}
65 changes: 28 additions & 37 deletions spec/Calculator/CalculatorBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace spec\Money\Calculator;

use Money\Calculator;
use spec\Money\RoundExamples;

/**
* Mocking with typehints usage won't work here as the trait is autoloaded.
Expand All @@ -12,8 +11,6 @@
*/
trait CalculatorBehavior
{
use RoundExamples;

function it_is_a_calculator()
{
$this->shouldImplement(Calculator::class);
Expand All @@ -26,67 +23,61 @@ function it_compares_two_values()
$this->compare(1, 1)->shouldReturn(0);
}

function it_adds_a_value()
function it_adds_two_values()
{
$this->add(1, 1)->shouldReturn('2');
$this->add(rand(-100, 100), rand(-100, 100))->shouldBeString();
}

function it_subtracts_a_value()
function it_subtracts_a_value_from_another()
{
$this->subtract(2, 1)->shouldReturn('1');
$this->subtract(rand(-100, 100), rand(-100, 100))->shouldBeString();
}

// TODO Examine comparison
function it_multiplies_a_value()
function it_multiplies_a_value_by_another()
{
$this->multiply(1, 1.5)->shouldReturn('1.5');
$this->multiply(10, 1.2500)->shouldBeLike('12.50');
$this->multiply(100, 0.29)->shouldBeLike('29');
$this->multiply(100, 0.029)->shouldBeLike('2.9');
$this->multiply(100, 0.0029)->shouldBeLike('0.29');
$this->multiply(1000, 0.29)->shouldBeLike('290');
$this->multiply(1000, 0.029)->shouldBeLike('29');
$this->multiply(1000, 0.0029)->shouldBeLike('2.9');
$this->multiply(2000, 0.0029)->shouldBeLike('5.8');
$this->multiply(rand(-100, 100), rand(-100, 100))->shouldBeString();
}

// TODO Examine comparison
function it_divides_a_value()
function it_divides_a_value_by_another()
{
$this->divide(3, 2)->shouldBeLike('1.5');
$this->divide(10, 4)->shouldBeLike('2.5');
$this->divide(rand(-100, 100), rand(-100, 100))->shouldBeString();
}

function it_ceils_a_value()
{
$this->ceil(1.2)->shouldReturn('2');
$this->ceil(-1.2)->shouldReturn('-1');
$this->ceil('2.00')->shouldReturn('2');
$this->ceil(rand(-100, 100) / 100)->shouldBeString();
}

function it_floors_a_value()
{
$this->floor(2.7)->shouldReturn('2');
$this->floor(-2.7)->shouldReturn('-3');
$this->floor('2.00')->shouldReturn('2');
$this->floor(rand(-100, 100) / 100)->shouldBeString();
}

function it_calculates_the_absolute_value()
{
$this->absolute(2)->shouldReturn('2');
$this->absolute(-2)->shouldReturn('2');
$result = $this->absolute(rand(1, 100));

$result->shouldBeGreaterThanZero();
$result->shouldBeString();


$result = $this->absolute(rand(-100, -1));

$result->shouldBeGreaterThanZero();
$result->shouldBeString();
}

function testShare()
function it_shares_a_value()
{
$this->share(10, 2, 4)->shouldReturn('5');
$this->share(10, 2, 4)->shouldBeString();
}

/**
* @dataProvider roundExamples
*/
function it_rounds_a_value($input, $mode, $expected)
public function getMatchers()
{
$this->round($input, $mode)->shouldReturn($expected);
return [
'beGreaterThanZero' => function ($subject) {
return $subject > 0;
},
];
}
}
25 changes: 2 additions & 23 deletions spec/Calculator/GmpCalculatorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace spec\Money\Calculator;

use Money\Calculator\GmpCalculator;
use PhpSpec\ObjectBehavior;

class GmpCalculatorSpec extends ObjectBehavior
Expand All @@ -10,28 +11,6 @@ class GmpCalculatorSpec extends ObjectBehavior

function it_is_initializable()
{
$this->shouldHaveType('Money\Calculator\GmpCalculator');
}

function it_adds_with_scale_set()
{
$defaultScale = ini_get('bcmath.scale');

bcscale(1);

$this->add(1, 1)->shouldReturn('2');

bcscale($defaultScale);
}

function it_subtracts_with_scale_set()
{
$defaultScale = ini_get('bcmath.scale');

bcscale(1);

$this->subtract(2, 1)->shouldReturn('1');

bcscale($defaultScale);
$this->shouldHaveType(GmpCalculator::class);
}
}
3 changes: 2 additions & 1 deletion spec/Calculator/PhpCalculatorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace spec\Money\Calculator;

use Money\Calculator\PhpCalculator;
use PhpSpec\ObjectBehavior;

class PhpCalculatorSpec extends ObjectBehavior
Expand All @@ -10,7 +11,7 @@ class PhpCalculatorSpec extends ObjectBehavior

function it_is_initializable()
{
$this->shouldHaveType('Money\Calculator\PhpCalculator');
$this->shouldHaveType(PhpCalculator::class);
}

function it_throws_an_exception_when_overflown()
Expand Down
51 changes: 16 additions & 35 deletions spec/ConverterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

namespace spec\Money;

use Money\Calculator;
use Money\Converter;
use Money\Currencies;
use Money\Currency;
use Money\CurrencyPair;
use Money\Exchange;
use Money\Money;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class ConverterSpec extends ObjectBehavior
{
Expand All @@ -18,37 +17,29 @@ function let(Currencies $currencies, Exchange $exchange)
$this->beConstructedWith($currencies, $exchange);
}

/**
* @dataProvider convertExamples
*/
function it_converts_to_a_different_currency(
$baseCurrencyCode,
$counterCurrencyCode,
$subunitBase,
$subunitCounter,
$ratio,
$amount,
$expectedAmount,
Currencies $currencies,
Exchange $exchange
) {
$baseCurrency = new Currency($baseCurrencyCode);
$counterCurrency = new Currency($counterCurrencyCode);
$pair = new CurrencyPair($baseCurrency, $counterCurrency, $ratio);

$currencies->subunitFor($baseCurrency)->willReturn($subunitBase);
$currencies->subunitFor($counterCurrency)->willReturn($subunitCounter);
function it_is_initializable()
{
$this->shouldHaveType(Converter::class);
}

function it_converts_to_a_different_currency(Currencies $currencies, Exchange $exchange) {
$baseCurrency = new Currency($baseCurrencyCode = 'ABC');
$counterCurrency = new Currency($counterCurrencyCode = 'XYZ');
$pair = new CurrencyPair($baseCurrency, $counterCurrency, 0.5);

$currencies->subunitFor($baseCurrency)->willReturn(100);
$currencies->subunitFor($counterCurrency)->willReturn(100);

$exchange->quote($baseCurrency, $counterCurrency)->willReturn($pair);

$money = $this->convert(
new Money($amount, new Currency($baseCurrencyCode)),
new Money(2, new Currency($baseCurrencyCode)),
$counterCurrency
);

$money->shouldHaveType(Money::class);
$money->getAmount()->shouldBeLike($expectedAmount);
$money->getCurrency()->shouldBeLike($counterCurrencyCode);
$money->getAmount()->shouldBe('1');
$money->getCurrency()->getCode()->shouldBe($counterCurrencyCode);
}

function it_converts_using_rounding_modes(Currencies $currencies, Exchange $exchange)
Expand All @@ -75,14 +66,4 @@ function it_converts_using_rounding_modes(Currencies $currencies, Exchange $exch
$resultMoney->getAmount()->shouldBeLike(12);
$resultMoney->getCurrency()->getCode()->shouldReturn('USD');
}

public function convertExamples()
{
return [
['USD', 'JPY', 2, 0, 101, 100, 101],
['JPY', 'USD', 0, 2, 0.0099, 1000, 990],
['USD', 'EUR', 2, 2, 0.89, 100, 89],
['EUR', 'USD', 2, 2, 1.12, 100, 112],
];
}
}
Loading

0 comments on commit c8bb277

Please sign in to comment.