MultiEdgeTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 150
Duplicated Lines 25.33 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 5
dl 38
loc 150
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 13 2
A tearDown() 0 4 1
A testBuild() 0 42 1
A testBuildAttributesNormal() 12 12 1
A testBuildAttributesEmptyMiddle() 13 13 1
A testBuildAttributesOnlyEmpty() 0 11 1
A testBuildAttributesEmptyLast() 13 13 1
A testGetAllowedChildTypes() 0 3 1
A testGetType() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * @file
5
 * Grafizzi\Graph\Tests\MultiEdgeTest: a component of the Grafizzi library.
6
 *
7
 * (c) 2012 Frédéric G. MARAND <[email protected]>
8
 *
9
 * Grafizzi is free software: you can redistribute it and/or modify it under the
10
 * terms of the GNU Lesser General Public License as published by the Free
11
 * Software Foundation, either version 3 of the License, or (at your option) any
12
 * later version.
13
 *
14
 * Grafizzi is distributed in the hope that it will be useful, but WITHOUT ANY
15
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16
 * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
17
 * details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with Grafizzi, in the COPYING.LESSER.txt file.  If not, see
21
 * <http://www.gnu.org/licenses/>
22
 */
23
24
namespace Grafizzi\Graph\Tests;
25
26
use Grafizzi\Graph\Attribute;
27
use Grafizzi\Graph\MultiEdge;
28
use Grafizzi\Graph\Node;
29
30
require 'vendor/autoload.php';
31
32
/**
33
 * A test for Grafizzi "Multiedges".
34
 *
35
 * No equivalent in Image_GraphViz.
36
 *
37
 * @author Frédéric G. Marand <[email protected]>
38
 *
39
 * Graph definition taken from Neato documentation, fig. 5 p. 6
40
 * "Drawing graphs with NEATO" / Stephen C. North / April 26, 2004
41
 */
