CollectionTest::testAddDuplicate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Graze\DataStructure\Collection;
4
5
use Graze\Sort as s;
6
use PHPUnit_Framework_TestCase as TestCase;
7
8
class CollectionTest extends TestCase
9
{
10
    public function testInterface()
11
    {
12
        $coll = new Collection();
13
14
        $this->assertInstanceOf('Graze\DataStructure\Collection\CollectionInterface', $coll);
15
        $this->assertInstanceOf('Serializable', $coll);
16
    }
17
18
    public function testConstructor()
19
    {
20
        $items = ['foo', 'bar', 'baz'];
21
        $coll = new Collection($items);
22
23
        $this->assertEquals($items, $coll->getAll());
24
    }
25
26
    public function testAdd()
27
    {
28
        $coll = new Collection(['foo', 'bar']);
29
        $result = $coll->add('baz');
30
31
        $this->assertEquals(['foo', 'bar', 'baz'], $coll->getAll());
32
        $this->assertSame($coll, $result);
33
    }
34
35
    public function testAddDuplicate()
36
    {
37
        $coll = new Collection(['foo', 'bar', 'baz']);
38
        $result = $coll->add('baz');
39
40
        $this->assertEquals(['foo', 'bar', 'baz', 'baz'], $coll->getAll());
41
        $this->assertSame($coll, $result);
42
    }
43
44
    public function testContainsIsTrue()
45
    {
46
        $coll = new Collection(['foo', 'bar', 'baz']);
47
48
        $this->assertTrue($coll->contains('foo'));
49
    }
50
51
    public function testContainsIsFalse()
52
    {
53
        $coll = new Collection(['FOO', 'bar', 'baz']);
54
55
        $this->assertFalse($coll->contains('foo'));
56
    }
57
58
    public function testContainsIsStrict()
59
    {
60
        $coll = new Collection([0, 1]);
61
62
        $this->assertFalse($coll->contains(false));
63
    }
64
65
    public function testCount()
66
    {
67
        $coll = new Collection(['foo', 'bar', 'baz']);
68
69
        $this->assertEquals(3, count($coll));
70
    }
71
72 View Code Duplication
    public function testFilter()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
        $coll = new Collection(['foo', 'bar', 'baz']);
75
        $result = $coll->filter(function ($item) {
76
            return 'foo' !== $item;
77
        });
78
79
        $this->assertEquals(['bar', 'baz'], $coll->getAll());
80
        $this->assertSame($coll, $result);
81
    }
82
83
    public function testGetIterator()
84
    {
85
        $coll = new Collection();
86
87
        $this->assertInstanceOf('Iterator', $coll->getIterator());
88
    }
89
90 View Code Duplication
    public function testMap()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        $coll = new Collection(['foo', 'bar', 'baz']);
93
        $result = $coll->map(function ($item) {
94
            return $item[0];
95
        });
96
97
        $this->assertEquals(['f', 'b', 'b'], $result);
98
    }
99
100 View Code Duplication
    public function testReduce()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102
        $coll = new Collection(['foo', 'bar', 'baz']);
103
        $result = $coll->reduce(function ($carry, $item) {
104
            return $carry . $item;
105
        });
106
107
        $this->assertEquals('foobarbaz', $result);
108
    }
109
110 View Code Duplication
    public function testSort()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112
        $coll = new Collection([2, 3, 1]);
113
        $result = $coll->sort(s\comparison_fn(function ($item) {
114
            return $item;
115
        }));
116
117
        $this->assertEquals([1, 2, 3], $coll->getAll());
118
        $this->assertSame($coll, $result);
119
    }
120
121 View Code Duplication
    public function testSortOnAsc()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
    {
123
        $coll = new Collection([2, 3, 1]);
124
        $result = $coll->sortOn(function ($item) {
125
            return $item;
126
        });
127
128
        $this->assertEquals([1, 2, 3], $coll->getAll());
129
        $this->assertSame($coll, $result);
130
    }
131
132 View Code Duplication
    public function testSortOnDesc()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
    {
134
        $coll = new Collection([2, 3, 1]);
135
        $result = $coll->sortOn(function ($item) {
136
            return $item;
137
        }, s\DESC);
138
139
        $this->assertEquals([3, 2, 1], $coll->getAll());
140
        $this->assertSame($coll, $result);
141
    }
142
143
    public function testSerialize()
144
    {
145
        $cont = new Collection(['foo', 'bar', 'baz']);
146
147
        $this->assertEquals('C:41:"Graze\DataStructure\Collection\Collection":48:{a:3:{i:0;s:3:"foo";i:1;s:3:"bar";i:2;s:3:"baz";}}', serialize($cont));
148
    }
149
150
    public function testUnserialize()
151
    {
152
        $cont = unserialize('C:41:"Graze\DataStructure\Collection\Collection":48:{a:3:{i:0;s:3:"foo";i:1;s:3:"bar";i:2;s:3:"baz";}}');
153
154
        $this->assertEquals(['foo', 'bar', 'baz'], $cont->getAll());
155
    }
156
}
157