Skip to content

Commit fb08604

Browse files
committed
Starting working on ambiguity exception
1 parent bc613ff commit fb08604

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/SchemaAnalyzer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
/**
1616
* This class can analyze a database model.
17-
* In this class you will find:
17+
* In this class you will find.
1818
*
1919
* - Functions to automatically detect **junction tables**
2020
* - Functions to compute the shortest path between 2 tables based on the relationships stored in the schema.
@@ -66,6 +66,7 @@ public function __construct(AbstractSchemaManager $schemaManager, Cache $cache =
6666
/**
6767
* Detect all junctions tables in the schema.
6868
* A table is a junction table if:
69+
*
6970
* - it has exactly 2 foreign keys
7071
* - it has only 2 columns (or 3 columns if the third one is an autoincremented primary key).
7172
*
@@ -86,6 +87,7 @@ public function detectJunctionTables()
8687
/**
8788
* Returns true if $table is a junction table.
8889
* I.e:
90+
*
8991
* - it must have exactly 2 foreign keys
9092
* - it must have only 2 columns (or 3 columns if the third one is an autoincremented primary key).
9193
*
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
namespace Mouf\Database\SchemaAnalyzer;
3+
4+
5+
class ShortestPathAmbiguityException extends SchemaAnalyzerException
6+
{
7+
8+
}

tests/SchemaAnalyzerTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,5 +259,31 @@ public function testCache() {
259259
$r2 = $this->assertNotFalse($cache->fetch('mykey_shortest_role_right```role'));
260260
$this->assertTrue($r1 === $r2);
261261
}
262+
263+
/**
264+
* @expectedException \Mouf\Database\SchemaAnalyzer\ShortestPathAmbiguityException
265+
*/
266+
public function testAmbiguityException() {
267+
$schema = $this->getBaseSchema();
268+
269+
$role_right = $schema->createTable("role_right");
270+
$role_right->addColumn("role_id", "integer", array("unsigned" => true));
271+
$role_right->addColumn("right_id", "integer", array("unsigned" => true));
272+
$role_right->addForeignKeyConstraint($schema->getTable('role'), array("role_id"), array("id"), array("onUpdate" => "CASCADE"));
273+
$role_right->addForeignKeyConstraint($schema->getTable('right'), array("right_id"), array("id"), array("onUpdate" => "CASCADE"));
274+
$role_right->setPrimaryKey(["role_id", "right_id"]);
275+
276+
$role_right2 = $schema->createTable("role_right2");
277+
$role_right2->addColumn("role_id", "integer", array("unsigned" => true));
278+
$role_right2->addColumn("right_id", "integer", array("unsigned" => true));
279+
$role_right2->addForeignKeyConstraint($schema->getTable('role'), array("role_id"), array("id"), array("onUpdate" => "CASCADE"));
280+
$role_right2->addForeignKeyConstraint($schema->getTable('right'), array("right_id"), array("id"), array("onUpdate" => "CASCADE"));
281+
$role_right2->setPrimaryKey(["role_id", "right_id"]);
282+
283+
$schemaAnalyzer = new SchemaAnalyzer($schema);
284+
285+
$schemaAnalyzer->getShortestPath("role", "right");
286+
}
287+
262288
}
263289

0 commit comments

Comments
 (0)