コード例 #1
0
 public function testSumTimes()
 {
     $fiveBucks = Money::dollar(5);
     $tenFrancs = Money::franc(10);
     $bank = new Bank();
     $bank->addRate('CHF', 'USD', 2);
     $step1 = new Sum($fiveBucks, $tenFrancs);
     $sum = $step1->times(2);
     $result = $bank->reduce($sum, 'USD');
     $this->assertEquals(Money::dollar(20), $result);
 }
コード例 #2
0
ファイル: tdd-money.php プロジェクト: JlR0-tt/memo
$five = Money::dollar(5);
$t->ok($five->times(2)->equals(Money::dollar(10)));
$t->ok($five->times(3)->equals(Money::dollar(15)));
unset($five);
// multiplication
$five = Money::franc(5);
$t->ok($five->times(2)->equals(Money::franc(10)));
$t->ok($five->times(3)->equals(Money::franc(15)));
unset($five);
// equality
$five = Money::dollar(5);
$t->ok($five->equals(Money::dollar(5)), 'equals');
$t->ok(!$five->equals(Money::dollar(6)), 'equals');
$five = Money::franc(5);
$t->ok($five->equals(Money::franc(5)), 'equals');
$t->ok(!$five->equals(Money::franc(6)), 'equals');
$t->ok(!$five->equals(Money::dollar(5)), 'equals');
abstract class Money
{
    protected $amount;
    function equals($money)
    {
        return get_class($this) === get_class($money) && $this->amount === $money->amount;
    }
    static function dollar($amount)
    {
        return new Dollar($amount);
    }
    static function franc($amount)
    {
        return new Franc($amount);