TreeTest   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 3
dl 0
loc 166
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testSimpleParent() 0 9 1
A testSimpleChild() 0 10 1
A testIncorrectData() 0 16 1
A testComplexParents() 0 15 3
C testComplexChildren() 0 35 8
A getTestData() 0 7 1
A getComplexTestData() 0 15 1
A buildTree() 0 19 4
1
<?php
2
namespace ezTreeBuilder\Tests;
3
4
use ezTreeBuilder\Tree;
5
use ezTreeBuilder\Item\TreeItem;
6
7
class TreeTest extends \PHPUnit_Framework_TestCase
8
{
9
    /**
10
     * Simple unit test for parent
11
     */
12
    public function testSimpleParent()
13
    {
14
        $items = $this->getTestData();
15
        $treeBuilder = $this->buildTree($items);
16
17
        $item = $treeBuilder->getBranchById(2);
18
        $this->assertTrue($item->hasParent());
19
        $this->assertEquals($item->getParent()->getId(), 1);
20
    }
21
22
    /**
23
     * Simple unit test for child
24
     */
25
    public function testSimpleChild()
26
    {
27
        $items = $this->getTestData();
28
        $treeBuilder = $this->buildTree($items);
29
30
        $parent = $treeBuilder->getBranchById(1);
31
        $child = $treeBuilder->getBranchById(2);
32
        $this->assertTrue($parent->hasChildren());
33
        $this->assertContains($child, $parent->getChildren());
34
    }
35
36
    /**
37
     * Simple unit test for incorrect data
38
     */
39
    public function testIncorrectData()
40
    {
41
        $items = array();
42
        $items[] = array('id' => 1, 'parent_id' => 1);
43
        $items[] = array('id' => 2, 'parent_id' => 3);
44
        $treeBuilder = $this->buildTree($items);
45
        // No root branch defined
46
        $this->assertEmpty($treeBuilder->getTree());
47
48
        // add a root parent
49
        $items[] = array('id' => 3, 'parent_id' => 0);
50
        $treeBuilder = $this->buildTree($items);
51
52
        // Root branch defined
53
        $this->assertNotEmpty($treeBuilder->getTree());
54
    }
55
56
    /**
57
     * Test parents in complex form
58
     * NOT Recomanded type of test - too much nested logic for a test
59
     */
60
    public function testComplexParents()
61
    {
62
        // get data
63
        $items = $this->getComplexTestData();
64
        $treeBuilder = $this->buildTree($items);
65
        foreach ($items as $item) {
66
            $branch = $treeBuilder->getBranchById($item['id']);
67
            if ($item['parent_id'] == 0) {
68
                $this->assertFalse($branch->hasParent());
69
            } else {
70
                $this->assertTrue($branch->hasParent());
71
                $this->assertEquals($branch->getParent()->getId(), $item['parent_id']);
72
            }
73
        }
74
    }
75
76
    /**
77
     * Test Children in complex form
78
     * NOT Recomanded type of test - too much nested logic for a test
79
     */
80
    public function testComplexChildren()
81
    {
82
        // get data
83
        $items = $this->getComplexTestData();
84
        $treeBuilder = $this->buildTree($items);
85
        // associate items with childs
86
        $parents = array();
87
        $non_parents = array();
88
        $parentsToChildren = array();
89
        foreach ($items as $item) {
90
            $parents[] = $item['parent_id'];
91
        }
92
        foreach ($items as $item) {
93
            if (!in_array($item['id'], $parents)) {
94
                $non_parents[] = $item['id'];
95
            }
96
            if (!isset($parentsToChildren[$item['parent_id']])) {
97
                $parentsToChildren[$item['parent_id']] = array();
98
            }
99
            $parentsToChildren[$item['parent_id']][] = $item['id'];
100
        }
101
        // actual test
102
        foreach ($items as $item) {
103
            $branch = $treeBuilder->getBranchById($item['id']);
104
            if (in_array($item['id'], $non_parents)) {
105
                $this->assertFalse($branch->hasChildren());
106
            } else {
107
                $this->assertTrue($branch->hasChildren());
108
                $children = $branch->getChildren();
109
                foreach ($children as $child) {
110
                    $this->assertContains($child->getId(), $parentsToChildren[$item['id']]);
111
                }
112
            }
113
        }
114
    }
115
116
    /**
117
     * Get simple set of data for simple unit tests
118
     * @return array
119
     */
120
    private function getTestData()
121
    {
122
        $items = array();
123
        $items[] = array('id' => 1, 'parent_id' => 0);
124
        $items[] = array('id' => 2, 'parent_id' => 1);
125
        return $items;
126
    }
127
128
    /**
129
     * Builds a set off tree items
130
     * @return array
131
     */
132
    private function getComplexTestData()
133
    {
134
        $items = array();
135
        $items[] = array('id' => 1, 'parent_id' => 0, 'data' => array());
136
        $items[] = array('id' => 2, 'parent_id' => 1, 'data' => array());
137
        $items[] = array('id' => 3, 'parent_id' => 2, 'data' => array());
138
        $items[] = array('id' => 4, 'parent_id' => 0, 'data' => array());
139
        $items[] = array('id' => 5, 'parent_id' => 3, 'data' => array());
140
        $items[] = array('id' => 6, 'parent_id' => 3, 'data' => array());
141
        $items[] = array('id' => 7, 'parent_id' => 6, 'data' => array());
142
        $items[] = array('id' => 8, 'parent_id' => 6, 'data' => array());
143
        $items[] = array('id' => 9, 'parent_id' => 8, 'data' => array());
144
        $items[] = array('id' => 10, 'parent_id' => 7, 'data' => array());
145
        return $items;
146
    }
147
148
    /**
149
     * Build a tree
150
     * @param array $items
151
     * @return Tree
152
     */
153
    private function buildTree($items, $debug = false)
154
    {
155
        // build tree
156
        $treeBuilder = new Tree();
157
        if ($debug === true) {
158
            $treeBuilder->enableDebug();
159
        }
160
        foreach ($items as $item) {
161
            $branch = new TreeItem();
162
            $branch->setId($item['id']);
163
            $branch->setParentId($item['parent_id']);
164
            if (isset($item['data'])) {
165
                $branch->setData($item['data']);
166
            }
167
            $treeBuilder->addBranch($branch);
168
        }
169
        $treeBuilder->buildTree();
170
        return $treeBuilder;
171
    }
172
}
173