Permalink
Browse files

FEATURE Added initial commit of Datagrid with tests

  • Loading branch information...
1 parent 75c0fcd commit 85cdb3771cdd6fa506fe65f359acba2d4cdc682b Stig Lindqvist committed with stojg Sep 27, 2011
Showing with 163 additions and 0 deletions.
  1. +68 −0 forms/Datagrid.php
  2. +26 −0 tests/forms/DatagridFunctionalTest.php
  3. +66 −0 tests/forms/DatagridTest.php
  4. +3 −0 tests/forms/DatagridTest.yml
View
@@ -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));
+ }
+ }
+}
@@ -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);
+ }
+}
@@ -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'
+ );
+}
@@ -0,0 +1,3 @@
+DatagridTest_Person:
+ first:
+ Name: First Person

0 comments on commit 85cdb37

Please sign in to comment.