Skip to content

Commit 8527d67

Browse files
committed
1 parent 78009d9 commit 8527d67

File tree

10 files changed

+445
-0
lines changed

10 files changed

+445
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case428;
6+
7+
use Cycle\ORM\Select;
8+
use Cycle\ORM\Tests\Functional\Driver\Common\BaseTest;
9+
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case428\Entity\Order;
10+
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case428\Entity\PurchaseOrder;
11+
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case428\Entity\PurchaseOrderItem;
12+
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\IntegrationTestTrait;
13+
use Cycle\ORM\Tests\Traits\TableTrait;
14+
15+
abstract class CaseTest extends BaseTest
16+
{
17+
use IntegrationTestTrait;
18+
use TableTrait;
19+
20+
public function testSelect(): void
21+
{
22+
// Order 1
23+
$order = (new Select($this->orm, Entity\Order::class))
24+
->load('items')
25+
->wherePK(1)
26+
->fetchOne();
27+
28+
// Check result
29+
$this->assertInstanceOf(Entity\Order::class, $order);
30+
$this->assertCount(2, $order->items);
31+
32+
// Order 2
33+
$order = (new Select($this->orm, Entity\Order::class))
34+
->load('items')
35+
->wherePK(2)
36+
->fetchOne();
37+
38+
// Check result
39+
$this->assertInstanceOf(Entity\Order::class, $order);
40+
$this->assertCount(1, $order->items);
41+
}
42+
43+
public function testSave(): void
44+
{
45+
/** @var Order $order */
46+
$order = (new Select($this->orm, Entity\Order::class))
47+
->wherePK(1)
48+
->load('items')
49+
->fetchOne();
50+
51+
$purchaseOrder = new PurchaseOrder('PO001');
52+
$orderItems = [];
53+
foreach ($order->items as $orderItem) {
54+
$purchaseOrderItem = new PurchaseOrderItem($orderItem->quantity);
55+
$purchaseOrderItem->orderItem = $orderItem;
56+
$purchaseOrder->items[] = $purchaseOrderItem;
57+
$orderItem->purchaseOrder = $purchaseOrder;
58+
$orderItem->status = 1;
59+
$orderItems[] = $orderItem;
60+
}
61+
62+
$this->save($purchaseOrder, ...$orderItems);
63+
}
64+
65+
public function setUp(): void
66+
{
67+
// Init DB
68+
parent::setUp();
69+
$this->makeTables();
70+
$this->fillData();
71+
72+
$this->loadSchema(__DIR__ . '/schema.php');
73+
}
74+
75+
private function makeTables(): void
76+
{
77+
// Make tables
78+
$this->makeTable('order', [
79+
'id' => 'primary', // autoincrement
80+
'number' => 'string',
81+
]);
82+
83+
$this->makeTable('order_item', [
84+
'id' => 'primary',
85+
'order_id' => 'int',
86+
'sku' => 'string',
87+
'quantity' => 'int',
88+
'purchase_order_id' => 'int,nullable',
89+
'status' => 'int',
90+
]);
91+
92+
$this->makeTable('purchase_order', [
93+
'id' => 'primary',
94+
'number' => 'string',
95+
]);
96+
97+
$this->makeTable('purchase_order_item', [
98+
'id' => 'primary',
99+
'purchase_order_id' => 'int',
100+
'order_item_id' => 'int,nullable',
101+
'quantity' => 'int',
102+
]);
103+
104+
$this->makeFK('order_item', 'order_id', 'order', 'id', 'NO ACTION', 'CASCADE');
105+
106+
$this->makeFK('purchase_order_item', 'purchase_order_id', 'purchase_order', 'id', 'NO ACTION', 'CASCADE');
107+
108+
$this->makeFK('purchase_order_item', 'order_item_id', 'order_item', 'id', 'NO ACTION', 'CASCADE');
109+
}
110+
111+
private function fillData(): void
112+
{
113+
$this->getDatabase()->table('order')->insertMultiple(
114+
['number'],
115+
[
116+
['O001'],
117+
['O002'],
118+
['O003'],
119+
],
120+
);
121+
$this->getDatabase()->table('order_item')->insertMultiple(
122+
['order_id', 'sku', 'quantity', 'purchase_order_id', 'status'],
123+
[
124+
[1, 'A', 1, null, 0],
125+
[1, 'B', 1, null, 0],
126+
[2, 'A', 2, null, 0],
127+
[3, 'B', 2, null, 0],
128+
],
129+
);
130+
}
131+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case428\Entity;
6+
7+
class Order
8+
{
9+
10+
public ?int $id = null;
11+
public string $number;
12+
/** @var iterable<OrderItem> */
13+
public iterable $items = [];
14+
15+
public function __construct(string $number)
16+
{
17+
$this->number = $number;
18+
}
19+
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case428\Entity;
6+
7+
class OrderItem
8+
{
9+
10+
public ?int $id = null;
11+
public int $order_id;
12+
public string $sku;
13+
public int $quantity = 1;
14+
public ?int $purchase_order_id = null;
15+
public ?PurchaseOrder $purchaseOrder = null;
16+
public int $status = 0;
17+
18+
public function __construct(string $sku, int $quantity = 1)
19+
{
20+
$this->sku = $sku;
21+
$this->quantity = $quantity;
22+
}
23+
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case428\Entity;
6+
7+
class PurchaseOrder
8+
{
9+
10+
public ?int $id = null;
11+
public string $number;
12+
/** @var iterable<PurchaseOrderItem> */
13+
public iterable $items = [];
14+
15+
public function __construct(string $number)
16+
{
17+
$this->number = $number;
18+
}
19+
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case428\Entity;
6+
7+
class PurchaseOrderItem
8+
{
9+
10+
public ?int $id = null;
11+
public int $purchase_order_id;
12+
public ?int $order_item_id = null;
13+
public ?OrderItem $orderItem = null;
14+
public int $quantity = 1;
15+
16+
public function __construct(int $quantity = 1)
17+
{
18+
$this->quantity = $quantity;
19+
}
20+
21+
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Cycle\ORM\Mapper\Mapper;
6+
use Cycle\ORM\Relation;
7+
use Cycle\ORM\SchemaInterface as Schema;
8+
use Cycle\ORM\Select\Repository;
9+
use Cycle\ORM\Select\Source;
10+
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case428\Entity\Order;
11+
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case428\Entity\OrderItem;
12+
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case428\Entity\PurchaseOrder;
13+
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case428\Entity\PurchaseOrderItem;
14+
15+
return [
16+
'order' => [
17+
Schema::ENTITY => Order::class,
18+
Schema::SOURCE => Source::class,
19+
Schema::DATABASE => 'default',
20+
Schema::TABLE => 'order',
21+
Schema::PRIMARY_KEY => ['id'],
22+
Schema::FIND_BY_KEYS => ['id'],
23+
Schema::COLUMNS => [
24+
'id' => 'id',
25+
'number' => 'number',
26+
],
27+
Schema::RELATIONS => [
28+
'items' => [
29+
Relation::TYPE => Relation::HAS_MANY,
30+
Relation::TARGET => 'order_item',
31+
Relation::COLLECTION_TYPE => 'array',
32+
Relation::LOAD => Relation::LOAD_PROMISE,
33+
Relation::SCHEMA => [
34+
Relation::CASCADE => true,
35+
Relation::NULLABLE => false,
36+
Relation::WHERE => [],
37+
Relation::ORDER_BY => [],
38+
Relation::INNER_KEY => ['id'],
39+
Relation::OUTER_KEY => 'order_id',
40+
],
41+
],
42+
],
43+
Schema::TYPECAST => [
44+
'id' => 'int',
45+
'number' => 'string',
46+
],
47+
Schema::SCHEMA => [],
48+
],
49+
'order_item' => [
50+
Schema::ENTITY => OrderItem::class,
51+
Schema::SOURCE => Source::class,
52+
Schema::DATABASE => 'default',
53+
Schema::MAPPER => Mapper::class,
54+
Schema::TABLE => 'order_item',
55+
Schema::PRIMARY_KEY => ['id'],
56+
Schema::FIND_BY_KEYS => ['id'],
57+
Schema::COLUMNS => [
58+
'id' => 'id',
59+
'order_id' => 'order_id',
60+
'sku' => 'sku',
61+
'quantity' => 'quantity',
62+
'purchase_order_id' => 'purchase_order_id',
63+
'status' => 'status',
64+
],
65+
Schema::RELATIONS => [
66+
'purchaseOrder' => [
67+
Relation::TYPE => Relation::BELONGS_TO,
68+
Relation::TARGET => 'purchase_order',
69+
Relation::LOAD => Relation::LOAD_PROMISE,
70+
Relation::SCHEMA => [
71+
Relation::CASCADE => true,
72+
Relation::NULLABLE => true,
73+
Relation::INNER_KEY => 'purchase_order_id',
74+
Relation::OUTER_KEY => ['id'],
75+
],
76+
],
77+
],
78+
Schema::TYPECAST => [
79+
'id' => 'int',
80+
'order_id' => 'int',
81+
'sku' => 'string',
82+
'quantity' => 'int',
83+
'purchase_order_id' => 'int',
84+
'status' => 'int',
85+
],
86+
Schema::SCHEMA => [],
87+
],
88+
'purchase_order' => [
89+
Schema::ENTITY => PurchaseOrder::class,
90+
Schema::MAPPER => Mapper::class,
91+
Schema::SOURCE => Source::class,
92+
Schema::REPOSITORY => Repository::class,
93+
Schema::DATABASE => 'default',
94+
Schema::TABLE => 'purchase_order',
95+
Schema::PRIMARY_KEY => ['id'],
96+
Schema::FIND_BY_KEYS => ['id'],
97+
Schema::COLUMNS => [
98+
'id' => 'id',
99+
'number' => 'number',
100+
],
101+
Schema::RELATIONS => [
102+
'items' => [
103+
Relation::TYPE => Relation::HAS_MANY,
104+
Relation::TARGET => 'purchase_order_item',
105+
Relation::COLLECTION_TYPE => 'array',
106+
Relation::LOAD => Relation::LOAD_PROMISE,
107+
Relation::SCHEMA => [
108+
Relation::CASCADE => true,
109+
Relation::NULLABLE => false,
110+
Relation::WHERE => [],
111+
Relation::ORDER_BY => [],
112+
Relation::INNER_KEY => ['id'],
113+
Relation::OUTER_KEY => 'purchase_order_id',
114+
],
115+
],
116+
],
117+
Schema::SCOPE => null,
118+
Schema::TYPECAST => [
119+
'id' => 'int',
120+
'number' => 'string',
121+
],
122+
Schema::SCHEMA => [],
123+
],
124+
'purchase_order_item' => [
125+
Schema::ENTITY => PurchaseOrderItem::class,
126+
Schema::MAPPER => Mapper::class,
127+
Schema::SOURCE => Source::class,
128+
Schema::REPOSITORY => Repository::class,
129+
Schema::DATABASE => 'default',
130+
Schema::TABLE => 'purchase_order_item',
131+
Schema::PRIMARY_KEY => ['id'],
132+
Schema::FIND_BY_KEYS => ['id'],
133+
Schema::COLUMNS => [
134+
'id' => 'id',
135+
'purchase_order_id' => 'purchase_order_id',
136+
'order_item_id' => 'order_item_id',
137+
'quantity' => 'quantity',
138+
],
139+
Schema::RELATIONS => [
140+
'orderItem' => [
141+
Relation::TYPE => Relation::BELONGS_TO,
142+
Relation::TARGET => 'order_item',
143+
Relation::LOAD => Relation::LOAD_PROMISE,
144+
Relation::SCHEMA => [
145+
Relation::CASCADE => true,
146+
Relation::NULLABLE => true,
147+
Relation::INNER_KEY => 'order_item_id',
148+
Relation::OUTER_KEY => ['id'],
149+
],
150+
],
151+
],
152+
Schema::SCOPE => null,
153+
Schema::TYPECAST => [
154+
'id' => 'primary',
155+
'purchase_order_id' => 'int',
156+
'order_item_id' => 'int',
157+
'quantity' => 'int',
158+
],
159+
Schema::SCHEMA => [],
160+
],
161+
];

0 commit comments

Comments
 (0)