]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Samples/Money/MoneyBag.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Samples / Money / MoneyBag.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 'IMoney.php';
48 require_once 'Money.php';
49
50 /**
51  * A MoneyBag.
52  *
53  * @category   Testing
54  * @package    PHPUnit
55  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
56  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
57  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
58  * @version    Release: 3.3.17
59  * @link       http://www.phpunit.de/
60  * @since      Class available since Release 2.3.0
61  */
62 class MoneyBag implements IMoney
63 {
64     protected $fMonies = array();
65
66     public static function create(IMoney $m1, IMoney $m2)
67     {
68         $result = new MoneyBag;
69         $m1->appendTo($result);
70         $m2->appendTo($result);
71
72         return $result->simplify();
73     }
74
75     public function add(IMoney $m)
76     {
77         return $m->addMoneyBag($this);
78     }
79
80     public function addMoney(Money $m)
81     {
82         return MoneyBag::create($m, $this);
83     }
84
85     public function addMoneyBag(MoneyBag $s)
86     {
87         return MoneyBag::create($s, $this);
88     }
89
90     public function appendBag(MoneyBag $aBag)
91     {
92         foreach ($aBag->monies() as $aMoney) {
93             $this->appendMoney($aMoney);
94         }
95     }
96
97     public function monies()
98     {
99         return $this->fMonies;
100     }
101
102     public function appendMoney(Money $aMoney)
103     {
104         if ($aMoney->isZero()) {
105             return;
106         }
107
108         $old = $this->findMoney($aMoney->currency());
109
110         if ($old == NULL) {
111             $this->fMonies[] = $aMoney;
112             return;
113         }
114
115         $keys = array_keys($this->fMonies);
116         $max  = count($keys);
117
118         for ($i = 0; $i < $max; $i++) {
119             if ($this->fMonies[$keys[$i]] === $old) {
120                 unset($this->fMonies[$keys[$i]]);
121                 break;
122             }
123         }
124
125         $sum = $old->add($aMoney);
126
127         if ($sum->isZero()) {
128             return;
129         }
130
131         $this->fMonies[] = $sum;
132     }
133
134     public function equals($anObject)
135     {
136         if ($this->isZero() &&
137             $anObject instanceof IMoney) {
138             return $anObject->isZero();
139         }
140
141         if ($anObject instanceof MoneyBag) {
142             if (count($anObject->monies()) != count($this->fMonies)) {
143                 return FALSE;
144             }
145
146             foreach ($this->fMonies as $m) {
147                 if (!$anObject->contains($m)) {
148                     return FALSE;
149                 }
150             }
151
152             return TRUE;
153         }
154
155         return FALSE;
156     }
157
158     protected function findMoney($currency)
159     {
160         foreach ($this->fMonies as $m) {
161             if ($m->currency() == $currency) {
162                 return $m;
163             }
164         }
165
166         return NULL;
167     }
168
169     protected function contains(Money $m)
170     {
171         $found = $this->findMoney($m->currency());
172
173         if ($found == NULL) {
174             return FALSE;
175         }
176
177         return $found->amount() == $m->amount();
178     }
179
180     public function hashCode()
181     {
182         $hash = 0;
183
184         foreach ($this->fMonies as $m) {
185             $hash ^= $m->hashCode();
186         }
187
188         return $hash;
189     }
190
191     public function isZero()
192     {
193         return count($this->fMonies) == 0;
194     }
195
196     public function multiply($factor)
197     {
198         $result = new MoneyBag;
199
200         if ($factor != 0) {
201             foreach ($this->fMonies as $m) {
202                 $result->appendMoney($m->multiply($factor));
203             }
204         }
205
206         return $result;
207     }
208
209     public function negate()
210     {
211         $result = new MoneyBag;
212
213         foreach ($this->fMonies as $m) {
214                 $result->appendMoney($m->negate());
215         }
216
217         return $result;
218     }
219
220     protected function simplify()
221     {
222         if (count($this->fMonies) == 1) {
223             return array_pop($this->fMonies);
224         }
225
226         return $this;
227     }
228
229     public function subtract(IMoney $m)
230     {
231         return $this->add($m->negate());
232     }
233
234     public function toString()
235     {
236         $buffer = '{';
237
238         foreach ($this->fMonies as $m) {
239             $buffer .= $m->toString();
240         }
241
242         return $buffer . '}';
243     }
244
245     public function appendTo(MoneyBag $m)
246     {
247         $m->appendBag($this);
248     }
249 }
250 ?>