Skip to content

Commit

Permalink
test case and fix for #676
Browse files Browse the repository at this point in the history
  • Loading branch information
jkavalik committed Oct 9, 2024
1 parent e62b479 commit 803428f
Show file tree
Hide file tree
Showing 16 changed files with 288 additions and 19 deletions.
15 changes: 12 additions & 3 deletions src/Mapper/Dbal/DbalMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,10 @@ public function persist(IEntity $entity): void

} else {
$primary = [];
$id = (array) $entity->getPersistedId();
$id = $entity->getPersistedId();
if (!is_array($id)) {
$id = [$id];
}
foreach ($entity->getMetadata()->getPrimaryKey() as $key) {
$primary[$key] = array_shift($id);
}
Expand Down Expand Up @@ -458,7 +461,10 @@ protected function processMySQLAutoupdate(IEntity $entity, array $args): void

$conventions = $this->getConventions();

$id = (array) $entity->getPersistedId();
$id = $entity->getPersistedId();
if (!is_array($id)) {
$id = [$id];
}
$primary = [];
foreach ($entity->getMetadata()->getPrimaryKey() as $key) {
$primary[$key] = array_shift($id);
Expand Down Expand Up @@ -487,7 +493,10 @@ public function remove(IEntity $entity): void
$conventions = $this->getConventions();

$primary = [];
$id = (array) $entity->getPersistedId();
$id = $entity->getPersistedId();
if (!is_array($id)) {
$id = [$id];
}
foreach ($entity->getMetadata()->getPrimaryKey() as $key) {
$key = $conventions->convertEntityToStorageKey($key);
$primary[$key] = array_shift($id);
Expand Down
44 changes: 40 additions & 4 deletions tests/cases/integration/Entity/entity.compositePK.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ namespace NextrasTests\Orm\Integration\Entity;


use DateTimeImmutable;
use Nette\Utils\DateTime;
use Nextras\Dbal\IConnection;
use Nextras\Orm\Exception\InvalidArgumentException;
use NextrasTests\Orm\DataTestCase;
use NextrasTests\Orm\Helper;
use NextrasTests\Orm\User;
use NextrasTests\Orm\UserStat;
use NextrasTests\Orm\UserStatX;
use Tester\Assert;
use Tester\Environment;


require_once __DIR__ . '/../../../bootstrap.php';
Expand Down Expand Up @@ -46,19 +49,52 @@ class EntityCompositePKTest extends DataTestCase

$this->orm->clear();

$userStat = $this->orm->userStats->getBy(['user' => $userId, 'date' => $at]);
Assert::notNull($userStat);
$userStat = $this->orm->userStats->getByChecked(['user' => $userId, 'date' => $at]);
Assert::type(DateTimeImmutable::class, $userStat->id[1]);

$userStat->value = 101;
$this->orm->persistAndFlush($userStat);
}

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.
// http://stackoverflow.com/questions/2148091/syntax-for-inserting-into-a-table-with-no-values
Environment::skip('Inserting dummy rows when no arguments are passed is not supported.');
}

$user = new User();
$this->orm->persistAndFlush($user);

$stat = new UserStatX();
$stat->user = $user;
$stat->date = '2019-01-01';
$stat->value = 100;
$this->orm->persistAndFlush($stat);

$this->orm->clear();

$res = $this->orm->userStatsX->getByChecked(['date' => new DateTime('2019-01-01')]);
Assert::same(100, $res->value);

$res->value = 200;
$this->orm->persistAndFlush($res);
Assert::same(200, $res->value);

$this->orm->clear();

$res = $this->orm->userStatsX->getByChecked(['date' => new DateTime('2019-01-01')]);
Assert::same(200, $res->value);

Environment::$checkAssertions = false;
}



public function testGetBy(): void
{
$tagFollower = $this->orm->tagFollowers->getBy(['tag' => 3, 'author' => 1]);
Assert::notNull($tagFollower);
$tagFollower = $this->orm->tagFollowers->getByChecked(['tag' => 3, 'author' => 1]);
Assert::same($tagFollower->tag->name, 'Tag 3');
Assert::same($tagFollower->author->name, 'Writer 1');

Expand Down
17 changes: 17 additions & 0 deletions tests/cases/integration/Entity/entity.pk.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -32,6 +33,22 @@ 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->getByIdChecked($datetime);
$timeSeries->value = 5;
$this->orm->persistAndFlush($timeSeries);

$entry = $this->orm->timeSeries->getById($datetime);
Assert::true($entry !== null);
}
}


Expand Down
17 changes: 17 additions & 0 deletions tests/db/mssql-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ CREATE TABLE user_stats
CONSTRAINT user_stats_user_id FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE NO ACTION ON UPDATE CASCADE
);

CREATE TABLE user_stats_x
(
user_id int NOT NULL,
date date 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 NO ACTION ON UPDATE CASCADE
);


