Completed
Push — fix-2494 ( 3153ee )
by Sam
07:19
created

GridFieldAddNewButtonTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 107
Duplicated Lines 29.91 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
dl 32
loc 107
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A tearDown() 0 5 1
A testButtonPassesParentContextToSingletonWhenRelationListIsUsed() 0 16 2
A testButtonPassesNoParentContextToSingletonWhenRelationListIsNotUsed() 15 15 1
A testButtonPassesNoParentContextToSingletonWhenNoParentRecordExists() 17 17 1
A mockButtonFragments() 0 23 2
A mockSingleton() 0 9 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 SilverStripe\Forms\Tests\GridField;
4
5
use SilverStripe\Core\Injector\Injector;
6
use SilverStripe\Dev\SapphireTest;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\Form;
9
use SilverStripe\Forms\GridField\GridField;
10
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
11
use SilverStripe\Forms\GridField\GridFieldConfig;
12
use SilverStripe\Forms\Tests\GridField\GridFieldDetailFormTest\Person;
13
use SilverStripe\Forms\Tests\GridField\GridFieldDetailFormTest\PeopleGroup;
14
use SilverStripe\Forms\Tests\GridField\GridFieldDetailFormTest\Category;
15
use SilverStripe\Forms\Tests\GridField\GridFieldDetailFormTest\TestController;
16
use SilverStripe\ORM\SS_List;
17
18
class GridFieldAddNewButtonTest extends SapphireTest
19
{
20
    protected static $fixture_file = 'GridFieldDetailFormTest.yml';
21
22
    protected static $extra_dataobjects = [
23
        Person::class,
24
        PeopleGroup::class,
25
        Category::class,
26
    ];
27
28
    public function setUp()
29
    {
30
        parent::setUp();
31
        Injector::nest();
32
    }
33
34
    public function tearDown()
35
    {
36
        parent::tearDown();
37
        Injector::unnest();
38
    }
39
40
    public function testButtonPassesParentContextToSingletonWhenRelationListIsUsed()
41
    {
42
        $group = $this->objFromFixture(PeopleGroup::class, 'group');
43
        $list = $group->People();
44
        $this->mockSingleton(Person::class)
45
            ->expects($this->once())
46
            ->method('canCreate')
47
            ->with(
48
                $this->anything(),
49
                $this->callback(function ($arg) use ($group) {
50
                    return isset($arg['Parent']) && $arg['Parent']->ID == $group->ID;
51
                })
52
            );
53
54
        $this->mockButtonFragments($list, $group);
55
    }
56
57 View Code Duplication
    public function testButtonPassesNoParentContextToSingletonWhenRelationListIsNotUsed()
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...
58
    {
59
        $group = $this->objFromFixture(PeopleGroup::class, 'group');
60
        $this->mockSingleton(Person::class)
61
            ->expects($this->once())
62
            ->method('canCreate')
63
            ->with(
64
                $this->anything(),
65
                $this->callback(function ($arg) {
66
                    return !isset($arg['Parent']);
67
                })
68
            );
69
70
        $this->mockButtonFragments(Person::get(), $group);
71
    }
72
73 View Code Duplication
    public function testButtonPassesNoParentContextToSingletonWhenNoParentRecordExists()
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...
74
    {
75
        $group = $this->objFromFixture(PeopleGroup::class, 'group');
76
        $list = $group->People();
77
78
        $this->mockSingleton(Person::class)
79
            ->expects($this->once())
80
            ->method('canCreate')
81
            ->with(
82
                $this->anything(),
83
                $this->callback(function ($arg) {
84
                    return !isset($arg['Parent']);
85
                })
86
            );
87
88
        $this->mockButtonFragments($list, null);
89
    }
90
91
    protected function mockButtonFragments(SS_List $list, $parent = null)
92
    {
93
        $form = Form::create(
94
            new TestController(),
95
            'test',
96
            FieldList::create(
97
                $fakeGrid = GridField::create(
98
                    'dummy',
99
                    'dummy',
100
                    $list,
101
                    new GridFieldConfig(
102
                        $button = new GridFieldAddNewButton()
0 ignored issues
show
Unused Code introduced by
The call to GridFieldConfig::__construct() has too many arguments starting with $button = new \SilverStr...GridFieldAddNewButton().

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...
103
                    )
104
                )
105
            ),
106
            FieldList::create()
107
        );
108
        if ($parent) {
109
            $form->loadDataFrom($parent);
110
        }
111
112
        $button->getHTMLFragments($fakeGrid);
113
    }
114
115
    protected function mockSingleton($class)
116
    {
117
        $mock = $this->getMockBuilder($class)
118
            ->setMethods(['canCreate'])
119
            ->getMock();
120
        Injector::inst()->registerService($mock, $class);
121
122
        return $mock;
123
    }
124
}
125