1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace StraTDeS\VO\Tests\Collection; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use StraTDeS\VO\Collection\MoneyCollection; |
7
|
|
|
use StraTDeS\VO\Single\Currency; |
8
|
|
|
use StraTDeS\VO\Single\Money; |
9
|
|
|
|
10
|
|
|
class MoneyCollectionTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
public function testGivenValidMoneyAValidMoneyCollectionIsReturned(): void |
13
|
|
|
{ |
14
|
|
|
// Arrange |
15
|
|
|
$money1 = Money::create(2320, Currency::fromValue(Currency::EUR)); |
16
|
|
|
$money2 = Money::create(32423, Currency::fromValue(Currency::USD)); |
17
|
|
|
$money3 = Money::create(23423,Currency::fromValue(Currency::VES)); |
18
|
|
|
$moneyArray = [ |
19
|
|
|
$money1, |
20
|
|
|
$money2, |
21
|
|
|
$money3 |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
// Act |
25
|
|
|
$moneyCollection = MoneyCollection::create(); |
26
|
|
|
$moneyCollection->add($money1); |
27
|
|
|
$moneyCollection->add($money2); |
28
|
|
|
$moneyCollection->add($money3); |
29
|
|
|
|
30
|
|
|
// Assert |
31
|
|
|
$this->assertInstanceOf(MoneyCollection::class, $moneyCollection); |
32
|
|
|
$this->assertEquals($money1, $moneyCollection[0]); |
33
|
|
|
$this->assertEquals($money2, $moneyCollection[1]); |
34
|
|
|
$this->assertEquals($money3, $moneyCollection[2]); |
35
|
|
|
$this->assertEquals( |
36
|
|
|
$moneyArray, |
37
|
|
|
$moneyCollection->items() |
38
|
|
|
); |
39
|
|
|
$this->assertEquals(3, count($moneyCollection)); |
40
|
|
|
|
41
|
|
|
for($i = 0; $i < 3; $i++) { |
42
|
|
|
$this->assertEquals($moneyArray[$i], $moneyCollection[$i]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
foreach($moneyCollection as $key => $money) { |
46
|
|
|
$this->assertInstanceOf(Money::class, $money); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testGivenAValueByOffsetReturnsAValidValue(): void |
51
|
|
|
{ |
52
|
|
|
// Arrange |
53
|
|
|
$money1 = Money::create(2320, Currency::fromValue(Currency::EUR)); |
54
|
|
|
|
55
|
|
|
// Act |
56
|
|
|
$moneyCollection = MoneyCollection::create(); |
57
|
|
|
$moneyCollection[0] = $money1; |
58
|
|
|
|
59
|
|
|
// Assert |
60
|
|
|
$this->assertEquals($money1, $moneyCollection[0]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testGivenAnInvalidValueItThrowsAnException(): void |
64
|
|
|
{ |
65
|
|
|
$this->expectException(\InvalidArgumentException::class); |
66
|
|
|
$this->expectExceptionMessage("Provided value is not a valid " . Money::class); |
67
|
|
|
|
68
|
|
|
$money1 = new \stdClass(); |
69
|
|
|
|
70
|
|
|
$moneyCollection = MoneyCollection::create(); |
71
|
|
|
$moneyCollection->add($money1); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|