1+ <?php
2+
3+ namespace Generator \Normalization \Processors ;
4+
5+ use N3XT0R \MigrationGenerator \Service \Generator \Definition \Entity \FieldEntity ;
6+ use N3XT0R \MigrationGenerator \Service \Generator \Definition \Entity \PrimaryKeyEntity ;
7+ use N3XT0R \MigrationGenerator \Service \Generator \Definition \Entity \ResultEntity ;
8+ use N3XT0R \MigrationGenerator \Service \Generator \Normalization \Context \NormalizationContext ;
9+ use N3XT0R \MigrationGenerator \Service \Generator \Normalization \Processors \PivotProcessor ;
10+ use N3XT0R \MigrationGenerator \Service \Generator \Normalization \Processors \ProcessorInterface ;
11+ use Tests \TestCase ;
12+
13+ class PivotProcessorTest extends TestCase
14+ {
15+ protected ProcessorInterface $ processor ;
16+
17+ protected function setUp (): void
18+ {
19+ parent ::setUp ();
20+ $ this ->processor = new PivotProcessor ();
21+ }
22+
23+ public function test_pivot_processor_replaces_primary_with_id_and_adds_unique_index (): void
24+ {
25+ $ pk = new PrimaryKeyEntity ();
26+ $ pk ->setColumns (['role_id ' , 'user_id ' ]);
27+
28+ $ roleId = new FieldEntity ();
29+ $ roleId ->setType ('bigInteger ' );
30+ $ roleId ->setColumnName ('role_id ' );
31+ $ roleId ->setOptions (['nullable ' => false , 'unsigned ' => true , 'default ' => null ]);
32+
33+ $ userId = new FieldEntity ();
34+ $ userId ->setType ('bigInteger ' );
35+ $ userId ->setColumnName ('user_id ' );
36+ $ userId ->setOptions (['nullable ' => false , 'unsigned ' => true , 'default ' => null ]);
37+
38+ $ result = new ResultEntity ();
39+ $ result ->setTableName ('role_customer ' );
40+ $ result ->setResults ([
41+ 'role_customer ' => [
42+ 'table ' => [
43+ 'role_id ' => $ roleId ,
44+ 'user_id ' => $ userId ,
45+ ],
46+ 'primaryKey ' => ['PRIMARY ' => $ pk ],
47+ ],
48+ ]);
49+
50+ $ context = new NormalizationContext ($ result );
51+ $ table = (new PivotProcessor ())->process ($ context )->getResultByTable ('role_customer ' );
52+
53+ $ this ->assertArrayHasKey ('id ' , $ table ['table ' ]);
54+ $ this ->assertArrayNotHasKey ('primaryKey ' , $ table );
55+ $ this ->assertNotEmpty ($ table ['index ' ]);
56+
57+ $ unique = array_filter ($ table ['index ' ], fn ($ i ) => $ i ->getType () === 'unique ' );
58+ $ this ->assertSame (['role_id ' , 'user_id ' ], array_values ($ unique )[0 ]->getColumns ());
59+ }
60+
61+
62+ }
0 commit comments