Passed
Push — master ( 861401...d44c24 )
by Alex
50s
created

AssociationSetAnonymousTypeTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 121
Duplicated Lines 76.86 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 93
loc 121
rs 10
c 1
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testSetBadName() 14 14 2
A testSetBadAssociation() 14 14 2
A testAddBadEnd() 17 17 2
A testSetBadEnd() 17 17 2
A testAddGoodEnd() 12 12 1
A testIsOKNewObject() 9 9 1
A testIsOKMissingAssociation() 10 10 1
A testTooManyEnds() 0 18 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
namespace AlgoWeb\ODataMetadata\Tests\v3\edm\EntityContainer;
4
5
use AlgoWeb\ODataMetadata\Tests\TestCase;
6
use AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\AssociationSetAnonymousType;
7
use AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\AssociationSetAnonymousType\EndAnonymousType;
8
use Mockery as m;
9
10
class AssociationSetAnonymousTypeTest extends TestCase
11
{
12 View Code Duplication
    public function testSetBadName()
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...
13
    {
14
        $expected = "Name must be a valid TSimpleIdentifier";
15
        $actual = null;
16
17
        $foo = new AssociationSetAnonymousType();
18
19
        try {
20
            $foo->setName(" _ ");
21
        } catch (\InvalidArgumentException $e) {
22
            $actual = $e->getMessage();
23
        }
24
        $this->assertEquals($expected, $actual);
25
    }
26
27 View Code Duplication
    public function testSetBadAssociation()
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...
28
    {
29
        $expected = "Association must be a valid TQualifiedName";
30
        $actual = null;
31
32
        $foo = new AssociationSetAnonymousType();
33
34
        try {
35
            $foo->setAssociation(" _ ");
36
        } catch (\InvalidArgumentException $e) {
37
            $actual = $e->getMessage();
38
        }
39
        $this->assertEquals($expected, $actual);
40
    }
41
42 View Code Duplication
    public function testAddBadEnd()
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...
43
    {
44
        $expected = "";
45
        $actual = null;
46
47
        $end = m::mock(EndAnonymousType::class);
48
        $end->shouldReceive('isOK')->andReturn(false);
1 ignored issue
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
49
50
        $foo = new AssociationSetAnonymousType();
51
52
        try {
53
            $foo->addToEnd($end);
54
        } catch (\InvalidArgumentException $e) {
55
            $actual = $e->getMessage();
56
        }
57
        $this->assertEquals($expected, $actual);
58
    }
59
60 View Code Duplication
    public function testSetBadEnd()
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...
61
    {
62
        $expected = "";
63
        $actual = null;
64
65
        $end = m::mock(EndAnonymousType::class);
66
        $end->shouldReceive('isOK')->andReturn(false);
1 ignored issue
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
67
68
        $foo = new AssociationSetAnonymousType();
69
70
        try {
71
            $foo->setEnd([$end]);
72
        } catch (\InvalidArgumentException $e) {
73
            $actual = $e->getMessage();
74
        }
75
        $this->assertEquals($expected, $actual);
76
    }
77
78 View Code Duplication
    public function testAddGoodEnd()
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...
79
    {
80
        $end = m::mock(EndAnonymousType::class);
81
        $end->shouldReceive('isOK')->andReturn(true);
1 ignored issue
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
82
83
        $foo = new AssociationSetAnonymousType();
84
85
        $foo->addToEnd($end);
86
        $this->assertTrue($foo->issetEnd(0));
87
        $foo->unsetEnd(0);
88
        $this->assertFalse($foo->issetEnd(0));
89
    }
90
91 View Code Duplication
    public function testIsOKNewObject()
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...
92
    {
93
        $expected = "Name must be a valid TSimpleIdentifier";
94
        $actual = null;
95
96
        $foo = new AssociationSetAnonymousType();
97
        $this->assertFalse($foo->isOK($actual));
98
        $this->assertEquals($expected, $actual);
99
    }
100
101 View Code Duplication
    public function testIsOKMissingAssociation()
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...
102
    {
103
        $expected = "Association must be a valid TQualifiedName";
104
        $actual = null;
105
106
        $foo = new AssociationSetAnonymousType();
107
        $foo->setName('UnitPrice');
108
        $this->assertFalse($foo->isOK($actual));
109
        $this->assertEquals($expected, $actual);
110
    }
111
112
    public function testTooManyEnds()
113
    {
114
        $expected = "Supplied array not a valid array: ".
115
                    'AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\AssociationSetAnonymousType';
116
        $actual = null;
117
118
        $end = m::mock(EndAnonymousType::class);
119
        $end->shouldReceive('isOK')->andReturn(true);
1 ignored issue
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
120
121
        $foo = new AssociationSetAnonymousType();
122
        $foo->setName('UnitPrice');
123
        $foo->setAssociation("Org.OData.Publication.V1.DocumentationUrl");
124
        $foo->addToEnd($end);
125
        $foo->addToEnd($end);
126
        $foo->addToEnd($end);
127
        $this->assertFalse($foo->isOK($actual));
128
        $this->assertEquals($expected, $actual);
129
    }
130
}
131