Completed
Push — master ( c883b0...d4cdf1 )
by Robbie
11s
created

GridFieldRefreshButtonTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 100
Duplicated Lines 20 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testHasRunningJob() 0 5 1
A testDoesNotHaveCancelledCompletedOrBrokenJob() 0 7 1
A testHandleRefreshDoesNotCreateJobWhenJobIsRunning() 9 9 1
A testHandleRefreshCreatesJobWhenNoJobIsRunning() 11 11 1
A testHandleCheckReturnsValidJson() 0 5 1
A testButtonIsDisabledWhenJobIsRunning() 0 10 1
A testButtonIsEnabledWhenNoJobIsRunning() 0 12 1
A completeRunningJob() 0 6 1
A getGridFieldMock() 0 15 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
class GridFieldRefreshButtonTest extends SapphireTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    protected static $fixture_file = 'GridFieldRefreshButtonTest.yml';
6
7
    public function testHasRunningJob()
8
    {
9
        $button = new GridFieldRefreshButton('test');
10
        $this->assertTrue($button->hasActiveJob());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<GridFieldRefreshButtonTest>.

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...
11
    }
12
13
    public function testDoesNotHaveCancelledCompletedOrBrokenJob()
14
    {
15
        $this->completeRunningJob();
16
17
        $button = new GridFieldRefreshButton('test');
18
        $this->assertFalse($button->hasActiveJob());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<GridFieldRefreshButtonTest>.

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...
19
    }
20
21 View Code Duplication
    public function testHandleRefreshDoesNotCreateJobWhenJobIsRunning()
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...
22
    {
23
        $count = QueuedJobDescriptor::get()->count();
24
25
        $button = new GridFieldRefreshButton('test');
26
        $button->handleRefresh();
27
28
        $this->assertSame($count, QueuedJobDescriptor::get()->count());
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<GridFieldRefreshButtonTest>.

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...
29
    }
30
31 View Code Duplication
    public function testHandleRefreshCreatesJobWhenNoJobIsRunning()
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...
32
    {
33
        $this->completeRunningJob();
34
35
        $count = QueuedJobDescriptor::get()->count();
36
37
        $button = new GridFieldRefreshButton('test');
38
        $button->handleRefresh();
39
40
        $this->assertSame($count + 1, QueuedJobDescriptor::get()->count());
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<GridFieldRefreshButtonTest>.

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...
41
    }
42
43
    public function testHandleCheckReturnsValidJson()
44
    {
45
        $button = new GridFieldRefreshButton('test');
46
        $this->assertSame('true', $button->handleCheck());
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<GridFieldRefreshButtonTest>.

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...
47
    }
48
49
    public function testButtonIsDisabledWhenJobIsRunning()
50
    {
51
        $button = new GridFieldRefreshButton('test');
52
53
        $gridFieldMock = $this->getGridFieldMock();
54
55
        $output = $button->getHTMLFragments($gridFieldMock);
56
57
        $this->assertContains('disabled', $output['test']);
58
    }
59
60
    public function testButtonIsEnabledWhenNoJobIsRunning()
61
    {
62
        $this->completeRunningJob();
63
64
        $button = new GridFieldRefreshButton('test');
65
66
        $gridFieldMock = $this->getGridFieldMock();
67
68
        $output = $button->getHTMLFragments($gridFieldMock);
69
70
        $this->assertNotContains('disabled', $output['test']);
71
    }
72
73
    /**
74
     * Turns the running job in the fixture file into a completed job
75
     */
76
    protected function completeRunningJob()
77
    {
78
        $runningJob = $this->objFromFixture('QueuedJobDescriptor', 'runningjob');
79
        $runningJob->JobStatus = 'Complete';
80
        $runningJob->write();
81
    }
82
83
    /**
84
     * Mocks and returns a gridfield with name 'TestGridField' and 'Link' method, which returns a url
85
     * @return mixed
86
     */
87
    protected function getGridFieldMock()
88
    {
89
        $gridFieldMock = $this
0 ignored issues
show
Bug introduced by
The method getMockBuilder() does not seem to exist on object<GridFieldRefreshButtonTest>.

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...
90
            ->getMockBuilder(GridField::class)
91
            ->setConstructorArgs(['TestGridField'])
92
            ->setMethods(['Link'])
93
            ->getMock();
94
95
        $gridFieldMock
96
            ->expects($this->any())
0 ignored issues
show
Bug introduced by
The method any() does not seem to exist on object<GridFieldRefreshButtonTest>.

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...
97
            ->method('Link')
98
            ->will($this->returnValue('http://example.com'));
0 ignored issues
show
Bug introduced by
The method returnValue() does not seem to exist on object<GridFieldRefreshButtonTest>.

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...
99
100
        return $gridFieldMock;
101
    }
102
}
103