Completed
Pull Request — master (#192)
by
unknown
01:36
created

SetSupportedAddonsTaskTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 108
Duplicated Lines 18.52 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 20
loc 108
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testHelpIsShownWithNoArguments() 0 6 1
A testAddonsAreMarkedAsSupported() 0 8 1
A testAddonsAreNotReplaced() 0 8 1
A testMultipleAddonsCanBeMarkedAsSupported() 0 8 1
A testReplaceFlagWillReplaceSupportedAddons() 0 10 1
A testReplaceFlagWithNoAddonsWillNotDeleteEverything() 0 9 1
A testReplaceFlagWithNoAddonsWillDeleteEverything() 0 8 1
A assertAddonsAreSupported() 10 10 2
A assertAddonsAreNotSupported() 10 10 2
A runTask() 0 17 2

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
 * @mixin PHPUnit_Framework_TestCase
5
 */
6
class SetSupportedAddonsTaskTest extends FunctionalTest
7
{
8
    protected static $fixture_file = 'SetSupportedAddonsTaskTest.yml';
9
10
    public function testHelpIsShownWithNoArguments()
11
    {
12
        $output = $this->runTask();
13
14
        $this->assertContains('Usage:', $output);
15
    }
16
17
    public function testAddonsAreMarkedAsSupported()
18
    {
19
        $this->assertAddonsAreNotSupported('foo/unsupported-bar-a');
20
21
        $this->runTask('foo/unsupported-bar-a');
22
23
        $this->assertAddonsAreSupported('foo/unsupported-bar-a');
24
    }
25
26
    public function testAddonsAreNotReplaced()
27
    {
28
        $this->assertAddonsAreSupported('foo/supported-bar-a');
29
30
        $this->runTask('foo/unsupported-bar-a');
31
32
        $this->assertAddonsAreSupported('foo/supported-bar-a');
33
    }
34
35
    public function testMultipleAddonsCanBeMarkedAsSupported()
36
    {
37
        $this->assertAddonsAreNotSupported(['foo/unsupported-bar-a', 'foo/unsupported-bar-b']);
38
39
        $this->runTask('foo/unsupported-bar-a,foo/unsupported-bar-b');
40
41
        $this->assertAddonsAreSupported(['foo/unsupported-bar-a', 'foo/unsupported-bar-b']);
42
    }
43
44
    public function testReplaceFlagWillReplaceSupportedAddons()
45
    {
46
        $this->assertAddonsAreSupported('foo/supported-bar-a');
47
        $this->assertAddonsAreNotSupported('foo/unsupported-bar-a');
48
49
        $this->runTask('foo/unsupported-bar-a', ['-r']);
50
51
        $this->assertAddonsAreSupported('foo/unsupported-bar-a');
52
        $this->assertAddonsAreNotSupported('foo/supported-bar-a');
53
    }
54
55
    public function testReplaceFlagWithNoAddonsWillNotDeleteEverything()
56
    {
57
        $this->assertAddonsAreSupported('foo/supported-bar-a');
58
59
        $response = $this->runTask(null, ['-r']);
60
61
        $this->assertAddonsAreSupported('foo/supported-bar-a');
62
        $this->assertContains('Use -f to force', $response);
63
    }
64
65
    public function testReplaceFlagWithNoAddonsWillDeleteEverything()
66
    {
67
        $this->assertAddonsAreSupported('foo/supported-bar-a');
68
69
        $this->runTask(null, ['-rf']);
70
71
        $this->assertAddonsAreNotSupported('foo/supported-bar-a');
72
    }
73
74 View Code Duplication
    protected function assertAddonsAreSupported($addons)
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...
75
    {
76
        $addons = (array) $addons;
77
78
        $supportedAddons = Addon::get()->filter('Supported', true)->column('Name');
79
80
        foreach ($addons as $addon) {
81
            $this->assertContains($addon, $supportedAddons, $addon . ' is not supported');
82
        }
83
    }
84
85 View Code Duplication
    protected function assertAddonsAreNotSupported($addons)
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...
86
    {
87
        $addons = (array) $addons;
88
89
        $unsupportedAddons = Addon::get()->filter('Supported', false)->column('Name');
90
91
        foreach ($addons as $addon) {
92
            $this->assertContains($addon, $unsupportedAddons, $addon . ' is supported');
93
        }
94
    }
95
96
    protected function runTask($addons = null, array $args = [])
97
    {
98
        $vars = $addons ? ['addons' => $addons] : [];
99
100
        $request = $this->getMockBuilder(SS_HTTPRequest::class)
0 ignored issues
show
Bug introduced by
The method getMockBuilder() does not seem to exist on object<SetSupportedAddonsTaskTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
            ->setConstructorArgs(array('GET', 'test', $vars))
102
            ->getMock();
103
104
        $request->method('getVars')->willReturn($vars);
105
        $request->method('getVar')->will($this->onConsecutiveCalls($args, $addons));
0 ignored issues
show
Bug introduced by
The method onConsecutiveCalls() does not seem to exist on object<SetSupportedAddonsTaskTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106
107
        $task = new SetSupportedAddonsTask();
108
109
        ob_start();
110
        $task->run($request);
111
        return ob_get_clean();
112
    }
113
}
114