5
* Copyright (c) 2002-2010, Sebastian Bergmann <sebastian@phpunit.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.
38
* @author Sebastian Bergmann <sebastian@phpunit.de>
39
* @copyright 2002-2010 Sebastian Bergmann <sebastian@phpunit.de>
40
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
41
* @link http://www.phpunit.de/
42
* @since File available since Release 2.3.0
45
require_once 'PHPUnit/Framework/TestCase.php';
47
require_once 'Money.php';
48
require_once 'MoneyBag.php';
51
* Tests for the Money and MoneyBag classes.
54
* @author Sebastian Bergmann <sebastian@phpunit.de>
55
* @copyright 2002-2010 Sebastian Bergmann <sebastian@phpunit.de>
56
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
57
* @version Release: 3.5.5
58
* @link http://www.phpunit.de/
59
* @since Class available since Release 2.3.0
61
class MoneyTest extends PHPUnit_Framework_TestCase
71
protected function setUp()
73
$this->f12EUR = new Money(12, 'EUR');
74
$this->f14EUR = new Money(14, 'EUR');
75
$this->f7USD = new Money( 7, 'USD');
76
$this->f21USD = new Money(21, 'USD');
78
$this->fMB1 = MoneyBag::create($this->f12EUR, $this->f7USD);
79
$this->fMB2 = MoneyBag::create($this->f14EUR, $this->f21USD);
82
public function testBagMultiply()
84
// {[12 EUR][7 USD]} *2 == {[24 EUR][14 USD]}
85
$expected = MoneyBag::create(new Money(24, 'EUR'), new Money(14, 'USD'));
87
$this->assertTrue($expected->equals($this->fMB1->multiply(2)));
88
$this->assertTrue($this->fMB1->equals($this->fMB1->multiply(1)));
89
$this->assertTrue($this->fMB1->multiply(0)->isZero());
92
public function testBagNegate()
94
// {[12 EUR][7 USD]} negate == {[-12 EUR][-7 USD]}
95
$expected = MoneyBag::create(new Money(-12, 'EUR'), new Money(-7, 'USD'));
96
$this->assertTrue($expected->equals($this->fMB1->negate()));
99
public function testBagSimpleAdd()
101
// {[12 EUR][7 USD]} + [14 EUR] == {[26 EUR][7 USD]}
102
$expected = MoneyBag::create(new Money(26, 'EUR'), new Money(7, 'USD'));
103
$this->assertTrue($expected->equals($this->fMB1->add($this->f14EUR)));
106
public function testBagSubtract()
108
// {[12 EUR][7 USD]} - {[14 EUR][21 USD] == {[-2 EUR][-14 USD]}
109
$expected = MoneyBag::create(new Money(-2, 'EUR'), new Money(-14, 'USD'));
110
$this->assertTrue($expected->equals($this->fMB1->subtract($this->fMB2)));
113
public function testBagSumAdd()
115
// {[12 EUR][7 USD]} + {[14 EUR][21 USD]} == {[26 EUR][28 USD]}
116
$expected = MoneyBag::create(new Money(26, 'EUR'), new Money(28, 'USD'));
117
$this->assertTrue($expected->equals($this->fMB1->add($this->fMB2)));
120
public function testIsZero()
122
//$this->assertTrue($this->fMB1->subtract($this->fMB1)->isZero());
123
$this->assertTrue(MoneyBag::create(new Money (0, 'EUR'), new Money (0, 'USD'))->isZero());
126
public function testMixedSimpleAdd()
128
// [12 EUR] + [7 USD] == {[12 EUR][7 USD]}
129
$expected = MoneyBag::create($this->f12EUR, $this->f7USD);
130
$this->assertTrue($expected->equals($this->f12EUR->add($this->f7USD)));
133
public function testBagNotEquals()
135
$bag1 = MoneyBag::create($this->f12EUR, $this->f7USD);
136
$bag2 = new Money(12, 'CHF');
137
$bag2->add($this->f7USD);
138
$this->assertFalse($bag1->equals($bag2));
141
public function testMoneyBagEquals()
143
$this->assertTrue(!$this->fMB1->equals(NULL));
145
$this->assertTrue($this->fMB1->equals($this->fMB1));
146
$equal = MoneyBag::create(new Money(12, 'EUR'), new Money(7, 'USD'));
147
$this->assertTrue($this->fMB1->equals($equal));
148
$this->assertTrue(!$this->fMB1->equals($this->f12EUR));
149
$this->assertTrue(!$this->f12EUR->equals($this->fMB1));
150
$this->assertTrue(!$this->fMB1->equals($this->fMB2));
153
public function testMoneyBagHash()
155
$equal = MoneyBag::create(new Money(12, 'EUR'), new Money(7, 'USD'));
156
$this->assertEquals($this->fMB1->hashCode(), $equal->hashCode());
159
public function testMoneyEquals()
161
$this->assertTrue(!$this->f12EUR->equals(NULL));
162
$equalMoney = new Money(12, 'EUR');
163
$this->assertTrue($this->f12EUR->equals($this->f12EUR));
164
$this->assertTrue($this->f12EUR->equals($equalMoney));
165
$this->assertEquals($this->f12EUR->hashCode(), $equalMoney->hashCode());
166
$this->assertFalse($this->f12EUR->equals($this->f14EUR));
169
public function testMoneyHash()
171
$this->assertNotNull($this->f12EUR);
172
$equal= new Money(12, 'EUR');
173
$this->assertEquals($this->f12EUR->hashCode(), $equal->hashCode());
176
public function testSimplify()
178
$money = MoneyBag::create(new Money(26, 'EUR'), new Money(28, 'EUR'));
179
$this->assertTrue($money->equals(new Money(54, 'EUR')));
182
public function testNormalize2()
184
// {[12 EUR][7 USD]} - [12 EUR] == [7 USD]
185
$expected = new Money(7, 'USD');
186
$this->assertTrue($expected->equals($this->fMB1->subtract($this->f12EUR)));
189
public function testNormalize3()
191
// {[12 EUR][7 USD]} - {[12 EUR][3 USD]} == [4 USD]
192
$ms1 = MoneyBag::create(new Money(12, 'EUR'), new Money(3, 'USD'));
193
$expected = new Money(4, 'USD');
194
$this->assertTrue($expected->equals($this->fMB1->subtract($ms1)));
197
public function testNormalize4()
199
// [12 EUR] - {[12 EUR][3 USD]} == [-3 USD]
200
$ms1 = MoneyBag::create(new Money(12, 'EUR'), new Money(3, 'USD'));
201
$expected = new Money(-3, 'USD');
202
$this->assertTrue($expected->equals($this->f12EUR->subtract($ms1)));
205
public function testPrint()
207
$this->assertEquals('[12 EUR]', $this->f12EUR->toString());
210
public function testSimpleAdd()
212
// [12 EUR] + [14 EUR] == [26 EUR]
213
$expected = new Money(26, 'EUR');
214
$this->assertTrue($expected->equals($this->f12EUR->add($this->f14EUR)));
217
public function testSimpleBagAdd()
219
// [14 EUR] + {[12 EUR][7 USD]} == {[26 EUR][7 USD]}
220
$expected = MoneyBag::create(new Money(26, 'EUR'), new Money(7, 'USD'));
221
$this->assertTrue($expected->equals($this->f14EUR->add($this->fMB1)));
224
public function testSimpleMultiply()
226
// [14 EUR] *2 == [28 EUR]
227
$expected = new Money(28, 'EUR');
228
$this->assertTrue($expected->equals($this->f14EUR->multiply(2)));
231
public function testSimpleNegate()
233
// [14 EUR] negate == [-14 EUR]
234
$expected = new Money(-14, 'EUR');
235
$this->assertTrue($expected->equals($this->f14EUR->negate()));
238
public function testSimpleSubtract()
240
// [14 EUR] - [12 EUR] == [2 EUR]
241
$expected = new Money(2, 'EUR');
242
$this->assertTrue($expected->equals($this->f14EUR->subtract($this->f12EUR)));