CREATE TABLE users_x_users
(
Expand Down Expand Up @@ -176,3 +185,11 @@ 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 datetimeoffset NOT NULL,
value int NOT NULL,
PRIMARY KEY (date)
);
31 changes: 20 additions & 11 deletions tests/db/mysql-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ CREATE TABLE photos
) AUTO_INCREMENT = 1;


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();

ALTER TABLE photo_albums
ADD CONSTRAINT photo_albums_preview_id FOREIGN KEY (preview_id) REFERENCES photos (id) ON DELETE CASCADE ON UPDATE CASCADE;

Expand All @@ -141,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 date 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
(
Expand All @@ -152,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,
Expand All @@ -179,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)
)
63 changes: 63 additions & 0 deletions tests/db/pgsql-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,52 @@ CREATE TABLE "photos"
CONSTRAINT "photos_album_id" FOREIGN KEY ("album_id") REFERENCES "photo_albums" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

CREATE TABLE "user_stats_x" (
"user_id" int NOT NULL,
"date" TIMESTAMPTZ 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 FUNCTION "book_collections_before"() RETURNS TRIGGER AS $BODY$BEGIN
NEW."updated_at" = NOW();
return NEW;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE;

CREATE TRIGGER "book_collections_before_insert_trigger" BEFORE INSERT ON "book_collections"
FOR EACH ROW EXECUTE PROCEDURE "book_collections_before"();

CREATE TRIGGER "book_collections_before_update_trigger" BEFORE UPDATE ON "book_collections"
FOR EACH ROW EXECUTE PROCEDURE "book_collections_before"();


CREATE TABLE "photo_albums" (
"id" serial4 NOT NULL,
"title" varchar(255) NOT NULL,
"preview_id" int NULL,
PRIMARY KEY ("id")
);

CREATE TABLE "photos" (
"id" serial4 NOT NULL,
"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
);

ALTER TABLE "photo_albums"
ADD CONSTRAINT "photo_albums_preview_id" FOREIGN KEY ("preview_id") REFERENCES "photos" ("id") ON DELETE CASCADE ON UPDATE CASCADE;
Expand All @@ -141,6 +187,15 @@ 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" date 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 RESTRICT ON UPDATE CASCADE
);


CREATE TABLE "users_x_users"
(
Expand Down Expand Up @@ -190,3 +245,11 @@ 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 ON UPDATE CASCADE
);


CREATE TABLE "time_series"
(
"date" TIMESTAMPTZ NOT NULL,
"value" int NOT NULL,
PRIMARY KEY ("date")
);
2 changes: 2 additions & 0 deletions tests/inc/model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* @property-read TagFollowersRepository $tagFollowers
* @property-read UsersRepository $users
* @property-read UserStatsRepository $userStats
* @property-read UserStatsXRepository $userStatsX
* @property-read TimeSeriesRepository $timeSeries
*/
class Model extends OrmModel
{
Expand Down
17 changes: 17 additions & 0 deletions tests/inc/model/timeSeries/TimeSeries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types = 1);

namespace NextrasTests\Orm;


use DateTimeImmutable;
use Nextras\Orm\Entity\Entity;


/**
* @property DateTimeImmutable $id {primary-proxy}
* @property DateTimeImmutable $date {primary}
* @property int $value
*/
final class TimeSeries extends Entity
{
}
14 changes: 14 additions & 0 deletions tests/inc/model/timeSeries/TimeSeriesMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare(strict_types = 1);

namespace NextrasTests\Orm;


use Nextras\Orm\Mapper\Dbal\DbalMapper;


/**
* @extends DbalMapper<Log>
*/
final class TimeSeriesMapper extends DbalMapper
{
}
18 changes: 18 additions & 0 deletions tests/inc/model/timeSeries/TimeSeriesRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types = 1);

namespace NextrasTests\Orm;


use Nextras\Orm\Repository\Repository;


/**
* @extends Repository<Log>
*/
final class TimeSeriesRepository extends Repository
{
static function getEntityClassNames(): array
{
return [TimeSeries::class];
}
}
17 changes: 17 additions & 0 deletions tests/inc/model/userStatX/UserStatX.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types = 1);

namespace NextrasTests\Orm;

use DateTimeImmutable;
use Nextras\Orm\Entity\Entity;


/**
* @property array $id {primary-proxy}
* @property User $user {primary} {m:1 User, oneSided=true}
* @property DateTimeImmutable $date {primary}
* @property int $value
*/
final class UserStatX extends Entity
{
}
13 changes: 13 additions & 0 deletions tests/inc/model/userStatX/UserStatsXMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types = 1);

namespace NextrasTests\Orm;

use Nextras\Orm\Mapper\Dbal\DbalMapper;


/**
* @extends DbalMapper<UserStatX>
*/
final class UserStatsXMapper extends DbalMapper
{
}
Loading

0 comments on commit 803428f

Please sign in to comment.