5
* Copyright (c) 2002-2010, 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-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
41
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
42
* @link http://www.phpunit.de/
43
* @since File available since Release 2.3.0
46
require_once 'PHPUnit/Framework/TestCase.php';
48
require_once 'Money.php';
49
require_once 'MoneyBag.php';
52
* Tests for the Money and MoneyBag classes.
56
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
57
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
58
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
59
* @version Release: 3.4.14
60
* @link http://www.phpunit.de/
61
* @since Class available since Release 2.3.0
63
class MoneyTest extends PHPUnit_Framework_TestCase
73
protected function setUp()
75
$this->f12EUR = new Money(12, 'EUR');
76
$this->f14EUR = new Money(14, 'EUR');
77
$this->f7USD = new Money( 7, 'USD');
78
$this->f21USD = new Money(21, 'USD');
80
$this->fMB1 = MoneyBag::create($this->f12EUR, $this->f7USD);
81
$this->fMB2 = MoneyBag::create($this->f14EUR, $this->f21USD);
84
public function testBagMultiply()
86
// {[12 EUR][7 USD]} *2 == {[24 EUR][14 USD]}
87
$expected = MoneyBag::create(new Money(24, 'EUR'), new Money(14, 'USD'));
89
$this->assertTrue($expected->equals($this->fMB1->multiply(2)));
90
$this->assertTrue($this->fMB1->equals($this->fMB1->multiply(1)));
91
$this->assertTrue($this->fMB1->multiply(0)->isZero());
94
public function testBagNegate()
96
// {[12 EUR][7 USD]} negate == {[-12 EUR][-7 USD]}
97
$expected = MoneyBag::create(new Money(-12, 'EUR'), new Money(-7, 'USD'));
98
$this->assertTrue($expected->equals($this->fMB1->negate()));
101
public function testBagSimpleAdd()
103
// {[12 EUR][7 USD]} + [14 EUR] == {[26 EUR][7 USD]}
104
$expected = MoneyBag::create(new Money(26, 'EUR'), new Money(7, 'USD'));
105
$this->assertTrue($expected->equals($this->fMB1->add($this->f14EUR)));
108
public function testBagSubtract()
110
// {[12 EUR][7 USD]} - {[14 EUR][21 USD] == {[-2 EUR][-14 USD]}
111
$expected = MoneyBag::create(new Money(-2, 'EUR'), new Money(-14, 'USD'));
112
$this->assertTrue($expected->equals($this->fMB1->subtract($this->fMB2)));
115
public function testBagSumAdd()
117
// {[12 EUR][7 USD]} + {[14 EUR][21 USD]} == {[26 EUR][28 USD]}
118
$expected = MoneyBag::create(new Money(26, 'EUR'), new Money(28, 'USD'));
119
$this->assertTrue($expected->equals($this->fMB1->add($this->fMB2)));
122
public function testIsZero()
124
//$this->assertTrue($this->fMB1->subtract($this->fMB1)->isZero());
125
$this->assertTrue(MoneyBag::create(new Money (0, 'EUR'), new Money (0, 'USD'))->isZero());
128
public function testMixedSimpleAdd()
130
// [12 EUR] + [7 USD] == {[12 EUR][7 USD]}
131
$expected = MoneyBag::create($this->f12EUR, $this->f7USD);
132
$this->assertTrue($expected->equals($this->f12EUR->add($this->f7USD)));
135
public function testBagNotEquals()
137
$bag1 = MoneyBag::create($this->f12EUR, $this->f7USD);
138
$bag2 = new Money(12, 'CHF');
139
$bag2->add($this->f7USD);
140
$this->assertFalse($bag1->equals($bag2));
143
public function testMoneyBagEquals()
145
$this->assertTrue(!$this->fMB1->equals(NULL));
147
$this->assertTrue($this->fMB1->equals($this->fMB1));
148
$equal = MoneyBag::create(new Money(12, 'EUR'), new Money(7, 'USD'));
149
$this->assertTrue($this->fMB1->equals($equal));
150
$this->assertTrue(!$this->fMB1->equals($this->f12EUR));
151
$this->assertTrue(!$this->f12EUR->equals($this->fMB1));
152
$this->assertTrue(!$this->fMB1->equals($this->fMB2));
155
public function testMoneyBagHash()
157
$equal = MoneyBag::create(new Money(12, 'EUR'), new Money(7, 'USD'));
158
$this->assertEquals($this->fMB1->hashCode(), $equal->hashCode());
161
public function testMoneyEquals()
163
$this->assertTrue(!$this->f12EUR->equals(NULL));
164
$equalMoney = new Money(12, 'EUR');
165
$this->assertTrue($this->f12EUR->equals($this->f12EUR));
166
$this->assertTrue($this->f12EUR->equals($equalMoney));
167
$this->assertEquals($this->f12EUR->hashCode(), $equalMoney->hashCode());
168
$this->assertFalse($this->f12EUR->equals($this->f14EUR));
171
public function testMoneyHash()
173
$this->assertNotNull($this->f12EUR);
174
$equal= new Money(12, 'EUR');
175
$this->assertEquals($this->f12EUR->hashCode(), $equal->hashCode());
178
public function testSimplify()
180
$money = MoneyBag::create(new Money(26, 'EUR'), new Money(28, 'EUR'));
181
$this->assertTrue($money->equals(new Money(54, 'EUR')));
184
public function testNormalize2()
186
// {[12 EUR][7 USD]} - [12 EUR] == [7 USD]
187
$expected = new Money(7, 'USD');
188
$this->assertTrue($expected->equals($this->fMB1->subtract($this->f12EUR)));
191
public function testNormalize3()
193
// {[12 EUR][7 USD]} - {[12 EUR][3 USD]} == [4 USD]
194
$ms1 = MoneyBag::create(new Money(12, 'EUR'), new Money(3, 'USD'));
195
$expected = new Money(4, 'USD');
196
$this->assertTrue($expected->equals($this->fMB1->subtract($ms1)));
199
public function testNormalize4()
201
// [12 EUR] - {[12 EUR][3 USD]} == [-3 USD]
202
$ms1 = MoneyBag::create(new Money(12, 'EUR'), new Money(3, 'USD'));
203
$expected = new Money(-3, 'USD');
204
$this->assertTrue($expected->equals($this->f12EUR->subtract($ms1)));
207
public function testPrint()
209
$this->assertEquals('[12 EUR]', $this->f12EUR->toString());
212
public function testSimpleAdd()
214
// [12 EUR] + [14 EUR] == [26 EUR]
215
$expected = new Money(26, 'EUR');
216
$this->assertTrue($expected->equals($this->f12EUR->add($this->f14EUR)));
219
public function testSimpleBagAdd()
221
// [14 EUR] + {[12 EUR][7 USD]} == {[26 EUR][7 USD]}
222
$expected = MoneyBag::create(new Money(26, 'EUR'), new Money(7, 'USD'));
223
$this->assertTrue($expected->equals($this->f14EUR->add($this->fMB1)));
226
public function testSimpleMultiply()
228
// [14 EUR] *2 == [28 EUR]
229
$expected = new Money(28, 'EUR');
230
$this->assertTrue($expected->equals($this->f14EUR->multiply(2)));
233
public function testSimpleNegate()
235
// [14 EUR] negate == [-14 EUR]
236
$expected = new Money(-14, 'EUR');
237
$this->assertTrue($expected->equals($this->f14EUR->negate()));
240
public function testSimpleSubtract()
242
// [14 EUR] - [12 EUR] == [2 EUR]
243
$expected = new Money(2, 'EUR');
244
$this->assertTrue($expected->equals($this->f14EUR->subtract($this->f12EUR)));
b'\\ No newline at end of file'