Completed
Pull Request — master (#3)
by Harry
02:53
created

NodeCollectionAbstractTest::testIsCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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