Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * PHPUnit
4
 *
5
 * Copyright (c) 2002-2010, 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-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
44
 */
45
 
46
require_once 'PHPUnit/Framework/TestCase.php';
47
 
48
require_once 'Money.php';
49
require_once 'MoneyBag.php';
50
 
51
/**
52
 * Tests for the Money and MoneyBag classes.
53
 *
54
 * @category   Testing
55
 * @package    PHPUnit
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.15
60
 * @link       http://www.phpunit.de/
61
 * @since      Class available since Release 2.3.0
62
 */
63
class MoneyTest extends PHPUnit_Framework_TestCase
64
{
65
    protected $f12EUR;
66
    protected $f14EUR;
67
    protected $f7USD;
68
    protected $f21USD;
69
 
70
    protected $fMB1;
71
    protected $fMB2;
72
 
73
    protected function setUp()
74
    {
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');
79
 
80
        $this->fMB1 = MoneyBag::create($this->f12EUR, $this->f7USD);
81
        $this->fMB2 = MoneyBag::create($this->f14EUR, $this->f21USD);
82
    }
83
 
84
    public function testBagMultiply()
85
    {
86
        // {[12 EUR][7 USD]} *2 == {[24 EUR][14 USD]}
87
        $expected = MoneyBag::create(new Money(24, 'EUR'), new Money(14, 'USD'));
88
 
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());
92
    }
93
 
94
    public function testBagNegate()
95
    {
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()));
99
    }
100
 
101
    public function testBagSimpleAdd()
102
    {
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)));
106
    }
107
 
108
    public function testBagSubtract()
109
    {
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)));
113
    }
114
 
115
    public function testBagSumAdd()
116
    {
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)));
120
    }
121
 
122
    public function testIsZero()
123
    {
124
        //$this->assertTrue($this->fMB1->subtract($this->fMB1)->isZero());
125
        $this->assertTrue(MoneyBag::create(new Money (0, 'EUR'), new Money (0, 'USD'))->isZero());
126
    }
127
 
128
    public function testMixedSimpleAdd()
129
    {
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)));
133
    }
134
 
135
    public function testBagNotEquals()
136
    {
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));
141
    }
142
 
143
    public function testMoneyBagEquals()
144
    {
145
        $this->assertTrue(!$this->fMB1->equals(NULL));
146
 
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));
153
    }
154
 
155
    public function testMoneyBagHash()
156
    {
157
        $equal = MoneyBag::create(new Money(12, 'EUR'), new Money(7, 'USD'));
158
        $this->assertEquals($this->fMB1->hashCode(), $equal->hashCode());
159
    }
160
 
161
    public function testMoneyEquals()
162
    {
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));
169
    }
170
 
171
    public function testMoneyHash()
172
    {
173
        $this->assertNotNull($this->f12EUR);
174
        $equal= new Money(12, 'EUR');
175
        $this->assertEquals($this->f12EUR->hashCode(), $equal->hashCode());
176
    }
177
 
178
    public function testSimplify()
179
    {
180
        $money = MoneyBag::create(new Money(26, 'EUR'), new Money(28, 'EUR'));
181
        $this->assertTrue($money->equals(new Money(54, 'EUR')));
182
    }
183
 
184
    public function testNormalize2()
185
    {
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)));
189
    }
190
 
191
    public function testNormalize3()
192
    {
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)));
197
    }
198
 
199
    public function testNormalize4()
200
    {
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)));
205
    }
206
 
207
    public function testPrint()
208
    {
209
        $this->assertEquals('[12 EUR]', $this->f12EUR->toString());
210
    }
211
 
212
    public function testSimpleAdd()
213
    {
214
        // [12 EUR] + [14 EUR] == [26 EUR]
215
        $expected = new Money(26, 'EUR');
216
        $this->assertTrue($expected->equals($this->f12EUR->add($this->f14EUR)));
217
    }
218
 
219
    public function testSimpleBagAdd()
220
    {
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)));
224
    }
225
 
226
    public function testSimpleMultiply()
227
    {
228
        // [14 EUR] *2 == [28 EUR]
229
        $expected = new Money(28, 'EUR');
230
        $this->assertTrue($expected->equals($this->f14EUR->multiply(2)));
231
    }
232
 
233
    public function testSimpleNegate()
234
    {
235
        // [14 EUR] negate == [-14 EUR]
236
        $expected = new Money(-14, 'EUR');
237
        $this->assertTrue($expected->equals($this->f14EUR->negate()));
238
    }
239
 
240
    public function testSimpleSubtract()
241
    {
242
        // [14 EUR] - [12 EUR] == [2 EUR]
243
        $expected = new Money(2, 'EUR');
244
        $this->assertTrue($expected->equals($this->f14EUR->subtract($this->f12EUR)));
245
    }
246
}
247
?>