Skip to content

Commit 4b16dc0

Browse files
committed
Cleanup tests
1 parent aa705e3 commit 4b16dc0

File tree

4 files changed

+94
-138
lines changed

4 files changed

+94
-138
lines changed

tests/Behavior/Fixtures/OptimisticLock/InquiredRelations/Product.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,26 @@
1111
use Modules\Catalog\Domain\Entity\BoxItem;
1212
use Shared\Infrastructure\ValueObject\Sort;
1313

14-
#[Entity]
15-
#[OptimisticLock(field: OptimisticLock::RULE_INCREMENT)]
14+
#[Entity(table: 'products')]
15+
#[OptimisticLock(field: 'revision', rule: OptimisticLock::RULE_INCREMENT)]
1616
class Product
1717
{
1818
#[Column(type: 'primary')]
1919
public int $id;
2020

21-
#[Column(type: 'string')]
21+
#[Column(type: 'string', nullable: true)]
2222
public ?string $name=null;
2323

24+
#[Column(type: 'integer', name: 'parent_id', nullable: true)]
25+
public ?int $parentId = null;
26+
2427
#[HasMany(
2528
target: ProductBox::class,
2629
innerKey: 'id',
2730
outerKey: 'parentId'
2831
)]
2932
public array $boxItems = [];
33+
3034
#[Column(type: 'integer', name: 'revision')]
3135
public int $revision;
3236

tests/Behavior/Fixtures/OptimisticLock/InquiredRelations/ProductBox.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,35 @@
1010
use Cycle\ORM\Entity\Behavior\OptimisticLock;
1111
use Modules\Catalog\Domain\Entity\Element;
1212

13-
#[Entity]
13+
#[Entity(table: 'product_boxes')]
1414
class ProductBox
1515
{
1616
#[Column(type: 'primary')]
1717
public int $id;
1818

19+
#[Column(type: 'integer', name: 'parent_id')]
20+
public int $parentId;
21+
22+
#[Column(type: 'integer', name: 'box_item_id')]
23+
public int $boxItemId;
24+
25+
#[Column(type: 'integer')]
26+
public int $count;
27+
1928
#[BelongsTo(
2029
target: Product::class,
2130
innerKey: 'parentId',
2231
outerKey: 'id'
2332
)]
2433
public Product $parent;
2534

26-
public int $parentId;
27-
2835
#[BelongsTo(
2936
target: Product::class,
3037
innerKey: 'boxItemId',
3138
outerKey: 'id'
3239
)]
3340
public Product $boxItem;
3441

