Skip to content

Commit

Permalink
Merge pull request #16 from kyamashiro/chapter16
Browse files Browse the repository at this point in the history
complete chapter16
  • Loading branch information
kyamashiro authored Nov 18, 2018
2 parents dec4ae5 + e1c1cef commit 750fc9d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/Money/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

interface Expression
{
public function reduce(Bank $bank, string $to): Money;
function reduce(Bank $bank, string $to): Money;

public function plus(Expression $added): Expression;
function plus(Expression $added): Expression;

function times(int $multiplier): Expression;
}
7 changes: 6 additions & 1 deletion src/Money/Sum.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public function reduce(Bank $bank, string $to): Money

public function plus(Expression $added): Expression
{
return null;
return new Sum($this, $added);
}

public function times(int $multiplier): Expression
{
return new Sum($this->augend->times($multiplier), $this->addend->times($multiplier));
}
}
31 changes: 29 additions & 2 deletions src/Test/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,36 @@ public function testMixedAddition()
$bank = new Bank();
$bank->addRate("CHF", "USD", 2);
$result = $bank->reduce($fiveBucks->plus($tenFrancs), "USD");
var_dump($result);
var_dump(Money::dollar(10));
//15USDが返ってくる
$this->assertEquals(Money::dollar(10), $result);
}

public function testSumPlusMoney()
{
$fiveBucks = Money::dollar(5);
$tenFrancs = Money::franc(10);
$bank = new Bank();
$bank->addRate("CHF", "USD", 2);
$sum = (new Sum($fiveBucks, $tenFrancs))->plus($fiveBucks);
$result = $bank->reduce($sum, "USD");
$this->assertEquals(Money::dollar(15), $result);
}

public function testSumTimes()
{
$fiveBucks = Money::dollar(5);
$tenFrancs = Money::franc(10);
$bank = new Bank();
$bank->addRate("CHF", "USD", 2);
$sum = (new Sum($fiveBucks, $tenFrancs))->times(2);
$result = $bank->reduce($sum, "USD");
$this->assertEquals(Money::dollar(20), $result);
}

// public function testPlusSameCurrencyReturnsMoney()
// {
// $sum = Money::dollar(1)->plus(Money::dollar(1));
// var_dump($sum);
// $this->assertTrue($sum instanceof Money);
// }
}

0 comments on commit 750fc9d

Please sign in to comment.