FEATURE Added initial commit of Datagrid with tests

This commit is contained in:
Stig Lindqvist 2011-09-28 10:25:24 +13:00 committed by Stig Lindqvist
parent 75c0fcd052
commit 85cdb3771c
4 changed files with 163 additions and 0 deletions

68
forms/Datagrid.php Normal file
View File

@ -0,0 +1,68 @@
<?php
/**
* Description of Datagrid
*
*/
class Datagrid extends FormField {
/**
*
* @var SS_list
*/
protected $datasource = null;
/**
* Create a new field.
* @param name The internal field name, passed to forms.
* @param title The field label.
* @param value The value of the field.
* @param form Reference to the container form
* @param maxLength The Maximum length of the attribute
*/
function __construct($name, $title = null, SS_list $source = null, $form = null) {
parent::__construct($name, $title, null, $form);
}
/**
* Set the datasource
*
* @param SS_List $datasource
*/
public function setDatasource(SS_List $datasource ) {
$this->datasource = $datasource;
}
/**
* Get the datasource
*
* @return SS_list
*/
public function getDatasource() {
return $this->datasource;
}
/**
* Get the headers or column names for this grid
*
* The returning array will have the format of
* array(
* 'FirstName' => 'First name',
* 'Description' => 'A nice description'
* )
*
* @return array
*/
public function getHeaders() {
if($this->datasource instanceof DataList ) {
return singleton($this->datasource->dataClass)->summaryFields();
} else {
$firstItem = $this->datasource->first();
if(!$firstItem) {
return array();
}
return array_combine(array_keys($firstItem),array_keys($firstItem));
}
}
}

View File

@ -0,0 +1,26 @@
<?php
/**
* This is a functional test for DatagridFunctionalTest
*
*/
class DatagridFunctionalTest extends FunctionalTest {
public function testGetInstance() {
$this->assertTrue(new Datagrid('testgrid') instanceof Datagrid, 'Trying to find an instance of Datagrid.');
}
public function testAddToForm() {
$response = $this->get("DatagridFunctionalTest_Controller/");
$this->assertContains("form", $response->getBody());
}
}
class DatagridFunctionalTest_Controller extends ContentController {
public function index() {
$grid = new Datagrid('testgrid');
$form = new Form($this, 'gridform', new FieldList($grid), new FieldList(new FormAction('rerender', 'rerender')));
return array('Form'=>$form);
}
}

View File

@ -0,0 +1,66 @@
<?php
/**
* This is a Unittest class for DatagridTest
*
*/
class DatagridTest extends SapphireTest {
/**
*
* @var string
*/
static $fixture_file = 'sapphire/tests/forms/DatagridTest.yml';
/**
*
* @var array
*/
protected $extraDataObjects = array(
'DatagridTest_Person',
);
public function testGetInstance() {
$this->assertTrue(new Datagrid('Testgrid') instanceof FormField, 'Datagrid should be a FormField');
}
public function testSetDataSource() {
$grid = new Datagrid('Testgrid');
$source = new ArrayList();
$grid->setDatasource( $source );
$this->assertEquals($source, $grid->getDatasource());
}
public function testGetDefaultHeadersFromEmptyArrayList() {
$grid = new Datagrid('Testgrid');
$source = new ArrayList();
$grid->setDatasource($source);
$this->assertEquals(array(), $grid->getHeaders());
}
public function testGetDefaultHeadersFromArrayList() {
$grid = new Datagrid('Testgrid');
$source = new ArrayList(array(array('ID'=>1,'Name'=>'Aaron Aardwark')));
$grid->setDatasource($source);
$this->assertEquals(array('ID'=>'ID','Name'=>'Name'), $grid->getHeaders());
}
public function testGetDefaultHeadersFromDataList() {
$grid = new Datagrid('Testgrid');
$source = new DataList('DatagridTest_Person');
$grid->setDatasource($source);
$this->assertEquals(array('Name'=>'Name','ID'=>'ID'), $grid->getHeaders());
}
}
class DatagridTest_Person extends Dataobject implements TestOnly {
public static $db = array(
'Name' => 'Varchar'
);
public static $summary_fields = array(
'Name',
'ID'
);
}

View File

@ -0,0 +1,3 @@
DatagridTest_Person:
first:
Name: First Person