]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Samples/Money/MoneyTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Samples / Money / MoneyTest.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *
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
18  *     distribution.
19  *
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.
23  *
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.
36  *
37  * @category   Testing
38  * @package    PHPUnit
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
42
43  * @link       http://www.phpunit.de/
44  * @since      File available since Release 2.3.0
45  */
46
47 require_once 'PHPUnit/Framework/TestCase.php';
48
49 require_once 'Money.php';
50 require_once 'MoneyBag.php';
51
52 /**
53  * Tests for the Money and MoneyBag classes.
54  *
55  * @category   Testing
56  * @package    PHPUnit
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
63  */
64 class MoneyTest extends PHPUnit_Framework_TestCase
65 {
66     protected $f12EUR;
67     protected $f14EUR;
68     protected $f7USD;
69     protected $f21USD;
70
71     protected $fMB1;
72     protected $fMB2;
73
74     protected function setUp()
75     {
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');
80
81         $this->fMB1 = MoneyBag::create($this->f12EUR, $this->f7USD);
82         $this->fMB2 = MoneyBag::create($this->f14EUR, $this->f21USD);
83     }
84
85     public function testBagMultiply()
86     {
87         // {[12 EUR][7 USD]} *2 == {[24 EUR][14 USD]}
88         $expected = MoneyBag::create(new Money(24, 'EUR'), new Money(14, 'USD'));
89
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());
93     }
94
95     public function testBagNegate()
96     {
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()));
100     }
101
102     public function testBagSimpleAdd()
103     {
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)));
107     }
108
109     public function testBagSubtract()
110     {
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)));
114     }
115
116     public function testBagSumAdd()
117     {
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)));
121     }
122
123     public function testIsZero()
124     {
125         //$this->assertTrue($this->fMB1->subtract($this->fMB1)->isZero());
126         $this->assertTrue(MoneyBag::create(new Money (0, 'EUR'), new Money (0, 'USD'))->isZero());
127     }
128
129     public function testMixedSimpleAdd()
130     {
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)));
134     }
135
136     public function testBagNotEquals()
137     {
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));
142     }
143
144     public function testMoneyBagEquals()
145     {
146         $this->assertTrue(!$this->fMB1->equals(NULL));
147
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));
154     }
155
156     public function testMoneyBagHash()
157     {
158         $equal = MoneyBag::create(new Money(12, 'EUR'), new Money(7, 'USD'));
159         $this->assertEquals($this->fMB1->hashCode(), $equal->hashCode());
160     }
161
162     public function testMoneyEquals()
163     {
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));
170     }
171
172     public function testMoneyHash()
173     {
174         $this->assertNotNull($this->f12EUR);
175         $equal= new Money(12, 'EUR');
176         $this->assertEquals($this->f12EUR->hashCode(), $equal->hashCode());
177     }
178
179     public function testSimplify()
180     {
181         $money = MoneyBag::create(new Money(26, 'EUR'), new Money(28, 'EUR'));
182         $this->assertTrue($money->equals(new Money(54, 'EUR')));
183     }
184
185     public function testNormalize2()
186     {
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)));
190     }
191
192     public function testNormalize3()
193     {
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)));
198     }
199
200     public function testNormalize4()
201     {
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)));
206     }
207
208     public function testPrint()
209     {
210         $this->assertEquals('[12 EUR]', $this->f12EUR->toString());
211     }
212
213     public function testSimpleAdd()
214     {
215         // [12 EUR] + [14 EUR] == [26 EUR]
216         $expected = new Money(26, 'EUR');
217         $this->assertTrue($expected->equals($this->f12EUR->add($this->f14EUR)));
218     }
219
220     public function testSimpleBagAdd()
221     {
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)));
225     }
226
227     public function testSimpleMultiply()
228     {
229         // [14 EUR] *2 == [28 EUR]
230         $expected = new Money(28, 'EUR');
231         $this->assertTrue($expected->equals($this->f14EUR->multiply(2)));
232     }
233
234     public function testSimpleNegate()
235     {
236         // [14 EUR] negate == [-14 EUR]
237         $expected = new Money(-14, 'EUR');
238         $this->assertTrue($expected->equals($this->f14EUR->negate()));
239     }
240
241     public function testSimpleSubtract()
242     {
243         // [14 EUR] - [12 EUR] == [2 EUR]
244         $expected = new Money(2, 'EUR');
245         $this->assertTrue($expected->equals($this->f14EUR->subtract($this->f12EUR)));
246     }
247 }
248 ?>