Skip to content

Commit 2591fae

Browse files
committed
Merge pull request #6 from moufmouf/improved_exceptions
Improved exception messages when table not found
2 parents 966a70f + d99e7ef commit 2591fae

File tree

7 files changed

+65
-4
lines changed

7 files changed

+65
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
composer.lock
22
/vendor/
3+
/build/

.travis.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@ language: php
22

33
php:
44
- 5.5
5+
- 5.6
6+
- 7.0
7+
8+
env:
9+
matrix:
10+
- PREFER_LOWEST="--prefer-lowest"
11+
- PREFER_LOWEST=""
512

613
before_script:
7-
- wget http://getcomposer.org/composer.phar
8-
- php composer.phar install --no-interaction
14+
- composer update --prefer-source $PREFER_LOWEST
915

1016
script:
11-
- mkdir -p build/logs
12-
- phpunit --coverage-clover build/logs/clover.xml
17+
- ./vendor/bin/phpunit
1318

1419
after_script:
1520
- php vendor/bin/coveralls -v

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"require": {
1717
"clue/graph": "~0.9.0",
1818
"doctrine/dbal": "~2.4",
19+
"doctrine/cache": "^1.4.1",
1920
"graphp/algorithms": "~0.8.0"
2021
},
2122
"require-dev": {

phpunit.xml.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@
2121
<directory suffix=".php">src</directory>
2222
</whitelist>
2323
</filter>
24+
25+
<logging>
26+
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
27+
<log type="coverage-clover" target="build/logs/clover.xml"/>
28+
</logging>
2429
</phpunit>

src/SchemaAnalyzer.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\DBAL\Schema\AbstractSchemaManager;
88
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
99
use Doctrine\DBAL\Schema\Schema;
10+
use Doctrine\DBAL\Schema\SchemaException;
1011
use Doctrine\DBAL\Schema\Table;
1112
use Fhaculty\Graph\Edge\Base;
1213
use Fhaculty\Graph\Graph;
@@ -229,6 +230,9 @@ public function getShortestPath($fromTable, $toTable)
229230
*/
230231
private function getShortestPathWithoutCache($fromTable, $toTable)
231232
{
233+
$this->checkTableExists($fromTable);
234+
$this->checkTableExists($toTable);
235+
232236
$graph = $this->buildSchemaGraph();
233237

234238
try {
@@ -278,6 +282,15 @@ private function getShortestPathWithoutCache($fromTable, $toTable)
278282
return $foreignKeys;
279283
}
280284

285+
private function checkTableExists($tableName)
286+
{
287+
try {
288+
$this->getSchema()->getTable($tableName);
289+
} catch (SchemaException $e) {
290+
throw SchemaAnalyzerTableNotFoundException::tableNotFound($tableName, $this->schema, $e);
291+
}
292+
}
293+
281294
private function buildSchemaGraph()
282295
{
283296
$graph = new Graph();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Mouf\Database\SchemaAnalyzer;
4+
5+
use Doctrine\DBAL\Schema\Schema;
6+
7+
class SchemaAnalyzerTableNotFoundException extends SchemaAnalyzerException
8+
{
9+
public static function tableNotFound($tableName, Schema $schema, \Exception $previousException = null)
10+
{
11+
$closestTableName = '';
12+
$closestScore = INF;
13+
foreach ($schema->getTables() as $testedTable) {
14+
$testedTableName = $testedTable->getName();
15+
$l = levenshtein($testedTableName, $tableName);
16+
if ($l < $closestScore) {
17+
$closestScore = $l;
18+
$closestTableName = $testedTableName;
19+
}
20+
}
21+
22+
return new self("Could not find table '$tableName'. Did you mean '$closestTableName'?", 0, $previousException);
23+
}
24+
}

tests/SchemaAnalyzerTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,4 +509,16 @@ public function testInheritanceRelationship()
509509
$this->assertEquals('user', $schemaAnalyzer->getChildrenRelationships('contact')[0]->getLocalTableName());
510510
$this->assertEquals([], $schemaAnalyzer->getChildrenRelationships('user'));
511511
}
512+
513+
/**
514+
* @expectedException \Mouf\Database\SchemaAnalyzer\SchemaAnalyzerTableNotFoundException
515+
* @expectedExceptionMessage Could not find table 'rights'. Did you mean 'right'?
516+
*/
517+
public function testWringTableName()
518+
{
519+
$schemaManager = $this->getCompleteSchemaManager();
520+
521+
$schemaAnalyzer = new SchemaAnalyzer($schemaManager);
522+
$junctionTables = $schemaAnalyzer->getShortestPath('role', 'rights');
523+
}
512524
}

0 commit comments

Comments
 (0)