Skip to content

Commit

Permalink
Merge 80f3d88 into 581a674
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Mar 6, 2022
2 parents 581a674 + 80f3d88 commit cecd515
Show file tree
Hide file tree
Showing 217 changed files with 3,221 additions and 50 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
]
},
"scripts": {
"phpstan": "phpstan analyse -c .phpstan.neon"
"phpstan": "phpstan analyse -c .phpstan.neon",
"tests": "tester -C --colors 1 --setup ./tests/inc/setup.php ./tests/cases"
},
"config": {
"preferred-install": {
Expand Down
2 changes: 2 additions & 0 deletions src/Mapper/Dbal/RelationshipMapperManyHasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use function count;
use function implode;
use function json_encode;
use function ksort;
use function md5;
use function strpos;

Expand Down Expand Up @@ -178,6 +179,7 @@ private function fetchByTwoPassStrategy(QueryBuilder $builder, array $values): M
return new MultiEntityIterator([]);
}

ksort($values); // make ids sorted deterministically
$entitiesResult = $this->targetMapper->findAll()->findBy(['id' => array_keys($values)]);
$entities = $entitiesResult->fetchPairs('id', null);

Expand Down
2 changes: 2 additions & 0 deletions src/Mapper/Dbal/RelationshipMapperOneHasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use function implode;
use function json_encode;
use function md5;
use function sort;


class RelationshipMapperOneHasMany implements IRelationshipMapper
Expand Down Expand Up @@ -187,6 +188,7 @@ protected function fetchByTwoPassStrategy(QueryBuilder $builder, array $values):
return new MultiEntityIterator([]);
}

