silverstripe-framework/tests/php/ORM/DBTimeTest.php
Damian Mooyman 029a8b9586
API Substitute Zend_Currency with NumberFormatter based solution
API Substitute Zend_Locale with Locale / NumberFormatter
API Substitute Zend_Date with IntlDateFormatter
API Added DBTIme::Nice12, FormatFromSettings
API Added Short() method to DBDate / DBTime / DBDatetime
API Add Date::getTimestamp()
API Added setSubmittedValue api for FormField
API Add second arg to base FormField::setValue()
API Major refactor of i18n into component data parts
API Implement Resettable interface to reset objects between tests
ENHANCEMENT Changed DBField::create_field return type to `static` to support better type hinting
ENHANCEMENT i18nTextCollector supports __CLASS__
2017-02-09 15:28:59 +13:00

103 lines
2.5 KiB
PHP

<?php
namespace SilverStripe\ORM\Tests;
use SilverStripe\i18n\i18n;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\FieldType\DBTime;
use SilverStripe\Security\Member;
class DBTimeTest extends SapphireTest
{
public function setUp()
{
parent::setUp();
i18n::set_locale('en_NZ');
}
public function dataTestParse()
{
return [
// Test am-pm conversion
['11:01 pm', '23:01:00'],
['11:01 am', '11:01:00'],
['12:01 pm', '12:01:00'],
['12:01 am', '00:01:00'],
// Test seconds
['11:01.01 pm', '23:01:01'],
['12:01.01', '12:01:01'],
];
}
/**
* @dataProvider dataTestParse
* @param string $input
* @param string $expected
*/
public function testParse($input, $expected)
{
$time = DBField::create_field('Time', $input);
$this->assertEquals(
$expected,
$time->getValue(),
"Date parsed from {$input} should be {$expected}"
);
}
public function testNice()
{
$time = DBTime::create_field('Time', '17:15:55');
$this->assertEquals('5:15:55 PM', $time->Nice());
}
public function testShort()
{
$time = DBTime::create_field('Time', '17:15:55');
$this->assertEquals('5:15 PM', $time->Short());
}
public function testNice12()
{
$time = DBTime::create_field('Time', '17:15:55');
$this->assertEquals('5:15 PM', $time->Nice12());
}
public function testNice24()
{
$time = DBTime::create_field('Time', '17:15:55');
$this->assertEquals('17:15', $time->Nice24());
}
public function dataTestFormatFromSettings()
{
return [
['10:11:01', '10:11:01 (AM)'],
['21:11:01', '9:11:01 (PM)'],
];
}
/**
* @dataProvider dataTestFormatFromSettings
* @param string $from
* @param string $to
*/
public function testFormatFromSettings($from, $to)
{
$member = new Member();
$member->TimeFormat = 'h:mm:ss (a)';
$date = DBTime::create_field('Time', $from);
$this->assertEquals($to, $date->FormatFromSettings($member));
}
/**
* Test that FormatFromSettings without a member defaults to Nice()
*/
public function testFormatFromSettingsEmpty()
{
$date = DBTime::create_field('Time', '10:11:01');
$this->assertEquals('10:11:01 AM', $date->FormatFromSettings());
}
}