86 lines
2.1 KiB
PHP
Raw Normal View History

2021-07-06 16:43:54 +12:00
<?php
namespace SilverStripe\ORM\Tests;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\FieldType\DBInt;
2024-10-17 17:41:36 +13:00
use PHPUnit\Framework\Attributes\DataProvider;
2021-07-06 16:43:54 +12:00
class DBIntTest extends SapphireTest
{
2024-10-17 17:41:36 +13:00
public function testDefaultValue(): void
2021-07-06 16:43:54 +12:00
{
2024-10-17 17:41:36 +13:00
$field = new DBInt('MyField');
$this->assertSame(0, $field->getValue());
}
public static function provideSetValue(): array
{
return [
'int' => [
'value' => 3,
'expected' => 3,
],
'string-int' => [
'value' => '3',
'expected' => 3,
],
'negative-int' => [
'value' => -3,
'expected' => -3,
],
'negative-string-int' => [
'value' => '-3',
'expected' => -3,
],
'float' => [
'value' => 3.5,
'expected' => 3.5,
],
'string' => [
'value' => 'fish',
'expected' => 'fish',
],
'array' => [
'value' => [],
'expected' => [],
],
'null' => [
'value' => null,
'expected' => null,
],
];
}
#[DataProvider('provideSetValue')]
public function testSetValue(mixed $value, mixed $expected): void
{
$field = new DBInt('MyField');
$field->setValue($value);
$this->assertSame($expected, $field->getValue());
}
public static function provideValidate(): array
{
return [
'valid' => [
'value' => 123,
'expected' => true,
],
'invalid' => [
'value' => 'abc',
'expected' => false,
],
];
}
#[DataProvider('provideValidate')]
public function testValidate(mixed $value, bool $expected): void
{
$field = new DBInt('MyField');
$field->setValue($value);
$result = $field->validate();
$this->assertSame($expected, $result->isValid());
2021-07-06 16:43:54 +12:00
}
}