Passed
Pull Request — main (#8)
by Anatoly
02:31
created

ObjectCollectionTest::testContracts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 5
c 1
b 0
f 0
rs 10
cc 1
nc 1
nop 0
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 RuntimeException;
12
13
class ObjectCollectionTest extends TestCase
14
{
15
    public function testContracts() : void
16
    {
17
        $collection = new Fixtures\BarCollection();
18
19
        $this->assertInstanceOf(ObjectCollectionInterface::class, $collection);
20
    }
21
22
    public function testAdd() : void
23
    {
24
        $store = [
25
            new Fixtures\Bar(),
26
            new Fixtures\Bar(),
27
            new Fixtures\Bar(),
28
        ];
29
30
        $collection = new Fixtures\BarCollection();
31
32
        $collection->add(0, $store[0]);
33
        $collection->add(1, $store[1]);
34
        $collection->add(2, $store[2]);
35
36
        $this->assertSame($store, $collection->all());
37
    }
38
39
    public function testUntypedCollection() : void
40
    {
41
        $collection = new Fixtures\UntypedCollection();
42
43
        $this->expectException(RuntimeException::class);
44
        $this->expectExceptionMessage('The ' . Fixtures\UntypedCollection::class . ' collection ' .
45
                                      'must contain the T constant.');
46
47
        $collection->add(0, new Fixtures\Bar());
48
    }
49
50
    public function testUnexpectedObject() : void
51
    {
52
        $collection = new Fixtures\BarCollection();
53
54
        $this->expectException(InvalidArgumentException::class);
55
        $this->expectExceptionMessage('The ' . Fixtures\BarCollection::class . ' collection ' .
56
                                      'can contain the ' . Fixtures\Bar::class . ' objects only.');
57
58
        $collection->add(0, new Fixtures\Foo());
59
    }
60
}
61