Completed
Pull Request — master (#117)
by Simone
02:36
created

ContainerTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 167
Duplicated Lines 34.73 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 2
dl 58
loc 167
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testConfigurationCannotBeEmpty() 0 4 1
A testNeedsResourcesAsRootElement() 0 11 1
A testResourcesAreResourcesChild() 16 16 1
A testResourceDefinesConstraints() 0 12 1
A testCannotCreateResourceWithoutAllowedConstraints() 17 17 1
A testCon() 0 22 1
B testRewriteRules() 0 38 1
B testCreationSucceedReturnsTrue() 25 25 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
 * This file is part of sensorario/resources repository
5
 *
6
 * (c) Simone Gentili <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sensorario\Resources\Test\Resources;
13
14
use PHPUnit_Framework_TestCase;
15
use Sensorario\Resources\Container;
16
17
final class ContainerTest extends PHPUnit_Framework_TestCase
18
{
19
    /**
20
     * @expectedException \Sensorario\Resources\Exceptions\EmptyConfigurationException
21
     * @expectedExceptionMessage resources element is not defined
22
     */
23
    public function testConfigurationCannotBeEmpty()
24
    {
25
        $container = new Container([]);
0 ignored issues
show
Unused Code introduced by
$container 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...
26
    }
27
28
    public function testNeedsResourcesAsRootElement()
29
    {
30
        $container = new Container([
31
            'resources' => [],
32
        ]);
33
34
        $this->assertSame(
35
            0,
36
            $container->countResources()
37
        );
38
    }
39
40 View Code Duplication
    public function testResourcesAreResourcesChild()
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...
41
    {
42
        $container = new Container([
43
            'resources' => [
44
                'foo' => [],
45
                'bar' => [
46
                    'constraints' => [],
47
                ],
48
            ],
49
        ]);
50
51
        $this->assertSame(
52
            2,
53
            $container->countResources()
54
        );
55
    }
56
57
    /**
58
     * @expectedException \Sensorario\Resources\Exceptions\InvalidConstraintException
59
     * @expectedExceptionMessageRegExp /Invalid constraint/
60
     */
61
    public function testResourceDefinesConstraints()
62
    {
63
        $container = new Container([
0 ignored issues
show
Unused Code introduced by
$container 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...
64
            'resources' => [
65
                'foo' => [
66
                    'constraints' => [
67
                        'invalid' => 'constraint',
68
                    ],
69
                ],
70
            ],
71
        ]);
72
    }
73
74
    /**
75
     * @expectedException              \Sensorario\Resources\Exceptions\NotAllowedConstraintException
76
     * @expectedExceptionMessageRegExp /Not allowed `foo` constraint/
77
     */
78 View Code Duplication
    public function testCannotCreateResourceWithoutAllowedConstraints()
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
        $container = new Container([
81
            'resources' => [
82
                'foo' => [
83
                    'constraints' => [
84
                    ],
85
                ],
86
            ],
87
        ]);
88
89
        $container->create(
90
            'foo', [
91
                'foo' => 'bar',
92
            ]
93
        );
94
    }
95
96
    public function testCon()
97
    {
98
        $container = new Container([
99
            'resources' => [
100
                'foo' => [
101
                    'constraints' => [
102
                        'allowed' => [
103
                            'foo',
104
                            'bar',
105
                        ]
106
                    ],
107
                ],
108
            ],
109
        ]);
110
111
        $this->assertEquals( [
112
                'foo',
113
                'bar',
114
            ],
115
            $container->allowed('foo')
116
        );
117
    }
118
119
    public function testRewriteRules()
120
    {
121
        $container = new Container([
122
            'resources' => [
123
                'foo' => [
124
                    'rewrite' => [
125
                        'foo' => [
126
                            'set' => [
127
                                'equals_to' => 'bar',
128
                            ],
129
                            'when' => [
130
                                'greater_than' => 'bar',
131
                            ],
132
                        ],
133
                    ],
134
                    'constraints' => [
135
                        'allowed' => [
136
                            'foo',
137
                            'bar',
138
                        ]
139
                    ],
140
                ],
141
            ],
142
        ]);
143
144
        $this->assertEquals( [
145
                'foo' => [
146
                    'set' => [
147
                        'equals_to' => 'bar',
148
                    ],
149
                    'when' => [
150
                        'greater_than' => 'bar',
151
                    ],
152
                ],
153
            ],
154
            $container->rewrites('foo')
0 ignored issues
show
Unused Code introduced by
The call to Container::rewrites() has too many arguments starting with 'foo'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
155
        );
156
    }
157
158 View Code Duplication
    public function testCreationSucceedReturnsTrue()
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...
159
    {
160
        $container = new Container([
161
            'resources' => [
162
                'foo' => [
163
                    'constraints' => [
164
                        'allowed' => [
165
                            'foo'
166
                        ]
167
                    ],
168
                ],
169
            ],
170
        ]);
171
172
        $creationSucceed = $container->create(
173
            'foo', [
174
                'foo' => 'bar',
175
            ]
176
        );
177
178
        $this->assertSame(
179
            true,
180
            $creationSucceed
181
        );
182
    }
183
}
184