sort($values); // make ids sorted deterministically
if ($isComposite) {
$builder = $this->targetMapper->builder();
$builder->andWhere('%multiOr', $ids);
Expand Down
2 changes: 2 additions & 0 deletions src/Repository/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use Nextras\Orm\Model\MetadataStorage;
use ReflectionClass;
use function count;
use function sort;


/**
Expand Down Expand Up @@ -548,6 +549,7 @@ public function doRefreshAll(bool $allowOverwrite): void
$ids[] = $entity->getPersistedId();
}
if (count($ids) > 0) {
sort($ids); // make ids sorted deterministically
$this->findByIds($ids)->fetchAll();
}
foreach ($entities as $entity) {
Expand Down
12 changes: 7 additions & 5 deletions tests/cases/integration/Entity/entity.cloning.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ namespace NextrasTests\Orm\Integration\Entity;
use NextrasTests\Orm\Author;
use NextrasTests\Orm\Book;
use NextrasTests\Orm\DataTestCase;
use NextrasTests\Orm\Publisher;
use NextrasTests\Orm\Tag;
use Tester\Assert;


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


class EntityCloning2Test extends DataTestCase
class EntityCloningTest extends DataTestCase
{

public function testCloningOneHasMany(): void
Expand All @@ -44,8 +45,9 @@ class EntityCloning2Test extends DataTestCase

public function testCloningManyHasMany(): void
{
$author = $this->e(Author::class);
$book = $this->e(Book::class, ['author' => $author]);
$author = $this->e(Author::class, ['name' => 'New Author']);
$publisher = $this->e(Publisher::class, ['name' => 'Publisher']);
$book = $this->e(Book::class, ['author' => $author, 'title' => 'New Book', 'publisher' => $publisher]);
$tag1 = $this->e(Tag::class, ['name' => 'Tag 1']);
$tag2 = $this->e(Tag::class, ['name' => 'Tag 2']);
$tag3 = $this->e(Tag::class, ['name' => 'Tag 3']);
Expand All @@ -59,7 +61,7 @@ class EntityCloning2Test extends DataTestCase
Assert::same(3, $newBook->tags->count());
Assert::same([$tag1, $tag2, $tag3], iterator_to_array($newBook->tags));

$book->author = $this->e(Author::class);
$book->author = $this->e(Author::class, ['name' => 'New Author 2']);
$book->tags->set([$tag1, $tag2]);

Assert::same($author, $newBook->author);
Expand All @@ -69,5 +71,5 @@ class EntityCloning2Test extends DataTestCase
}


$test = new EntityCloning2Test();
$test = new EntityCloningTest();
$test->run();
2 changes: 1 addition & 1 deletion tests/cases/integration/Entity/entity.pk.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class EntityPkTest extends DataTestCase
public function testDateTimeWithProxyPk(): void
{
$log = new Log();
$log->id = $datetime = new DateTimeImmutable('tomorrow');
$log->id = $datetime = new DateTimeImmutable('2022-03-06T03:03:03Z');
$log->count = 3;
$this->orm->persistAndFlush($log);

Expand Down
1 change: 1 addition & 0 deletions tests/cases/integration/Mapper/mapper.datetimesimple.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class MapperDateTimeSimpleTest extends DataTestCase
$author = $this->e(
Author::class,
[
'name' => 'Random Author',
'born' => new DateTimeImmutable('2018-01-09 00:00:00'),
]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ require_once __DIR__ . '/../../../bootstrap.php';

class RelationshipsOneHasManyPersistenceTest extends DataTestCase
{
public function testPersiting(): void
public function testPersisting(): void
{
$author1 = $this->e(Author::class);
$this->e(Book::class, ['author' => $author1, 'title' => 'Book XX']);
$author2 = $this->e(Author::class);
$this->e(Book::class, ['author' => $author2, 'title' => 'Book YY']);
$publisher = $this->e(Publisher::class, ['name' => 'Publisher']);
$author1 = $this->e(Author::class, ['name' => 'Persistence Author']);
$this->e(Book::class, ['author' => $author1, 'title' => 'Book XX', 'publisher' => $publisher]);
$author2 = $this->e(Author::class, ['name' => 'Persistence Author 2']);
$this->e(Book::class, ['author' => $author2, 'title' => 'Book YY', 'publisher' => $publisher]);
$this->orm->authors->persist($author1);
$this->orm->authors->persist($author2);
$this->orm->authors->flush();
Expand Down Expand Up @@ -105,9 +106,11 @@ class RelationshipsOneHasManyPersistenceTest extends DataTestCase
{
if ($this->section === Helper::SECTION_ARRAY) {
Environment::skip('Only for DB with foreign key restriction');
} else if ($this->section === Helper::SECTION_MSSQL) {
$connection = $this->container->getByType(IConnection::class);
$connection->query('SET IDENTITY_INSERT users ON;');
} else {
if ($this->section === Helper::SECTION_MSSQL) {
$connection = $this->container->getByType(IConnection::class);
$connection->query('SET IDENTITY_INSERT users ON;');
}
}

$user = new User();
Expand All @@ -117,7 +120,7 @@ class RelationshipsOneHasManyPersistenceTest extends DataTestCase
$user->friendsWithMe->add($user2);
$userStat = new UserStat();
$userStat->user = $user;
$userStat->date = new DateTimeImmutable();
$userStat->date = new DateTimeImmutable("2021-12-14 22:03:00");
$userStat->value = 3;
$this->orm->persistAndFlush($userStat);

Expand Down
21 changes: 11 additions & 10 deletions tests/cases/integration/Relationships/relationships.oneHasMany.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,18 @@ class RelationshipOneHasManyTest extends DataTestCase

public function testPersistence(): void
{
$author1 = $this->e(Author::class);
$this->e(Book::class, ['author' => $author1, 'title' => 'Book 1']);
$this->e(Book::class, ['author' => $author1, 'title' => 'Book 2']);
$publisher = $this->e(Publisher::class, ['name' => 'Publisher']);
$author1 = $this->e(Author::class, ['name' => 'A1']);
$this->e(Book::class, ['author' => $author1, 'title' => 'Book 1', 'publisher' => $publisher]);
$this->e(Book::class, ['author' => $author1, 'title' => 'Book 2', 'publisher' => $publisher]);

$author2 = $this->e(Author::class);
$this->e(Book::class, ['author' => $author2, 'title' => 'Book 3']);
$this->e(Book::class, ['author' => $author2, 'title' => 'Book 4']);
$author2 = $this->e(Author::class, ['name' => 'A2']);
$this->e(Book::class, ['author' => $author2, 'title' => 'Book 3', 'publisher' => $publisher]);
$this->e(Book::class, ['author' => $author2, 'title' => 'Book 4', 'publisher' => $publisher]);

$author3 = $this->e(Author::class);
$this->e(Book::class, ['author' => $author3, 'title' => 'Book 5']);
$this->e(Book::class, ['author' => $author3, 'title' => 'Book 6']);
$author3 = $this->e(Author::class, ['name' => 'A3']);
$this->e(Book::class, ['author' => $author3, 'title' => 'Book 5', 'publisher' => $publisher]);
$this->e(Book::class, ['author' => $author3, 'title' => 'Book 6', 'publisher' => $publisher]);

$this->orm->authors->persist($author1);
$this->orm->authors->persist($author2);
Expand Down Expand Up @@ -270,7 +271,7 @@ class RelationshipOneHasManyTest extends DataTestCase
$this->orm->refreshAll(true);

$ids = [];
foreach ($tag->tagFollowers as $tagFollower) {
foreach ($tag->tagFollowers->orderBy('author') as $tagFollower) {
$ids[] = $tagFollower->author->id;
Assert::true($tagFollower->isPersisted());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use Nextras\Dbal\Utils\DateTimeImmutable;
use Nextras\Orm\Exception\NullValueException;
use NextrasTests\Orm\Author;
use NextrasTests\Orm\Book;
use NextrasTests\Orm\DataTestCase;
use NextrasTests\Orm\Publisher;
use NextrasTests\Orm\Tag;
use NextrasTests\Orm\TestCase;
Expand All @@ -21,7 +22,7 @@ use Tester\Assert;
require_once __DIR__ . '/../../../bootstrap.php';


class RepositoryPersistenceTest extends TestCase
class RepositoryPersistenceTest extends DataTestCase
{

public function testComplexPersistenceTree(): void
Expand Down Expand Up @@ -130,14 +131,18 @@ class RepositoryPersistenceTest extends TestCase
// assign all tags to publisher
$allTags = $this->orm->tags->findAll()->fetchAll();
$publisher = $this->e(Publisher::class, [
'name' => 'Nextras Publisher',
'tags' => $allTags,
]);
$this->orm->publishers->persistAndFlush($publisher);

// assign publisher and only one tag
$book = $this->e(Book::class, [
'author' => $this->e(Author::class, ['name' => 'A2']),
'title' => 'Some Book Title',
'publisher' => $this->e(Publisher::class, ['name' => 'P2']),
'tags' => [
$this->orm->tags->getBy([]),
$this->orm->tags->findAll()->orderBy('id')->fetch(),
],
]);

Expand Down
8 changes: 4 additions & 4 deletions tests/db/array-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
$book1->author = $author1;
$book1->translator = $author1;
$book1->publisher = $publisher1;
$book1->publishedAt = new \DateTimeImmutable('2017-04-20 20:00:00');
$book1->publishedAt = new \DateTimeImmutable('2021-12-14 21:10:04');
$book1->price = new Money(50, Currency::CZK());
$book1->tags->set([$tag1, $tag2]);
$orm->books->persist($book1);
Expand All @@ -52,7 +52,7 @@
$book2->title = 'Book 2';
$book2->author = $author1;
$book2->publisher = $publisher2;
$book2->publishedAt = new \DateTimeImmutable('2017-04-20 18:00:00');
$book2->publishedAt = new \DateTimeImmutable('2021-12-14 21:10:02');
$book2->price = new Money(150, Currency::CZK());
$book2->tags->set([$tag2, $tag3]);
$orm->books->persist($book2);
Expand All @@ -62,7 +62,7 @@
$book3->author = $author2;
$book3->translator = $author2;
$book3->publisher = $publisher3;
$book3->publishedAt = new \DateTimeImmutable('2017-04-20 19:00:00');
$book3->publishedAt = new \DateTimeImmutable('2021-12-14 21:10:03');
$book3->price = new Money(20, Currency::CZK());
$book3->tags->set([$tag3]);
$orm->books->persist($book3);
Expand All @@ -73,7 +73,7 @@
$book4->translator = $author2;
$book4->publisher = $publisher1;
$book4->nextPart = $book3;
$book4->publishedAt = new \DateTimeImmutable('2017-04-20 17:00:00');
$book4->publishedAt = new \DateTimeImmutable('2021-12-14 21:10:01');
$book4->price = new Money(220, Currency::CZK());
$orm->books->persist($book4);

Expand Down
10 changes: 6 additions & 4 deletions tests/db/mssql-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ SET IDENTITY_INSERT tags OFF;
DBCC checkident ('tags', reseed, 3) WITH NO_INFOMSGS;

SET IDENTITY_INSERT books ON;
INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, published_at, price, price_currency) VALUES (1, 1, 1, 'Book 1', NULL, 1, DATEADD(ss, 4, CURRENT_TIMESTAMP), 50, 'CZK');
INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, published_at, price, price_currency) VALUES (2, 1, NULL, 'Book 2', NULL, 2, DATEADD(ss, 2, CURRENT_TIMESTAMP), 150, 'CZK');
INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, published_at, price, price_currency) VALUES (3, 2, 2, 'Book 3', NULL, 3, DATEADD(ss, 3, CURRENT_TIMESTAMP), 20, 'CZK');
INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, published_at, price, price_currency) VALUES (4, 2, 2, 'Book 4', 3, 1, DATEADD(ss, 1, CURRENT_TIMESTAMP), 220, 'CZK');
INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, published_at, price, price_currency) VALUES (1, 1, 1, 'Book 1', NULL, 1, '2021-12-14 21:10:04', 50, 'CZK');
INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, published_at, price, price_currency) VALUES (2, 1, NULL, 'Book 2', NULL, 2, '2021-12-14 21:10:02', 150, 'CZK');
INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, published_at, price, price_currency) VALUES (3, 2, 2, 'Book 3', NULL, 3, '2021-12-14 21:10:03', 20, 'CZK');
INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, published_at, price, price_currency) VALUES (4, 2, 2, 'Book 4', 3, 1, '2021-12-14 21:10:01', 220, 'CZK');
SET IDENTITY_INSERT books OFF;

DBCC checkident ('books', reseed, 4) WITH NO_INFOMSGS;

DBCC checkident ('eans', reseed, 1) WITH NO_INFOMSGS;

INSERT INTO books_x_tags (book_id, tag_id) VALUES (1, 1);
INSERT INTO books_x_tags (book_id, tag_id) VALUES (1, 2);
INSERT INTO books_x_tags (book_id, tag_id) VALUES (2, 2);
Expand Down
8 changes: 4 additions & 4 deletions tests/db/mysql-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ INSERT INTO tags (id, name, is_global) VALUES (1, 'Tag 1', 'y');
INSERT INTO tags (id, name, is_global) VALUES (2, 'Tag 2', 'y');
INSERT INTO tags (id, name, is_global) VALUES (3, 'Tag 3', 'n');

INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, published_at, price, price_currency) VALUES (1, 1, 1, 'Book 1', NULL, 1, DATE_ADD(NOW(), INTERVAL 4 SECOND), 50, 'CZK');
INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, published_at, price, price_currency) VALUES (2, 1, NULL, 'Book 2', NULL, 2, DATE_ADD(NOW(), INTERVAL 2 SECOND), 150, 'CZK');
INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, published_at, price, price_currency) VALUES (3, 2, 2, 'Book 3', NULL, 3, DATE_ADD(NOW(), INTERVAL 3 SECOND), 20, 'CZK');
INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, published_at, price, price_currency) VALUES (4, 2, 2, 'Book 4', 3, 1, DATE_ADD(NOW(), INTERVAL 1 SECOND), 220, 'CZK');
INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, published_at, price, price_currency) VALUES (1, 1, 1, 'Book 1', NULL, 1, '2021-12-14 21:10:04', 50, 'CZK');
INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, published_at, price, price_currency) VALUES (2, 1, NULL, 'Book 2', NULL, 2, '2021-12-14 21:10:02', 150, 'CZK');
INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, published_at, price, price_currency) VALUES (3, 2, 2, 'Book 3', NULL, 3, '2021-12-14 21:10:03', 20, 'CZK');
INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, published_at, price, price_currency) VALUES (4, 2, 2, 'Book 4', 3, 1, '2021-12-14 21:10:01', 220, 'CZK');

INSERT INTO books_x_tags (book_id, tag_id) VALUES (1, 1);
INSERT INTO books_x_tags (book_id, tag_id) VALUES (1, 2);
Expand Down
16 changes: 11 additions & 5 deletions tests/db/pgsql-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@ INSERT INTO "tags" ("id", "name", "is_global") VALUES (3, 'Tag 3', 'n');
SELECT setval('tags_id_seq', 3, true);


INSERT INTO "books" ("id", "author_id", "translator_id", "title", "next_part", "publisher_id", "published_at", "price", "price_currency") VALUES (1, 1, 1, 'Book 1', NULL, 1, NOW() + interval '4 seconds', 50, 'CZK');
INSERT INTO "books" ("id", "author_id", "translator_id", "title", "next_part", "publisher_id", "published_at", "price", "price_currency") VALUES (2, 1, NULL, 'Book 2', NULL, 2, NOW() + interval '2 seconds', 150, 'CZK');
INSERT INTO "books" ("id", "author_id", "translator_id", "title", "next_part", "publisher_id", "published_at", "price", "price_currency") VALUES (3, 2, 2, 'Book 3', NULL, 3, NOW() + interval '3 seconds', 20, 'CZK');
INSERT INTO "books" ("id", "author_id", "translator_id", "title", "next_part", "publisher_id", "published_at", "price", "price_currency") VALUES (4, 2, 2, 'Book 4', 3, 1, NOW() + interval '1 seconds', 220, 'CZK');
INSERT INTO "books" ("id", "author_id", "translator_id", "title", "next_part", "publisher_id", "published_at", "price", "price_currency") VALUES (1, 1, 1, 'Book 1', NULL, 1, '2021-12-14 21:10:04', 50, 'CZK');
INSERT INTO "books" ("id", "author_id", "translator_id", "title", "next_part", "publisher_id", "published_at", "price", "price_currency") VALUES (2, 1, NULL, 'Book 2', NULL, 2, '2021-12-14 21:10:02', 150, 'CZK');
INSERT INTO "books" ("id", "author_id", "translator_id", "title", "next_part", "publisher_id", "published_at", "price", "price_currency") VALUES (3, 2, 2, 'Book 3', NULL, 3, '2021-12-14 21:10:03', 20, 'CZK');
INSERT INTO "books" ("id", "author_id", "translator_id", "title", "next_part", "publisher_id", "published_at", "price", "price_currency") VALUES (4, 2, 2, 'Book 4', 3, 1, '2021-12-14 21:10:01', 220, 'CZK');

SELECT setval('books_id_seq', 4, true);


INSERT INTO "books_x_tags" ("book_id", "tag_id") VALUES (1, 1);
INSERT INTO "books_x_tags" ("book_id", "tag_id") VALUES (1, 2);
INSERT INTO "books_x_tags" ("book_id", "tag_id") VALUES (2, 2);
Expand All @@ -54,3 +53,10 @@ INSERT INTO "tag_followers" ("tag_id", "author_id", "created_at") VALUES (2, 2,
INSERT INTO "contents" ("id", "type", "thread_id", "replied_at") VALUES (1, 'thread', NULL, NULL);
INSERT INTO "contents" ("id", "type", "thread_id", "replied_at") VALUES (2, 'comment', 1, '2020-01-01 12:00:00');
INSERT INTO "contents" ("id", "type", "thread_id", "replied_at") VALUES (3, 'comment', 1, '2020-01-02 12:00:00');

SELECT setval('contents_id_seq', 3, true);

ALTER SEQUENCE eans_id_seq RESTART WITH 1;
ALTER SEQUENCE photo_albums_id_seq RESTART WITH 1;
ALTER SEQUENCE photos_id_seq RESTART WITH 1;
ALTER SEQUENCE users_id_seq RESTART WITH 1;
3 changes: 1 addition & 2 deletions tests/inc/DataTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@

class DataTestCase extends TestCase
{
protected function setUp()
protected function setUpData(): void
{
parent::setUp();
switch ($this->section) {
case Helper::SECTION_MYSQL:
case Helper::SECTION_PGSQL:
Expand Down
Loading

0 comments on commit cecd515

Please sign in to comment.