1
1
<?php
2
2
namespace Mouf \Database \SchemaAnalyzer ;
3
3
4
+ use Doctrine \Common \Cache \ArrayCache ;
4
5
use Doctrine \DBAL \Schema \Schema ;
5
6
6
7
class SchemaAnalyzerTest extends \PHPUnit_Framework_TestCase
@@ -22,7 +23,7 @@ private function getBaseSchema() {
22
23
return $ schema ;
23
24
}
24
25
25
- public function testJointureTableDetectionWith2Columns () {
26
+ private function getCompleteSchemaManager () {
26
27
$ schema = $ this ->getBaseSchema ();
27
28
28
29
$ role_right = $ schema ->createTable ("role_right " );
@@ -32,7 +33,13 @@ public function testJointureTableDetectionWith2Columns() {
32
33
$ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
33
34
$ role_right ->setPrimaryKey (["role_id " , "right_id " ]);
34
35
35
- $ schemaAnalyzer = new SchemaAnalyzer ($ schema );
36
+ return new StubSchemaManager ($ schema );
37
+ }
38
+
39
+ public function testJointureTableDetectionWith2Columns () {
40
+ $ schemaManager = $ this ->getCompleteSchemaManager ();
41
+
42
+ $ schemaAnalyzer = new SchemaAnalyzer ($ schemaManager );
36
43
$ junctionTables = $ schemaAnalyzer ->detectJunctionTables ();
37
44
38
45
$ this ->assertCount (1 , $ junctionTables );
@@ -54,7 +61,7 @@ public function testJointureTableDetectionWith3ColumnsNoPrimaryKey() {
54
61
$ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
55
62
$ role_right ->setPrimaryKey (["role_id " , "right_id " ]);
56
63
57
- $ schemaAnalyzer = new SchemaAnalyzer ($ schema );
64
+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ( $ schema) );
58
65
$ junctionTables = $ schemaAnalyzer ->detectJunctionTables ();
59
66
60
67
$ this ->assertCount (0 , $ junctionTables );
@@ -72,7 +79,7 @@ public function testJointureTableDetectionWith3Columns() {
72
79
$ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
73
80
$ role_right ->setPrimaryKey (["id " ]);
74
81
75
- $ schemaAnalyzer = new SchemaAnalyzer ($ schema );
82
+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ( $ schema) );
76
83
$ junctionTables = $ schemaAnalyzer ->detectJunctionTables ();
77
84
78
85
$ this ->assertCount (1 , $ junctionTables );
@@ -94,7 +101,7 @@ public function testJointureTableDetectionWith3ColumnsNoAutoincrement() {
94
101
$ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
95
102
$ role_right ->setPrimaryKey (["id " ]);
96
103
97
- $ schemaAnalyzer = new SchemaAnalyzer ($ schema );
104
+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ( $ schema) );
98
105
$ junctionTables = $ schemaAnalyzer ->detectJunctionTables ();
99
106
100
107
$ this ->assertCount (0 , $ junctionTables );
@@ -113,7 +120,7 @@ public function testJointureTableDetectionWith4Columns() {
113
120
$ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
114
121
$ role_right ->setPrimaryKey (["id " ]);
115
122
116
- $ schemaAnalyzer = new SchemaAnalyzer ($ schema );
123
+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ( $ schema) );
117
124
$ junctionTables = $ schemaAnalyzer ->detectJunctionTables ();
118
125
119
126
$ this ->assertCount (0 , $ junctionTables );
@@ -130,7 +137,7 @@ public function testJointureTableDetectionWith2ColumnsAndOnePk() {
130
137
$ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
131
138
$ role_right ->setPrimaryKey (["role_id " ]);
132
139
133
- $ schemaAnalyzer = new SchemaAnalyzer ($ schema );
140
+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ( $ schema) );
134
141
$ junctionTables = $ schemaAnalyzer ->detectJunctionTables ();
135
142
136
143
$ this ->assertCount (0 , $ junctionTables );
@@ -148,7 +155,7 @@ public function testJointureTableDetectionWith2ColumnsWith2FkOnOneCol() {
148
155
$ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
149
156
$ role_right ->setPrimaryKey (["role_id " , "right_id " ]);
150
157
151
- $ schemaAnalyzer = new SchemaAnalyzer ($ schema );
158
+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ( $ schema) );
152
159
$ junctionTables = $ schemaAnalyzer ->detectJunctionTables ();
153
160
154
161
$ this ->assertCount (0 , $ junctionTables );
@@ -166,7 +173,7 @@ public function testJointureTableDetectionWith3ColumnsWithPkIsFk() {
166
173
$ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
167
174
$ role_right ->setPrimaryKey (["id " ]);
168
175
169
- $ schemaAnalyzer = new SchemaAnalyzer ($ schema );
176
+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ( $ schema) );
170
177
$ junctionTables = $ schemaAnalyzer ->detectJunctionTables ();
171
178
172
179
$ this ->assertCount (0 , $ junctionTables );
@@ -182,7 +189,7 @@ public function testShortestPathInJointure() {
182
189
$ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
183
190
$ role_right ->setPrimaryKey (["role_id " , "right_id " ]);
184
191
185
- $ schemaAnalyzer = new SchemaAnalyzer ($ schema );
192
+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ( $ schema) );
186
193
187
194
$ fks = $ schemaAnalyzer ->getShortestPath ("role " , "right " );
188
195
@@ -211,7 +218,7 @@ public function testShortestPathInLine() {
211
218
$ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
212
219
$ role_right ->setPrimaryKey (["role_id " , "right_id " ]);
213
220
214
- $ schemaAnalyzer = new SchemaAnalyzer ($ schema );
221
+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ( $ schema) );
215
222
216
223
$ fks = $ schemaAnalyzer ->getShortestPath ("role " , "role_right " );
217
224
@@ -225,5 +232,32 @@ public function testShortestPathInLine() {
225
232
$ this ->assertEquals ("role_right " , $ fks [0 ]->getLocalTable ()->getName ());
226
233
$ this ->assertEquals ("role " , $ fks [0 ]->getForeignTableName ());
227
234
}
235
+
236
+ /**
237
+ * @expectedException \Mouf\Database\SchemaAnalyzer\SchemaAnalyzerException
238
+ */
239
+ public function testWrongConstructor () {
240
+ $ schema = $ this ->getBaseSchema ();
241
+ new SchemaAnalyzer (new StubSchemaManager ($ schema ), new ArrayCache ());
242
+ }
243
+
244
+ public function testCache () {
245
+ $ cache = new ArrayCache ();
246
+
247
+ $ schemaManager = $ this ->getCompleteSchemaManager ();
248
+
249
+ $ schemaAnalyzer = new SchemaAnalyzer ($ schemaManager , $ cache , "mykey " );
250
+ $ schemaAnalyzer ->detectJunctionTables ();
251
+
252
+ $ this ->assertNotFalse ($ cache ->fetch ('mykey_schema ' ));
253
+ $ this ->assertNotFalse ($ cache ->fetch ('mykey_junctiontables ' ));
254
+ $ r1 = $ schemaAnalyzer ->getShortestPath ("role_right " , "role " );
255
+ $ r2 = $ schemaAnalyzer ->getShortestPath ("role_right " , "role " );
256
+ $ this ->assertTrue ($ r1 === $ r2 );
257
+
258
+ $ r1 = $ this ->assertNotFalse ($ cache ->fetch ('mykey_shortest_role_right```role ' ));
259
+ $ r2 = $ this ->assertNotFalse ($ cache ->fetch ('mykey_shortest_role_right```role ' ));
260
+ $ this ->assertTrue ($ r1 === $ r2 );
261
+ }
228
262
}
229
263
0 commit comments