Skip to content

Commit 0870923

Browse files
authored
Merge pull request #24 from xinhus/fix-empty-date-column-warning
Fix a warning when DateTime column has empty value
2 parents ebf7a97 + 70e4629 commit 0870923

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/DataIntegrity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public static function coerceValueToColumn(
215215
$value .= ' 00:00:00';
216216
}
217217

218-
if ($value[0] === '-' || $value === '') {
218+
if ($value === '' || $value[0] === '-') {
219219
$value = '0000-00-00 00:00:00';
220220
} elseif (\preg_match(
221221
'/^([0-9]{2,4}-[0-1][0-9]-[0-3][0-9]|[0-9]+)$/',

tests/DataIntegrityTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Vimeo\MysqlEngine\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Vimeo\MysqlEngine\DataIntegrity;
7+
use Vimeo\MysqlEngine\FakePdoInterface;
8+
use Vimeo\MysqlEngine\Php7\FakePdo;
9+
use Vimeo\MysqlEngine\Schema\Column\DateTime;
10+
use const PHP_MAJOR_VERSION;
11+
12+
class DataIntegrityTest extends TestCase
13+
{
14+
15+
public function testEmptyDateTimeColumnShouldReturnDefaultDate()
16+
{
17+
$dateTimeColumn = new DateTime();
18+
19+
$result = DataIntegrity::coerceValueToColumn($this->getPdo(), $dateTimeColumn, '');
20+
21+
$this->assertEquals('0000-00-00 00:00:00', $result);
22+
}
23+
24+
private static function getPdo(): FakePdoInterface
25+
{
26+
if (PHP_MAJOR_VERSION === 8) {
27+
return new \Vimeo\MysqlEngine\Php8\FakePdo('mysql:foo;dbname=test;');
28+
}
29+
30+
return new FakePdo('mysql:foo;dbname=test;');
31+
}
32+
}

0 commit comments

Comments
 (0)