Skip to content

Commit

Permalink
result: date time is converted as DateTimeImmutable instance (BC BREAK!)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Jan 1, 2017
1 parent a607482 commit fa44614
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/Result/Result.php
Expand Up @@ -12,7 +12,7 @@
use Nextras\Dbal\Drivers\IDriver;
use Nextras\Dbal\Drivers\IResultAdapter;
use Nextras\Dbal\InvalidArgumentException;
use Nextras\Dbal\Utils\DateTime;
use Nextras\Dbal\Utils\DateTimeImmutable;


class Result implements \SeekableIterator
Expand Down Expand Up @@ -226,7 +226,7 @@ protected function normalize(array $data): array

foreach ($this->toDateTimeColumns as $column) {
if ($data[$column] !== NULL) {
$data[$column] = (new DateTime($data[$column]))->setTimezone($this->applicationTimeZone);
$data[$column] = (new DateTimeImmutable($data[$column]))->setTimezone($this->applicationTimeZone);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Utils/DateTime.php → src/Utils/DateTimeImmutable.php
Expand Up @@ -9,7 +9,7 @@
namespace Nextras\Dbal\Utils;


class DateTime extends \DateTime
class DateTimeImmutable extends \DateTimeImmutable
{
public function __toString()
{
Expand All @@ -20,7 +20,7 @@ public function __toString()
public function setTimestamp($timestamp)
{
$zone = $this->getTimezone();
$this->__construct('@' . $timestamp);
return $this->setTimeZone($zone);
$datetime = new static('@' . $timestamp);
return $datetime->setTimezone($zone);
}
}
14 changes: 7 additions & 7 deletions tests/cases/integration/datetime.mysql.phpt
Expand Up @@ -8,7 +8,7 @@
namespace NextrasTests\Dbal;

use DateTime;
use DateTimeImmutable;
use Nextras\Dbal\Utils\DateTimeImmutable;
use Tester\Assert;
use Tester\Environment;

Expand Down Expand Up @@ -85,8 +85,8 @@ class DateTimeMysqlTest extends IntegrationTestCase

$connection->query(
'INSERT INTO dates_write2 VALUES (%dts, %dt)',
new DateTimeImmutable('2015-01-01 12:00:00'), // 11:00 UTC
new DateTimeImmutable('2015-01-01 12:00:00') // 11:00 UTC
new \DateTimeImmutable('2015-01-01 12:00:00'), // 11:00 UTC
new \DateTimeImmutable('2015-01-01 12:00:00') // 11:00 UTC
);

$result = $connection->query('SELECT * FROM dates_write2');
Expand All @@ -100,8 +100,8 @@ class DateTimeMysqlTest extends IntegrationTestCase
$connection->query('DELETE FROM dates_write2');
$connection->query(
'INSERT INTO dates_write2 VALUES (%dts, %dt)',
new DateTimeImmutable('2015-01-01 12:00:00'), // 11:00 UTC
new DateTimeImmutable('2015-01-01 12:00:00 Europe/Kiev') // 10:00 UTC
new \DateTimeImmutable('2015-01-01 12:00:00'), // 11:00 UTC
new \DateTimeImmutable('2015-01-01 12:00:00 Europe/Kiev') // 10:00 UTC
);

$result = $connection->query('SELECT * FROM dates_write2');
Expand Down Expand Up @@ -138,7 +138,7 @@ class DateTimeMysqlTest extends IntegrationTestCase
$result = $connection->query('SELECT * FROM dates_read');

$row = $result->fetch();
Assert::type('Nextras\Dbal\Utils\DateTime', $row->a);
Assert::type(DateTimeImmutable::class, $row->a);
Assert::same('2015-01-01T14:00:00+02:00', $row->a->format('c'));
Assert::same('2015-01-01T13:00:00+02:00', $row->b->format('c'));
}
Expand Down Expand Up @@ -169,7 +169,7 @@ class DateTimeMysqlTest extends IntegrationTestCase
$result = $connection->query('SELECT * FROM dates_read2');

$row = $result->fetch();
Assert::type('Nextras\Dbal\Utils\DateTime', $row->a);
Assert::type(DateTimeImmutable::class, $row->a);
Assert::same('2015-01-01T13:00:00+02:00', $row->a->format('c'));
Assert::same('2015-01-01T13:00:00+02:00', $row->b->format('c'));
}
Expand Down
14 changes: 7 additions & 7 deletions tests/cases/integration/datetime.pgsql.phpt
Expand Up @@ -8,7 +8,7 @@
namespace NextrasTests\Dbal;

use DateTime;
use DateTimeImmutable;
use Nextras\Dbal\Utils\DateTimeImmutable;
use Tester\Assert;
use Tester\Environment;

Expand Down Expand Up @@ -85,8 +85,8 @@ class DateTimePostgreTest extends IntegrationTestCase

$connection->query(
'INSERT INTO dates_write2 VALUES (%dts, %dt)',
new DateTimeImmutable('2015-01-01 12:00:00'), // 11:00 UTC
new DateTimeImmutable('2015-01-01 12:00:00') // 11:00 UTC
new \DateTimeImmutable('2015-01-01 12:00:00'), // 11:00 UTC
new \DateTimeImmutable('2015-01-01 12:00:00') // 11:00 UTC
);

$result = $connection->query('SELECT * FROM dates_write2');
Expand All @@ -100,8 +100,8 @@ class DateTimePostgreTest extends IntegrationTestCase
$connection->query('DELETE FROM dates_write2');
$connection->query(
'INSERT INTO dates_write2 VALUES (%dts, %dt)',
new DateTimeImmutable('2015-01-01 12:00:00'), // 11:00 UTC
new DateTimeImmutable('2015-01-01 12:00:00 Europe/Kiev') // 10:00 UTC
new \DateTimeImmutable('2015-01-01 12:00:00'), // 11:00 UTC
new \DateTimeImmutable('2015-01-01 12:00:00 Europe/Kiev') // 10:00 UTC
);

$result = $connection->query('SELECT * FROM dates_write2');
Expand Down Expand Up @@ -138,7 +138,7 @@ class DateTimePostgreTest extends IntegrationTestCase
$result = $connection->query('SELECT * FROM dates_read');

$row = $result->fetch();
Assert::type('Nextras\Dbal\Utils\DateTime', $row->a);
Assert::type(DateTimeImmutable::class, $row->a);
Assert::same('2015-01-01T14:00:00+02:00', $row->a->format('c'));
Assert::same('2015-01-01T13:00:00+02:00', $row->b->format('c'));
}
Expand Down Expand Up @@ -169,7 +169,7 @@ class DateTimePostgreTest extends IntegrationTestCase
$result = $connection->query('SELECT * FROM dates_read2');

