Commit 41821ead authored by Mathias Verraes's avatar Mathias Verraes
Browse files

renamed Currency classes, to have a single name for currencies

parent 2d929cc6
......@@ -27,7 +27,7 @@ down to 3 and 1 cent leaves me 1 cent. Apart from re-investing that cent in the
is to keep handing out the remainder until all money is spent. In other words:
<?php
$profit = new Money(5, new Euro);
$profit = new Money(5, new EUR);
list($my_cut, $investors_cut) = $profit->allocate(70, 30);
// $my_cut is 4 cents, $investors_cut is 1 cent
......@@ -41,12 +41,12 @@ Immutability
Jim and Hannah both want to buy a copy of book priced at EUR 25.
<?php
$jim_price = $hannah_price = new Money(2500, new Euro);
$jim_price = $hannah_price = new Money(2500, new EUR);
Jim has a coupon for EUR 5.
<?php
$coupon = new Money(500, new Euro);
$coupon = new Money(500, new EUR);
$jim_price->subtract($coupon);
Because $jim_price and $hannah_price are the same object, you'd expect Hannah to now have the reduced
......@@ -60,7 +60,7 @@ The correct way of doing operations is:
$jim_price = $jim_price->subtract($coupon);
$jim_price->lessThan($hannah_price); // true
$jim_price->equals(Money::euro(2000)); // true
$jim_price->equals(Money::EUR(2000)); // true
The goal
......
......@@ -10,7 +10,7 @@
namespace Verraes\Money;
class Euro implements Currency
class EUR implements Currency
{
/**
* @return string
......
......@@ -46,18 +46,18 @@ class Money
* Convenience factory method for an amount in EURO
* @return Money
*/
public static function euro($units)
public static function EUR($units)
{
return new Money($units, new Euro);
return new Money($units, new EUR);
}
/**
* Convenience factory method for an amount in USD
* @return Money
*/
public static function usd($units)
public static function USD($units)
{
return new Money($units, new Usd);
return new Money($units, new USD);
}
private function isSameCurrency(Money $other)
......
......@@ -10,7 +10,7 @@
namespace Verraes\Money;
class Usd implements Currency
class USD implements Currency
{
/**
* @return string
......
......@@ -11,17 +11,17 @@
require_once 'bootstrap.php';
use Verraes\Money\Currency;
use Verraes\Money\Euro;
use Verraes\Money\Usd;
use Verraes\Money\EUR;
use Verraes\Money\USD;
class CurrencyTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->euro1 = new Euro;
$this->euro2 = new Euro;
$this->usd1 = new Usd;
$this->usd2 = new Usd;
$this->euro1 = new EUR;
$this->euro2 = new EUR;
$this->usd1 = new USD;
$this->usd2 = new USD;
}
public function testDifferentInstancesAreEqual()
......
......@@ -12,8 +12,8 @@ require_once 'bootstrap.php';
use Verraes\Money\Money;
use Verraes\Money\Currency;
use Verraes\Money\Usd;
use Verraes\Money\Euro;
use Verraes\Money\USD;
use Verraes\Money\EUR;
class MoneyTest extends PHPUnit_Framework_TestCase
{
......@@ -35,18 +35,18 @@ class MoneyTest extends PHPUnit_Framework_TestCase
public function testFactoryMethods()
{
$this->assertMoneyEquals(
Money::euro(25),
Money::euro(10)->add(Money::euro(15))
Money::EUR(25),
Money::EUR(10)->add(Money::EUR(15))
);
$this->assertMoneyEquals(
Money::usd(25),
Money::usd(10)->add(Money::usd(15))
Money::USD(25),
Money::USD(10)->add(Money::USD(15))
);
}
public function testGetters()
{
$m = new Money(100, $euro = new Euro);
$m = new Money(100, $euro = new EUR);
$this->assertEquals(100, $m->getUnits());
$this->assertEquals($euro, $m->getCurrency());
}
......@@ -56,7 +56,7 @@ class MoneyTest extends PHPUnit_Framework_TestCase
*/
public function testDecimalsThrowException()
{
$money = new Money(0.01, new Euro);
$money = new Money(0.01, new EUR);
}
/**
......@@ -64,15 +64,15 @@ class MoneyTest extends PHPUnit_Framework_TestCase
*/
public function testStringThrowsException()
{
$money = new Money('100', new Euro);
$money = new Money('100', new EUR);
}
public function testEquality()
{
$m1 = new Money(100, new Euro);
$m2 = new Money(100, new Euro);
$m3 = new Money(100, new Usd);
$m4 = new Money(50, new Euro);
$m1 = new Money(100, new EUR);
$m2 = new Money(100, new EUR);
$m3 = new Money(100, new USD);
$m4 = new Money(50, new EUR);
$this->assertTrue($m1->equals($m2));
$this->assertFalse($m1->equals($m3));
......@@ -81,10 +81,10 @@ class MoneyTest extends PHPUnit_Framework_TestCase
public function testAddition()
{
$m1 = new Money(100, new Euro);
$m2 = new Money(100, new Euro);
$m1 = new Money(100, new EUR);
$m2 = new Money(100, new EUR);
$sum = $m1->add($m2);
$expected = new Money(200, new Euro);
$expected = new Money(200, new EUR);
$this->assertMoneyEquals($expected, $sum);
......@@ -98,17 +98,17 @@ class MoneyTest extends PHPUnit_Framework_TestCase
*/
public function testDifferentCurrenciesCannotBeAdded()
{
$m1 = new Money(100, new Euro);
$m2 = new Money(100, new Usd);
$m1 = new Money(100, new EUR);
$m2 = new Money(100, new USD);
$m1->add($m2);
}
public function testSubtraction()
{
$m1 = new Money(100, new Euro);
$m2 = new Money(200, new Euro);
$m1 = new Money(100, new EUR);
$m2 = new Money(200, new EUR);
$diff = $m1->subtract($m2);
$expected = new Money(-100, new Euro);
$expected = new Money(-100, new EUR);
$this->assertMoneyEquals($expected, $diff);
......@@ -122,20 +122,20 @@ class MoneyTest extends PHPUnit_Framework_TestCase
*/
public function testDifferentCurrenciesCannotBeSubtracted()
{
$m1 = new Money(100, new Euro);
$m2 = new Money(100, new Usd);
$m1 = new Money(100, new EUR);
$m2 = new Money(100, new USD);
$m1->subtract($m2);
}
public function testMultiplication()
{
$m = new Money(1, new Euro);
$m = new Money(1, new EUR);
$this->assertMoneyEquals(
new Money(2, new Euro),
new Money(2, new EUR),
$m->multiply(1.5)
);
$this->assertMoneyEquals(
new Money(1, new Euro),
new Money(1, new EUR),
$m->multiply(1.5, Money::ROUND_HALF_DOWN)
);
......@@ -144,17 +144,17 @@ class MoneyTest extends PHPUnit_Framework_TestCase
public function testDivision()
{
$m = new Money(10, new Euro);
$m = new Money(10, new EUR);
$this->assertMoneyEquals(
new Money(3, new Euro),
new Money(3, new EUR),
$m->divide(3)
);
$this->assertMoneyEquals(
new Money(2, new Euro),
new Money(2, new EUR),
$m->divide(4, Money::ROUND_HALF_EVEN)
);
$this->assertMoneyEquals(
new Money(3, new Euro),
new Money(3, new EUR),
$m->divide(3, Money::ROUND_HALF_ODD)
);
......@@ -163,9 +163,9 @@ class MoneyTest extends PHPUnit_Framework_TestCase
public function testComparison()
{
$euro1 = new Money(1, new Euro);
$euro2 = new Money(2, new Euro);
$usd = new Money(1, new Usd);
$euro1 = new Money(1, new EUR);
$euro2 = new Money(2, new EUR);
$usd = new Money(1, new USD);
$this->assertTrue($euro2->greaterThan($euro1));
$this->assertFalse($euro1->greaterThan($euro2));
......@@ -182,36 +182,36 @@ class MoneyTest extends PHPUnit_Framework_TestCase
*/
public function testDifferentCurrenciesCannotBeCompared()
{
Money::euro(1)->compare(Money::usd(1));
Money::EUR(1)->compare(Money::USD(1));
}
public function testAllocation()
{
$m = new Money(100, new Euro);
$m = new Money(100, new EUR);
list($part1, $part2, $part3) = $m->allocate(array(1, 1, 1));
$this->assertMoneyEquals(new Money(34, new Euro), $part1);
$this->assertMoneyEquals(new Money(33, new Euro), $part2);
$this->assertMoneyEquals(new Money(33, new Euro), $part3);
$this->assertMoneyEquals(new Money(34, new EUR), $part1);
$this->assertMoneyEquals(new Money(33, new EUR), $part2);
$this->assertMoneyEquals(new Money(33, new EUR), $part3);
$m = new Money(101, new Euro);
$m = new Money(101, new EUR);
list($part1, $part2, $part3) = $m->allocate(array(1, 1, 1));
$this->assertMoneyEquals(new Money(34, new Euro), $part1);
$this->assertMoneyEquals(new Money(34, new Euro), $part2);
$this->assertMoneyEquals(new Money(33, new Euro), $part3);
$this->assertMoneyEquals(new Money(34, new EUR), $part1);
$this->assertMoneyEquals(new Money(34, new EUR), $part2);
$this->assertMoneyEquals(new Money(33, new EUR), $part3);
}
public function testAllocationOrderIsImportant()
{
$m = new Money(5, new Euro);
$m = new Money(5, new EUR);
list($part1, $part2) = $m->allocate(array(3, 7));
$this->assertMoneyEquals(new Money(2, new Euro), $part1);
$this->assertMoneyEquals(new Money(3, new Euro), $part2);
$this->assertMoneyEquals(new Money(2, new EUR), $part1);
$this->assertMoneyEquals(new Money(3, new EUR), $part2);
$m = new Money(5, new Euro);
$m = new Money(5, new EUR);
list($part1, $part2) = $m->allocate(array(7, 3));
$this->assertMoneyEquals(new Money(4, new Euro), $part1);
$this->assertMoneyEquals(new Money(1, new Euro), $part2);
$this->assertMoneyEquals(new Money(4, new EUR), $part1);
$this->assertMoneyEquals(new Money(1, new EUR), $part2);
}
}
\ No newline at end of file
......@@ -10,8 +10,8 @@
require_once 'PHPUnit/Framework/TestCase.php';
require_once __DIR__.'/../lib/Verraes/Money/Currency.php';
require_once __DIR__.'/../lib/Verraes/Money/Euro.php';
require_once __DIR__.'/../lib/Verraes/Money/EUR.php';
require_once __DIR__.'/../lib/Verraes/Money/Exception.php';
require_once __DIR__.'/../lib/Verraes/Money/InvalidArgumentException.php';
require_once __DIR__.'/../lib/Verraes/Money/Money.php';
require_once __DIR__.'/../lib/Verraes/Money/Usd.php';
require_once __DIR__.'/../lib/Verraes/Money/USD.php';
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment