Skip to content

Commit

Permalink
testcase showing nextras#320
Browse files Browse the repository at this point in the history
  • Loading branch information
jkavalik committed Oct 8, 2024
1 parent f4a0444 commit c2ccb16
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tests/cases/integration/Entity/entity.compositePK.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use NextrasTests\Orm\DataTestCase;
use NextrasTests\Orm\Helper;
use NextrasTests\Orm\User;
use NextrasTests\Orm\UserStat;
use NextrasTests\Orm\UserStatX;
use Tester\Assert;


Expand Down Expand Up @@ -54,6 +55,41 @@ class EntityCompositePKTest extends DataTestCase
$this->orm->persistAndFlush($userStat);
}

public function testCompositePKDateTime2()
{
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->getBy(['date' => '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->getBy(['date' => '2019-01-01']);
Assert::same(200, $res->value);

Environment::$checkAssertions = false;
}



public function testGetBy(): void
{
Expand Down
39 changes: 39 additions & 0 deletions tests/db/mysql-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,45 @@ 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();

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;
Expand Down
46 changes: 46 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 Down
1 change: 1 addition & 0 deletions tests/inc/model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @property-read TagFollowersRepository $tagFollowers
* @property-read UsersRepository $users
* @property-read UserStatsRepository $userStats
* @property-read UserStatsXRepository $userStatsX
*/
class Model extends OrmModel
{
Expand Down
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
{
}
10 changes: 10 additions & 0 deletions tests/inc/model/userStatX/UserStatsXMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types = 1);

namespace NextrasTests\Orm;

use Nextras\Orm\Mapper\Mapper;


final class UserStatsXMapper extends Mapper
{
}
14 changes: 14 additions & 0 deletions tests/inc/model/userStatX/UserStatsXRepository.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\Repository\Repository;


final class UserStatsXRepository extends Repository
{
static function getEntityClassNames(): array
{
return [UserStatX::class];
}
}

0 comments on commit c2ccb16

Please sign in to comment.