$row = $result->fetch();
Assert::type('Nextras\Dbal\Utils\DateTime', $row->a);
Assert::type(DateTimeImmutable::class, $row->a);
Assert::same('2015-01-01T13:00:00+02:00', $row->a->format('c'));
Assert::same('2015-01-01T13:00:00+02:00', $row->b->format('c'));
}
Expand Down
5 changes: 3 additions & 2 deletions tests/cases/integration/result.phpt
Expand Up @@ -8,9 +8,10 @@
namespace NextrasTests\Dbal;

use Nextras\Dbal\InvalidStateException;
use Nextras\Dbal\Utils\DateTime;
use Nextras\Dbal\Utils\DateTimeImmutable;
use Tester\Assert;


require_once __DIR__ . '/../../bootstrap.php';


Expand All @@ -37,7 +38,7 @@ class ResultIntegrationTest extends IntegrationTestCase

Assert::same(1, $follower->tag_id);
Assert::same(1, $follower->author_id);
Assert::type(DateTime::class, $follower->created_at);
Assert::type(DateTimeImmutable::class, $follower->created_at);
Assert::same('2014-01-01 00:10:00', $follower->created_at->format('Y-m-d H:i:s'));


Expand Down
6 changes: 3 additions & 3 deletions tests/cases/unit/ResultTest.phpt
Expand Up @@ -10,7 +10,7 @@ use Nextras\Dbal\Drivers\IResultAdapter;
use Nextras\Dbal\InvalidArgumentException;
use Nextras\Dbal\Result\Result;
use Nextras\Dbal\Result\Row;
use Nextras\Dbal\Utils\DateTime;
use Nextras\Dbal\Utils\DateTimeImmutable;
use Tester\Assert;

require_once __DIR__ . '/../../bootstrap.php';
Expand Down Expand Up @@ -149,12 +149,12 @@ class ResultTest extends TestCase
{
$one = [
'name' => 'jon snow',
'born' => new DateTime('2014-01-01'),
'born' => new DateTimeImmutable('2014-01-01'),
'n' => 10
];
$two = [
'name' => 'oberyn martell',
'born' => new DateTime('2014-01-03'),
'born' => new DateTimeImmutable('2014-01-03'),
'n' => 12,
];
$createResult = function() use ($one, $two) {
Expand Down

0 comments on commit fa44614

Please sign in to comment.