~ubuntu-branches/ubuntu/natty/phpunit/natty

« back to all changes in this revision

Viewing changes to PHPUnit-3.5.5/Samples/Money/MoneyTest.php

  • Committer: Package Import Robot
  • Author(s): Ivan Borzenkov
  • Date: 2010-12-11 18:19:39 UTC
  • mfrom: (0.11.1) (1.5.3) (12.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20101211181939-8650nbu08hf2z9v1
Tags: 3.5.5-2
fix doc-base-file-references-missing-file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * PHPUnit
 
4
 *
 
5
 * Copyright (c) 2002-2010, Sebastian Bergmann <sebastian@phpunit.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
 * @package    PHPUnit
 
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
 
43
 */
 
44
 
 
45
require_once 'PHPUnit/Framework/TestCase.php';
 
46
 
 
47
require_once 'Money.php';
 
48
require_once 'MoneyBag.php';
 
49
 
 
50
/**
 
51
 * Tests for the Money and MoneyBag classes.
 
52
 *
 
53
 * @package    PHPUnit
 
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
 
60
 */
 
61
class MoneyTest extends PHPUnit_Framework_TestCase
 
62
{
 
63
    protected $f12EUR;
 
64
    protected $f14EUR;
 
65
    protected $f7USD;
 
66
    protected $f21USD;
 
67
 
 
68
    protected $fMB1;
 
69
    protected $fMB2;
 
70
 
 
71
    protected function setUp()
 
72
    {
 
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');
 
77
 
 
78
        $this->fMB1 = MoneyBag::create($this->f12EUR, $this->f7USD);
 
79
        $this->fMB2 = MoneyBag::create($this->f14EUR, $this->f21USD);
 
80
    }
 
81
 
 
82
    public function testBagMultiply()
 
83
    {
 
84
        // {[12 EUR][7 USD]} *2 == {[24 EUR][14 USD]}
 
85
        $expected = MoneyBag::create(new Money(24, 'EUR'), new Money(14, 'USD'));
 
86
 
 
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());
 
90
    }
 
91
 
 
92
    public function testBagNegate()
 
93
    {
 
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()));
 
97
    }
 
98
 
 
99
    public function testBagSimpleAdd()
 
100
    {
 
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)));
 
104
    }
 
105
 
 
106
    public function testBagSubtract()
 
107
    {
 
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)));
 
111
    }
 
112
 
 
113
    public function testBagSumAdd()
 
114
    {
 
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)));
 
118
    }
 
119
 
 
120
    public function testIsZero()
 
121
    {
 
122
        //$this->assertTrue($this->fMB1->subtract($this->fMB1)->isZero());
 
123
        $this->assertTrue(MoneyBag::create(new Money (0, 'EUR'), new Money (0, 'USD'))->isZero());
 
124
    }
 
125
 
 
126
    public function testMixedSimpleAdd()
 
127
    {
 
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)));
 
131
    }
 
132
 
 
133
    public function testBagNotEquals()
 
134
    {
 
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));
 
139
    }
 
140
 
 
141
    public function testMoneyBagEquals()
 
142
    {
 
143
        $this->assertTrue(!$this->fMB1->equals(NULL));
 
144
 
 
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));
 
151
    }
 
152
 
 
153
    public function testMoneyBagHash()
 
154
    {
 
155
        $equal = MoneyBag::create(new Money(12, 'EUR'), new Money(7, 'USD'));
 
156
        $this->assertEquals($this->fMB1->hashCode(), $equal->hashCode());
 
157
    }
 
158
 
 
159
    public function testMoneyEquals()
 
160
    {
 
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));
 
167
    }
 
168
 
 
169
    public function testMoneyHash()
 
170
    {
 
171
        $this->assertNotNull($this->f12EUR);
 
172
        $equal= new Money(12, 'EUR');
 
173
        $this->assertEquals($this->f12EUR->hashCode(), $equal->hashCode());
 
174
    }
 
175
 
 
176
    public function testSimplify()
 
177
    {
 
178
        $money = MoneyBag::create(new Money(26, 'EUR'), new Money(28, 'EUR'));
 
179
        $this->assertTrue($money->equals(new Money(54, 'EUR')));
 
180
    }
 
181
 
 
182
    public function testNormalize2()
 
183
    {
 
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)));
 
187
    }
 
188
 
 
189
    public function testNormalize3()
 
190
    {
 
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)));
 
195
    }
 
196
 
 
197
    public function testNormalize4()
 
198
    {
 
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)));
 
203
    }
 
204
 
 
205
    public function testPrint()
 
206
    {
 
207
        $this->assertEquals('[12 EUR]', $this->f12EUR->toString());
 
208
    }
 
209
 
 
210
    public function testSimpleAdd()
 
211
    {
 
212
        // [12 EUR] + [14 EUR] == [26 EUR]
 
213
        $expected = new Money(26, 'EUR');
 
214
        $this->assertTrue($expected->equals($this->f12EUR->add($this->f14EUR)));
 
215
    }
 
216
 
 
217
    public function testSimpleBagAdd()
 
218
    {
 
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)));
 
222
    }
 
223
 
 
224
    public function testSimpleMultiply()
 
225
    {
 
226
        // [14 EUR] *2 == [28 EUR]
 
227
        $expected = new Money(28, 'EUR');
 
228
        $this->assertTrue($expected->equals($this->f14EUR->multiply(2)));
 
229
    }
 
230
 
 
231
    public function testSimpleNegate()
 
232
    {
 
233
        // [14 EUR] negate == [-14 EUR]
 
234
        $expected = new Money(-14, 'EUR');
 
235
        $this->assertTrue($expected->equals($this->f14EUR->negate()));
 
236
    }
 
237
 
 
238
    public function testSimpleSubtract()
 
239
    {
 
240
        // [14 EUR] - [12 EUR] == [2 EUR]
 
241
        $expected = new Money(2, 'EUR');
 
242
        $this->assertTrue($expected->equals($this->f14EUR->subtract($this->f12EUR)));
 
243
    }
 
244
}