NodeTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 137
Duplicated Lines 18.98 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 26
loc 137
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A tearDown() 0 6 1
A test__construct() 0 3 1
A testBuild() 0 28 1
A testBuildAttributesNormal() 0 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\NodeTest: 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
require 'vendor/autoload.php';
27
28
use Grafizzi\Graph\Attribute;
29
use Grafizzi\Graph\Node;
30
31
/**
32
 * Node test case.
33
 */
34
class NodeTest extends BaseGraphTest {
35
36
  /**
37
   *
38
   * @var Node
39
   */
40
  private $Node;
41
42
  /**
43
   * Prepares the environment before running a test.
44
   */
45
  protected function setUp() : void {
46
    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...
47
48
    $this->Node = new Node($this->dic, 'n1');
49
  }
50
51
  /**
52
   * Cleans up the environment after running a test.
53
   */
54
  protected function tearDown() : void {
55
    // TODO Auto-generated NodeTest::tearDown()
56
    $this->Node = null;
57
58
    parent::tearDown();
59
  }
60
61
  /**
62
   * Tests Node->__construct()
63
   */
64
  public function test__construct() {
65
    $this->assertEquals('n1', $this->Node->getName());
66
  }
67
68
  /**
69
   * Tests Node->build()
70
   */
71
  public function testBuild() {
72
    // Test unbound node build.
73
    $expected = <<<EOT
74
n1;
75
76
EOT;
77
    $build = $this->Node->build();
78
    $this->assertEquals($expected, $build, 'Unbound node built correctly.');
79
80
    // Test build of node built at level 1.
81
    $this->Graph->addChild($this->Node);
82
    $expected = <<<EOT
83
  n1;
84
85
EOT;
86
    $build = $this->Node->build();
87
    $this->assertEquals($expected, $build, "Node bound at level 1 built correctly.");
88
89
    // Test build of node within a root graph.
90
    $expected = <<<EOT
91
digraph G {
92
  n1;
93
} /* /digraph G */
94
95
EOT;
96
    $build = $this->Graph->build();
97
    $this->assertEquals($expected, $build, "Graph with a single node built correctly.");
98
  }
99
100
  // Normal attributes list.
101
  public function testBuildAttributesNormal() {
102
    $this->Node->setAttributes(array(
103
      new Attribute($this->dic, 'foo', 'bar'),
104
      new Attribute($this->dic, 'baz', 'quux'),
105
    ));
106
    $expected = <<<EOT
107
n1 [ foo=bar, baz=quux ];
108
109
EOT;
110
    $actual = $this->Node->build();
111
    $this->assertEquals($expected, $actual);
112
  }
113
114
  // Attribute list with empty title in middle.
115 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...
116
    $this->Node->setAttributes(array(
117
      new Attribute($this->dic, 'foo', 'bar'),
118
      new Attribute($this->dic, 'title', ''),
119
      new Attribute($this->dic, 'baz', 'quux'),
120
    ));
121
    $expected = <<<EOT
122
n1 [ foo=bar, baz=quux ];
123
124
EOT;
125
    $actual = $this->Node->build();
126
    $this->assertEquals($expected, $actual);
127
  }
128
129
  // Attribute list with empty title as single attribute.
130
  public function testBuildAttributesOnlyEmpty() {
131
    $this->Node->setAttributes(array(
132
      new Attribute($this->dic, 'title', ''),
133
    ));
134
    $expected = <<<EOT
135
n1;
136
137
EOT;
138
    $actual = $this->Node->build();
139
    $this->assertEquals($expected, $actual);
140
  }
141
142
  // Attribute list with empty title as last attribute.
143 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...
144
    $this->Node->setAttributes(array(
145
      new Attribute($this->dic, 'foo', 'bar'),
146
      new Attribute($this->dic, 'baz', 'quux'),
147
      new Attribute($this->dic, 'title', ''),
148
    ));
149
    $expected = <<<EOT
150
n1 [ foo=bar, baz=quux ];
151
152
EOT;
153
    $actual = $this->Node->build();
154
    $this->assertEquals($expected, $actual);
155
  }
156
157
  /**
158
   * Tests Node::getAllowedChildTypes()
159
   */
160
  public function testGetAllowedChildTypes() {
161
    $this->assertEmpty(Node::getAllowedChildTypes());
162
  }
163
164
  /**
165
   * Tests Node::getType()
166
   */
167
  public function testGetType() {
168
    $this->assertEquals('node', $this->Node->getType());
169
  }
170
}
171