42
class MultiEdgeTest extends BaseGraphTest {
43
44
  /**
45
   *
46
   * @var MultiEdge
47
   */
48
  private $MultiEdge;
49
50
  /**
51
   * Prepares the environment before running a test.
52
   */
53
  protected function setUp() : void {
54
    parent::setUpExtended();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (setUpExtended() instead of setUp()). Are you sure this is correct? If so, you might want to change this to $this->setUpExtended().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
55
56
    $this->Graph->setDirected(false);
57
    $nodes = array();
58
    for ($i = 0 ; $i < 4 ; $i++) {
59
      $this->Graph->addChild($nodes[] = new Node($this->dic, "n$i", Node::implicit()));
60
    }
61
62
    // Test case is n0 -- n1 -- n2 -- n3 -- n0, so append n0
63
    $nodes[] = $nodes[0];
64
    $this->MultiEdge = new MultiEdge($this->dic, $nodes);
65
  }
66
67
  /**
68
   * Cleans up the environment after running a test.
69
   */
70
  protected function tearDown() : void {
71
    $this->MultiEdge = null;
72
    parent::tearDown();
73
  }
74
75
  /**
76
   * Tests MultiEdge->build()
77
   */
78
  public function testBuild() {
79
    // Test unbound multiedge.
80
$expected = <<<EOT
81
n0 -- n1 -- n2 -- n3 -- n0;
82
83
EOT;
84
    $build = $this->MultiEdge->build(false);
85
    $this->assertEquals($expected, $build, "Unbound undirected multiedge without attributes built correctly.");
86
87
    // Test bound multiedge.
88
    $this->Graph->setDirected(false);
89
    $this->Graph->addChild($this->MultiEdge);
90
    $expected = <<<EOT
91
  n0 -- n1 -- n2 -- n3 -- n0;
92
93
EOT;
94
    $build = $this->MultiEdge->build();
95
    $this->assertEquals($expected, $build, "Undirected multiedge bound to root graph without attributes built correctly.");
96
97
    // Test full graph build.
98
    $expected = <<<EOT
99
graph G {
100
  n0 -- n1 -- n2 -- n3 -- n0;
101
} /* /graph G */
102
103
EOT;
104
    $build = $this->Graph->build();
105
    $this->assertEquals($expected, $build, "Graph with undirected multiedge without attributes built correctly.");
106
107
    // Test full graph with attributes.
108
    $this->MultiEdge->setAttributes(array(
109
      new Attribute($this->dic, 'label', 'Multiedge label'),
110
    ));
111
    $expected = <<<EOT
112
graph G {
113
  n0 -- n1 -- n2 -- n3 -- n0 [ label="Multiedge label" ];
114
} /* /graph G */
115
116
EOT;
117
    $build = $this->Graph->build();
118
    $this->assertEquals($expected, $build, "Graph with undirected multiedge with attributes built correctly.");
119
  }
120
121
  // Normal attributes list on unbound multiedge.
122 View Code Duplication
  public function testBuildAttributesNormal() {
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...
123
    $this->MultiEdge->setAttributes(array(
124
      new Attribute($this->dic, 'foo', 'bar'),
125
      new Attribute($this->dic, 'baz', 'quux'),
126
    ));
127
    $expected = <<<EOT
128
n0 -- n1 -- n2 -- n3 -- n0 [ foo=bar, baz=quux ];
129
130
EOT;
131
    $actual = $this->MultiEdge->build(false);
132
    $this->assertEquals($expected, $actual);
133
  }
134
135
  // Attribute list with empty title in middle on unbound multiedge.
136 View Code Duplication
  public function testBuildAttributesEmptyMiddle() {
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...
137
    $this->MultiEdge->setAttributes(array(
138
      new Attribute($this->dic, 'foo', 'bar'),
139
      new Attribute($this->dic, 'title', ''),
140
      new Attribute($this->dic, 'baz', 'quux'),
141
    ));
142
    $expected = <<<EOT
143
n0 -- n1 -- n2 -- n3 -- n0 [ foo=bar, baz=quux ];
144
145
EOT;
146
    $actual = $this->MultiEdge->build(false);
147
    $this->assertEquals($expected, $actual);
148
  }
149
150
  // Attribute list with empty title as single attribute on unbound multiedge.
151
  public function testBuildAttributesOnlyEmpty() {
152
    $this->MultiEdge->setAttributes(array(
153
      new Attribute($this->dic, 'title', ''),
154
    ));
155
    $expected = <<<EOT
156
n0 -- n1 -- n2 -- n3 -- n0;
157
158
EOT;
159
    $actual = $this->MultiEdge->build(false);
160
    $this->assertEquals($expected, $actual);
161
  }
162
163
  // Attribute list with empty title as last attribute on unbound multiedge.
164 View Code Duplication
  public function testBuildAttributesEmptyLast() {
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...
165
    $this->MultiEdge->setAttributes(array(
166
      new Attribute($this->dic, 'foo', 'bar'),
167
      new Attribute($this->dic, 'baz', 'quux'),
168
      new Attribute($this->dic, 'title', ''),
169
    ));
170
    $expected = <<<EOT
171
n0 -- n1 -- n2 -- n3 -- n0 [ foo=bar, baz=quux ];
172
173
EOT;
174
    $actual = $this->MultiEdge->build(false);
175
    $this->assertEquals($expected, $actual);
176
  }
177
178
  /**
179
   * Tests MultiEdge::getAllowedChildTypes()
180
   */
181
  public function testGetAllowedChildTypes() {
182
    $this->assertEmpty(MultiEdge::getAllowedChildTypes(), 'MultiEdge does not support children.');
183
  }
184
185
  /**
186
   * Tests MultiEdge->getType()
187
   */
188
  public function testGetType() {
189
    $this->assertEquals('multiedge', $this->MultiEdge->getType());
190
  }
191
}
192