From ffc716b8ce24924bae63f8b42c0efc74f3092606 Mon Sep 17 00:00:00 2001 From: jkavalik Date: Tue, 8 Oct 2024 11:33:43 +0200 Subject: [PATCH] test case for #676 --- .../Entity/entity.compositePK.phpt | 14 +++-- tests/cases/integration/Entity/entity.pk.phpt | 18 ++++++ tests/db/mysql-init.sql | 58 +++++-------------- tests/inc/model/Model.php | 1 + tests/inc/model/timeSeries/TimeSeries.php | 17 ++++++ .../inc/model/timeSeries/TimeSeriesMapper.php | 14 +++++ .../model/timeSeries/TimeSeriesRepository.php | 18 ++++++ .../inc/model/userStatX/UserStatsXMapper.php | 4 +- 8 files changed, 92 insertions(+), 52 deletions(-) create mode 100644 tests/inc/model/timeSeries/TimeSeries.php create mode 100644 tests/inc/model/timeSeries/TimeSeriesMapper.php create mode 100644 tests/inc/model/timeSeries/TimeSeriesRepository.php diff --git a/tests/cases/integration/Entity/entity.compositePK.phpt b/tests/cases/integration/Entity/entity.compositePK.phpt index 2bcaddfe..b76f009a 100644 --- a/tests/cases/integration/Entity/entity.compositePK.phpt +++ b/tests/cases/integration/Entity/entity.compositePK.phpt @@ -9,6 +9,7 @@ namespace NextrasTests\Orm\Integration\Entity; use DateTimeImmutable; +use Nette\Utils\DateTime; use Nextras\Dbal\IConnection; use Nextras\Orm\Exception\InvalidArgumentException; use NextrasTests\Orm\DataTestCase; @@ -17,6 +18,7 @@ use NextrasTests\Orm\User; use NextrasTests\Orm\UserStat; use NextrasTests\Orm\UserStatX; use Tester\Assert; +use Tester\Environment; require_once __DIR__ . '/../../../bootstrap.php'; @@ -55,7 +57,7 @@ class EntityCompositePKTest extends DataTestCase $this->orm->persistAndFlush($userStat); } - public function testCompositePKDateTime2() + public function testCompositePKDateTime2(): void { if ($this->section === Helper::SECTION_MSSQL) { // An explicit value for the identity column in table 'users' can only be specified when a column list is used and IDENTITY_INSERT is ON. @@ -74,7 +76,7 @@ class EntityCompositePKTest extends DataTestCase $this->orm->clear(); - $res = $this->orm->userStatsX->getBy(['date' => '2019-01-01']); + $res = $this->orm->userStatsX->getBy(['date' => new DateTime('2019-01-01')]); Assert::same(100, $res->value); $res->value = 200; @@ -83,13 +85,13 @@ class EntityCompositePKTest extends DataTestCase $this->orm->clear(); - $res = $this->orm->userStatsX->getBy(['date' => '2019-01-01']); + $res = $this->orm->userStatsX->getBy(['date' => new DateTime('2019-01-01')]); Assert::same(200, $res->value); - + Environment::$checkAssertions = false; } - - + + public function testGetBy(): void { diff --git a/tests/cases/integration/Entity/entity.pk.phpt b/tests/cases/integration/Entity/entity.pk.phpt index d474230f..59420839 100644 --- a/tests/cases/integration/Entity/entity.pk.phpt +++ b/tests/cases/integration/Entity/entity.pk.phpt @@ -11,6 +11,7 @@ namespace NextrasTests\Orm\Integration\Entity; use DateTimeImmutable; use NextrasTests\Orm\DataTestCase; use NextrasTests\Orm\Log; +use NextrasTests\Orm\TimeSeries; use Tester\Assert; @@ -32,6 +33,23 @@ class EntityPkTest extends DataTestCase $entry = $this->orm->logs->getById($datetime); Assert::true($entry !== null); } + + public function testDateTimeWithProxyPkUpdate(): void + { + $timeSeries = new TimeSeries(); + $timeSeries->id = $datetime = new DateTimeImmutable('2022-03-06T03:03:03Z'); + $timeSeries->value = 3; + $this->orm->persistAndFlush($timeSeries); + + $this->orm->clear(); + $timeSeries = $this->orm->timeSeries->getById($datetime); + $timeSeries = $this->orm->timeSeries->getById($datetime); + $timeSeries->value = 5; + $this->orm->persistAndFlush($timeSeries); + + $entry = $this->orm->timeSeries->getById($datetime); + Assert::true($entry !== null); + } } diff --git a/tests/db/mysql-init.sql b/tests/db/mysql-init.sql index 3b944863..3f793efe 100644 --- a/tests/db/mysql-init.sql +++ b/tests/db/mysql-init.sql @@ -120,23 +120,6 @@ CREATE TABLE photos CONSTRAINT photos_album_id FOREIGN KEY (album_id) REFERENCES photo_albums (id) ON DELETE CASCADE ON UPDATE CASCADE ) AUTO_INCREMENT = 1; -CREATE TABLE user_stats_x ( - user_id int NOT NULL, - date DATETIME NOT NULL, - value int NOT NULL, - PRIMARY KEY(user_id, date), - CONSTRAINT user_stats_x_user_id FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE -); - - -CREATE TABLE users_x_users ( - my_friends_id int NOT NULL, - friends_with_me_id int NOT NULL, - PRIMARY KEY (my_friends_id, friends_with_me_id), - CONSTRAINT my_friends_key FOREIGN KEY (my_friends_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE, - CONSTRAINT friends_with_me_key FOREIGN KEY (friends_with_me_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE -); - CREATE TRIGGER `book_collections_bu_trigger` BEFORE UPDATE ON `book_collections` FOR EACH ROW SET NEW.updated_at = NOW(); @@ -144,22 +127,6 @@ FOR EACH ROW SET NEW.updated_at = NOW(); CREATE TRIGGER `book_collections_bi_trigger` BEFORE INSERT ON `book_collections` FOR EACH ROW SET NEW.updated_at = NOW(); - -CREATE TABLE photo_albums ( - id int NOT NULL AUTO_INCREMENT, - title varchar(255) NOT NULL, - preview_id int NULL, - PRIMARY KEY(id) -) AUTO_INCREMENT=1; - -CREATE TABLE photos ( - id int NOT NULL AUTO_INCREMENT, - title varchar(255) NOT NULL, - album_id int NOT NULL, - PRIMARY KEY(id), - CONSTRAINT photos_album_id FOREIGN KEY (album_id) REFERENCES photo_albums (id) ON DELETE CASCADE ON UPDATE CASCADE -) AUTO_INCREMENT=1; - ALTER TABLE photo_albums ADD CONSTRAINT photo_albums_preview_id FOREIGN KEY (preview_id) REFERENCES photos (id) ON DELETE CASCADE ON UPDATE CASCADE; @@ -180,6 +147,13 @@ CREATE TABLE user_stats CONSTRAINT user_stats_user_id FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE RESTRICT ON UPDATE CASCADE ); +CREATE TABLE user_stats_x ( + user_id int NOT NULL, + date DATETIME NOT NULL, + value int NOT NULL, + PRIMARY KEY(user_id, date), + CONSTRAINT user_stats_x_user_id FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE +); CREATE TABLE users_x_users ( @@ -191,17 +165,6 @@ CREATE TABLE users_x_users ); -CREATE TRIGGER `book_collections_bu_trigger` - BEFORE UPDATE - ON `book_collections` - FOR EACH ROW SET NEW.updated_at = NOW(); - -CREATE TRIGGER `book_collections_bi_trigger` - BEFORE INSERT - ON `book_collections` - FOR EACH ROW SET NEW.updated_at = NOW(); - - CREATE TABLE logs ( date TIMESTAMP NOT NULL, @@ -218,3 +181,10 @@ CREATE TABLE publishers_x_tags CONSTRAINT publishers_x_tags_tag FOREIGN KEY (tag_id) REFERENCES tags (id), CONSTRAINT publishers_x_tags_publisher FOREIGN KEY (publisher_id) REFERENCES publishers (publisher_id) ON DELETE CASCADE ); + +CREATE TABLE time_series +( + date DATETIME NOT NULL, + value int NOT NULL, + PRIMARY KEY (date) +) diff --git a/tests/inc/model/Model.php b/tests/inc/model/Model.php index feeebf3e..fc548a50 100644 --- a/tests/inc/model/Model.php +++ b/tests/inc/model/Model.php @@ -22,6 +22,7 @@ * @property-read UsersRepository $users * @property-read UserStatsRepository $userStats * @property-read UserStatsXRepository $userStatsX + * @property-read TimeSeriesRepository $timeSeries */ class Model extends OrmModel { diff --git a/tests/inc/model/timeSeries/TimeSeries.php b/tests/inc/model/timeSeries/TimeSeries.php new file mode 100644 index 00000000..14a74999 --- /dev/null +++ b/tests/inc/model/timeSeries/TimeSeries.php @@ -0,0 +1,17 @@ + + */ +final class TimeSeriesMapper extends DbalMapper +{ +} diff --git a/tests/inc/model/timeSeries/TimeSeriesRepository.php b/tests/inc/model/timeSeries/TimeSeriesRepository.php new file mode 100644 index 00000000..f3db3efd --- /dev/null +++ b/tests/inc/model/timeSeries/TimeSeriesRepository.php @@ -0,0 +1,18 @@ + + */ +final class TimeSeriesRepository extends Repository +{ + static function getEntityClassNames(): array + { + return [TimeSeries::class]; + } +} diff --git a/tests/inc/model/userStatX/UserStatsXMapper.php b/tests/inc/model/userStatX/UserStatsXMapper.php index 63f60c6d..f09022e2 100644 --- a/tests/inc/model/userStatX/UserStatsXMapper.php +++ b/tests/inc/model/userStatX/UserStatsXMapper.php @@ -2,9 +2,9 @@ namespace NextrasTests\Orm; -use Nextras\Orm\Mapper\Mapper; +use Nextras\Orm\Mapper\Dbal\DbalMapper; -final class UserStatsXMapper extends Mapper +final class UserStatsXMapper extends DbalMapper { }