EdgeTest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 143
Duplicated Lines 16.78 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 5
dl 24
loc 143
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 11 2
A tearDown() 0 4 1
A test__construct() 0 4 1
A testBuild() 0 8 1
A buildTestHelper() 0 11 2
A testBuildAttributesNormal() 0 10 1
A testBuildAttributesEmptyMiddle() 12 12 1
A testBuildAttributesOnlyEmpty() 0 10 1
A testBuildAttributesEmptyLast() 12 12 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\EdgeTest: 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
/** @noinspection PhpIncludeInspection */
27
require 'vendor/autoload.php';
28
29
use Grafizzi\Graph\Attribute;
30
use Grafizzi\Graph\Edge;
31
use Grafizzi\Graph\Node;
32
33
/**
34
 * Edge test case.
35
 */
36
class EdgeTest extends BaseGraphTest {
37
38
  /**
39
   * @var Edge
40
   */
41
  private $Edge;
42
43
  /**
44
   * @var Attribute
45
   */
46
  private $Attribute;
47
48
  /**
49
   * Prepares the environment before running a test.
50
   */
51
  protected function setUp() : void {
52
    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...
53
    $src = new Node($this->dic, 'source');
54
    $dst = new Node($this->dic, 'destination');
55
    $this->Attribute = new Attribute($this->dic, 'label', 'Source to Destination');
56
    $this->Edge = new Edge($this->dic, $src, $dst);
57
    $this->Edge->setAttribute($this->Attribute);
58
    foreach (array($src, $dst, $this->Edge) as $child) {
59
      $this->Graph->addChild($child);
60
    }
61
  }
62
63
  /**
64
   * Cleans up the environment after running a test.
65
   */
66
  protected function tearDown() : void {
67
    $this->Edge = null;
68
    parent::tearDown();
69
  }
70
71
  /**
72
   * Tests Edge->__construct()
73
   */
74
  public function test__construct() {
75
    $this->assertEquals($this->Attribute, $this->Edge->getAttributeByName('label'),
76
      'Edge is correctly labeled');
77
  }
78
79
  /**
80
   * Tests Edge->build()
81
   *
82
   * @TODO test graph build
83
   */
84
  public function testBuild() {
85
    $dot = $this->Edge->build($this->Graph->getDirected());
86
    $this->assertEquals(<<<EOT
87
  source -> destination [ label="Source to Destination" ];
88
89
EOT
90
      , $dot, "Edge builds correctly");
91
  }
92
93
  /**
94
   * Common logic for the testBuild* test methods.
95
   *
96
   * @param string $expected
97
   *   The expected GraphViz output.
98
   * @param array $edges
99
   *
100
   * @internal param array $toSet An array of edges to add.*   An array of edges to add.
101
   */
102
  public function buildTestHelper($expected, array $edges) {
103
    $this->Edge->removeAttribute($this->Attribute);
104
    $toSet = array();
105
    foreach ($edges as $edge) {
106
      list($name, $value) = $edge;
107
      $toSet[] = new Attribute($this->dic, $name, $value);
108
    }
109
    $this->Edge->setAttributes($toSet);
110
    $actual = $this->Edge->build();
111
    $this->assertEquals($expected, $actual);
112
  }
113
114
  public function testBuildAttributesNormal() {
115
    $expected = <<<EOT
116
  source -> destination [ foo=bar, baz=quux ];
117
118
EOT;
119
    $this->buildTestHelper($expected, array(
120
      array('foo', 'bar'),
121
      array('baz', 'quux'),
122
    ));
123
  }
124
125
  // Attribute list with empty title in middle on edge bound to root graph.
126 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...
127
    $expected = <<<EOT
128
  source -> destination [ foo=bar, baz=quux ];
129
130
EOT;
131
132
    $this->buildTestHelper($expected, array(
133
      array('foo', 'bar'),
134
      array('title', ''),
135
      array('baz', 'quux'),
136
    ));
137
  }
138
139
  // Attribute list with empty title as single attribute on edge bound to root graph.
140
  public function testBuildAttributesOnlyEmpty() {
141
    $expected = <<<EOT
142
  source -> destination;
143
144
EOT;
145
146
    $this->buildTestHelper($expected, array(
147
      array('title', ''),
148
    ));
149
  }
150
151
  // Attribute list with empty title as last attribute on edge bound to root graph.
152 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...
153
    $expected = <<<EOT
154
  source -> destination [ foo=bar, baz=quux ];
155
156
EOT;
157
158
    $this->buildTestHelper($expected, array(
159
      array('foo', 'bar'),
160
      array('baz', 'quux'),
161
      array('title', ''),
162
    ));
163
  }
164
165
  /**
166
   * Tests Edge::getAllowedChildTypes()
167
   */
168
  public function testGetAllowedChildTypes() {
169
    $this->assertEmpty(Edge::getAllowedChildTypes());
170
  }
171
172
  /**
173
   * Tests Edge::getType()
174
   */
175
  public function testGetType() {
176
    $this->assertEquals('edge', $this->Edge->getType());
177
  }
178
}
179
180