Merge pull request #273 from ajoneil/sapphire

---

This allows DataList::create(SiteTree) as equivalent to Object::create(DataList, SiteTree), without
having to have a create() function on DataList.

Required for E_STRICT compliance, as child classes cant override create() if they change the arguments.

DBField::create() is also renamed to DBField::create_field(), as this does not just call the constructor, which all other cases of create() do.

Conflicts:
	tests/model/DateTest.php
	tests/model/DatetimeTest.php
This commit is contained in:
Ingo Schommer 2012-04-04 16:48:16 +02:00
commit 6517f4496b
26 changed files with 107 additions and 131 deletions

View File

@ -467,7 +467,7 @@ class LeftAndMain extends Controller implements PermissionProvider {
$menu->push(new ArrayData(array( $menu->push(new ArrayData(array(
"MenuItem" => $menuItem, "MenuItem" => $menuItem,
"Title" => Convert::raw2xml($title), "Title" => Convert::raw2xml($title),
"Code" => DBField::create('Text', $code), "Code" => DBField::create_field('Text', $code),
"Link" => $menuItem->url, "Link" => $menuItem->url,
"LinkingMode" => $linkingmode "LinkingMode" => $linkingmode
))); )));
@ -1256,7 +1256,7 @@ class LeftAndMain extends Controller implements PermissionProvider {
* @return String * @return String
*/ */
function Locale() { function Locale() {
return DBField::create('DBLocale', i18n::get_locale()); return DBField::create_field('DBLocale', i18n::get_locale());
} }
function providePermissions() { function providePermissions() {

View File

@ -83,13 +83,27 @@ abstract class Object {
* overload is found, an instance of this is returned rather than the original class. To overload a class, use * overload is found, an instance of this is returned rather than the original class. To overload a class, use
* {@link Object::useCustomClass()} * {@link Object::useCustomClass()}
* *
* This can be called in one of two ways - either calling via the class directly,
* or calling on Object and passing the class name as the first parameter. The following
* are equivalent:
* $list = DataList::create('SiteTree');
* $list = Object::create('DataList', 'SiteTree');
*
* @param string $class the class name * @param string $class the class name
* @param mixed $arguments,... arguments to pass to the constructor * @param mixed $arguments,... arguments to pass to the constructor
* @return Object * @return Object
*/ */
public static function create() { public static function create() {
$args = func_get_args(); $args = func_get_args();
$class = self::getCustomClass(array_shift($args));
// Class to create should be the calling class if not Object,
// otherwise the first parameter
$class = get_called_class();
if($class == 'Object')
$class = array_shift($args);
$class = self::getCustomClass($class);
$reflector = new ReflectionClass($class); $reflector = new ReflectionClass($class);
if($reflector->getConstructor()) { if($reflector->getConstructor()) {
return $reflector->newInstanceArgs($args); return $reflector->newInstanceArgs($args);

View File

@ -45,10 +45,6 @@ class FormAction extends FormField {
parent::__construct($this->action, $title, null, $form); parent::__construct($this->action, $title, null, $form);
} }
static function create($action, $title = "") {
return new FormAction($action, $title);
}
function actionName() { function actionName() {
return substr($this->name, 7); return substr($this->name, 7);
} }

View File

@ -28,9 +28,4 @@ class HiddenField extends FormField {
array('type' => 'hidden') array('type' => 'hidden')
); );
} }
static function create($name) {
return new HiddenField($name);
}
} }

View File

@ -103,7 +103,7 @@ class MoneyField extends FormField {
function saveInto($dataObject) { function saveInto($dataObject) {
$fieldName = $this->name; $fieldName = $this->name;
if($dataObject->hasMethod("set$fieldName")) { if($dataObject->hasMethod("set$fieldName")) {
$dataObject->$fieldName = DBField::create('Money', array( $dataObject->$fieldName = DBField::create_field('Money', array(
"Currency" => $this->fieldCurrency->Value(), "Currency" => $this->fieldCurrency->Value(),
"Amount" => $this->fieldAmount->Value() "Amount" => $this->fieldAmount->Value()
)); ));

View File

@ -637,8 +637,8 @@ JS
$summaryFields[] = new ArrayData(array( $summaryFields[] = new ArrayData(array(
'Function' => $function, 'Function' => $function,
'SummaryValue' => $summaryValue, 'SummaryValue' => $summaryValue,
'Name' => DBField::create('Varchar', $fieldName), 'Name' => DBField::create_field('Varchar', $fieldName),
'Title' => DBField::create('Varchar', $fieldTitle), 'Title' => DBField::create_field('Varchar', $fieldTitle),
)); ));
} }
return new ArrayList($summaryFields); return new ArrayList($summaryFields);
@ -1234,13 +1234,13 @@ JS
} }
if(strpos($castingDefinition,'->') === false) { if(strpos($castingDefinition,'->') === false) {
$castingFieldType = $castingDefinition; $castingFieldType = $castingDefinition;
$castingField = DBField::create($castingFieldType, $value); $castingField = DBField::create_field($castingFieldType, $value);
$value = call_user_func_array(array($castingField,'XML'),$castingParams); $value = call_user_func_array(array($castingField,'XML'),$castingParams);
} else { } else {
$fieldTypeParts = explode('->', $castingDefinition); $fieldTypeParts = explode('->', $castingDefinition);
$castingFieldType = $fieldTypeParts[0]; $castingFieldType = $fieldTypeParts[0];
$castingMethod = $fieldTypeParts[1]; $castingMethod = $fieldTypeParts[1];
$castingField = DBField::create($castingFieldType, $value); $castingField = DBField::create_field($castingFieldType, $value);
$value = call_user_func_array(array($castingField,$castingMethod),$castingParams); $value = call_user_func_array(array($castingField,$castingMethod),$castingParams);
} }

View File

@ -59,7 +59,7 @@ class ToggleField extends ReadonlyField {
$rawInput = Convert::html2raw($valforInput); $rawInput = Convert::html2raw($valforInput);
if($this->charNum) $reducedVal = substr($rawInput,0,$this->charNum); if($this->charNum) $reducedVal = substr($rawInput,0,$this->charNum);
else $reducedVal = DBField::create('Text',$rawInput)->{$this->truncateMethod}(); else $reducedVal = DBField::create_field('Text',$rawInput)->{$this->truncateMethod}();
// only create togglefield if the truncated content is shorter // only create togglefield if the truncated content is shorter
if(strlen($reducedVal) < strlen($rawInput)) { if(strlen($reducedVal) < strlen($rawInput)) {

View File

@ -239,13 +239,13 @@ class GridField extends FormField {
if(strpos($castingDefinition,'->') === false) { if(strpos($castingDefinition,'->') === false) {
$castingFieldType = $castingDefinition; $castingFieldType = $castingDefinition;
$castingField = DBField::create($castingFieldType, $value); $castingField = DBField::create_field($castingFieldType, $value);
$value = call_user_func_array(array($castingField,'XML'),$castingParams); $value = call_user_func_array(array($castingField,'XML'),$castingParams);
} else { } else {
$fieldTypeParts = explode('->', $castingDefinition); $fieldTypeParts = explode('->', $castingDefinition);
$castingFieldType = $fieldTypeParts[0]; $castingFieldType = $fieldTypeParts[0];
$castingMethod = $fieldTypeParts[1]; $castingMethod = $fieldTypeParts[1];
$castingField = DBField::create($castingFieldType, $value); $castingField = DBField::create_field($castingFieldType, $value);
$value = call_user_func_array(array($castingField,$castingMethod),$castingParams); $value = call_user_func_array(array($castingField,$castingMethod),$castingParams);
} }

View File

@ -14,18 +14,6 @@ class ArrayList extends ViewableData implements SS_List, SS_Filterable, SS_Sorta
*/ */
protected $items; protected $items;
/**
* Synonym of the constructor. Can be chained with literate methods.
* ArrayList::create("SiteTree")->sort("Title") is legal, but
* new ArrayList("SiteTree")->sort("Title") is not.
*
* @param array $items - an initial array to fill this object with
*/
public static function create(array $items = array()) {
return new ArrayList($items);
}
/** /**
* *
* @param array $items - an initial array to fill this object with * @param array $items - an initial array to fill this object with

View File

@ -27,17 +27,6 @@ class DataList extends ViewableData implements SS_List, SS_Filterable, SS_Sortab
* @var DataModel * @var DataModel
*/ */
protected $model; protected $model;
/**
* Synonym of the constructor. Can be chained with literate methods.
* DataList::create("SiteTree")->sort("Title") is legal, but
* new DataList("SiteTree")->sort("Title") is not.
*
* @param string $dataClass - The DataObject class to query.
*/
public static function create($dataClass) {
return new DataList($dataClass);
}
/** /**
* Create a new DataList. * Create a new DataList.

View File

@ -1092,7 +1092,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
// if database column doesn't correlate to a DBField instance... // if database column doesn't correlate to a DBField instance...
if(!$fieldObj) { if(!$fieldObj) {
$fieldObj = DBField::create('Varchar', $this->record[$fieldName], $fieldName); $fieldObj = DBField::create_field('Varchar', $this->record[$fieldName], $fieldName);
} }
// Both CompositeDBFields and regular fields need to be repopulated // Both CompositeDBFields and regular fields need to be repopulated
@ -2380,12 +2380,12 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
// Special case for has_one relationships // Special case for has_one relationships
} else if(preg_match('/ID$/', $fieldName) && $this->has_one(substr($fieldName,0,-2))) { } else if(preg_match('/ID$/', $fieldName) && $this->has_one(substr($fieldName,0,-2))) {
$val = (isset($this->record[$fieldName])) ? $this->record[$fieldName] : null; $val = (isset($this->record[$fieldName])) ? $this->record[$fieldName] : null;
return DBField::create('ForeignKey', $val, $fieldName, $this); return DBField::create_field('ForeignKey', $val, $fieldName, $this);
// Special case for ClassName // Special case for ClassName
} else if($fieldName == 'ClassName') { } else if($fieldName == 'ClassName') {
$val = get_class($this); $val = get_class($this);
return DBField::create('Varchar', $val, $fieldName, $this); return DBField::create_field('Varchar', $val, $fieldName, $this);
} }
} }

View File

@ -12,24 +12,6 @@ class ManyManyList extends RelationList {
protected $foreignKey, $foreignID; protected $foreignKey, $foreignID;
protected $extraFields; protected $extraFields;
/**
* Synonym of the constructor. Can be chained with literate methods.
* ManyManyList::create("Group","Member","ID", "GroupID")->sort("Title") is legal, but
* new ManyManyList("Group","Member","ID", "GroupID")->sort("Title") is not.
*
* @param string $dataClass The class of the DataObjects that this will list.
* @param string $joinTable The name of the table whose entries define the content of this many_many relation.
* @param string $localKey The key in the join table that maps to the dataClass' PK.
* @param string $foreignKey The key in the join table that maps to joined class' PK.
* @param string $extraFields A map of field => fieldtype of extra fields on the join table.
*
* @see ManyManyList::__construct();
* @example ManyManyList::create('Group','Group_Members', 'GroupID', 'MemberID');
*/
public static function create($dataClass, $joinTable, $localKey, $foreignKey, $extraFields = array()) {
return new ManyManyList($dataClass, $joinTable, $localKey, $foreignKey, $extraFields = array());
}
/** /**
* Create a new ManyManyList object. * Create a new ManyManyList object.

View File

@ -25,13 +25,13 @@
* if($this->getStreetName()) { * if($this->getStreetName()) {
* $manipulation['fields']["{$this->name}Name"] = $this->prepValueForDB($this->getStreetName()); * $manipulation['fields']["{$this->name}Name"] = $this->prepValueForDB($this->getStreetName());
* } else { * } else {
* $manipulation['fields']["{$this->name}Name"] = DBField::create('Varchar', $this->getStreetName())->nullValue(); * $manipulation['fields']["{$this->name}Name"] = DBField::create_field('Varchar', $this->getStreetName())->nullValue();
* } * }
* *
* if($this->getStreetNumber()) { * if($this->getStreetNumber()) {
* $manipulation['fields']["{$this->name}Number"] = $this->prepValueForDB($this->getStreetNumber()); * $manipulation['fields']["{$this->name}Number"] = $this->prepValueForDB($this->getStreetNumber());
* } else { * } else {
* $manipulation['fields']["{$this->name}Number"] = DBField::create('Int', $this->getStreetNumber())->nullValue(); * $manipulation['fields']["{$this->name}Number"] = DBField::create_field('Int', $this->getStreetNumber())->nullValue();
* } * }
* } * }
* *

View File

@ -63,11 +63,18 @@ abstract class DBField extends ViewableData {
parent::__construct(); parent::__construct();
} }
static function create() {
Deprecation::notice('3.0', 'DBField::create_field() is deprecated as it clashes with Object::create(). Use DBField::create_field() instead.');
return call_user_func_array(array('DBField', 'create_field'), func_get_args());
}
/** /**
* Create a DBField object that's not bound to any particular field. * Create a DBField object that's not bound to any particular field.
* Useful for accessing the classes behaviour for other parts of your code. * Useful for accessing the classes behaviour for other parts of your code.
*/ */
static function create($className, $value, $name = null, $object = null) { static function create_field($className, $value, $name = null, $object = null) {
$dbField = Object::create($className, $name, $object); $dbField = Object::create($className, $name, $object);
$dbField->setValue($value, null, false); $dbField->setValue($value, null, false);
return $dbField; return $dbField;

View File

@ -96,7 +96,7 @@ class SS_Datetime extends Date {
if(self::$mock_now) { if(self::$mock_now) {
return self::$mock_now; return self::$mock_now;
} else { } else {
return DBField::create('SS_Datetime', date('Y-m-d H:i:s')); return DBField::create_field('SS_Datetime', date('Y-m-d H:i:s'));
} }
} }
@ -111,7 +111,7 @@ class SS_Datetime extends Date {
if($datetime instanceof SS_Datetime) { if($datetime instanceof SS_Datetime) {
self::$mock_now = $datetime; self::$mock_now = $datetime;
} elseif(is_string($datetime)) { } elseif(is_string($datetime)) {
self::$mock_now = DBField::create('SS_Datetime', $datetime); self::$mock_now = DBField::create_field('SS_Datetime', $datetime);
} else { } else {
throw new Exception('SS_Datetime::set_mock_now(): Wrong format: ' . $datetime); throw new Exception('SS_Datetime::set_mock_now(): Wrong format: ' . $datetime);
} }

View File

@ -84,13 +84,13 @@ class Money extends DBField implements CompositeDBField {
if($this->getCurrency()) { if($this->getCurrency()) {
$manipulation['fields'][$this->name.'Currency'] = $this->prepValueForDB($this->getCurrency()); $manipulation['fields'][$this->name.'Currency'] = $this->prepValueForDB($this->getCurrency());
} else { } else {
$manipulation['fields'][$this->name.'Currency'] = DBField::create('Varchar', $this->getCurrency())->nullValue(); $manipulation['fields'][$this->name.'Currency'] = DBField::create_field('Varchar', $this->getCurrency())->nullValue();
} }
if($this->getAmount()) { if($this->getAmount()) {
$manipulation['fields'][$this->name.'Amount'] = $this->getAmount(); $manipulation['fields'][$this->name.'Amount'] = $this->getAmount();
} else { } else {
$manipulation['fields'][$this->name.'Amount'] = DBField::create('Decimal', $this->getAmount())->nullValue(); $manipulation['fields'][$this->name.'Amount'] = DBField::create_field('Decimal', $this->getAmount())->nullValue();
} }
} }

View File

@ -544,7 +544,7 @@ class RestfulServerTest_AuthorRating extends DataObject implements TestOnly {
static $db = array( static $db = array(
'Rating' => 'Int', 'Rating' => 'Int',
'SecretField' => 'Text', 'SecretField' => 'Text',
'WriteProtectedField' => 'Text' 'WriteProtectedField' => 'Text',
); );
static $has_one = array( static $has_one = array(

View File

@ -119,6 +119,11 @@ class ObjectTest extends SapphireTest {
$strongObj = Object::strong_create('ObjectTest_CreateTest', 'arg1', 'arg2', array(), null, 'arg5'); $strongObj = Object::strong_create('ObjectTest_CreateTest', 'arg1', 'arg2', array(), null, 'arg5');
$this->assertEquals($strongObj->constructArguments, array('arg1', 'arg2', array(), null, 'arg5')); $this->assertEquals($strongObj->constructArguments, array('arg1', 'arg2', array(), null, 'arg5'));
} }
public function testCreateLateStaticBinding() {
$createdObj = ObjectTest_CreateTest::create('arg1', 'arg2', array(), null, 'arg5');
$this->assertEquals($createdObj->constructArguments, array('arg1', 'arg2', array(), null, 'arg5'));
}
/** /**
* Tests that {@link Object::useCustomClass()} correnctly replaces normal and strong objects * Tests that {@link Object::useCustomClass()} correnctly replaces normal and strong objects

View File

@ -131,7 +131,7 @@ class CsvBulkLoaderTest extends SapphireTest {
$this->assertEquals($testPlayer->ContractID, $testContract->ID, 'Creating new has_one relation works'); $this->assertEquals($testPlayer->ContractID, $testContract->ID, 'Creating new has_one relation works');
// Test nested setting of relation properties // Test nested setting of relation properties
$contractAmount = DBField::create('Currency', $compareRow[5])->RAW(); $contractAmount = DBField::create_field('Currency', $compareRow[5])->RAW();
$this->assertEquals($testPlayer->Contract()->Amount, $contractAmount, 'Setting nested values in a relation works'); $this->assertEquals($testPlayer->Contract()->Amount, $contractAmount, 'Setting nested values in a relation works');
fclose($file); fclose($file);

View File

@ -202,7 +202,7 @@ class DBFieldTest extends SapphireTest {
$value = 'üåäöÜÅÄÖ'; $value = 'üåäöÜÅÄÖ';
foreach ($allFields as $stringField) { foreach ($allFields as $stringField) {
$stringField = DBField::create($stringField, $value); $stringField = DBField::create_field($stringField, $value);
for ($i = 1; $i < mb_strlen($value); $i++) { for ($i = 1; $i < mb_strlen($value); $i++) {
$expected = mb_substr($value, 0, $i) . '...'; $expected = mb_substr($value, 0, $i) . '...';
$this->assertEquals($expected, $stringField->LimitCharacters($i)); $this->assertEquals($expected, $stringField->LimitCharacters($i));
@ -211,12 +211,12 @@ class DBFieldTest extends SapphireTest {
$value = '<p>üåäö&amp;ÜÅÄÖ</p>'; $value = '<p>üåäö&amp;ÜÅÄÖ</p>';
foreach ($htmlFields as $stringField) { foreach ($htmlFields as $stringField) {
$stringField = DBField::create($stringField, $value); $stringField = DBField::create_field($stringField, $value);
$this->assertEquals('üåäö&amp;ÜÅÄ...', $stringField->LimitCharacters(8)); $this->assertEquals('üåäö&amp;ÜÅÄ...', $stringField->LimitCharacters(8));
} }
$this->assertEquals('ÅÄÖ', DBField::create('Text', 'åäö')->UpperCase()); $this->assertEquals('ÅÄÖ', DBField::create_field('Text', 'åäö')->UpperCase());
$this->assertEquals('åäö', DBField::create('Text', 'ÅÄÖ')->LowerCase()); $this->assertEquals('åäö', DBField::create_field('Text', 'ÅÄÖ')->LowerCase());
} }
} }

View File

@ -5,17 +5,17 @@
*/ */
class DBLocaleTest extends SapphireTest { class DBLocaleTest extends SapphireTest {
function testNice() { function testNice() {
$l = DBField::create('DBLocale', 'de_DE'); $l = DBField::create_field('DBLocale', 'de_DE');
$this->assertEquals($l->Nice(), 'German'); $this->assertEquals($l->Nice(), 'German');
} }
function testNiceNative() { function testNiceNative() {
$l = DBField::create('DBLocale', 'de_DE'); $l = DBField::create_field('DBLocale', 'de_DE');
$this->assertEquals($l->Nice(true), 'Deutsch'); $this->assertEquals($l->Nice(true), 'Deutsch');
} }
function testNativeName() { function testNativeName() {
$l = DBField::create('DBLocale', 'de_DE'); $l = DBField::create_field('DBLocale', 'de_DE');
$this->assertEquals($l->getNativeName(), 'Deutsch'); $this->assertEquals($l->getNativeName(), 'Deutsch');
} }
} }

View File

@ -20,96 +20,96 @@ class DateTest extends SapphireTest {
} }
function testNiceDate() { function testNiceDate() {
$this->assertEquals('31/03/2008', DBField::create('Date', 1206968400)->Nice(), $this->assertEquals('31/03/2008', DBField::create_field('Date', 1206968400)->Nice(),
"Date->Nice() works with timestamp integers" "Date->Nice() works with timestamp integers"
); );
$this->assertEquals('30/03/2008', DBField::create('Date', 1206882000)->Nice(), $this->assertEquals('30/03/2008', DBField::create_field('Date', 1206882000)->Nice(),
"Date->Nice() works with timestamp integers" "Date->Nice() works with timestamp integers"
); );
$this->assertEquals('31/03/2008', DBField::create('Date', '1206968400')->Nice(), $this->assertEquals('31/03/2008', DBField::create_field('Date', '1206968400')->Nice(),
"Date->Nice() works with timestamp strings" "Date->Nice() works with timestamp strings"
); );
$this->assertEquals('30/03/2008', DBField::create('Date', '1206882000')->Nice(), $this->assertEquals('30/03/2008', DBField::create_field('Date', '1206882000')->Nice(),
"Date->Nice() works with timestamp strings" "Date->Nice() works with timestamp strings"
); );
$this->assertEquals('04/03/2003', DBField::create('Date', '4/3/03')->Nice(), $this->assertEquals('04/03/2003', DBField::create_field('Date', '4/3/03')->Nice(),
"Date->Nice() works with D/M/YY format" "Date->Nice() works with D/M/YY format"
); );
$this->assertEquals('04/03/2003', DBField::create('Date', '04/03/03')->Nice(), $this->assertEquals('04/03/2003', DBField::create_field('Date', '04/03/03')->Nice(),
"Date->Nice() works with DD/MM/YY format" "Date->Nice() works with DD/MM/YY format"
); );
$this->assertEquals('04/03/2003', DBField::create('Date', '4/3/03')->Nice(), $this->assertEquals('04/03/2003', DBField::create_field('Date', '4/3/03')->Nice(),
"Date->Nice() works with D/M/YY format" "Date->Nice() works with D/M/YY format"
); );
$this->assertEquals('04/03/2003', DBField::create('Date', '4/03/03')->Nice(), $this->assertEquals('04/03/2003', DBField::create_field('Date', '4/03/03')->Nice(),
"Date->Nice() works with D/M/YY format" "Date->Nice() works with D/M/YY format"
); );
$this->assertEquals('04/03/2003', DBField::create('Date', '4/3/2003')->Nice(), $this->assertEquals('04/03/2003', DBField::create_field('Date', '4/3/2003')->Nice(),
"Date->Nice() works with D/M/YYYY format" "Date->Nice() works with D/M/YYYY format"
); );
$this->assertEquals('04/03/2003', DBField::create('Date', '4-3-2003')->Nice(), $this->assertEquals('04/03/2003', DBField::create_field('Date', '4-3-2003')->Nice(),
"Date->Nice() works with D-M-YYYY format" "Date->Nice() works with D-M-YYYY format"
); );
$this->assertEquals('04/03/2003', DBField::create('Date', '2003-03-04')->Nice(), $this->assertEquals('04/03/2003', DBField::create_field('Date', '2003-03-04')->Nice(),
"Date->Nice() works with YYYY-MM-DD format" "Date->Nice() works with YYYY-MM-DD format"
); );
$this->assertEquals('04/03/2003', DBField::create('Date', '04/03/2003')->Nice(), $this->assertEquals('04/03/2003', DBField::create_field('Date', '04/03/2003')->Nice(),
"Date->Nice() works with DD/MM/YYYY format" "Date->Nice() works with DD/MM/YYYY format"
); );
$this->assertEquals('04/03/2003', DBField::create('Date', '04-03-2003')->Nice(), $this->assertEquals('04/03/2003', DBField::create_field('Date', '04-03-2003')->Nice(),
"Date->Nice() works with DD/MM/YYYY format" "Date->Nice() works with DD/MM/YYYY format"
); );
} }
function testLongDate() { function testLongDate() {
$this->assertEquals('31 March 2008', DBField::create('Date', 1206968400)->Long(), $this->assertEquals('31 March 2008', DBField::create_field('Date', 1206968400)->Long(),
"Date->Long() works with numeric timestamp" "Date->Long() works with numeric timestamp"
); );
$this->assertEquals('31 March 2008', DBField::create('Date', '1206968400')->Long(), $this->assertEquals('31 March 2008', DBField::create_field('Date', '1206968400')->Long(),
"Date->Long() works with string timestamp" "Date->Long() works with string timestamp"
); );
$this->assertEquals('30 March 2008', DBField::create('Date', 1206882000)->Long(), $this->assertEquals('30 March 2008', DBField::create_field('Date', 1206882000)->Long(),
"Date->Long() works with numeric timestamp" "Date->Long() works with numeric timestamp"
); );
$this->assertEquals('30 March 2008', DBField::create('Date', '1206882000')->Long(), $this->assertEquals('30 March 2008', DBField::create_field('Date', '1206882000')->Long(),
"Date->Long() works with numeric timestamp" "Date->Long() works with numeric timestamp"
); );
$this->assertEquals('3 April 2003', DBField::create('Date', '2003-4-3')->Long(), $this->assertEquals('3 April 2003', DBField::create_field('Date', '2003-4-3')->Long(),
"Date->Long() works with YYYY-M-D" "Date->Long() works with YYYY-M-D"
); );
$this->assertEquals('3 April 2003', DBField::create('Date', '3/4/2003')->Long(), $this->assertEquals('3 April 2003', DBField::create_field('Date', '3/4/2003')->Long(),
"Date->Long() works with D/M/YYYY" "Date->Long() works with D/M/YYYY"
); );
} }
function testSetNullAndZeroValues() { function testSetNullAndZeroValues() {
$date = DBField::create('Date', ''); $date = DBField::create_field('Date', '');
$this->assertNull($date->getValue(), 'Empty string evaluates to NULL'); $this->assertNull($date->getValue(), 'Empty string evaluates to NULL');
$date = DBField::create('Date', null); $date = DBField::create_field('Date', null);
$this->assertNull($date->getValue(), 'NULL is set as NULL'); $this->assertNull($date->getValue(), 'NULL is set as NULL');
$date = DBField::create('Date', false); $date = DBField::create_field('Date', false);
$this->assertNull($date->getValue(), 'Boolean FALSE evaluates to NULL'); $this->assertNull($date->getValue(), 'Boolean FALSE evaluates to NULL');
$date = DBField::create('Date', array()); $date = DBField::create_field('Date', array());
$this->assertNull($date->getValue(), 'Empty array evaluates to NULL'); $this->assertNull($date->getValue(), 'Empty array evaluates to NULL');
$date = DBField::create('Date', '0'); $date = DBField::create_field('Date', '0');
$this->assertEquals('1970-01-01', $date->getValue(), 'Zero is UNIX epoch date'); $this->assertEquals('1970-01-01', $date->getValue(), 'Zero is UNIX epoch date');
$date = DBField::create('Date', 0); $date = DBField::create_field('Date', 0);
$this->assertEquals('1970-01-01', $date->getValue(), 'Zero is UNIX epoch date'); $this->assertEquals('1970-01-01', $date->getValue(), 'Zero is UNIX epoch date');
} }
function testDayOfMonth() { function testDayOfMonth() {
$date = DBField::create('Date', '2000-10-10'); $date = DBField::create_field('Date', '2000-10-10');
$this->assertEquals('10', $date->DayOfMonth()); $this->assertEquals('10', $date->DayOfMonth());
$this->assertEquals('10th', $date->DayOfMonth(true)); $this->assertEquals('10th', $date->DayOfMonth(true));
$range = $date->RangeString(DBField::create('Date', '2000-10-20')); $range = $date->RangeString(DBField::create_field('Date', '2000-10-20'));
$this->assertEquals('10 - 20 Oct 2000', $range); $this->assertEquals('10 - 20 Oct 2000', $range);
$range = $date->RangeString(DBField::create('Date', '2000-10-20'), true); $range = $date->RangeString(DBField::create_field('Date', '2000-10-20'), true);
$this->assertEquals('10th - 20th Oct 2000', $range); $this->assertEquals('10th - 20th Oct 2000', $range);
} }
} }

View File

@ -12,7 +12,7 @@
*/ */
class SS_DatetimeTest extends SapphireTest { class SS_DatetimeTest extends SapphireTest {
function testNowWithSystemDate() { function testNowWithSystemDate() {
$systemDatetime = DBField::create('SS_Datetime', date('Y-m-d H:i:s')); $systemDatetime = DBField::create_field('SS_Datetime', date('Y-m-d H:i:s'));
$nowDatetime = SS_Datetime::now(); $nowDatetime = SS_Datetime::now();
$this->assertEquals($systemDatetime->Date(), $nowDatetime->Date()); $this->assertEquals($systemDatetime->Date(), $nowDatetime->Date());
@ -22,32 +22,32 @@ class SS_DatetimeTest extends SapphireTest {
// Test setting // Test setting
$mockDate = '2001-12-31 22:10:59'; $mockDate = '2001-12-31 22:10:59';
SS_Datetime::set_mock_now($mockDate); SS_Datetime::set_mock_now($mockDate);
$systemDatetime = DBField::create('SS_Datetime', date('Y-m-d H:i:s')); $systemDatetime = DBField::create_field('SS_Datetime', date('Y-m-d H:i:s'));
$nowDatetime = SS_Datetime::now(); $nowDatetime = SS_Datetime::now();
$this->assertNotEquals($systemDatetime->Date(), $nowDatetime->Date()); $this->assertNotEquals($systemDatetime->Date(), $nowDatetime->Date());
$this->assertEquals($nowDatetime->getValue(), $mockDate); $this->assertEquals($nowDatetime->getValue(), $mockDate);
// Test clearing // Test clearing
SS_Datetime::clear_mock_now(); SS_Datetime::clear_mock_now();
$systemDatetime = DBField::create('SS_Datetime', date('Y-m-d H:i:s')); $systemDatetime = DBField::create_field('SS_Datetime', date('Y-m-d H:i:s'));
$nowDatetime = SS_Datetime::now(); $nowDatetime = SS_Datetime::now();
$this->assertEquals($systemDatetime->Date(), $nowDatetime->Date()); $this->assertEquals($systemDatetime->Date(), $nowDatetime->Date());
} }
function testSetNullAndZeroValues() { function testSetNullAndZeroValues() {
$date = DBField::create('SS_Datetime', ''); $date = DBField::create_field('SS_Datetime', '');
$this->assertNull($date->getValue(), 'Empty string evaluates to NULL'); $this->assertNull($date->getValue(), 'Empty string evaluates to NULL');
$date = DBField::create('SS_Datetime', null); $date = DBField::create_field('SS_Datetime', null);
$this->assertNull($date->getValue(), 'NULL is set as NULL'); $this->assertNull($date->getValue(), 'NULL is set as NULL');
$date = DBField::create('SS_Datetime', false); $date = DBField::create_field('SS_Datetime', false);
$this->assertNull($date->getValue(), 'Boolean FALSE evaluates to NULL'); $this->assertNull($date->getValue(), 'Boolean FALSE evaluates to NULL');
$date = DBField::create('SS_Datetime', '0'); $date = DBField::create_field('SS_Datetime', '0');
$this->assertEquals('1970-01-01 00:00:00', $date->getValue(), 'String zero is UNIX epoch time'); $this->assertEquals('1970-01-01 00:00:00', $date->getValue(), 'String zero is UNIX epoch time');
$date = DBField::create('SS_Datetime', 0); $date = DBField::create_field('SS_Datetime', 0);
$this->assertEquals('1970-01-01 00:00:00', $date->getValue(), 'Numeric zero is UNIX epoch time'); $this->assertEquals('1970-01-01 00:00:00', $date->getValue(), 'Numeric zero is UNIX epoch time');
} }

View File

@ -108,30 +108,30 @@ class HTMLTextTest extends SapphireTest {
} }
public function testRAW() { public function testRAW() {
$data = DBField::create('HTMLText', 'This &amp; This'); $data = DBField::create_field('HTMLText', 'This &amp; This');
$this->assertEquals($data->RAW(), 'This &amp; This'); $this->assertEquals($data->RAW(), 'This &amp; This');
$data = DBField::create('HTMLText', 'This & This'); $data = DBField::create_field('HTMLText', 'This & This');
$this->assertEquals($data->RAW(), 'This & This'); $this->assertEquals($data->RAW(), 'This & This');
} }
public function testXML() { public function testXML() {
$data = DBField::create('HTMLText', 'This & This'); $data = DBField::create_field('HTMLText', 'This & This');
$this->assertEquals($data->XML(), 'This &amp; This'); $this->assertEquals($data->XML(), 'This &amp; This');
} }
public function testHTML() { public function testHTML() {
$data = DBField::create('HTMLText', 'This & This'); $data = DBField::create_field('HTMLText', 'This & This');
$this->assertEquals($data->HTML(), 'This &amp; This'); $this->assertEquals($data->HTML(), 'This &amp; This');
} }
public function testJS() { public function testJS() {
$data = DBField::create('HTMLText', '"this is a test"'); $data = DBField::create_field('HTMLText', '"this is a test"');
$this->assertEquals($data->JS(), '\"this is a test\"'); $this->assertEquals($data->JS(), '\"this is a test\"');
} }
public function testATT() { public function testATT() {
$data = DBField::create('HTMLText', '"this is a test"'); $data = DBField::create_field('HTMLText', '"this is a test"');
$this->assertEquals($data->ATT(), '&quot;this is a test&quot;'); $this->assertEquals($data->ATT(), '&quot;this is a test&quot;');
} }
} }

View File

@ -12,7 +12,7 @@ class StringFieldTest extends SapphireTest {
function testLowerCase() { function testLowerCase() {
$this->assertEquals( $this->assertEquals(
'this is a test!', 'this is a test!',
DBField::create('StringFieldTest_MyStringField', 'This is a TEST!')->LowerCase() DBField::create_field('StringFieldTest_MyStringField', 'This is a TEST!')->LowerCase()
); );
} }
@ -22,7 +22,7 @@ class StringFieldTest extends SapphireTest {
function testUpperCase() { function testUpperCase() {
$this->assertEquals( $this->assertEquals(
'THIS IS A TEST!', 'THIS IS A TEST!',
DBField::create('StringFieldTest_MyStringField', 'This is a TEST!')->UpperCase() DBField::create_field('StringFieldTest_MyStringField', 'This is a TEST!')->UpperCase()
); );
} }

View File

@ -99,7 +99,7 @@ class TextTest extends SapphireTest {
); );
foreach($cases as $originalValue => $expectedValue) { foreach($cases as $originalValue => $expectedValue) {
$textObj = DBField::create('Text', $originalValue); $textObj = DBField::create_field('Text', $originalValue);
$this->assertEquals($expectedValue, $textObj->BigSummary(4)); $this->assertEquals($expectedValue, $textObj->BigSummary(4));
} }
} }
@ -115,7 +115,7 @@ class TextTest extends SapphireTest {
$testKeyword3 = 'a'; $testKeyword3 = 'a';
$testKeyword3a = 'ate'; $testKeyword3a = 'ate';
$textObj = DBField::create('Text', $testString1, 'Text'); $textObj = DBField::create_field('Text', $testString1, 'Text');
$this->assertEquals( $this->assertEquals(
'... text. It is a <span class="highlight">test</span>...', '... text. It is a <span class="highlight">test</span>...',
@ -145,27 +145,27 @@ class TextTest extends SapphireTest {
} }
public function testRAW() { public function testRAW() {
$data = DBField::create('Text', 'This &amp; This'); $data = DBField::create_field('Text', 'This &amp; This');
$this->assertEquals($data->RAW(), 'This &amp; This'); $this->assertEquals($data->RAW(), 'This &amp; This');
} }
public function testXML() { public function testXML() {
$data = DBField::create('Text', 'This & This'); $data = DBField::create_field('Text', 'This & This');
$this->assertEquals($data->XML(), 'This &amp; This'); $this->assertEquals($data->XML(), 'This &amp; This');
} }
public function testHTML() { public function testHTML() {
$data = DBField::create('Text', 'This & This'); $data = DBField::create_field('Text', 'This & This');
$this->assertEquals($data->HTML(), 'This &amp; This'); $this->assertEquals($data->HTML(), 'This &amp; This');
} }
public function testJS() { public function testJS() {
$data = DBField::create('Text', '"this is a test"'); $data = DBField::create_field('Text', '"this is a test"');
$this->assertEquals($data->JS(), '\"this is a test\"'); $this->assertEquals($data->JS(), '\"this is a test\"');
} }
public function testATT() { public function testATT() {
$data = DBField::create('Text', '"this is a test"'); $data = DBField::create_field('Text', '"this is a test"');
$this->assertEquals($data->ATT(), '&quot;this is a test&quot;'); $this->assertEquals($data->ATT(), '&quot;this is a test&quot;');
} }
} }