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() |
|
|
|
|
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() |
|
|
|
|
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 |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
private static $db = array('Name' => 'Varchar'); |
|
|
|
|
135
|
|
|
|
136
|
|
|
private static $has_one = array('GridRowItemPage' => 'GridRowItemPageTO'); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
class GridRowItemPageTO extends Page implements TestOnly |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
private static $has_many = array('GridRowItems' => 'GridRowItemTO'); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
class GridRowItemPageTO_Controller extends PageController implements TestOnly |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
|
147
|
|
|
/* |
148
|
|
|
This is a test method on the *controller* |
149
|
|
|
*/ |
150
|
|
|
public function GridItems() |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
return $this->model->GridRowItems(); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
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.