1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class GridRowsExtensionTest extends FunctionalTest |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
protected static $fixture_file = 'GridRowsExtensionTest.yml'; |
6
|
|
|
|
7
|
|
|
protected $extraDataObjects = array( |
8
|
|
|
'GridRowItemTO' |
9
|
|
|
); |
10
|
|
|
|
11
|
|
|
public function setUp() { |
12
|
|
|
$this->requiredExtensions = array( |
13
|
|
|
'GridRowItemTO' => array('GridRowsExtension') |
14
|
|
|
); |
15
|
|
|
|
16
|
|
|
parent::setUp(); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
public function testSplitDataListMethodDoesNotExist() |
21
|
|
|
{ |
22
|
|
|
$message = 'Method not found. A grid cannot be formed from the method' |
23
|
|
|
. ' GridRowItemsTHISMETHODDOESNOTEXIST'; |
24
|
|
|
$this->setExpectedException(\InvalidArgumentException::class, $message); |
|
|
|
|
25
|
|
|
|
26
|
|
|
$page = $this->objFromFixture('GridRowItemPageTO', 'page001'); |
27
|
|
|
$page->doPublish(); |
28
|
|
|
$controller = new GridRowItemPageTO_Controller(); |
29
|
|
|
$controller->setDataModel($page); |
30
|
|
|
|
31
|
|
|
for ($columns=1; $columns < 15; $columns++) { |
32
|
|
|
$grid = $controller->SplitDataListIntoGridRows( |
33
|
|
|
'GridRowItemsTHISMETHODDOESNOTEXIST', // method from model |
34
|
|
|
$columns |
35
|
|
|
); |
36
|
|
|
$this->checkGrid($grid, $columns, 10); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
View Code Duplication |
public function testSplitDataListFromModelIntoGridRows() |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$page = $this->objFromFixture('GridRowItemPageTO', 'page001'); |
43
|
|
|
$page->doPublish(); |
44
|
|
|
$controller = new GridRowItemPageTO_Controller(); |
45
|
|
|
$controller->setDataModel($page); |
46
|
|
|
|
47
|
|
|
for ($columns=1; $columns < 15; $columns++) { |
48
|
|
|
$grid = $controller->SplitDataListIntoGridRows( |
49
|
|
|
'GridRowItems', // method from model |
50
|
|
|
$columns |
51
|
|
|
); |
52
|
|
|
$this->checkGrid($grid, $columns, 10); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
public function testSplitDataListFromControllerIntoGridRows() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$page = $this->objFromFixture('GridRowItemPageTO', 'page001'); |
59
|
|
|
$page->doPublish(); |
60
|
|
|
$controller = new GridRowItemPageTO_Controller(); |
61
|
|
|
$controller->setDataModel($page); |
62
|
|
|
for ($columns=1; $columns < 15; $columns++) { |
63
|
|
|
$grid = $controller->SplitDataListIntoGridRows( |
64
|
|
|
'GridItems', // method from controller |
65
|
|
|
$columns |
66
|
|
|
); |
67
|
|
|
$this->checkGrid($grid, $columns, 10); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/* |
72
|
|
|
Check multiple number of columns with amounts from 1 to just over the |
73
|
|
|
total number of grid items, namely 12 |
74
|
|
|
*/ |
75
|
|
|
public function testSplitClassNameDataListIntoGridRows() |
76
|
|
|
{ |
77
|
|
|
$page = $this->objFromFixture('GridRowItemPageTO', 'page001'); |
78
|
|
|
$page->doPublish(); |
79
|
|
|
$controller = new Page_Controller(); |
80
|
|
|
$controller->setDataModel($page); |
81
|
|
|
for ($columns=1; $columns < 15; $columns++) { |
82
|
|
|
for ($i=1; $i < 15; $i++) { |
83
|
|
|
$grid = $controller->SplitClassNameDataListIntoGridRows( |
84
|
|
|
'GridRowItemTO', |
85
|
|
|
$columns, |
86
|
|
|
$i, |
87
|
|
|
$sort = 'LastEdited DESC'); |
88
|
|
|
$amount = $i > 12 ? 12: $i; |
89
|
|
|
$this->checkGrid($grid, $columns, $amount); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
private function checkGrid($grid, $maxWidth, $amount) { |
96
|
|
|
$items = 0; |
97
|
|
|
$rows = 0; |
98
|
|
|
$widths = array(); |
99
|
|
|
foreach ($grid->getIterator() as $row) { |
100
|
|
|
$rows++; |
101
|
|
|
$width = 0; |
102
|
|
|
foreach ($row->Columns->getIterator() as $column) { |
103
|
|
|
$items++; |
104
|
|
|
$width++; |
105
|
|
|
} |
106
|
|
|
array_push($widths, $width); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// last value will be <= max width |
110
|
|
|
$lastVal = array_pop($widths); |
111
|
|
|
$this->assertLessThanOrEqual($maxWidth, $lastVal); |
|
|
|
|
112
|
|
|
|
113
|
|
|
// All but the last row should equal the expected width, $maxWidth |
114
|
|
|
foreach ($widths as $width) { |
115
|
|
|
$this->assertEquals($maxWidth, $width); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
$this->assertEquals($amount, $items); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
class GridRowItemTO extends DataObject implements TestOnly { |
|
|
|
|
123
|
|
|
private static $db = array('Name' => 'Varchar'); |
|
|
|
|
124
|
|
|
|
125
|
|
|
private static $has_one = array('GridRowItemPage' => 'GridRowItemPageTO'); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
class GridRowItemPageTO extends Page implements TestOnly { |
|
|
|
|
129
|
|
|
private static $has_many = array('GridRowItems' => 'GridRowItemTO'); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
class GridRowItemPageTO_Controller extends Page_Controller implements TestOnly { |
|
|
|
|
133
|
|
|
|
134
|
|
|
/* |
135
|
|
|
This is a test method on the *controller* |
136
|
|
|
*/ |
137
|
|
|
public function GridItems() { |
|
|
|
|
138
|
|
|
return $this->model->GridRowItems(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.