Completed
Push — master ( e592be...fb09e0 )
by Ingo
07:42
created

GridFieldAddNewButtonTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
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 testButtonPassesParentContextToSingletonWhenRelationListIsUsed()
29
    {
30
        $group = $this->objFromFixture(PeopleGroup::class, 'group');
31
        $list = $group->People();
32
        $this->mockSingleton(Person::class)
33
            ->expects($this->once())
34
            ->method('canCreate')
35
            ->with(
36
                $this->anything(),
37
                $this->callback(function ($arg) use ($group) {
38
                    return isset($arg['Parent']) && $arg['Parent']->ID == $group->ID;
39
                })
40
            );
41
42
        $this->mockButtonFragments($list, $group);
43
    }
44
45
    public function testButtonPassesNoParentContextToSingletonWhenRelationListIsNotUsed()
46
    {
47
        $group = $this->objFromFixture(PeopleGroup::class, 'group');
48
        $this->mockSingleton(Person::class)
49
            ->expects($this->once())
50
            ->method('canCreate')
51
            ->with(
52
                $this->anything(),
53
                $this->callback(function ($arg) {
54
                    return !isset($arg['Parent']);
55
                })
56
            );
57
58
        $this->mockButtonFragments(Person::get(), $group);
59
    }
60
61
    public function testButtonPassesNoParentContextToSingletonWhenNoParentRecordExists()
62
    {
63
        $group = $this->objFromFixture(PeopleGroup::class, 'group');
64
        $list = $group->People();
65
66
        $this->mockSingleton(Person::class)
67
            ->expects($this->once())
68
            ->method('canCreate')
69
            ->with(
70
                $this->anything(),
71
                $this->callback(function ($arg) {
72
                    return !isset($arg['Parent']);
73
                })
74
            );
75
76
        $this->mockButtonFragments($list, null);
77
    }
78
79
    protected function mockButtonFragments(SS_List $list, $parent = null)
80
    {
81
        $form = Form::create(
82
            new TestController(),
83
            'test',
84
            FieldList::create(
85
                $fakeGrid = GridField::create(
86
                    'dummy',
87
                    'dummy',
88
                    $list,
89
                    new GridFieldConfig(
90
                        $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...
91
                    )
92
                )
93
            ),
94
            FieldList::create()
95
        );
96
        if ($parent) {
97
            $form->loadDataFrom($parent);
98
        }
99
100
        $button->getHTMLFragments($fakeGrid);
101
    }
102
103
    protected function mockSingleton($class)
104
    {
105
        $mock = $this->getMockBuilder($class)
106
            ->setMethods(['canCreate'])
107
            ->getMock();
108
        Injector::inst()->registerService($mock, $class);
109
110
        return $mock;
111
    }
112
}
113