Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Jun 9, 2024
1 parent 48a060d commit 4851017
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 1 deletion.
32 changes: 32 additions & 0 deletions tests/Driver/Pgsql/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'));
}
}
27 changes: 27 additions & 0 deletions tests/Driver/Pgsql/Stubs/Item.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Yiisoft\ActiveRecord\Tests\Driver\Pgsql\Stubs;

use Yiisoft\ActiveRecord\ActiveQueryInterface;

/**
* Class Item.
*/
final class Item extends \Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Item
{
public function relationQuery(string $name): ActiveQueryInterface
{
return match ($name) {
'promotions' => $this->hasMany(Promotion::class, ['item_ids' => 'id']),
default => parent::relationQuery($name),
};
}

/** @return Promotion[] */
public function getPromotions(): array
{
return $this->relation('promotions');
}
}
35 changes: 35 additions & 0 deletions tests/Driver/Pgsql/Stubs/Promotion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Yiisoft\ActiveRecord\Tests\Driver\Pgsql\Stubs;

use Yiisoft\ActiveRecord\ActiveQueryInterface;
use Yiisoft\ActiveRecord\ActiveRecord;

final class Promotion extends ActiveRecord
{
public int $id;
/** @var int[] $item_ids */
public array $item_ids;
public string $title;

public function getTableName(): string
{
return '{{%promotion}}';
}

public function relationQuery(string $name): ActiveQueryInterface
{
return match ($name) {
'items' => $this->hasMany(Item::class, ['id' => 'item_ids'])->inverseOf('promotions'),
default => parent::relationQuery($name),
};
}

/** @return Item[] */
public function getItems(): array
{
return $this->relation('items');
}
}
2 changes: 1 addition & 1 deletion tests/Stubs/ActiveRecord/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* Class Item.
*/
final class Item extends ActiveRecord
class Item extends ActiveRecord
{
protected int $id;
protected string $name;
Expand Down
12 changes: 12 additions & 0 deletions tests/data/pgsql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 4851017

Please sign in to comment.