Completed
Push — master ( a98c98...92b268 )
by Bartko
02:00
created

ManipulatorJoinTableTest::tearDown()   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\Manipulator;
6
7
use StefanoTree\NestedSet\Manipulator\Manipulator;
8
use StefanoTree\NestedSet\Manipulator\ManipulatorInterface;
9
use StefanoTree\NestedSet\Options;
10
use StefanoTreeTest\IntegrationTestCase;
11
use StefanoTreeTest\TestUtil;
12
13
class ManipulatorJoinTableTest extends IntegrationTestCase
14
{
15
    /**
16
     * @var ManipulatorInterface
17
     */
18
    protected $manipulator;
19
20
    protected function setUp()
21
    {
22
        $this->manipulator = $this->getManipulator();
23
24
        parent::setUp();
25
    }
26
27
    protected function tearDown()
28
    {
29
        $this->manipulator = null;
30
        parent::tearDown();
31
    }
32
33
    /**
34
     * @return ManipulatorInterface
35
     */
36
    protected function getManipulator(): ManipulatorInterface
37
    {
38
        $options = new Options(array(
39
                                   'tableName' => 'tree_traversal_with_scope',
40
                                   'idColumnName' => 'tree_traversal_id',
41
                                   'scopeColumnName' => 'scope',
42
                                   'dbSelectBuilder' => function () {
43
                                       $sql = 'SELECT tree_traversal_with_scope.*, ttm.name AS metadata FROM tree_traversal_with_scope'
44
                                           .' LEFT JOIN tree_traversal_metadata AS ttm'
45
                                           .' ON ttm.tree_traversal_id = tree_traversal_with_scope.tree_traversal_id';
46
47
                                       return $sql;
48
                                   }
49
                               ));
50
51
        if ('pgsql' == TEST_STEFANO_DB_VENDOR) {
52
            $options->setSequenceName('tree_traversal_with_scope_tree_traversal_id_seq');
53
        }
54
55
        $manipulator = new Manipulator($options, TestUtil::buildAdapter($options));
56
57
        return $manipulator;
58
    }
59
60
    protected function getDataSet()
61
    {
62
        return $this->createMySQLXMLDataSet(__DIR__.'/_files/adapter/join_table/initDataSet.xml');
63
    }
64
65
    public function testGetNode()
66
    {
67
        $nodes = $this->manipulator
68
            ->getDescendants(10);
69
70
        $expected = array(
71
            array(
72
                'tree_traversal_id' => 10,
73
                'name' => null,
74
                'lft' => 5,
75
                'rgt' => 6,
76
                'parent_id' => 9,
77
                'level' => 4,
78
                'scope' => 1,
79
                'metadata' => 'meta-10',
80
            ),
81
        );
82
        $this->assertEquals($expected, $nodes);
83
    }
84
85
    public function testGetAncestors()
86
    {
87
        $nodes = $this->manipulator
88
            ->getAncestors(10, 2, 1);
89
90
        $expected = array(
91
            array(
92
                'tree_traversal_id' => 8,
93
                'name' => null,
94
                'lft' => 3,
95
                'rgt' => 8,
96
                'parent_id' => 7,
97
                'level' => 2,
98
                'scope' => 1,
99
                'metadata' => null,
100
            ),
101
            array(
102
                'tree_traversal_id' => 9,
103
                'name' => null,
104
                'lft' => 4,
105
                'rgt' => 7,
106
                'parent_id' => 8,
107
                'level' => 3,
108
                'scope' => 1,
109
                'metadata' => 'meta-9',
110
            ),
111
        );
112
        $this->assertEquals($expected, $nodes);
113
    }
114
115
    public function testGetDescendants()
116
    {
117
        $nodes = $this->manipulator
118
            ->getDescendants(2, 1, 1, 4);
119
120
        $expected = array(
121
            array(
122
                'tree_traversal_id' => 3,
123
                'name' => null,
124
                'lft' => 3,
125
                'rgt' => 4,
126
                'parent_id' => 2,
127
                'level' => 2,
128
                'scope' => 2,
129
                'metadata' => null,
130
            ),
131
            array(
132
                'tree_traversal_id' => 5,
133
                'name' => null,
134
                'lft' => 7,
135
                'rgt' => 8,
136
                'parent_id' => 2,
137
                'level' => 2,
138
                'scope' => 2,
139
                'metadata' => null,
140
            ),
141
        );
142
        $this->assertEquals($expected, $nodes);
143
    }
144
145
    public function testGetChildrenNodeInfo()
146
    {
147
        $nodes = $this->manipulator
148
            ->getChildrenNodeInfo(2);
149
150
        $this->assertEquals(3, count($nodes));
151
    }
152
153
    public function testGetNodeInfo()
154
    {
155
        $nodeInfo = $this->manipulator
156
            ->getNodeInfo(2);
157
158
        $this->assertEquals($nodeInfo->getId(), 2);
159
        $this->assertEquals($nodeInfo->getParentId(), 1);
160
        $this->assertEquals($nodeInfo->getLeft(), 2);
161
        $this->assertEquals($nodeInfo->getRight(), 9);
162
        $this->assertEquals($nodeInfo->getLevel(), 1);
163
        $this->assertEquals($nodeInfo->getScope(), 2);
164
    }
165
}
166