2017-01-26 17:20:08 +13:00
|
|
|
<?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;
|
2024-09-18 13:53:44 +12:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
2017-01-26 17:20:08 +13:00
|
|
|
|
|
|
|
class DBTimeTest extends SapphireTest
|
|
|
|
{
|
2021-10-27 15:39:47 +13:00
|
|
|
protected function setUp(): void
|
2017-01-26 17:20:08 +13:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
i18n::set_locale('en_NZ');
|
|
|
|
}
|
|
|
|
|
2024-10-17 17:41:36 +13:00
|
|
|
public static function provideSetValue()
|
2017-01-26 17:20:08 +13:00
|
|
|
{
|
|
|
|
return [
|
2024-10-17 17:41:36 +13:00
|
|
|
'time-11pm' => [
|
|
|
|
'value' => '11:01 pm',
|
|
|
|
'expected' => '23:01:00'
|
|
|
|
],
|
|
|
|
'time-11am' => [
|
|
|
|
'value' => '11:01 am',
|
|
|
|
'expected' => '11:01:00'
|
|
|
|
],
|
|
|
|
'time-12am' => [
|
|
|
|
'value' => '12:01 am',
|
|
|
|
'expected' => '00:01:00'
|
|
|
|
],
|
|
|
|
'time-12pm' => [
|
|
|
|
'value' => '12:01 pm',
|
|
|
|
'expected' => '12:01:00'
|
|
|
|
],
|
|
|
|
'time-11pm-seconds' => [
|
|
|
|
'value' => '11:01.01 pm',
|
|
|
|
'expected' => '23:01:01'
|
|
|
|
],
|
|
|
|
'time-12-seconds' => [
|
|
|
|
'value' => '12:01.01',
|
|
|
|
'expected' => '12:01:01'
|
|
|
|
],
|
|
|
|
'wrong-format-works' => [
|
|
|
|
'value' => '12.34.56',
|
|
|
|
'expected' => '12:34:56',
|
|
|
|
],
|
|
|
|
'int' => [
|
|
|
|
'value' => 6789,
|
|
|
|
'expected' => '01:53:09'
|
|
|
|
],
|
|
|
|
'int-string' => [
|
|
|
|
'value' => '6789',
|
|
|
|
'expected' => '01:53:09'
|
|
|
|
],
|
|
|
|
'zero-string' => [
|
|
|
|
'value' => '0',
|
|
|
|
'expected' => '00:00:00'
|
|
|
|
],
|
|
|
|
'zero-int' => [
|
|
|
|
'value' => 0,
|
|
|
|
'expected' => '00:00:00'
|
|
|
|
],
|
|
|
|
'blank-string' => [
|
|
|
|
'value' => '',
|
|
|
|
'expected' => ''
|
|
|
|
],
|
|
|
|
'null' => [
|
|
|
|
'value' => null,
|
|
|
|
'expected' => null
|
|
|
|
],
|
|
|
|
'false' => [
|
|
|
|
'value' => false,
|
|
|
|
'expected' => false
|
|
|
|
],
|
|
|
|
'empty-array' => [
|
|
|
|
'value' => [],
|
|
|
|
'expected' => []
|
|
|
|
],
|
2017-01-26 17:20:08 +13:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-10-17 17:41:36 +13:00
|
|
|
#[DataProvider('provideSetValue')]
|
|
|
|
public function testSetValue(mixed $value, mixed $expected)
|
2017-01-26 17:20:08 +13:00
|
|
|
{
|
2024-10-17 17:41:36 +13:00
|
|
|
$field = new DBTime('MyField');
|
|
|
|
$field->setValue($value);
|
|
|
|
$this->assertSame($expected, $field->getValue());
|
2017-01-26 17:20:08 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNice()
|
|
|
|
{
|
|
|
|
$time = DBTime::create_field('Time', '17:15:55');
|
2024-09-30 18:32:17 +13:00
|
|
|
$this->assertMatchesRegularExpression('#5:15:55\hPM#iu', $time->Nice());
|
2017-01-26 17:20:08 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testShort()
|
|
|
|
{
|
|
|
|
$time = DBTime::create_field('Time', '17:15:55');
|
2024-09-30 18:32:17 +13:00
|
|
|
$this->assertMatchesRegularExpression('#5:15\hPM#iu', $time->Short());
|
2017-01-26 17:20:08 +13:00
|
|
|
}
|
|
|
|
}
|