Respect semver and add tests

This commit is contained in:
Damian Mooyman 2018-06-15 11:04:12 +12:00
parent c414388220
commit b636587945
No known key found for this signature in database
GPG Key ID: 78B823A10DE27D1A
3 changed files with 31 additions and 7 deletions

View File

@ -37,6 +37,8 @@ class DBDate extends DBField
/**
* Fixed locale to use for ISO date formatting. This is necessary to prevent
* locale-specific numeric localisation breaking internal date strings.
*
* @internal (remove internal in 4.2)
*/
const ISO_LOCALE = 'en_US';
@ -214,6 +216,8 @@ class DBDate extends DBField
/**
* Return formatter in a given locale. Useful if localising in a format other than the current locale.
*
* @internal (Remove internal in 4.2)
*
* @param string|null $locale The current locale, or null to use default
* @param string|null $pattern Custom pattern to use for this, if required
* @param int $dateLength
@ -262,11 +266,14 @@ class DBDate extends DBField
* for the day of the month ("1st", "2nd", "3rd" etc)
*
* @param string $format Format code string. See http://userguide.icu-project.org/formatparse/datetime
* @param string $locale Custom locale to use
* @param string $locale Custom locale to use (add to signature in 5.0)
* @return string The date in the requested format
*/
public function Format($format, $locale = null)
public function Format($format)
{
// Note: soft-arg uses func_get_args() to respect semver. Add to signature in 5.0
$locale = func_num_args() > 1 ? func_get_arg(1) : null;
if (!$this->value) {
return null;
}

View File

@ -2,15 +2,14 @@
namespace SilverStripe\ORM\FieldType;
use Exception;
use IntlDateFormatter;
use InvalidArgumentException;
use SilverStripe\Forms\DatetimeField;
use SilverStripe\i18n\i18n;
use SilverStripe\ORM\DB;
use SilverStripe\Security\Member;
use SilverStripe\Security\Security;
use SilverStripe\View\TemplateGlobalProvider;
use Exception;
use InvalidArgumentException;
/**
* Represents a date-time field.
@ -233,6 +232,8 @@ class DBDatetime extends DBDate implements TemplateGlobalProvider
/**
* Return formatter in a given locale. Useful if localising in a format other than the current locale.
*
* @internal (Remove internal in 4.2)
*
* @param string|null $locale The current locale, or null to use default
* @param string|null $pattern Custom pattern to use for this, if required
* @param int $dateLength

View File

@ -2,10 +2,9 @@
namespace SilverStripe\ORM\Tests;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\i18n\i18n;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Security\Member;
/**
* Tests for {@link Datetime} class.
@ -70,6 +69,23 @@ class DBDatetimeTest extends SapphireTest
$this->assertEquals('10 Oct 3000 15 32 24', $date->Format('d MMM y H m s'));
}
/**
* Coverage for dates using hindi-numerals
*/
public function testHindiNumerals()
{
// Parent locale is english; Can be localised to arabic
$date = DBDatetime::create_field('Datetime', '1600-10-10 15:32:24');
$this->assertEquals('10 Oct 1600 15 32 24', $date->Format('d MMM y H m s'));
$this->assertEquals('١٠ أكتوبر ١٦٠٠ ١٥ ٣٢ ٢٤', $date->Format('d MMM y H m s', 'ar'));
// Parent locale is arabic; Datavalue uses ISO date
i18n::set_locale('ar');
$date = DBDatetime::create_field('Datetime', '1600-10-10 15:32:24');
$this->assertEquals('١٠ أكتوبر ١٦٠٠ ١٥ ٣٢ ٢٤', $date->Format('d MMM y H m s'));
$this->assertEquals('1600-10-10 15:32:24', $date->getValue());
}
public function testNice()
{
$date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59');