44
55namespace 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 ;
1214use Cycle \ORM \Entity \Behavior \Tests \Fixtures \OptimisticLock \InquiredRelations \Product ;
1315use Cycle \ORM \Entity \Behavior \Tests \Fixtures \OptimisticLock \InquiredRelations \ProductBox ;
14- use Cycle \ORM \Entity \Behavior \Tests \Fixtures \OptimisticLock \InquiredRelations \ProductRepository ;
1516use Cycle \ORM \Entity \Behavior \Tests \Functional \Driver \Common \BaseListenerTest ;
1617use 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 ;
1921use Cycle \ORM \Schema ;
2022use 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
2538abstract 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