5
5
namespace Cycle \Schema \Tests \Generator ;
6
6
7
7
use Cycle \Schema \Compiler ;
8
+ use Cycle \Schema \Definition \Field ;
8
9
use Cycle \Schema \Definition \ForeignKey ;
9
10
use Cycle \Schema \Generator \ForeignKeys ;
10
11
use Cycle \Schema \Generator \RenderTables ;
@@ -18,7 +19,7 @@ abstract class ForeignKeysTest extends BaseTest
18
19
{
19
20
public function testTableSchemaShouldBeModified (): void
20
21
{
21
- $ author = Author::define ();
22
+ $ author = Author::defineWithUser ();
22
23
$ user = User::define ();
23
24
$ plain = Plain::define ();
24
25
@@ -31,7 +32,7 @@ public function testTableSchemaShouldBeModified(): void
31
32
32
33
$ fk = new ForeignKey ();
33
34
$ fk ->setTarget ('user ' );
34
- $ fk ->setInnerColumns (['id ' ]);
35
+ $ fk ->setInnerColumns (['user_id ' ]);
35
36
$ fk ->setOuterColumns (['id ' ]);
36
37
$ fk ->setAction ('CASCADE ' );
37
38
$ fk ->createIndex (true );
@@ -46,10 +47,67 @@ public function testTableSchemaShouldBeModified(): void
46
47
47
48
$ this ->assertStringContainsString ('authors ' , $ expectedFk ->getTable ());
48
49
$ this ->assertStringContainsString ('users ' , $ expectedFk ->getForeignTable ());
49
- $ this ->assertSame (['id ' ], $ expectedFk ->getColumns ());
50
+ $ this ->assertSame (['user_id ' ], $ expectedFk ->getColumns ());
50
51
$ this ->assertSame (['id ' ], $ expectedFk ->getForeignKeys ());
51
52
$ this ->assertSame ('CASCADE ' , $ expectedFk ->getDeleteRule ());
52
53
$ this ->assertSame ('CASCADE ' , $ expectedFk ->getUpdateRule ());
53
54
$ this ->assertTrue ($ expectedFk ->hasIndex ());
54
55
}
56
+
57
+ public function testCreateIndex (): void
58
+ {
59
+ $ author = Author::defineWithUser ();
60
+ $ user = User::define ();
61
+ $ user ->getFields ()->set ('u_other_id ' , (new Field ())->setType ('integer ' )->setColumn ('other_id ' ));
62
+ $ plain = Plain::define ();
63
+
64
+ $ registry = new Registry ($ this ->dbal );
65
+ $ registry ->register ($ author )->linkTable ($ author , 'default ' , 'authors ' );
66
+ $ registry ->register ($ user )->linkTable ($ user , 'default ' , 'users ' );
67
+ $ registry ->register ($ plain )->linkTable ($ plain , 'default ' , 'plain ' );
68
+
69
+ $ this ->assertSame ([], $ registry ->getTableSchema ($ author )->getForeignKeys ());
70
+
71
+ $ fk = new ForeignKey ();
72
+ $ fk ->setTarget ('user ' );
73
+ $ fk ->setInnerColumns (['user_id ' ]);
74
+ $ fk ->setOuterColumns (['other_id ' ]);
75
+ $ fk ->setAction ('CASCADE ' );
76
+ $ fk ->createIndex (true );
77
+
78
+ $ author ->getForeignKeys ()->set ($ fk );
79
+
80
+ $ compiler = new Compiler ();
81
+ $ compiler ->compile ($ registry , [new RenderTables (), new ForeignKeys ()]);
82
+
83
+ $ this ->assertCount (1 , $ registry ->getTableSchema ($ user )->getIndexes ());
84
+ }
85
+
86
+ public function testShouldNotCreateIndexOnPk (): void
87
+ {
88
+ $ author = Author::defineWithUser ();
89
+ $ user = User::define ();
90
+ $ plain = Plain::define ();
91
+
92
+ $ registry = new Registry ($ this ->dbal );
93
+ $ registry ->register ($ author )->linkTable ($ author , 'default ' , 'authors ' );
94
+ $ registry ->register ($ user )->linkTable ($ user , 'default ' , 'users ' );
95
+ $ registry ->register ($ plain )->linkTable ($ plain , 'default ' , 'plain ' );
96
+
97
+ $ this ->assertSame ([], $ registry ->getTableSchema ($ author )->getForeignKeys ());
98
+
99
+ $ fk = new ForeignKey ();
100
+ $ fk ->setTarget ('user ' );
101
+ $ fk ->setInnerColumns (['user_id ' ]);
102
+ $ fk ->setOuterColumns (['id ' ]);
103
+ $ fk ->setAction ('CASCADE ' );
104
+ $ fk ->createIndex (true );
105
+
106
+ $ author ->getForeignKeys ()->set ($ fk );
107
+
108
+ $ compiler = new Compiler ();
109
+ $ compiler ->compile ($ registry , [new RenderTables (), new ForeignKeys ()]);
110
+
111
+ $ this ->assertEmpty ($ registry ->getTableSchema ($ user )->getIndexes ());
112
+ }
55
113
}
0 commit comments