From 48510178e1691cddcf6e1f575425020616f532a5 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Sun, 9 Jun 2024 08:47:10 +0700 Subject: [PATCH] Add tests --- tests/Driver/Pgsql/ActiveRecordTest.php | 32 ++++++++++++++++++++++ tests/Driver/Pgsql/Stubs/Item.php | 27 +++++++++++++++++++ tests/Driver/Pgsql/Stubs/Promotion.php | 35 +++++++++++++++++++++++++ tests/Stubs/ActiveRecord/Item.php | 2 +- tests/data/pgsql.sql | 12 +++++++++ 5 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 tests/Driver/Pgsql/Stubs/Item.php create mode 100644 tests/Driver/Pgsql/Stubs/Promotion.php diff --git a/tests/Driver/Pgsql/ActiveRecordTest.php b/tests/Driver/Pgsql/ActiveRecordTest.php index 74707f18a..82540eb4a 100644 --- a/tests/Driver/Pgsql/ActiveRecordTest.php +++ b/tests/Driver/Pgsql/ActiveRecordTest.php @@ -7,6 +7,9 @@ use ArrayAccess; use Traversable; use Yiisoft\ActiveRecord\ActiveQuery; +use Yiisoft\ActiveRecord\ArArrayHelper; +use Yiisoft\ActiveRecord\Tests\Driver\Pgsql\Stubs\Item; +use Yiisoft\ActiveRecord\Tests\Driver\Pgsql\Stubs\Promotion; use Yiisoft\ActiveRecord\Tests\Driver\Pgsql\Stubs\Type; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\ArrayAndJsonTypes; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Beta; @@ -441,4 +444,33 @@ public function testToArrayWithClosure(): void $customer->toArray(), ); } + + public function testRelationViaArray() + { + $this->checkFixture($this->db, 'promotion'); + + $promotionQuery = new ActiveQuery(Promotion::class, $this->db); + /** @var Promotion[] $promotions */ + $promotions = $promotionQuery->with('items')->all(); + + $this->assertSame([1, 2], ArArrayHelper::getColumn($promotions[0]->getItems(), 'id')); + $this->assertSame([3, 4, 5], ArArrayHelper::getColumn($promotions[1]->getItems(), 'id')); + $this->assertSame([1, 3], ArArrayHelper::getColumn($promotions[2]->getItems(), 'id')); + $this->assertCount(0, $promotions[3]->getItems()); + + /** Test inverse relation */ + foreach ($promotions as $promotion) { + foreach ($promotion->getItems() as $item) { + $this->assertTrue($item->isRelationPopulated('promotions')); + } + } + + $this->assertSame([1, 3], ArArrayHelper::getColumn($promotions[0]->getItems()[0]->getPromotions(), 'id')); + $this->assertSame([1], ArArrayHelper::getColumn($promotions[0]->getItems()[1]->getPromotions(), 'id')); + $this->assertSame([2, 3], ArArrayHelper::getColumn($promotions[1]->getItems()[0]->getPromotions(), 'id')); + $this->assertSame([2], ArArrayHelper::getColumn($promotions[1]->getItems()[1]->getPromotions(), 'id')); + $this->assertSame([2], ArArrayHelper::getColumn($promotions[1]->getItems()[2]->getPromotions(), 'id')); + $this->assertSame([1, 3], ArArrayHelper::getColumn($promotions[2]->getItems()[0]->getPromotions(), 'id')); + $this->assertSame([2, 3], ArArrayHelper::getColumn($promotions[2]->getItems()[1]->getPromotions(), 'id')); + } } diff --git a/tests/Driver/Pgsql/Stubs/Item.php b/tests/Driver/Pgsql/Stubs/Item.php new file mode 100644 index 000000000..fcb19dc49 --- /dev/null +++ b/tests/Driver/Pgsql/Stubs/Item.php @@ -0,0 +1,27 @@ + $this->hasMany(Promotion::class, ['item_ids' => 'id']), + default => parent::relationQuery($name), + }; + } + + /** @return Promotion[] */ + public function getPromotions(): array + { + return $this->relation('promotions'); + } +} diff --git a/tests/Driver/Pgsql/Stubs/Promotion.php b/tests/Driver/Pgsql/Stubs/Promotion.php new file mode 100644 index 000000000..730728bd5 --- /dev/null +++ b/tests/Driver/Pgsql/Stubs/Promotion.php @@ -0,0 +1,35 @@ + $this->hasMany(Item::class, ['id' => 'item_ids'])->inverseOf('promotions'), + default => parent::relationQuery($name), + }; + } + + /** @return Item[] */ + public function getItems(): array + { + return $this->relation('items'); + } +} diff --git a/tests/Stubs/ActiveRecord/Item.php b/tests/Stubs/ActiveRecord/Item.php index 27c4635fb..40d149586 100644 --- a/tests/Stubs/ActiveRecord/Item.php +++ b/tests/Stubs/ActiveRecord/Item.php @@ -11,7 +11,7 @@ /** * Class Item. */ -final class Item extends ActiveRecord +class Item extends ActiveRecord { protected int $id; protected string $name; diff --git a/tests/data/pgsql.sql b/tests/data/pgsql.sql index 2ce4fe6b0..8083b5b16 100644 --- a/tests/data/pgsql.sql +++ b/tests/data/pgsql.sql @@ -28,6 +28,7 @@ DROP TABLE IF EXISTS "employee" CASCADE; DROP TABLE IF EXISTS "department" CASCADE; DROP TABLE IF EXISTS "alpha" CASCADE; DROP TABLE IF EXISTS "beta" CASCADE; +DROP TABLE IF EXISTS "promotion" CASCADE; DROP VIEW IF EXISTS "animal_view" CASCADE; DROP TABLE IF EXISTS "T_constraints_4" CASCADE; DROP TABLE IF EXISTS "T_constraints_3" CASCADE; @@ -224,6 +225,12 @@ CREATE TABLE "beta" ( PRIMARY KEY (id) ); +CREATE TABLE "promotion" ( + id serial primary key, + item_ids integer[] not null, + title varchar(126) not null +); + CREATE VIEW "animal_view" AS SELECT * FROM "animal"; INSERT INTO "animal" (type) VALUES ('Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Cat'); @@ -302,6 +309,11 @@ INSERT INTO "beta" (id, alpha_string_identifier) VALUES (6, '2b'); INSERT INTO "beta" (id, alpha_string_identifier) VALUES (7, '2b'); INSERT INTO "beta" (id, alpha_string_identifier) VALUES (8, '02'); +INSERT INTO "promotion" (item_ids, title) VALUES ('{1,2}', 'Discounted items'); +INSERT INTO "promotion" (item_ids, title) VALUES ('{3,4,5}', 'New arrivals'); +INSERT INTO "promotion" (item_ids, title) VALUES ('{1,3}', 'Free shipping'); +INSERT INTO "promotion" (item_ids, title) VALUES ('{}', 'Free!'); + /** * (Postgres-)Database Schema for validator tests */