Completed
Push — master ( 241a00...e1f50e )
by Gordon
18:01 queued 13s
created

testSplitDataListFromModelIntoGridRows()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 15
Ratio 100 %
Metric Value
dl 15
loc 15
rs 9.4285
cc 2
eloc 10
nc 2
nop 0
1
<?php
2
3
class GridRowsExtensionTest extends FunctionalTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method setExpectedException() does not seem to exist on object<GridRowsExtensionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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
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...
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
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...
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);
0 ignored issues
show
Bug introduced by
The method assertLessThanOrEqual() does not seem to exist on object<GridRowsExtensionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
112
113
    	// All but the last row should equal the expected width, $maxWidth
114
    	foreach ($widths as $width) {
115
    		$this->assertEquals($maxWidth, $width);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<GridRowsExtensionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
116
    	}
117
    	$this->assertEquals($amount, $items);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<GridRowsExtensionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
    }
119
}
120
121
122
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...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
123
	private static $db = array('Name' => 'Varchar');
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
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...
124
125
	private static $has_one = array('GridRowItemPage' => 'GridRowItemPageTO');
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
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...
126
}
127
128
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...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
129
	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...
130
}
131
132
class GridRowItemPageTO_Controller extends Page_Controller 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 Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

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...
133
134
	/*
135
	This is a test method on the *controller*
136
	 */
137
	public function GridItems() {
0 ignored issues
show
Coding Style introduced by
Method name "GridRowItemPageTO_Controller::GridItems" is not in camel caps format
Loading history...
138
		return $this->model->GridRowItems();
139
	}
140
}
141