AbstractConfigTest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 178
Duplicated Lines 17.98 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 32
loc 178
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 3

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 19 19 1
A tearDown() 0 4 1
A getConfigFileContent() 0 10 1
A getConfigArray() 0 6 1
A testToArray() 0 9 1
A testMerge() 0 12 1
A testGetConfigValue() 0 22 1
A test__get() 0 8 1
A test__set() 13 13 1
A testOffsetExists() 0 6 1
A testOffsetGet() 0 6 1
A testOffsetSet() 0 5 1
A testOffsetUnset() 0 6 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 KochTest\Config;
4
5
use Koch\Config\Config;
6
use org\bovigo\vfs\vfsStream;
7
use org\bovigo\vfs\vfsStreamDirectory;
8
use org\bovigo\vfs\vfsStreamWrapper;
9
10
class AbstractConfigTest extends \PHPUnit_Framework_TestCase
11
{
12
    /**
13
     * @var AbstractConfig
14
     */
15
    protected $object;
16
17 View Code Duplication
    public function setUp()
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...
18
    {
19
        // we are using the config adapter native PHP here,
20
        // it's a class extending the abstract class
21
        // abstract classes cannot be instantiated
22
        $this->object = new Config();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Koch\Config\Config() of type object<Koch\Config\Config> is incompatible with the declared type object<KochTest\Config\AbstractConfig> of property $object.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
23
24
        vfsStreamWrapper::register();
25
        $this->configFileURL = vfsStream::url('root/test.config.php');
0 ignored issues
show
Bug introduced by
The property configFileURL does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
        $this->file          = vfsStream::newFile('test.config.php', 0777)->withContent($this->getConfigFileContent());
0 ignored issues
show
Bug introduced by
The property file does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
28
        $this->configFileURL2 = vfsStream::url('root/test2.config.php');
0 ignored issues
show
Bug introduced by
The property configFileURL2 does not seem to exist. Did you mean configFileURL?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
29
        $this->file2          = vfsStream::newFile('test2.config.php', 0777)->withContent('');
0 ignored issues
show
Bug introduced by
The property file2 does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30
31
        $this->root = new vfsStreamDirectory('root');
0 ignored issues
show
Bug introduced by
The property root does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
        $this->root->addChild($this->file);
33
        $this->root->addChild($this->file2);
34
        vfsStreamWrapper::setRoot($this->root);
35
    }
36
37
    public function tearDown()
38
    {
39
        unset($this->object);
40
    }
41
42
    public function getConfigFileContent()
43
    {
44
        return <<<EOF
45
<?php
46
// Configuration File generated by Koch Framework.
47
return array(
48
  "oldKey" => "value"
49
);
50
EOF;
51
    }
52
53
    public function getConfigArray()
54
    {
55
        return [
56
            'oldKey' => 'value',
57
        ];
58
    }
59
60
    /**
61
     * @covers Koch\Config\Adapter\PHP::read
62
     * @covers Koch\Config\AbstractConfig::toArray
63
     */
64
    public function testToArray()
65
    {
66
        $array = $this->object->read($this->configFileURL);
67
        $this->assertEquals($this->getConfigArray(), $this->object->toArray());
68
69
       // unset, returns the array one last time
70
       $this->assertEquals($array, $this->object->toArray(true));
71
        $this->assertEquals([], $this->object->toArray());
72
    }
73
74
    /**
75
     * @covers Koch\Config\AbstractConfig::merge
76
     */
77
    public function testMerge()
78
    {
79
        // read old config
80
        $this->object->read($this->configFileURL);
81
82
        // merge new values
83
        $newConfig = ['newKey' => 'newKeyValue'];
84
        $this->object->merge($newConfig);
85
86
        $this->assertArrayHasKey('oldKey', $this->object->toArray());
87
        $this->assertArrayHasKey('newKey', $this->object->toArray());
88
    }
89
90
    /**
91
     * @covers Koch\Config\AbstractConfig::getConfigValue
92
     */
93
    public function testGetConfigValue()
94
    {
95
        // read old config
96
        $this->object->read($this->configFileURL);
97
98
        $this->assertEquals('value', $this->object->getConfigValue('oldKey'));
99
100
        // not existing key, returns null
101
        $this->assertEquals(null, $this->object->getConfigValue('notExistingKey'));
102
103
        $this->assertEquals('defaultValue', $this->object->getConfigValue('notExistingKey', 'defaultValue'));
104
105
        $this->assertEquals(
106
            'defaultValueTwo',
107
            $this->object->getConfigValue('notExistingKey', null, 'defaultValueTwo')
108
        );
109
110
        $this->assertEquals(
111
            'defaultValue',
112
            $this->object->getConfigValue('notExistingKey', 'defaultValue', 'defaultValueTwo')
113
        );
114
    }
115
116
    /**
117
     * @covers Koch\Config\AbstractConfig::__get
118
     */
119
    public function test__get()
0 ignored issues
show
Coding Style introduced by
function test__get() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
120
    {
121
        // read old config
122
        $this->object->read($this->configFileURL);
123
        $this->assertEquals('value', $this->object->oldKey);
124
125
        $this->assertNull($this->object->notExistingKey);
126
    }
127
128
    /**
129
     * @covers Koch\Config\AbstractConfig::__set
130
     * @covers Koch\Config\AbstractConfig::__isset
131
     * @covers Koch\Config\AbstractConfig::__unset
132
     */
133 View Code Duplication
    public function test__set()
0 ignored issues
show
Coding Style introduced by
function test__set() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
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...
134
    {
135
        // __set
136
        $this->object->newKey = 'someValue';
137
        $this->assertEquals('someValue', $this->object->newKey);
138
139
        // __isset
140
        $this->assertTrue(isset($this->object->newKey));
141
142
        // __unset
143
        unset($this->object->newKey);
144
        $this->assertFalse(isset($this->object->newKey));
145
    }
146
147
    /**
148
     * @covers Koch\Config\AbstractConfig::offsetExists
149
     */
150
    public function testOffsetExists()
151
    {
152
        $this->object->newKey = 'someValue';
153
154
        $this->assertFalse(empty($this->object['newKey']));
155
    }
156
157
    /**
158
     * @covers Koch\Config\AbstractConfig::offsetGet
159
     */
160
    public function testOffsetGet()
161
    {
162
        $this->object->newKey = 'someValue';
163
164
        $this->assertEquals('someValue', $this->object['newKey']);
165
    }
166
167
    /**
168
     * @covers Koch\Config\AbstractConfig::offsetSet
169
     */
170
    public function testOffsetSet()
171
    {
172
        $this->object['newKey'] = 'someValue';
173
        $this->assertEquals('someValue', $this->object['newKey']);
174
    }
175
176
    /**
177
     * @covers Koch\Config\AbstractConfig::offsetUnset
178
     *
179
     * @todo   Implement testOffsetUnset().
180
     */
181
    public function testOffsetUnset()
182
    {
183
        unset($this->object['newKey']);
184
185
        $this->assertFalse(isset($this->object->newKey));
186
    }
187
}
188