These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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() |
|
0 ignored issues
–
show
|
|||
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() |
|
0 ignored issues
–
show
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...
|
|||
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'); |
||
0 ignored issues
–
show
|
|||
124 | |||
125 | private static $has_one = array('GridRowItemPage' => 'GridRowItemPageTO'); |
||
0 ignored issues
–
show
|
|||
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 { |
||
0 ignored issues
–
show
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
Loading history...
|
|||
133 | |||
134 | /* |
||
135 | This is a test method on the *controller* |
||
136 | */ |
||
137 | public function GridItems() { |
||
0 ignored issues
–
show
|
|||
138 | return $this->model->GridRowItems(); |
||
139 | } |
||
140 | } |
||
141 |
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.