35-
public int $boxItemId;
36-
37-
public int $count;
38-
3942
public function __construct(
4043
Product $parent,
4144
Product $boxItem,

tests/Behavior/Fixtures/OptimisticLock/InquiredRelations/ProductRepository.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/Behavior/Functional/Driver/Common/OptimisticLock/InquiredRelationTest.php

Lines changed: 77 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,36 @@
44

55
namespace Cycle\ORM\Entity\Behavior\Tests\Functional\Driver\Common\OptimisticLock;
66

7-
use Cycle\ORM\Entity\Behavior\Exception\OptimisticLock\ChangedVersionException;
8-
use Cycle\ORM\Entity\Behavior\Exception\OptimisticLock\RecordIsLockedException;
9-
use Cycle\ORM\Entity\Behavior\Listener\OptimisticLock;
10-
use Cycle\ORM\Entity\Behavior\Tests\Fixtures\OptimisticLock\Comment;
11-
use Cycle\ORM\Entity\Behavior\Tests\Fixtures\OptimisticLock\Author;
7+
use Cycle\Annotated\Embeddings;
8+
use Cycle\Annotated\Entities;
9+
use Cycle\Annotated\MergeColumns;
10+
use Cycle\Annotated\MergeIndexes;
11+
use Cycle\ORM\Collection\ArrayCollectionFactory;
12+
use Cycle\ORM\Config\RelationConfig;
13+
use Cycle\ORM\Entity\Behavior\EventDrivenCommandGenerator;
1214
use Cycle\ORM\Entity\Behavior\Tests\Fixtures\OptimisticLock\InquiredRelations\Product;
1315
use Cycle\ORM\Entity\Behavior\Tests\Fixtures\OptimisticLock\InquiredRelations\ProductBox;
14-
use Cycle\ORM\Entity\Behavior\Tests\Fixtures\OptimisticLock\InquiredRelations\ProductRepository;
1516
use Cycle\ORM\Entity\Behavior\Tests\Functional\Driver\Common\BaseListenerTest;
1617
use Cycle\ORM\Entity\Behavior\Tests\Traits\TableTrait;
17-
use Cycle\ORM\EntityManager;
18-
use Cycle\ORM\Heap\Heap;
18+
use Cycle\ORM\Entity\Behavior\Tests\Utils\SimpleContainer;
19+
use Cycle\ORM\Factory;
20+
use Cycle\ORM\ORM;
1921
use Cycle\ORM\Schema;
2022
use Cycle\ORM\SchemaInterface;
21-
use Cycle\ORM\Relation;
22-
use Cycle\ORM\Select;
23-
use Cycle\ORM\Transaction;
23+
use Cycle\Schema\Compiler;
24+
use Cycle\Schema\Generator\GenerateModifiers;
25+
use Cycle\Schema\Generator\GenerateRelations;
26+
use Cycle\Schema\Generator\GenerateTypecast;
27+
use Cycle\Schema\Generator\RenderModifiers;
28+
use Cycle\Schema\Generator\RenderRelations;
29+
use Cycle\Schema\Generator\RenderTables;
30+
use Cycle\Schema\Generator\ResetTables;
31+
use Cycle\Schema\Generator\SyncTables;
32+
use Cycle\Schema\Generator\ValidateEntities;
33+
use Cycle\Schema\Registry;
34+
use Spiral\Attributes\AttributeReader;
35+
use Spiral\Tokenizer\Config\TokenizerConfig;
36+
use Spiral\Tokenizer\Tokenizer;
2437

2538
abstract class InquiredRelationTest extends BaseListenerTest
2639
{
@@ -30,140 +43,88 @@ public function setUp(): void
3043
{
3144
parent::setUp();
3245

33-
$this->makeTable(
34-
'products2',
35-
[
36-
'id' => 'primary',
37-
'name' => 'string,nullable',
38-
'revision' => 'int',
39-
]
46+
$schema = $this->compileSchema(new Tokenizer(new TokenizerConfig([
47+
'directories' => [dirname(__DIR__, 4) . '/Fixtures/OptimisticLock/InquiredRelations'],
48+
'exclude' => [],
49+
])));
50+
51+
$this->orm = new ORM(
52+
new Factory(
53+
$this->dbal,
54+
RelationConfig::getDefault(),
55+
null,
56+
new ArrayCollectionFactory(),
57+
),
58+
$schema,
59+
new EventDrivenCommandGenerator($schema, new SimpleContainer()),
4060
);
41-
42-
$this->makeTable(
43-
'product_boxes',
44-
[
45-
'id' => 'primary',
46-
'parent_id' => 'int',
47-
'box_item_id' => 'int',
48-
'count' => 'int',
49-
]
50-
);
51-
$this->withSchema(new Schema([
52-
Product::class => [
53-
SchemaInterface::ROLE => 'product2',
54-
SchemaInterface::DATABASE => 'default',
55-
SchemaInterface::REPOSITORY => ProductRepository::class,
56-
SchemaInterface::TABLE => 'products2',
57-
SchemaInterface::PRIMARY_KEY => 'id',
58-
SchemaInterface::COLUMNS => [
59-
'id' => 'id',
60-
'name' => 'name',
61-
'revision' => 'revision',
62-
],
63-
SchemaInterface::LISTENERS => [
64-
[
65-
OptimisticLock::class,
66-
[
67-
'field' => 'revision',
68-
'rule' => OptimisticLock::RULE_INCREMENT
69-
]
70-
]
71-
],
72-
SchemaInterface::TYPECAST => [
73-
'id' => 'int',
74-
'revision' => 'int'
75-
],
76-
SchemaInterface::SCHEMA => [],
77-
SchemaInterface::RELATIONS => [
78-
'boxItems' => [
79-
Relation::TYPE => Relation::HAS_MANY,
80-
Relation::TARGET => ProductBox::class,
81-
Relation::LOAD => Relation::LOAD_EAGER,
82-
Relation::SCHEMA => [
83-
Relation::INNER_KEY => 'id',
84-
Relation::OUTER_KEY => 'parentId',
85-
],
86-
]
87-
],
88-
],
89-
ProductBox::class => [
90-
SchemaInterface::ENTITY => ProductBox::class,
91-
SchemaInterface::DATABASE => 'default',
92-
SchemaInterface::TABLE => 'product_boxes',
93-
SchemaInterface::PRIMARY_KEY => ['id'],
94-
SchemaInterface::COLUMNS => [
95-
'parentId' => 'parent_id',
96-
'boxItemId' => 'box_item_id',
97-
'count' => 'count',
98-
],
99-
SchemaInterface::LISTENERS => [],
100-
SchemaInterface::TYPECAST => [
101-
'id' => 'int',
102-
'parentId' => 'int',
103-
'boxItemId' => 'int',
104-
'count' => 'int'
105-
],
106-
SchemaInterface::SCHEMA => [],
107-
SchemaInterface::RELATIONS => [
108-
'parent' => [
109-
Relation::TYPE => Relation::REFERS_TO,
110-
Relation::TARGET => Product::class,
111-
Relation::SCHEMA => [
112-
Relation::INNER_KEY => 'parentId',
113-
Relation::OUTER_KEY => 'id',
114-
],
115-
],
116-
'boxItem' => [
117-
Relation::TYPE => Relation::REFERS_TO,
118-
Relation::TARGET => Product::class,
119-
Relation::SCHEMA => [
120-
Relation::INNER_KEY => 'boxItemId',
121-
Relation::OUTER_KEY => 'id',
122-
],
123-
]
124-
],
125-
]
126-
]));
12761
}
12862

12963
public function testUpdateWithSelectCollection(): void
13064
{
131-
$em = new EntityManager($this->orm);
13265
$repo = $this->orm->getRepository(Product::class);
13366

13467
// Make 2 products
135-
$product1= new Product();
68+
$product1 = new Product();
13669
$product1->name = 'test';
13770
$product1->revision = 1;
13871

139-
$product2= new Product();
72+
$product2 = new Product();
14073
$product2->name = 'test2';
14174
$product2->revision = 1;
14275

14376
/// Add box item
144-
$product1->addBoxItem(new ProductBox(
145-
$product1,
146-
$product2
147-
));
77+
$product1->addBoxItem(
78+
new ProductBox(
79+
$product1,
80+
$product2,
81+
),
82+
);
14883

14984
// Persist 2 products
150-
$em->persist($product1)
151-
->persist($product2)
152-
->run();
85+
$this->save($product1, $product2);
15386

15487
/// Persist 2 with Fetch all products
15588
$this->assertEquals(1, $product1->revision);
89+
$this->assertEquals(1, $product2->revision);
15690
$product1->name = '222';
15791

15892
/// Persist 2
159-
$em->persist($product1)->run();
93+
$this->save($product1);
16094
$this->assertEquals(2, $product1->revision);
16195

16296
/// Persist 3 with Fetch all products
16397
$product1->name = '333';
16498

16599
$products = $repo->findAll();
166-
$em->persist($product1)->run();
100+
$this->save($product1);
167101
$this->assertEquals(3, $product1->revision);
168102
}
103+
104+
private function compileSchema(Tokenizer $tokenizer): SchemaInterface
105+
{
106+
$reader = new AttributeReader();
107+
$classLocator = $tokenizer->classLocator();
108+
return new Schema(
109+
(new Compiler())
110+
->compile(
111+
new Registry($this->dbal),
112+
[
113+
new Embeddings($classLocator, $reader),
114+
new Entities($classLocator, $reader),
115+
new ResetTables(),
116+
new MergeColumns($reader),
117+
new MergeIndexes($reader),
118+
new GenerateRelations(),
119+
new GenerateModifiers(),
120+
new ValidateEntities(),
121+
new RenderTables(),
122+
new RenderRelations(),
123+
new RenderModifiers(),
124+
new GenerateTypecast(),
125+
new SyncTables(),
126+
],
127+
),
128+
);
129+
}
169130
}

0 commit comments

Comments
 (0)