Completed
Pull Request — master (#448)
by
unknown
08:31
created

testICanGetInfoAboutAverageHeightOfTree()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 38
rs 9.312
c 0
b 0
f 0
1
<?php
2
3
namespace Test;
4
5
use Hal\Component\Tree\Graph;
6
use Hal\Component\Tree\GraphFactory;
7
use Hal\Component\Tree\Node;
8
use Hal\Component\Tree\Operator\SizeOfTree;
9
10
/**
11
 * @group tree
12
 */
13
class SizeOfTreeTest extends \PHPUnit\Framework\TestCase
14
{
15
16
    /**
17
     * @expectedException  LogicException
18
     * @expectedExceptionMessage Cannot get size informations of cyclic graph
19
     */
20
    public function testICannotGetInfoAboutGraphWhenItIsCyclic()
21
    {
22
        $graph = new Graph();
23
        $a = new Node('A');
24
        $b = new Node('B');
25
26
        $graph->insert($a)->insert($b);
27
28
        // case 1
29
        $graph->addEdge($a, $b); // A -> B
30
        $graph->addEdge($b, $a); // B -> C
31
32
        $size = new SizeOfTree($graph);
0 ignored issues
show
Unused Code introduced by
$size is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
33
    }
34
35 View Code Duplication
    public function testICanGetDepthOfNode()
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...
36
    {
37
        $graph = new Graph();
38
        $a = new Node('A');
39
        $b = new Node('B');
40
        $c = new Node('C');
41
        $d = new Node('D');
42
        $e = new Node('E');
43
44
        $graph->insert($a)->insert($b)->insert($c)->insert($d)->insert($e);
45
46
        // case 1
47
        $graph->addEdge($a, $b); // A -> B
48
        $graph->addEdge($b, $c); // B -> C
49
        $graph->addEdge($c, $d); // C -> D
50
        $graph->addEdge($a, $e); // A -> E  (node with multiple childs)
51
52
        $size = new SizeOfTree($graph);
53
54
        $this->assertEquals(0, $size->getDepthOfNode($a));
55
        $this->assertEquals(1, $size->getDepthOfNode($b));
56
        $this->assertEquals(2, $size->getDepthOfNode($c));
57
        $this->assertEquals(3, $size->getDepthOfNode($d));
58
        $this->assertEquals(1, $size->getDepthOfNode($e));
59
    }
60
61 View Code Duplication
    public function testICanGetNbChildsOfNode()
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...
62
    {
63
        $graph = new Graph();
64
        $a = new Node('A');
65
        $b = new Node('B');
66
        $c = new Node('C');
67
        $d = new Node('D');
68
        $e = new Node('E');
69
70
        $graph->insert($a)->insert($b)->insert($c)->insert($d)->insert($e);
71
72
        // case 1
73
        $graph->addEdge($a, $b); // A -> B
74
        $graph->addEdge($b, $c); // B -> C
75
        $graph->addEdge($c, $d); // C -> D
76
        $graph->addEdge($a, $e); // A -> E  (node with multiple childs)
77
78
        $size = new SizeOfTree($graph);
79
80
        $this->assertEquals(4, $size->getNumberOfChilds($a));
81
        $this->assertEquals(2, $size->getNumberOfChilds($b));
82
        $this->assertEquals(1, $size->getNumberOfChilds($c));
83
        $this->assertEquals(0, $size->getNumberOfChilds($d));
84
        $this->assertEquals(0, $size->getNumberOfChilds($e));
85
    }
86
87
    public function testICanGetInfoAboutAverageHeightOfTree()
88
    {
89
        $graph = new Graph();
90
        $a = new Node('A');
91
        $b = new Node('B');
92
        $c = new Node('C');
93
        $d = new Node('D');
94
        $e = new Node('E');
95
        $f = new Node('F');
96
        $g = new Node('G');
97
        $x = new Node('X');
98
        $y = new Node('Y');
99
        $z = new Node('Z');
100
101
        $graph->insert($a)->insert($b)->insert($c)->insert($d)->insert($e)->insert($f)->insert($g);
102
        $graph->insert($x)->insert($y)->insert($z);
103
104
        // case 1
105
        $graph->addEdge($a, $b); // A -> B
106
        $graph->addEdge($b, $c); // B -> C
107
        $graph->addEdge($c, $d); // C -> D
108
        $graph->addEdge($c, $f); // C -> F
109
        $graph->addEdge($f, $g); // F -> G
110
        $graph->addEdge($a, $e); // A -> E
111
        $graph->addEdge($x, $y); // X -> Y
112
113
        // longest branch A = 5      A -> B -> C -> F -> G
114
        // longest branch X = 2      X -> Y
115
        // longest branch Z = 1      Z
116
117
        $size = new SizeOfTree($graph);
118
119
        $this->assertEquals(5, $size->getLongestBranch($a));
120
        $this->assertEquals(2, $size->getLongestBranch($x));
121
        $this->assertEquals(1, $size->getLongestBranch($z));
122
123
        $this->assertEquals(2.67, $size->getAverageHeightOfGraph());
124
    }
125
}
126