Skip to content

Fix a warning when DateTime column has empty value #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/DataIntegrity.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public static function coerceValueToColumn(
$value .= ' 00:00:00';
}

if ($value[0] === '-' || $value === '') {
if ($value === '' || $value[0] === '-') {
$value = '0000-00-00 00:00:00';
} elseif (\preg_match(
'/^([0-9]{2,4}-[0-1][0-9]-[0-3][0-9]|[0-9]+)$/',
Expand Down
32 changes: 32 additions & 0 deletions tests/DataIntegrityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Vimeo\MysqlEngine\Tests;

use PHPUnit\Framework\TestCase;
use Vimeo\MysqlEngine\DataIntegrity;
use Vimeo\MysqlEngine\FakePdoInterface;
use Vimeo\MysqlEngine\Php7\FakePdo;
use Vimeo\MysqlEngine\Schema\Column\DateTime;
use const PHP_MAJOR_VERSION;

class DataIntegrityTest extends TestCase
{

public function testEmptyDateTimeColumnShouldReturnDefaultDate()
{
$dateTimeColumn = new DateTime();

$result = DataIntegrity::coerceValueToColumn($this->getPdo(), $dateTimeColumn, '');

$this->assertEquals('0000-00-00 00:00:00', $result);
}

private static function getPdo(): FakePdoInterface
{
if (PHP_MAJOR_VERSION === 8) {
return new \Vimeo\MysqlEngine\Php8\FakePdo('mysql:foo;dbname=test;');
}

return new FakePdo('mysql:foo;dbname=test;');
}
}