5 * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * * Neither the name of Sebastian Bergmann nor the names of his
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
40 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 * @link http://www.phpunit.de/
44 * @since File available since Release 2.3.0
47 require_once 'PHPUnit/Framework/TestCase.php';
49 require_once 'Money.php';
50 require_once 'MoneyBag.php';
53 * Tests for the Money and MoneyBag classes.
57 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
58 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
59 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
60 * @version Release: 3.3.17
61 * @link http://www.phpunit.de/
62 * @since Class available since Release 2.3.0
64 class MoneyTest extends PHPUnit_Framework_TestCase
74 protected function setUp()
76 $this->f12EUR = new Money(12, 'EUR');
77 $this->f14EUR = new Money(14, 'EUR');
78 $this->f7USD = new Money( 7, 'USD');
79 $this->f21USD = new Money(21, 'USD');
81 $this->fMB1 = MoneyBag::create($this->f12EUR, $this->f7USD);
82 $this->fMB2 = MoneyBag::create($this->f14EUR, $this->f21USD);
85 public function testBagMultiply()
87 // {[12 EUR][7 USD]} *2 == {[24 EUR][14 USD]}
88 $expected = MoneyBag::create(new Money(24, 'EUR'), new Money(14, 'USD'));
90 $this->assertTrue($expected->equals($this->fMB1->multiply(2)));
91 $this->assertTrue($this->fMB1->equals($this->fMB1->multiply(1)));
92 $this->assertTrue($this->fMB1->multiply(0)->isZero());
95 public function testBagNegate()
97 // {[12 EUR][7 USD]} negate == {[-12 EUR][-7 USD]}
98 $expected = MoneyBag::create(new Money(-12, 'EUR'), new Money(-7, 'USD'));
99 $this->assertTrue($expected->equals($this->fMB1->negate()));
102 public function testBagSimpleAdd()
104 // {[12 EUR][7 USD]} + [14 EUR] == {[26 EUR][7 USD]}
105 $expected = MoneyBag::create(new Money(26, 'EUR'), new Money(7, 'USD'));
106 $this->assertTrue($expected->equals($this->fMB1->add($this->f14EUR)));
109 public function testBagSubtract()
111 // {[12 EUR][7 USD]} - {[14 EUR][21 USD] == {[-2 EUR][-14 USD]}
112 $expected = MoneyBag::create(new Money(-2, 'EUR'), new Money(-14, 'USD'));
113 $this->assertTrue($expected->equals($this->fMB1->subtract($this->fMB2)));
116 public function testBagSumAdd()
118 // {[12 EUR][7 USD]} + {[14 EUR][21 USD]} == {[26 EUR][28 USD]}
119 $expected = MoneyBag::create(new Money(26, 'EUR'), new Money(28, 'USD'));
120 $this->assertTrue($expected->equals($this->fMB1->add($this->fMB2)));
123 public function testIsZero()
125 //$this->assertTrue($this->fMB1->subtract($this->fMB1)->isZero());
126 $this->assertTrue(MoneyBag::create(new Money (0, 'EUR'), new Money (0, 'USD'))->isZero());
129 public function testMixedSimpleAdd()
131 // [12 EUR] + [7 USD] == {[12 EUR][7 USD]}
132 $expected = MoneyBag::create($this->f12EUR, $this->f7USD);
133 $this->assertTrue($expected->equals($this->f12EUR->add($this->f7USD)));
136 public function testBagNotEquals()
138 $bag1 = MoneyBag::create($this->f12EUR, $this->f7USD);
139 $bag2 = new Money(12, 'CHF');
140 $bag2->add($this->f7USD);
141 $this->assertFalse($bag1->equals($bag2));
144 public function testMoneyBagEquals()
146 $this->assertTrue(!$this->fMB1->equals(NULL));
148 $this->assertTrue($this->fMB1->equals($this->fMB1));
149 $equal = MoneyBag::create(new Money(12, 'EUR'), new Money(7, 'USD'));
150 $this->assertTrue($this->fMB1->equals($equal));
151 $this->assertTrue(!$this->fMB1->equals($this->f12EUR));
152 $this->assertTrue(!$this->f12EUR->equals($this->fMB1));
153 $this->assertTrue(!$this->fMB1->equals($this->fMB2));
156 public function testMoneyBagHash()
158 $equal = MoneyBag::create(new Money(12, 'EUR'), new Money(7, 'USD'));
159 $this->assertEquals($this->fMB1->hashCode(), $equal->hashCode());
162 public function testMoneyEquals()
164 $this->assertTrue(!$this->f12EUR->equals(NULL));
165 $equalMoney = new Money(12, 'EUR');
166 $this->assertTrue($this->f12EUR->equals($this->f12EUR));
167 $this->assertTrue($this->f12EUR->equals($equalMoney));
168 $this->assertEquals($this->f12EUR->hashCode(), $equalMoney->hashCode());
169 $this->assertFalse($this->f12EUR->equals($this->f14EUR));
172 public function testMoneyHash()
174 $this->assertNotNull($this->f12EUR);
175 $equal= new Money(12, 'EUR');
176 $this->assertEquals($this->f12EUR->hashCode(), $equal->hashCode());
179 public function testSimplify()
181 $money = MoneyBag::create(new Money(26, 'EUR'), new Money(28, 'EUR'));
182 $this->assertTrue($money->equals(new Money(54, 'EUR')));
185 public function testNormalize2()
187 // {[12 EUR][7 USD]} - [12 EUR] == [7 USD]
188 $expected = new Money(7, 'USD');
189 $this->assertTrue($expected->equals($this->fMB1->subtract($this->f12EUR)));
192 public function testNormalize3()
194 // {[12 EUR][7 USD]} - {[12 EUR][3 USD]} == [4 USD]
195 $ms1 = MoneyBag::create(new Money(12, 'EUR'), new Money(3, 'USD'));
196 $expected = new Money(4, 'USD');
197 $this->assertTrue($expected->equals($this->fMB1->subtract($ms1)));
200 public function testNormalize4()
202 // [12 EUR] - {[12 EUR][3 USD]} == [-3 USD]
203 $ms1 = MoneyBag::create(new Money(12, 'EUR'), new Money(3, 'USD'));
204 $expected = new Money(-3, 'USD');
205 $this->assertTrue($expected->equals($this->f12EUR->subtract($ms1)));
208 public function testPrint()
210 $this->assertEquals('[12 EUR]', $this->f12EUR->toString());
213 public function testSimpleAdd()
215 // [12 EUR] + [14 EUR] == [26 EUR]
216 $expected = new Money(26, 'EUR');
217 $this->assertTrue($expected->equals($this->f12EUR->add($this->f14EUR)));
220 public function testSimpleBagAdd()
222 // [14 EUR] + {[12 EUR][7 USD]} == {[26 EUR][7 USD]}
223 $expected = MoneyBag::create(new Money(26, 'EUR'), new Money(7, 'USD'));
224 $this->assertTrue($expected->equals($this->f14EUR->add($this->fMB1)));
227 public function testSimpleMultiply()
229 // [14 EUR] *2 == [28 EUR]
230 $expected = new Money(28, 'EUR');
231 $this->assertTrue($expected->equals($this->f14EUR->multiply(2)));
234 public function testSimpleNegate()
236 // [14 EUR] negate == [-14 EUR]
237 $expected = new Money(-14, 'EUR');
238 $this->assertTrue($expected->equals($this->f14EUR->negate()));
241 public function testSimpleSubtract()
243 // [14 EUR] - [12 EUR] == [2 EUR]
244 $expected = new Money(2, 'EUR');
245 $this->assertTrue($expected->equals($this->f14EUR->subtract($this->f12EUR)));