|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sunrise\Hydrator\Tests; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Sunrise\Hydrator\ObjectCollection; |
|
9
|
|
|
use Sunrise\Hydrator\ObjectCollectionInterface; |
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
use JsonSerializable; |
|
12
|
|
|
use RuntimeException; |
|
13
|
|
|
|
|
14
|
|
|
class ObjectCollectionTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
public function testContracts() : void |
|
17
|
|
|
{ |
|
18
|
|
|
$collection = new Fixtures\BarCollection(); |
|
19
|
|
|
|
|
20
|
|
|
$this->assertInstanceOf(ObjectCollectionInterface::class, $collection); |
|
21
|
|
|
$this->assertInstanceOf(JsonSerializable::class, $collection); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function testAdd() : void |
|
25
|
|
|
{ |
|
26
|
|
|
$store = [ |
|
27
|
|
|
new Fixtures\Bar(), |
|
28
|
|
|
new Fixtures\Bar(), |
|
29
|
|
|
new Fixtures\Bar(), |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
$collection = new Fixtures\BarCollection(); |
|
33
|
|
|
|
|
34
|
|
|
$this->assertTrue($collection->isEmpty()); |
|
35
|
|
|
|
|
36
|
|
|
$collection->add(0, $store[0]); |
|
37
|
|
|
$collection->add(1, $store[1]); |
|
38
|
|
|
$collection->add(2, $store[2]); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertFalse($collection->isEmpty()); |
|
41
|
|
|
|
|
42
|
|
|
$this->assertSame($store, $collection->all()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testUntypedCollection() : void |
|
46
|
|
|
{ |
|
47
|
|
|
$collection = new Fixtures\UntypedCollection(); |
|
48
|
|
|
|
|
49
|
|
|
$this->expectException(RuntimeException::class); |
|
50
|
|
|
$this->expectExceptionMessage('The ' . Fixtures\UntypedCollection::class . ' collection ' . |
|
51
|
|
|
'must contain the T constant.'); |
|
52
|
|
|
|
|
53
|
|
|
$collection->add(0, new Fixtures\Bar()); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function testUnexpectedObject() : void |
|
57
|
|
|
{ |
|
58
|
|
|
$collection = new Fixtures\BarCollection(); |
|
59
|
|
|
|
|
60
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
61
|
|
|
$this->expectExceptionMessage('The ' . Fixtures\BarCollection::class . ' collection ' . |
|
62
|
|
|
'can contain the ' . Fixtures\Bar::class . ' objects only.'); |
|
63
|
|
|
|
|
64
|
|
|
$collection->add(0, new Fixtures\Foo()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testJsonSerialize() : void |
|
68
|
|
|
{ |
|
69
|
|
|
$collection = new Fixtures\BarCollection(); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertSame($collection->all(), $collection->jsonSerialize()); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|