Skip to content

Commit 69ae349

Browse files
committed
Fix tests
1 parent 4ce4450 commit 69ae349

File tree

3 files changed

+19
-41
lines changed

3 files changed

+19
-41
lines changed

lib/Money/CurrencyPair.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function convert(Money $money)
7575
}
7676

7777
// @todo add rounding mode?
78-
return new Money((int) round($money->getAmount() * $this->ratio), $this->counterCurrency);
78+
return new Money((float) round($money->getAmount() * $this->ratio), $this->counterCurrency);
7979
}
8080

8181
/** @return \Money\Currency */

lib/Money/Money.php

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010

1111
namespace Money;
1212

13+
/**
14+
* Class Money
15+
* @package Money
16+
* @method static Money EUR(float $amount) Create a Money object with EUR currency.
17+
* @method static Money USD(float $amount) Create a Money object with USD currency.
18+
*/
1319
class Money
1420
{
1521
const ROUND_HALF_UP = PHP_ROUND_HALF_UP;
@@ -27,14 +33,14 @@ class Money
2733

2834
/**
2935
* Create a Money instance
30-
* @param float $amount Amount
36+
* @param int|float $amount Amount
3137
* @param \Money\Currency $currency
3238
* @throws \Money\InvalidArgumentException
3339
*/
3440
public function __construct($amount, Currency $currency)
3541
{
36-
if (!is_float($amount)) {
37-
throw new InvalidArgumentException("The first parameter of Money must be a float.");
42+
if (!is_numeric($amount)) {
43+
throw new InvalidArgumentException("The first parameter of Money must be numeric.");
3844
}
3945
$this->amount = $amount;
4046
$this->currency = $currency;
@@ -49,7 +55,7 @@ public function __construct($amount, Currency $currency)
4955
*/
5056
public static function __callStatic($method, $arguments)
5157
{
52-
return new Money($arguments[0], new Currency($method));
58+
return new Money((float) $arguments[0], new Currency($method));
5359
}
5460

5561
/**
@@ -243,7 +249,7 @@ public function allocate(array $ratios)
243249
/** @return bool */
244250
public function isZero()
245251
{
246-
return $this->amount === 0;
252+
return $this->amount == 0;
247253
}
248254

249255
/** @return bool */
@@ -277,21 +283,6 @@ public static function stringToUnits( $string )
277283
return (int) $units;
278284
}
279285

280-
public function format()
281-
{
282-
return '$' . number_format($this->getAmount() / 100, 2);
283-
}
284-
285-
/**
286-
* Convert into formatted amount (hardcoded to USD for the time being).
287-
*
288-
* @return string
289-
*/
290-
public function __toString()
291-
{
292-
return number_format($this->getAmount() / 100, 2);
293-
}
294-
295286
/**
296287
* Extracts a formatted money string.
297288
* @example echo Money::USD(500)->formattedString();
@@ -328,18 +319,13 @@ public function formattedString()
328319
return $prefix . $value . $suffix;
329320
}
330321

331-
public function format()
332-
{
333-
return '$' . number_format($this->getAmount() / 100, 2);
334-
}
335-
336322
/**
337323
* Convert into formatted amount (hardcoded to USD for the time being).
338324
*
339325
* @return string
340326
*/
341327
public function __toString()
342328
{
343-
return number_format($this->getAmount() / 100, 2);
329+
return $this->formattedString();
344330
}
345331
}

tests/Money/Tests/MoneyTest.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,11 @@ public function testGetters()
3636
}
3737

3838
/**
39-
* @expectedException Money\InvalidArgumentException
40-
*/
41-
public function testDecimalsThrowException()
42-
{
43-
$money = new Money(0.01, new Currency('EUR'));
44-
}
45-
46-
/**
47-
* @expectedException Money\InvalidArgumentException
39+
* @expectedException \Money\InvalidArgumentException
4840
*/
4941
public function testStringThrowsException()
5042
{
51-
$money = new Money('100', new Currency('EUR'));
43+
new Money('f100', new Currency('EUR'));
5244
}
5345

5446
public function testEquality()
@@ -78,7 +70,7 @@ public function testAddition()
7870
}
7971

8072
/**
81-
* @expectedException Money\InvalidArgumentException
73+
* @expectedException \Money\InvalidArgumentException
8274
*/
8375
public function testDifferentCurrenciesCannotBeAdded()
8476
{
@@ -102,7 +94,7 @@ public function testSubtraction()
10294
}
10395

10496
/**
105-
* @expectedException Money\InvalidArgumentException
97+
* @expectedException \Money\InvalidArgumentException
10698
*/
10799
public function testDifferentCurrenciesCannotBeSubtracted()
108100
{
@@ -149,7 +141,7 @@ public function testComparison()
149141
{
150142
$euro1 = new Money(1, new Currency('EUR'));
151143
$euro2 = new Money(2, new Currency('EUR'));
152-
$usd = new Money(1, new Currency('USD'));
144+
// TODO $usd = new Money(1, new Currency('USD'));
153145

154146
$this->assertTrue($euro2->greaterThan($euro1));
155147
$this->assertFalse($euro1->greaterThan($euro2));
@@ -162,7 +154,7 @@ public function testComparison()
162154
}
163155

164156
/**
165-
* @expectedException Money\InvalidArgumentException
157+
* @expectedException \Money\InvalidArgumentException
166158
*/
167159
public function testDifferentCurrenciesCannotBeCompared()
168160
{

0 commit comments

Comments
 (0)