Passed
Pull Request — main (#8)
by Alex
03:11 queued 01:35
created

SlugCollectionTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 62
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testGivenValidSlugsAValidSlugCollectionIsReturned() 0 35 3
A testGivenAValueByOffsetReturnsAValidValue() 0 11 1
A testGivenAnInvalidValueItThrowsAnException() 0 9 1
1
<?php
2
3
namespace StraTDeS\VO\Tests\Collection;
4
5
use PHPUnit\Framework\TestCase;
6
use StraTDeS\VO\Collection\SlugCollection;
7
use StraTDeS\VO\Single\Slug;
8
9
class SlugCollectionTest extends TestCase
10
{
11
    public function testGivenValidSlugsAValidSlugCollectionIsReturned(): void
12
    {
13
        // Arrange
14
        $slug1 = Slug::fromValue("foo");
15
        $slug2 = Slug::fromValue("bar");
16
        $slug3 = Slug::fromValue("foobar");
17
        $slugArray = [
18
            $slug1,
19
            $slug2,
20
            $slug3
21
        ];
22
23
        // Act
24
        $slugCollection = SlugCollection::create();
25
        $slugCollection->add($slug1);
26
        $slugCollection->add($slug2);
27
        $slugCollection->add($slug3);
28
29
        // Assert
30
        $this->assertInstanceOf(SlugCollection::class, $slugCollection);
31
        $this->assertEquals($slug1, $slugCollection[0]);
32
        $this->assertEquals($slug2, $slugCollection[1]);
33
        $this->assertEquals($slug3, $slugCollection[2]);
34
        $this->assertEquals(
35
            $slugArray,
36
            $slugCollection->items()
37
        );
38
        $this->assertEquals(3, count($slugCollection));
39
40
        for($i = 0; $i < 3; $i++) {
41
            $this->assertEquals($slugArray[$i], $slugCollection[$i]);
42
        }
43
44
        foreach($slugCollection as $key => $slug) {
45
            $this->assertInstanceOf(Slug::class, $slug);
46
        }
47
    }
48
49
    public function testGivenAValueByOffsetReturnsAValidValue(): void
50
    {
51
        // Arrange
52
        $slug1 = Slug::fromValue("foo");
53
54
        // Act
55
        $slugCollection = SlugCollection::create();
56
        $slugCollection[0] = $slug1;
57
58
        // Assert
59
        $this->assertEquals($slug1, $slugCollection[0]);
60
    }
61
62
    public function testGivenAnInvalidValueItThrowsAnException(): void
63
    {
64
        $this->expectException(\InvalidArgumentException::class);
65
        $this->expectExceptionMessage("Provided value is not a valid " . Slug::class);
66
67
        $slug1 = new \stdClass();
68
69
        $slugCollection = SlugCollection::create();
70
        $slugCollection->add($slug1);
71
    }
72
}
73