Completed
Branch release/v1.0.0 (b932d7)
by Edward
03:07 queued 40s
created

BaseCollectionTest::testTraversable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4286
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
/**
3
 * File BaseCollectionTest.php
4
 *
5
 * @author Edward Pfremmer <[email protected]>
6
 */
7
namespace Epfremme\Tests\Collection;
8
9
use Epfremme\Collection\BaseCollection;
10
11
/**
12
 * Class BaseCollectionTest
13
 *
14
 * @package Epfremme\Tests\Collection
15
 */
16
class BaseCollectionTest extends \PHPUnit_Framework_TestCase
17
{
18
    /**
19
     * Test collection construct
20
     *
21
     * @return void
22
     */
23
    public function testConstruct()
24
    {
25
        $collection = new BaseCollection([1,2,3,4,5]);
26
27
        $this->assertAttributeInternalType('array', 'elements', $collection);
28
        $this->assertAttributeNotEmpty('elements', $collection);
29
        $this->assertAttributeCount(5, 'elements', $collection);
30
        $this->assertAttributeEquals([1,2,3,4,5], 'elements', $collection);
31
    }
32
33
    /**
34
     * Test empty collection construct
35
     *
36
     * @return void
37
     */
38
    public function testConstructEmpty()
39
    {
40
        $collection = new BaseCollection();
41
42
        $this->assertAttributeInternalType('array', 'elements', $collection);
43
        $this->assertAttributeEmpty('elements', $collection);
44
        $this->assertAttributeCount(0, 'elements', $collection);
45
        $this->assertAttributeEquals([], 'elements', $collection);
46
    }
47
48
    /**
49
     * Test associative collection construct
50
     *
51
     * @return void
52
     */
53
    public function testConstructAssociative()
54
    {
55
        $collection = new BaseCollection(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5]);
56
57
        $this->assertAttributeInternalType('array', 'elements', $collection);
58
        $this->assertAttributeNotEmpty('elements', $collection);
59
        $this->assertAttributeCount(5, 'elements', $collection);
60
        $this->assertAttributeEquals(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5], 'elements', $collection);
61
    }
62
63
    /**
64
     * Test collection is traversable
65
     *
66
     * @return void
67
     */
68
    public function testTraversable()
69
    {
70
        $collection = new BaseCollection([1,2,3,4,5]);
71
72
        foreach ($collection as $element) {
73
            $this->assertAttributeContains($element, 'elements', $collection);
74
        }
75
    }
76
77
    /**
78
     * Test get element
79
     *
80
     * @return void
81
     */
82
    public function testGet()
83
    {
84
        $collection = new BaseCollection([1, 2, 3,'a' => 4, 'b' => 5]);
85
86
        $this->assertEquals(2, $collection->get(1));
87
        $this->assertEquals(4, $collection->get('a'));
88
    }
89
90
    /**
91
     * Test getting collection keys
92
     *
93
     * @return void
94
     */
95
    public function testGetKeys()
96
    {
97
        $collection = new BaseCollection([1, 2, 3,'a' => 4, 'b' => 5]);
98
99
        $this->assertEquals([0,1,2,'a','b'], $collection->getKeys());
100
    }
101
102
    /**
103
     * Test getting collection values
104
     *
105
     * @return void
106
     */
107
    public function testGetValues()
108
    {
109
        $collection = new BaseCollection([1, 2, 3,'a' => 4, 'b' => 5]);
110
111
        $this->assertEquals([1,2,3,4,5], $collection->getValues());
112
    }
113
114
    /**
115
     * Test converting back to array
116
     *
117
     * @return void
118
     */
119
    public function testToArray()
120
    {
121
        $collection = new BaseCollection([1, 2, 3,'a' => 4, 'b' => 5]);
122
123
        $this->assertEquals([1, 2, 3,'a' => 4, 'b' => 5], $collection->toArray());
124
    }
125
}
126