11<?php
22namespace Mouf \Database \SchemaAnalyzer ;
33
4+ use Doctrine \Common \Cache \ArrayCache ;
45use Doctrine \DBAL \Schema \Schema ;
56
67class SchemaAnalyzerTest extends \PHPUnit_Framework_TestCase
@@ -22,7 +23,7 @@ private function getBaseSchema() {
2223 return $ schema ;
2324 }
2425
25- public function testJointureTableDetectionWith2Columns () {
26+ private function getCompleteSchemaManager () {
2627 $ schema = $ this ->getBaseSchema ();
2728
2829 $ role_right = $ schema ->createTable ("role_right " );
@@ -32,7 +33,13 @@ public function testJointureTableDetectionWith2Columns() {
3233 $ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
3334 $ role_right ->setPrimaryKey (["role_id " , "right_id " ]);
3435
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 );
3643 $ junctionTables = $ schemaAnalyzer ->detectJunctionTables ();
3744
3845 $ this ->assertCount (1 , $ junctionTables );
@@ -54,7 +61,7 @@ public function testJointureTableDetectionWith3ColumnsNoPrimaryKey() {
5461 $ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
5562 $ role_right ->setPrimaryKey (["role_id " , "right_id " ]);
5663
57- $ schemaAnalyzer = new SchemaAnalyzer ($ schema );
64+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ( $ schema) );
5865 $ junctionTables = $ schemaAnalyzer ->detectJunctionTables ();
5966
6067 $ this ->assertCount (0 , $ junctionTables );
@@ -72,7 +79,7 @@ public function testJointureTableDetectionWith3Columns() {
7279 $ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
7380 $ role_right ->setPrimaryKey (["id " ]);
7481
75- $ schemaAnalyzer = new SchemaAnalyzer ($ schema );
82+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ( $ schema) );
7683 $ junctionTables = $ schemaAnalyzer ->detectJunctionTables ();
7784
7885 $ this ->assertCount (1 , $ junctionTables );
@@ -94,7 +101,7 @@ public function testJointureTableDetectionWith3ColumnsNoAutoincrement() {
94101 $ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
95102 $ role_right ->setPrimaryKey (["id " ]);
96103
97- $ schemaAnalyzer = new SchemaAnalyzer ($ schema );
104+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ( $ schema) );
98105 $ junctionTables = $ schemaAnalyzer ->detectJunctionTables ();
99106
100107 $ this ->assertCount (0 , $ junctionTables );
@@ -113,7 +120,7 @@ public function testJointureTableDetectionWith4Columns() {
113120 $ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
114121 $ role_right ->setPrimaryKey (["id " ]);
115122
116- $ schemaAnalyzer = new SchemaAnalyzer ($ schema );
123+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ( $ schema) );
117124 $ junctionTables = $ schemaAnalyzer ->detectJunctionTables ();
118125
119126 $ this ->assertCount (0 , $ junctionTables );
@@ -130,7 +137,7 @@ public function testJointureTableDetectionWith2ColumnsAndOnePk() {
130137 $ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
131138 $ role_right ->setPrimaryKey (["role_id " ]);
132139
133- $ schemaAnalyzer = new SchemaAnalyzer ($ schema );
140+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ( $ schema) );
134141 $ junctionTables = $ schemaAnalyzer ->detectJunctionTables ();
135142
136143 $ this ->assertCount (0 , $ junctionTables );
@@ -148,7 +155,7 @@ public function testJointureTableDetectionWith2ColumnsWith2FkOnOneCol() {
148155 $ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
149156 $ role_right ->setPrimaryKey (["role_id " , "right_id " ]);
150157
151- $ schemaAnalyzer = new SchemaAnalyzer ($ schema );
158+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ( $ schema) );
152159 $ junctionTables = $ schemaAnalyzer ->detectJunctionTables ();
153160
154161 $ this ->assertCount (0 , $ junctionTables );
@@ -166,7 +173,7 @@ public function testJointureTableDetectionWith3ColumnsWithPkIsFk() {
166173 $ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
167174 $ role_right ->setPrimaryKey (["id " ]);
168175
169- $ schemaAnalyzer = new SchemaAnalyzer ($ schema );
176+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ( $ schema) );
170177 $ junctionTables = $ schemaAnalyzer ->detectJunctionTables ();
171178
172179 $ this ->assertCount (0 , $ junctionTables );
@@ -182,7 +189,7 @@ public function testShortestPathInJointure() {
182189 $ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
183190 $ role_right ->setPrimaryKey (["role_id " , "right_id " ]);
184191
185- $ schemaAnalyzer = new SchemaAnalyzer ($ schema );
192+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ( $ schema) );
186193
187194 $ fks = $ schemaAnalyzer ->getShortestPath ("role " , "right " );
188195
@@ -211,7 +218,7 @@ public function testShortestPathInLine() {
211218 $ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
212219 $ role_right ->setPrimaryKey (["role_id " , "right_id " ]);
213220
214- $ schemaAnalyzer = new SchemaAnalyzer ($ schema );
221+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ( $ schema) );
215222
216223 $ fks = $ schemaAnalyzer ->getShortestPath ("role " , "role_right " );
217224
@@ -225,5 +232,32 @@ public function testShortestPathInLine() {
225232 $ this ->assertEquals ("role_right " , $ fks [0 ]->getLocalTable ()->getName ());
226233 $ this ->assertEquals ("role " , $ fks [0 ]->getForeignTableName ());
227234 }
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+ }
228262}
229263
0 commit comments