GridRowItemPageTO_Controller   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 11
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 0
dl 0
loc 11
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A GridItems() 0 4 1
1
<?php
2
3
namespace WebOfTalent\GridRows\Tests;
4
5
use PageController;
6
use SilverStripe\Dev\FunctionalTest;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\Dev\TestOnly;
9
10
class GridRowsExtensionTest extends FunctionalTest
11
{
12
    protected static $fixture_file = 'GridRowsExtensionTest.yml';
13
14
    protected $extraDataObjects = array(
15
        'GridRowItemTO'
16
    );
17
18
    public function setUp()
19
    {
20
        $this->requiredExtensions = array(
21
            'GridRowItemTO' => array('GridRowsExtension')
22
        );
23
24
        parent::setUp();
25
    }
26
27
28
    public function testSplitDataListMethodDoesNotExist()
29
    {
30
        $message = 'Method not found.  A grid cannot be formed from the method'
31
                 . ' GridRowItemsTHISMETHODDOESNOTEXIST';
32
        $this->setExpectedException('InvalidArgumentException', $message);
33
34
        $page = $this->objFromFixture('GridRowItemPageTO', 'page001');
35
        $page->doPublish();
36
        $controller = new GridRowItemPageTO_Controller();
37
        $controller->setDataModel($page);
38
39
        for ($columns=1; $columns < 15; $columns++) {
40
            $grid = $controller->SplitDataListIntoGridRows(
41
                'GridRowItemsTHISMETHODDOESNOTEXIST', // method from model
42
                $columns
43
            );
44
            $this->checkGrid($grid, $columns, 10);
45
        }
46
    }
47
48 View Code Duplication
    public function testSplitDataListFromModelIntoGridRows()
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...
49
    {
50
        $page = $this->objFromFixture('GridRowItemPageTO', 'page001');
51
        $page->doPublish();
52
        $controller = new GridRowItemPageTO_Controller();
53
        $controller->setDataModel($page);
54
55
        for ($columns=1; $columns < 15; $columns++) {
56
            $grid = $controller->SplitDataListIntoGridRows(
57
                'GridRowItems', // method from model
58
                $columns
59
            );
60
            $this->checkGrid($grid, $columns, 10);
61
        }
62
    }
63
64 View Code Duplication
    public function testSplitDataListFromControllerIntoGridRows()
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...
65
    {
66
        $page = $this->objFromFixture('GridRowItemPageTO', 'page001');
67
        $page->doPublish();
68
        $controller = new GridRowItemPageTO_Controller();
69
        $controller->setDataModel($page);
70
        for ($columns=1; $columns < 15; $columns++) {
71
            $grid = $controller->SplitDataListIntoGridRows(
72
                'GridItems', // method from controller
73
                $columns
74
            );
75
            $this->checkGrid($grid, $columns, 10);
76
        }
77
    }
78
79
    /*
80
    Check multiple number of columns with amounts from 1 to just over the
81
    total number of grid items, namely 12
82
     */
83
    public function testSplitClassNameDataListIntoGridRows()
84
    {
85
        $page = $this->objFromFixture('GridRowItemPageTO', 'page001');
86
        $page->doPublish();
87
        $controller = new PageController();
88
        $controller->setDataModel($page);
89
        for ($columns=1; $columns < 15; $columns++) {
90
            for ($i=1; $i < 15; $i++) {
91
                $grid = $controller->SplitClassNameDataListIntoGridRows(
92
                    'GridRowItemTO',
93
                    $columns,
94
                    $i,
95
                    $sort = 'LastEdited DESC'
96
                );
97
                $amount = $i > 12 ? 12: $i;
98
                $this->checkGrid($grid, $columns, $amount);
99
            }
100
        }
101
    }
102
103
104
    private function checkGrid($grid, $maxWidth, $amount)
105
    {
106
        $items = 0;
107
        $rows = 0;
108
        $widths = array();
109
        foreach ($grid->getIterator() as $row) {
110
            $rows++;
111
            $width = 0;
112
            foreach ($row->Columns->getIterator() as $column) {
113
                $items++;
114
                $width++;
115
            }
116
            array_push($widths, $width);
117
        }
118
119
        // last value will be <= max width
120
        $lastVal = array_pop($widths);
121
        $this->assertLessThanOrEqual($maxWidth, $lastVal);
122
123
        // All but the last row should equal the expected width, $maxWidth
124
        foreach ($widths as $width) {
125
            $this->assertEquals($maxWidth, $width);
126
        }
127
        $this->assertEquals($amount, $items);
128
    }
129
}
130
131
132
class GridRowItemTO extends DataObject implements TestOnly
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
133
{
134
    private static $db = array('Name' => 'Varchar');
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
135
136
    private static $has_one = array('GridRowItemPage' => 'GridRowItemPageTO');
0 ignored issues
show
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
137
}
138
139
class GridRowItemPageTO extends Page implements TestOnly
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
140
{
141
    private static $has_many = array('GridRowItems' => 'GridRowItemTO');
0 ignored issues
show
Unused Code introduced by
The property $has_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
142
}
143
144
class GridRowItemPageTO_Controller extends PageController implements TestOnly
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
145
{
146
147
    /*
148
    This is a test method on the *controller*
149
     */
150
    public function GridItems()
0 ignored issues
show
Coding Style introduced by
Method name "GridRowItemPageTO_Controller::GridItems" is not in camel caps format
Loading history...
151
    {
152
        return $this->model->GridRowItems();
153
    }
154
}
155