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

NodeCollectionTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 184
Duplicated Lines 71.2 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 14
c 3
b 0
f 0
lcom 2
cbo 4
dl 131
loc 184
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A testIsCollection() 0 4 1
A testCanAddADataNode() 0 6 1
A testAddingANonDataNodeWillThrowAnException() 0 9 1
A testCallingApplyWillModifyTheContentsUsingReference() 17 17 1
A testCallingApplyWillModifyTheContentsUsingReturnValue() 18 18 1
A testFirstWithNoCallbackWillReturnTheFirstEntry() 9 9 1
A testLastWithNoCallbackWillReturnTheFirstEntry() 9 9 1
A testFirstWithCallbackWillReturnTheFirstThatMatches() 19 19 1
A testLastWithCallbackWillReturnTheFirstThatMatches() 19 19 1
A testFirstWithCallbackWillReturnDefaultIfNoMatchesAreFound() 20 20 1
A testLastWithCallbackWillReturnDefaultIfNoMatchesAreFound() 20 20 1
A testCloneWillCloneTheChildObjects() 0 14 2
A testToString() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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