Completed
Push — master ( f2ef3e...a98c98 )
by Bartko
01:49
created

AdapterTestAbstract::testLockTreeDoesNotFail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace StefanoTreeTest\Integration\Adapter;
6
7
use StefanoTree\NestedSet\Adapter\AdapterInterface as TreeAdapterInterface;
8
use StefanoTree\NestedSet\NodeInfo;
9
use StefanoTreeTest\IntegrationTestCase;
10
11
abstract class AdapterTestAbstract extends IntegrationTestCase
12
{
13
    /**
14
     * @var TreeAdapterInterface
15
     */
16
    protected $adapter;
17
18
    protected function setUp()
19
    {
20
        $this->adapter = $this->getAdapter();
21
22
        parent::setUp();
23
    }
24
25
    protected function tearDown()
26
    {
27
        $this->adapter = null;
28
        parent::tearDown();
29
    }
30
31
    /**
32
     * @return TreeAdapterInterface
33
     */
34
    abstract protected function getAdapter();
35
36
    protected function getDataSet()
37
    {
38
        return $this->createMySQLXMLDataSet(__DIR__.'/_files/adapter/initDataSet.xml');
39
    }
40
41
    public function testLockTreeDoesNotFail()
42
    {
43
        $this->adapter
44
            ->lockTree();
45
    }
46
47
    public function testDbTransactionDoesNotFail()
48
    {
49
        $this->adapter
50
            ->beginTransaction();
51
        $this->adapter
52
            ->commitTransaction();
53
54
        $this->adapter
55
            ->beginTransaction();
56
        $this->adapter
57
            ->rollbackTransaction();
58
    }
59
60
    public function testUpdateData()
61
    {
62
        $this->adapter
63
            ->update(2, array('name' => 'changed'));
64
65
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testUpdateData.xml');
66
    }
67
68
    public function testUpdateDataDoesNotChangeMetadata()
69
    {
70
        $data = array(
71
            'name' => 'changed',
72
            'lft' => 'a',
73
            'rgt' => 'b',
74
            'parent_id' => 'c',
75
            'tree_traversal_id' => 1596,
76
            'level' => 'd',
77
        );
78
79
        $this->adapter
80
            ->update(2, $data);
81
82
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testUpdateData.xml');
83
    }
84
85
    public function testInsertData()
86
    {
87
        $nodeInfo = new NodeInfo(null, 6, 100, 1000, 1001, null);
88
89
        $generatedId = $this->adapter
90
            ->insert($nodeInfo, array('name' => 'some-name'));
91
92
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testInsertData.xml');
93
        $this->assertEquals(26, $generatedId);
94
    }
95
96
    public function testInsertDataUserDefinedId()
97
    {
98
        $uuid = 753;
99
        $nodeInfo = new NodeInfo(null, 6, 100, 1000, 1001, null);
100
101
        $generatedId = $this->adapter
102
            ->insert($nodeInfo, array('name' => 'some-name', 'tree_traversal_id' => $uuid));
103
104
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testInsertDataUserDefinedId.xml');
105
        $this->assertEquals($uuid, $generatedId);
106
    }
107
108
    public function testInsertDataDoesNotChangeMetadata()
109
    {
110
        $nodeInfo = new NodeInfo(null, 6, 100, 1000, 1001, null);
111
112
        $data = array(
113
            'name' => 'some-name',
114
            'lft' => 'a',
115
            'rgt' => 'b',
116
            'parent_id' => 'c',
117
            'level' => 'd',
118
        );
119
120
        $generatedId = $this->adapter
121
            ->insert($nodeInfo, $data);
122
123
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testInsertData.xml');
124
        $this->assertEquals(26, $generatedId);
125
    }
126
127
    public function testDeleteBranch()
128
    {
129
        $this->adapter
130
            ->delete(3);
131
132
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testDeleteBranch.xml');
133
    }
134
135
    public function testMoveLeftIndexes()
136
    {
137
        $this->adapter
138
            ->moveLeftIndexes(12, 500);
139
140
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testMoveLeftIndexes.xml');
141
    }
142
143
    public function testMoveRightIndexes()
144
    {
145
        $this->adapter
146
            ->moveRightIndexes(15, 500);
147
148
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testMoveRightIndexes.xml');
149
    }
150
151
    public function testUpdateParentId()
152
    {
153
        $this->adapter
154
            ->updateParentId(3, 22);
155
156
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testUpdateParentId.xml');
157
    }
158
159
    public function testUpdateLevels()
160
    {
161
        $this->adapter
162
            ->updateLevels(16, 35, 500);
163
164
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testUpdateLevels.xml');
165
    }
166
167
    public function testMoveBranch()
168
    {
169
        $this->adapter
170
            ->moveBranch(17, 32, 500);
171
172
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testMoveBranch.xml');
173
    }
174
175
    public function testGetRoots()
176
    {
177
        $roots = $this->adapter
178
            ->getRoots();
179
180
        $expected = include __DIR__.'/_files/adapter/testGetRoots.php';
181
        $this->assertEquals($expected, $roots);
182
    }
183
184
    public function testGetRoot()
185
    {
186
        $roots = $this->adapter
187
            ->getRoot();
188
189
        $expected = include __DIR__.'/_files/adapter/testGetRoot.php';
190
        $this->assertEquals($expected, $roots);
191
    }
192
193
    public function testGetNodeReturnNullIfNodeDoesNotExist()
194
    {
195
        $node = $this->adapter
196
            ->getNode(1000000);
197
        $this->assertNull($node);
198
    }
199
200
    public function testGetNode()
201
    {
202
        $node = $this->adapter
203
            ->getNode(11);
204
205
        $expected = include __DIR__.'/_files/adapter/testGetNode.php';
206
        $this->assertEquals($expected, $node);
207
    }
208
209
    public function testGetNodeInfoReturnNullIfNodeInfoDoesNotExist()
210
    {
211
        $nodeInfo = $this->adapter
212
            ->getNodeInfo(10000000);
213
        $this->assertNull($nodeInfo);
214
    }
215
216
    public function testGetNodeInfo()
217
    {
218
        $nodeInfo = $this->adapter
219
            ->getNodeInfo(10);
220
221
        $this->assertEquals($nodeInfo->getId(), 10);
222
        $this->assertEquals($nodeInfo->getParentId(), 5);
223
        $this->assertEquals($nodeInfo->getLeft(), 4);
224
        $this->assertEquals($nodeInfo->getRight(), 11);
225
        $this->assertEquals($nodeInfo->getLevel(), 3);
226
    }
227
228
    public function testGetChildrenNodeInfoReturnEmptyArrayIfNodeDoesNotHaveChildrenNodes()
229
    {
230
        $nodeInfo = $this->adapter
231
            ->getChildrenNodeInfo(7);
232
233
        $this->assertEquals(array(), $nodeInfo);
234
    }
235
236
    public function testGetChildrenNodeInfo()
237
    {
238
        $nodeInfo = $this->adapter
239
            ->getChildrenNodeInfo(4);
240
241
        $this->assertCount(2, $nodeInfo);
0 ignored issues
show
Documentation introduced by
$nodeInfo is of type array<integer,object<Ste...ee\NestedSet\NodeInfo>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
242
243
        // check first node info
244
        $this->assertEquals($nodeInfo[0]->getId(), 8);
245
        $this->assertEquals($nodeInfo[0]->getParentId(), 4);
246
        $this->assertEquals($nodeInfo[0]->getLeft(), 37);
247
        $this->assertEquals($nodeInfo[0]->getRight(), 38);
248
        $this->assertEquals($nodeInfo[0]->getLevel(), 2);
249
250
        // check second node info
251
        $this->assertEquals($nodeInfo[1]->getId(), 9);
252
        $this->assertEquals($nodeInfo[1]->getParentId(), 4);
253
        $this->assertEquals($nodeInfo[1]->getLeft(), 39);
254
        $this->assertEquals($nodeInfo[1]->getRight(), 48);
255
        $this->assertEquals($nodeInfo[1]->getLevel(), 2);
256
    }
257
258
    public function testUpdateNodeMetadata()
259
    {
260
        $nodeInfo = new NodeInfo(2, 100, 101, 102, 103, null);
261
262
        $this->adapter
263
            ->updateNodeMetadata($nodeInfo);
264
265
        $this->assertCompareDataSet(array('tree_traversal'), __DIR__.'/_files/adapter/testUpdateNodeMetadata.xml');
266
    }
267
268
    public function testGetAncestorsReturnEmptyArrayIfNodeDoestNotExist()
269
    {
270
        $path = $this->adapter
271
            ->getAncestors(1000);
272
273
        $this->assertEquals(array(), $path);
274
    }
275
276
    public function testGetAncestors()
277
    {
278
        $path = $this->adapter
279
            ->getAncestors(10);
280
281
        $this->assertCount(4, $path);
0 ignored issues
show
Documentation introduced by
$path is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
282
283
        $expected = include __DIR__.'/_files/adapter/testGetAncestors.php';
284
        $this->assertEquals($expected, $path);
285
    }
286
287
    public function testGetAncestorsFromLevel()
288
    {
289
        $path = $this->adapter
290
            ->getAncestors(10, 2);
291
292
        $this->assertCount(2, $path);
0 ignored issues
show
Documentation introduced by
$path is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
293
294
        $expected = include __DIR__.'/_files/adapter/testGetAncestorsStartFromLevel.php';
295
        $this->assertEquals($expected, $path);
296
    }
297
298
    public function testGetAncestorsExcludeLastNode()
299
    {
300
        // test exclude last node
301
        $path = $this->adapter
302
            ->getAncestors(10, 0, 1);
303
304
        $this->assertCount(3, $path);
0 ignored issues
show
Documentation introduced by
$path is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
305
        $expected = include __DIR__.'/_files/adapter/testGetAncestorsExcludeLastNode.php';
306
        $this->assertEquals($expected, $path);
307
308
        // test exclude last two node
309
        $path = $this->adapter
310
            ->getAncestors(10, 0, 2);
311
312
        $this->assertCount(2, $path);
0 ignored issues
show
Documentation introduced by
$path is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
313
        $expected = include __DIR__.'/_files/adapter/testGetAncestorsExcludeTwoLastNode.php';
314
        $this->assertEquals($expected, $path);
315
    }
316
317
    public function testGetDescendantsReturnEmptyArrayIfNodeDoesNotExist()
318
    {
319
        $nodes = $this->adapter
320
            ->getDescendants(1000);
321
322
        $this->assertEquals(array(), $nodes);
323
    }
324
325
    public function testGetDescendants()
326
    {
327
        $nodes = $this->adapter
328
            ->getDescendants(1);
329
330
        $this->assertCount(25, $nodes);
0 ignored issues
show
Documentation introduced by
$nodes is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
331
332
        $expected = include __DIR__.'/_files/adapter/testGetDescendants.php';
333
        $this->assertEquals($expected, $nodes);
334
    }
335
336
    public function testGetDescendantsDefinedNodeId()
337
    {
338
        $nodes = $this->adapter
339
            ->getDescendants(6);
340
341
        $this->assertCount(8, $nodes);
0 ignored issues
show
Documentation introduced by
$nodes is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
342
343
        $expected = include __DIR__.'/_files/adapter/testGetDescendantsDefinedNodeId.php';
344
        $this->assertEquals($expected, $nodes);
345
    }
346
347
    public function testGetDescendantsFromLevel()
348
    {
349
        $nodes = $this->adapter
350
            ->getDescendants(6, 2);
351
352
        $this->assertCount(5, $nodes);
0 ignored issues
show
Documentation introduced by
$nodes is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
353
354
        $expected = include __DIR__.'/_files/adapter/testGetDescendantsFromLevel.php';
355
        $this->assertEquals($expected, $nodes);
356
    }
357
358
    public function testGetDescendantsFixLevels()
359
    {
360
        $nodes = $this->adapter
361
            ->getDescendants(6, 2, 2);
362
363
        $this->assertCount(3, $nodes);
0 ignored issues
show
Documentation introduced by
$nodes is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
364
365
        $expected = include __DIR__.'/_files/adapter/testGetDescendantsFixLevels.php';
366
        $this->assertEquals($expected, $nodes);
367
    }
368
369
    public function testGetDescendantsExcludeBranch()
370
    {
371
        $nodes = $this->adapter
372
            ->getDescendants(1, 0, null, 9);
373
374
        $this->assertCount(20, $nodes);
0 ignored issues
show
Documentation introduced by
$nodes is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
375
376
        $expected = include __DIR__.'/_files/adapter/testGetDescendantsExcludeBranch.php';
377
        $this->assertEquals($expected, $nodes);
378
    }
379
}
380