Completed
Push — master ( c49890...76bd48 )
by Harry
31s
created

testLastWithCallbackWillReturnDefaultIfNoMatchesAreFound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 20
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 20
loc 20
rs 9.4285
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
3
namespace Graze\DataNode\Test\Unit\Node;
4
5
use Graze\DataNode\NodeCollection;
6
use Graze\DataNode\NodeInterface;
7
use Graze\DataNode\Test\TestCase;
8
use Graze\DataStructure\Collection\Collection;
9
use InvalidArgumentException;
10
use Mockery as m;
11
12
class NodeCollectionTest extends TestCase
13
{
14
    public function testIsCollection()
15
    {
16
        static::assertInstanceOf(Collection::class, new NodeCollection());
17
    }
18
19
    public function testCanAddADataNode()
20
    {
21
        $collection = new NodeCollection();
22
        $node = m::mock(NodeInterface::class);
23
        static::assertSame($collection, $collection->add($node));
24
    }
25
26
    public function testAddingANonDataNodeWillThrowAnException()
27
    {
28
        $node = m::mock('Graze\Extensible\ExtensibleInterface');
29
30
        $this->expectException(InvalidArgumentException::class);
31
32
        $collection = new NodeCollection();
33
        $collection->add($node);
34
    }
35
36 View Code Duplication
    public function testCallingApplyWillModifyTheContentsUsingReference()
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...
37
    {
38
        $node = m::mock(NodeInterface::class);
39
        $node->shouldReceive('someMethod')
40
             ->once()
41
             ->andReturn(null);
42
43
        $collection = new NodeCollection();
44
        $collection->add($node);
45
46
        $collection->apply(function ($item) {
47
            $item->someMethod();
48
        });
49
50
        $item = $collection->getAll()[0];
51
        static::assertSame($node, $item);
52
    }
53
54 View Code Duplication
    public function testCallingApplyWillModifyTheContentsUsingReturnValue()
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...
55
    {
56
        $node = m::mock(NodeInterface::class);
57
        $node->shouldReceive('someMethod')
58
             ->once()
59
             ->andReturn(null);
60
61
        $collection = new NodeCollection();
62
        $collection->add($node);
63
64
        $collection->apply(function ($item) {
65
            $item->someMethod();
66
            return $item;
67
        });
68
69
        $item = $collection->getAll()[0];
70
        static::assertSame($node, $item);
71
    }
72
73 View Code Duplication
    public function testFirstWithNoCallbackWillReturnTheFirstEntry()
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...
74
    {
75
        $first = m::mock(NodeInterface::class);
76
        $second = m::mock(NodeInterface::class);
77
78
        $collection = new NodeCollection([$first, $second]);
79
80
        static::assertSame($first, $collection->first());
81
    }
82
83 View Code Duplication
    public function testLastWithNoCallbackWillReturnTheFirstEntry()
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...
84
    {
85
        $first = m::mock(NodeInterface::class);
86
        $second = m::mock(NodeInterface::class);
87
88
        $collection = new NodeCollection([$first, $second]);
89
90
        static::assertSame($second, $collection->last());
91
    }
92
93 View Code Duplication
    public function testFirstWithCallbackWillReturnTheFirstThatMatches()
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...
94
    {
95
        $first = m::mock(NodeInterface::class);
96
        $second = m::mock(NodeInterface::class);
97
        $third = m::mock(NodeInterface::class);
98
99
        $first->shouldReceive('thisOne')
100
              ->andReturn(false);
101
        $second->shouldReceive('thisOne')
102
               ->andReturn(true);
103
        $third->shouldReceive('thosOne')
104
              ->andReturn(true);
105
106
        $collection = new NodeCollection([$first, $second, $third]);
107
108
        static::assertSame($second, $collection->first(function ($item) {
109
            return $item->thisOne();
110
        }));
111
    }
112
113 View Code Duplication
    public function testLastWithCallbackWillReturnTheFirstThatMatches()
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...
114
    {
115
        $first = m::mock(NodeInterface::class);
116
        $second = m::mock(NodeInterface::class);
117
        $third = m::mock(NodeInterface::class);
118
119
        $first->shouldReceive('thisOne')
120
              ->andReturn(true);
121
        $second->shouldReceive('thisOne')
122
               ->andReturn(true);
123
        $third->shouldReceive('thisOne')
124
              ->andReturn(false);
125
126
        $collection = new NodeCollection([$first, $second, $third]);
127
128
        static::assertSame($second, $collection->last(function ($item) {
129
            return $item->thisOne();
130
        }));
131
    }
132
133 View Code Duplication
    public function testFirstWithCallbackWillReturnDefaultIfNoMatchesAreFound()
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...
134
    {
135
        $first = m::mock(NodeInterface::class);
136
        $second = m::mock(NodeInterface::class);
137
        $default = m::mock(NodeInterface::class);
138
139
        $first->shouldReceive('thisOne')
140
              ->andReturn(false);
141
        $second->shouldReceive('thisOne')
142
               ->andReturn(false);
143
144
        $collection = new NodeCollection([$first, $second]);
145
146
        static::assertSame($default, $collection->first(function ($item) {
147
            return $item->thisOne();
148
        }, $default));
149
        static::assertNull($collection->first(function ($item) {
150
            return $item->thisOne();
151
        }));
152
    }
153
154 View Code Duplication
    public function testLastWithCallbackWillReturnDefaultIfNoMatchesAreFound()
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...
155
    {
156
        $first = m::mock(NodeInterface::class);
157
        $second = m::mock(NodeInterface::class);
158
        $default = m::mock(NodeInterface::class);
159
160
        $first->shouldReceive('thisOne')
161
              ->andReturn(false);
162
        $second->shouldReceive('thisOne')
163
               ->andReturn(false);
164
165
        $collection = new NodeCollection([$first, $second]);
166
167
        static::assertSame($default, $collection->last(function ($item) {
168
            return $item->thisOne();
169
        }, $default));
170
        static::assertNull($collection->last(function ($item) {
171
            return $item->thisOne();
172
        }));
173
    }
174
175
    public function testCloneWillCloneTheChildObjects()
176
    {
177
        $first = m::mock(NodeInterface::class);
178
        $second = m::mock(NodeInterface::class);
179
180
        $collection = new NodeCollection([$first, $second]);
181
        $collection2 = $collection->getClone();
182
183
        static::assertNotSame($collection, $collection2);
184
        static::assertEquals($collection->count(), $collection2->count());
185
        for ($i = 0; $i < $collection->count(); $i++) {
186
            static::assertNotSame($collection->getAll()[$i], $collection2->getAll()[$i]);
187
        }
188
    }
189
190
    public function testToString()
191
    {
192
        $collection = new NodeCollection();
193
        static::assertEquals("NodeCollection", "$collection");
194
    }
195
}
196