Skip to content

Commit

Permalink
test case for #676
Browse files Browse the repository at this point in the history
  • Loading branch information
jkavalik committed Oct 8, 2024
1 parent 454bb65 commit ffc716b
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 52 deletions.
14 changes: 8 additions & 6 deletions tests/cases/integration/Entity/entity.compositePK.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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';
Expand Down Expand Up @@ -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.
Expand All @@ -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);

Check failure on line 80 in tests/cases/integration/Entity/entity.compositePK.phpt

View workflow job for this annotation

GitHub Actions / PHPStan (8.2)

Cannot access property $value on NextrasTests\Orm\UserStatX|null.

Check failure on line 80 in tests/cases/integration/Entity/entity.compositePK.phpt

View workflow job for this annotation

GitHub Actions / PHPStan (8.3)

Cannot access property $value on NextrasTests\Orm\UserStatX|null.

Check failure on line 80 in tests/cases/integration/Entity/entity.compositePK.phpt

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Cannot access property $value on NextrasTests\Orm\UserStatX|null.

$res->value = 200;

Check failure on line 82 in tests/cases/integration/Entity/entity.compositePK.phpt

View workflow job for this annotation

GitHub Actions / PHPStan (8.2)

Cannot access property $value on NextrasTests\Orm\UserStatX|null.

Check failure on line 82 in tests/cases/integration/Entity/entity.compositePK.phpt

View workflow job for this annotation

GitHub Actions / PHPStan (8.3)

Cannot access property $value on NextrasTests\Orm\UserStatX|null.

Check failure on line 82 in tests/cases/integration/Entity/entity.compositePK.phpt

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Cannot access property $value on NextrasTests\Orm\UserStatX|null.
Expand All @@ -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);

Check failure on line 89 in tests/cases/integration/Entity/entity.compositePK.phpt

View workflow job for this annotation

GitHub Actions / PHPStan (8.2)

Cannot access property $value on NextrasTests\Orm\UserStatX|null.

Check failure on line 89 in tests/cases/integration/Entity/entity.compositePK.phpt

View workflow job for this annotation

GitHub Actions / PHPStan (8.3)

Cannot access property $value on NextrasTests\Orm\UserStatX|null.

Check failure on line 89 in tests/cases/integration/Entity/entity.compositePK.phpt

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Cannot access property $value on NextrasTests\Orm\UserStatX|null.

Environment::$checkAssertions = false;
}



public function testGetBy(): void
{
Expand Down
18 changes: 18 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,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;

Check failure on line 47 in tests/cases/integration/Entity/entity.pk.phpt

View workflow job for this annotation

GitHub Actions / PHPStan (8.2)

Cannot access property $value on NextrasTests\Orm\TimeSeries|null.

Check failure on line 47 in tests/cases/integration/Entity/entity.pk.phpt

View workflow job for this annotation

GitHub Actions / PHPStan (8.3)

Cannot access property $value on NextrasTests\Orm\TimeSeries|null.

Check failure on line 47 in tests/cases/integration/Entity/entity.pk.phpt

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Cannot access property $value on NextrasTests\Orm\TimeSeries|null.
$this->orm->persistAndFlush($timeSeries);

Check failure on line 48 in tests/cases/integration/Entity/entity.pk.phpt

View workflow job for this annotation

GitHub Actions / PHPStan (8.2)

Parameter #1 $entity of method Nextras\Orm\Model\Model::persistAndFlush() expects E of Nextras\Orm\Entity\IEntity, NextrasTests\Orm\TimeSeries|null given.

Check failure on line 48 in tests/cases/integration/Entity/entity.pk.phpt

View workflow job for this annotation

GitHub Actions / PHPStan (8.3)

Parameter #1 $entity of method Nextras\Orm\Model\Model::persistAndFlush() expects E of Nextras\Orm\Entity\IEntity, NextrasTests\Orm\TimeSeries|null given.

Check failure on line 48 in tests/cases/integration/Entity/entity.pk.phpt

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Parameter #1 $entity of method Nextras\Orm\Model\Model::persistAndFlush() expects E of Nextras\Orm\Entity\IEntity, NextrasTests\Orm\TimeSeries|null given.

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


Expand Down
58 changes: 14 additions & 44 deletions tests/db/mysql-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -120,46 +120,13 @@ 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 All @@ -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
(
Expand All @@ -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,
Expand All @@ -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)
)
1 change: 1 addition & 0 deletions tests/inc/model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @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];
}
}
4 changes: 2 additions & 2 deletions tests/inc/model/userStatX/UserStatsXMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 8 in tests/inc/model/userStatX/UserStatsXMapper.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2)

Class NextrasTests\Orm\UserStatsXMapper extends generic class Nextras\Orm\Mapper\Dbal\DbalMapper but does not specify its types: E

Check failure on line 8 in tests/inc/model/userStatX/UserStatsXMapper.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3)

Class NextrasTests\Orm\UserStatsXMapper extends generic class Nextras\Orm\Mapper\Dbal\DbalMapper but does not specify its types: E

Check failure on line 8 in tests/inc/model/userStatX/UserStatsXMapper.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Class NextrasTests\Orm\UserStatsXMapper extends generic class Nextras\Orm\Mapper\Dbal\DbalMapper but does not specify its types: E
{
}

0 comments on commit ffc716b

Please sign in to comment.