diff --git a/src/Bdd.php b/src/Bdd.php index 70a2ade01dd49e5a148927b01afa6725c6ea1e6f..4707b45276100592bb38efc5021a35831605e441 100644 --- a/src/Bdd.php +++ b/src/Bdd.php @@ -11,6 +11,7 @@ use Unicaen\BddAdmin\Ddl\DdlFilters; use Unicaen\BddAdmin\Driver\DriverInterface; use Unicaen\BddAdmin\Event\EventManagerAwareTrait; use Unicaen\BddAdmin\Exception\BddCompileException; +use Unicaen\BddAdmin\Exception\BddException; use Unicaen\BddAdmin\Logger\DefaultLogger; use Unicaen\BddAdmin\Logger\LoggerAwareTrait; use Unicaen\BddAdmin\Manager\CompilableInterface; @@ -498,6 +499,23 @@ class Bdd + public function normalizeObjectName(string|array $object): string + { + if (is_string($object)) { + [$schema, $name] = Util::explodedFullObjectName($object); + }else{ + if (isset($object['name'])){ + throw new BddException('Propriété d\'objet "name" non fournie'); + } + $schema = $object['schema'] ?? null; + $name = $object['name']; + } + + return Util::fullObjectName($schema, $name); + } + + + public function data(): DataManager { if (!isset($this->data)) { diff --git a/src/Manager/AbstractManager.php b/src/Manager/AbstractManager.php index 0bbdc407c75f9686e9b4ba861b349c8f1e99b9a7..d918035ee0eec465b6ff3ff9eab7f3b20fb0ab92 100644 --- a/src/Manager/AbstractManager.php +++ b/src/Manager/AbstractManager.php @@ -4,6 +4,8 @@ namespace Unicaen\BddAdmin\Manager; use Unicaen\BddAdmin\Bdd; use Unicaen\BddAdmin\Event\EventManagerAwareTrait; +use Unicaen\BddAdmin\Exception\BddException; +use Unicaen\BddAdmin\Util; abstract class AbstractManager implements ManagerInterface @@ -75,6 +77,7 @@ abstract class AbstractManager implements ManagerInterface public function exists(string $name): bool { + $name = $this->bdd->normalizeObjectName($name); $list = $this->getList($name); return count($list) == 1 && in_array($name, $list); diff --git a/tests/AbstractBddProtocoleTestCase.php b/tests/AbstractBddProtocoleTestCase.php index 1c4c6e41ebd0d2c4056cb873870ed4c67506dba5..3dba83862b2a927c5aae2dbea77aa9e37597f340 100644 --- a/tests/AbstractBddProtocoleTestCase.php +++ b/tests/AbstractBddProtocoleTestCase.php @@ -33,6 +33,7 @@ abstract class AbstractBddProtocoleTestCase extends AbstractBddTestCase $tested = true; } if (isset($action['expected'])) { + $action['expected'] = $this->normalizeExpected($action['expected']); if (is_array($action['expected'])) { $this->assertArrayEquals($action['expected'], $result); } else { @@ -56,6 +57,22 @@ abstract class AbstractBddProtocoleTestCase extends AbstractBddTestCase + protected function normalizeExpected(array $expected): array + { + + $res = []; + foreach( $expected as $k => $v ){ + if (is_int($k) && is_string($v)){ + $res[$k] = $this->bdd->normalizeObjectName($v); + }elseif(is_string($k)){ + $res[$this->bdd->normalizeObjectName($k)] = $v; + } + } + return $res; + } + + + public function testProtocole() { foreach ($this->protocole as $action) { diff --git a/tests/PostgresqlTest.php b/tests/PostgresqlTest.php index a128cb3898f511a78b4ff736cecda47910bc9f11..0ec3fd9ba3125c6a5dea4de2728913feda66e392 100644 --- a/tests/PostgresqlTest.php +++ b/tests/PostgresqlTest.php @@ -8,8 +8,6 @@ final class PostgresqlTest extends AbstractBddProtocoleTestCase { parent::setUp(); - $sql = "DROP SCHEMA IF EXISTS bddadmintests CASCADE"; - - $this->bdd->exec($sql); + $this->bdd->drop(); } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 77b0a53f1db09e94bcc3b3a782902ff94192a630..e6f201180292ee5d038542a15c8a7ee201d3a271 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -43,7 +43,7 @@ class BddAdminTest // Fichier présent dans le répertoire de config de l'application $filename = dirname(dirname(dirname(dirname(__DIR__)))) . '/config/autoload/bddadmin-tests.local.php'; - var_dump($filename); + //var_dump($filename); if (file_exists($filename)) { return require $filename; } diff --git a/tests/protocole/Postgresql.php b/tests/protocole/Postgresql.php index 5fa974f4fb48e98eed36c8c0af3c742b82504e9f..b6dc372f7afd6c841c41cc8e70ffbd698463bd6d 100644 --- a/tests/protocole/Postgresql.php +++ b/tests/protocole/Postgresql.php @@ -1,8 +1,9 @@ <?php $schema = 'bddadmintests'; +//$schema = 'public'; -$sequence = 'album_id_seq'; +$sequence = $schema.'_album_id_seq'; $tableName = 'taux_remu'; @@ -76,13 +77,13 @@ $table = [ ], ]; -$tableOriginale = $table; +$tableOriginale = $table; $tableOriginale['name'] = $tableName; -$tableModified = $tableOriginale; +$tableModified = $tableOriginale; $tableModified['commentaire'] = 'comm table modifié'; unset($tableModified['columns']['histo_createur_id']); -$tableModified['columns']['libelle2'] = [ +$tableModified['columns']['libelle2'] = [ 'name' => 'libelle2', 'type' => 'string', 'bdd-type' => 'character varying', @@ -94,21 +95,21 @@ $tableModified['columns']['libelle2'] = [ 'position' => 5, 'commentaire' => 'comm de lib2', ]; -$tableModified['columns']['libelle']['length'] = 100; -$tableModified['columns']['libelle']['nullable'] = true; -$tableModified['columns']['libelle']['default'] = "'mon libellé'"; +$tableModified['columns']['libelle']['length'] = 100; +$tableModified['columns']['libelle']['nullable'] = true; +$tableModified['columns']['libelle']['default'] = "'mon libellé'"; $tableModified['columns']['histo_creation']['bdd-type'] = 'timestamp with time zone'; $viewName = 'v_' . $tableName; -$view = ['schema' => $schema, 'name' => $viewName . '1', 'definition' => 'SELECT * FROM ' . $schema . '.' . $tableName]; +$view = ['schema' => $schema, 'name' => $viewName . '1', 'definition' => 'SELECT * FROM ' . $schema . '.' . $tableName]; $mviewName = 'mv_' . $tableName; -$mview = ['schema' => $schema, 'name' => $mviewName . '1', 'definition' => 'SELECT * FROM ' . $schema . '.' . $tableName]; +$mview = ['schema' => $schema, 'name' => $mviewName . '1', 'definition' => 'SELECT * FROM ' . $schema . '.' . $tableName]; $indexName = 'libelle_idx'; -$index = [ +$index = [ 'schema' => $schema, 'name' => $indexName . '1', 'table' => $tableName, @@ -116,12 +117,11 @@ $index = [ ]; - $functionName = 'insert_into_taux_remu'; -$function = [ - 'schema' => $schema, - 'name' => $functionName.'1', - 'definition' => "CREATE OR REPLACE FUNCTION $schema.$functionName"."1( +$function = [ + 'schema' => $schema, + 'name' => $functionName . '1', + 'definition' => "CREATE OR REPLACE FUNCTION $schema.$functionName" . "1( p_id integer, p_code varchar, p_libelle varchar, @@ -136,40 +136,44 @@ $$ LANGUAGE plpgsql", ]; -$triggerName = 'update_libelle_trigger'; +$triggerName = 'update_libelle_trigger'; $triggerFunction = [ - 'schema' => $schema, - 'name' => 'append_lib_to_libelle', + 'schema' => $schema, + 'name' => 'append_lib_to_libelle', 'definition' => "CREATE OR REPLACE FUNCTION $schema.append_lib_to_libelle() RETURNS TRIGGER AS $$ BEGIN NEW.libelle := NEW.libelle || '_lib'; RETURN NEW; END; -$$ LANGUAGE plpgsql;" +$$ LANGUAGE plpgsql;", ]; $trigger = [ - 'schema' => $schema, - 'name' => $triggerName.'1', - 'definition' => "CREATE TRIGGER $triggerName"."1 + 'schema' => $schema, + 'name' => $triggerName . '1', + 'definition' => "CREATE TRIGGER $triggerName" . "1 BEFORE INSERT OR UPDATE ON $schema.$tableName FOR EACH ROW -EXECUTE FUNCTION $schema.append_lib_to_libelle();" +EXECUTE FUNCTION $schema.append_lib_to_libelle();", ]; -return [ - // création du schéma - [ + +$tests = []; + + +if ($schema != 'public') { +// création du schéma + $tests[] = [ 'manager' => 'schema', 'method' => 'create', 'args' => [ ['name' => $schema . '1'], ], 'test-exists' => $schema . '1', - ], + ]; - [ + $tests[] = [ 'manager' => 'schema', 'method' => 'rename', 'args' => [ @@ -177,315 +181,320 @@ return [ $schema, ], 'test-exists' => $schema, - ], + ]; - [ + $tests[] = [ 'manager' => 'schema', 'method' => 'getList', 'expected' => [$schema], - ], + ]; - [ + + $tests[] = [ 'manager' => 'schema', 'method' => 'get', 'expected' => [$schema => ['name' => $schema]], - ], + ]; +} - // création de la séquence - [ - 'manager' => 'sequence', - 'method' => 'create', - 'args' => [ - ['schema' => $schema, 'name' => $sequence], - ], - 'test-exists' => $schema . '.' . $sequence +// création de la séquence +$tests[] = [ + 'manager' => 'sequence', + 'method' => 'create', + 'args' => [ + ['schema' => $schema, 'name' => $sequence], ], + 'test-exists' => $schema . '.' . $sequence, +]; - [ - 'manager' => 'sequence', - 'method' => 'getList', - 'expected' => [$schema . '.' . $sequence], - ], +$tests[] = [ + 'manager' => 'sequence', + 'method' => 'getList', + 'expected' => [$schema . '.' . $sequence], +]; - [ - 'manager' => 'sequence', - 'method' => 'get', - 'expected' => [$schema . '.' . $sequence => ['schema' => $schema, 'name' => $sequence]], - ], +$tests[] = [ + 'manager' => 'sequence', + 'method' => 'get', + 'expected' => [$schema . '.' . $sequence => ['schema' => $schema, 'name' => $sequence]], +]; - // destruction de la séquence - [ - 'manager' => 'sequence', - 'method' => 'drop', - 'args' => [ - $schema . '.' . $sequence, - ], - 'test-not-exists' => $schema . '.' . $sequence, +// destruction de la séquence +$tests[] = [ + 'manager' => 'sequence', + 'method' => 'drop', + 'args' => [ + $schema . '.' . $sequence, ], + 'test-not-exists' => $schema . '.' . $sequence, +]; - // Création de la table - [ - 'manager' => 'table', - 'method' => 'create', - 'args' => [ - $table - ], - 'test-exists' => $schema . '.' . $tableName . '1', +// Création de la table +$tests[] = [ + 'manager' => 'table', + 'method' => 'create', + 'args' => [ + $table, ], + 'test-exists' => $schema . '.' . $tableName . '1', +]; - // renommage de la table - [ - 'manager' => 'table', - 'method' => 'rename', - 'args' => [ - $schema . '.' . $tableName . '1', - $schema . '.' . $tableName, - ], - 'test-exists' => $schema . '.' . $tableName, - 'test-not-exists' => $schema . '.' . $tableName . '1', +// renommage de la table +$tests[] = [ + 'manager' => 'table', + 'method' => 'rename', + 'args' => [ + $schema . '.' . $tableName . '1', + $schema . '.' . $tableName, ], + 'test-exists' => $schema . '.' . $tableName, + 'test-not-exists' => $schema . '.' . $tableName . '1', +]; - // Test de création de vues - [ - 'manager' => 'view', - 'method' => 'create', - 'args' => [ - $view - ], - 'test-exists' => $schema . '.' . $viewName . '1', +// Test de création de vues +$tests[] = [ + 'manager' => 'view', + 'method' => 'create', + 'args' => [ + $view, ], + 'test-exists' => $schema . '.' . $viewName . '1', +]; - // renommage de vue - [ - 'manager' => 'view', - 'method' => 'rename', - 'args' => [ - $schema . '.' . $viewName . '1', - $schema . '.' . $viewName, - ], - 'test-not-exists' => $schema . '.' . $viewName . '1', - 'test-exists' => $schema . '.' . $viewName, +// renommage de vue +$tests[] = [ + 'manager' => 'view', + 'method' => 'rename', + 'args' => [ + $schema . '.' . $viewName . '1', + $schema . '.' . $viewName, ], + 'test-not-exists' => $schema . '.' . $viewName . '1', + 'test-exists' => $schema . '.' . $viewName, +]; - // Test de suppression de vues - [ - 'manager' => 'view', - 'method' => 'drop', - 'args' => [ - $schema . '.' . $viewName - ], - 'test-not-exists' => $schema . '.' . $viewName, +// Test de suppression de vues +$tests[] = [ + 'manager' => 'view', + 'method' => 'drop', + 'args' => [ + $schema . '.' . $viewName, ], + 'test-not-exists' => $schema . '.' . $viewName, +]; - // Test de création de vues matérialisées - [ - 'manager' => 'materialized-view', - 'method' => 'create', - 'args' => [ - $mview - ], - 'test-exists' => $schema . '.' . $mviewName . '1', +// Test de création de vues matérialisées +$tests[] = [ + 'manager' => 'materialized-view', + 'method' => 'create', + 'args' => [ + $mview, ], + 'test-exists' => $schema . '.' . $mviewName . '1', +]; - // renommage de vue matérialisée - [ - 'manager' => 'materialized-view', - 'method' => 'rename', - 'args' => [ - $schema . '.' . $mviewName . '1', - $schema . '.' . $mviewName, - ], - 'test-not-exists' => $schema . '.' . $mviewName . '1', - 'test-exists' => $schema . '.' . $mviewName, +// renommage de vue matérialisée +$tests[] = [ + 'manager' => 'materialized-view', + 'method' => 'rename', + 'args' => [ + $schema . '.' . $mviewName . '1', + $schema . '.' . $mviewName, ], + 'test-not-exists' => $schema . '.' . $mviewName . '1', + 'test-exists' => $schema . '.' . $mviewName, +]; - // Test de suppression de vues matérialisées - [ - 'manager' => 'materialized-view', - 'method' => 'drop', - 'args' => [ - $schema . '.' . $mviewName - ], - 'test-not-exists' => $schema . '.' . $mviewName, +// Test de suppression de vues matérialisées +$tests[] = [ + 'manager' => 'materialized-view', + 'method' => 'drop', + 'args' => [ + $schema . '.' . $mviewName, ], + 'test-not-exists' => $schema . '.' . $mviewName, +]; - // Modifications sur la table - [ - 'manager' => 'table', - 'method' => 'alter', - 'args' => [ - $tableOriginale, - $tableModified, - ], - +// Modifications sur la table +$tests[] = [ + 'manager' => 'table', + 'method' => 'alter', + 'args' => [ + $tableOriginale, + $tableModified, ], - // test d'application des modifs de table - [ - 'manager' => 'table', - 'method' => 'get', - 'args' => [ - [$schema . '.' . $tableName], - ], - 'expected' => [ - $schema . '.' . $tableName => $tableModified, - ], +]; + +// test d'application des modifs de table +$tests[] = [ + 'manager' => 'table', + 'method' => 'get', + 'args' => [ + [$schema . '.' . $tableName], ], + 'expected' => [ + $schema . '.' . $tableName => $tableModified, + ], +]; - // tests d'index - [ - 'manager' => 'index', - 'method' => 'create', - 'args' => [ - $index - ], - 'test-exists' => $schema . '.' . $indexName . '1', +// tests d'index +$tests[] = [ + 'manager' => 'index', + 'method' => 'create', + 'args' => [ + $index, ], + 'test-exists' => $schema . '.' . $indexName . '1', +]; - [ - 'manager' => 'index', - 'method' => 'rename', - 'args' => [ - $schema . '.' . $indexName . '1', - $schema . '.' . $indexName - ], - 'test-exists' => $schema . '.' . $indexName, - 'test-not-exists' => $schema . '.' . $indexName . '1', +$tests[] = [ + 'manager' => 'index', + 'method' => 'rename', + 'args' => [ + $schema . '.' . $indexName . '1', + $schema . '.' . $indexName, ], + 'test-exists' => $schema . '.' . $indexName, + 'test-not-exists' => $schema . '.' . $indexName . '1', +]; - [ - 'manager' => 'index', - 'method' => 'drop', - 'args' => [ - $schema . '.' . $indexName - ], - 'test-not-exists' => $indexName, +$tests[] = [ + 'manager' => 'index', + 'method' => 'drop', + 'args' => [ + $schema . '.' . $indexName, ], + 'test-not-exists' => $indexName, +]; - // tests functions - [ - 'manager' => 'function', - 'method' => 'create', - 'args' => [ - $function, - ], - 'test-exists' => $schema . '.' . $functionName.'1', +// tests functions +$tests[] = [ + 'manager' => 'function', + 'method' => 'create', + 'args' => [ + $function, ], + 'test-exists' => $schema . '.' . $functionName . '1', +]; - [ - 'manager' => 'function', - 'method' => 'rename', - 'args' => [ - $schema . '.' . $functionName.'1', - $schema . '.' . $functionName, - ], - 'test-not-exists' => $schema . '.' . $functionName.'1', - 'test-exists' => $schema . '.' . $functionName, +$tests[] = [ + 'manager' => 'function', + 'method' => 'rename', + 'args' => [ + $schema . '.' . $functionName . '1', + $schema . '.' . $functionName, ], + 'test-not-exists' => $schema . '.' . $functionName . '1', + 'test-exists' => $schema . '.' . $functionName, +]; - [ - 'manager' => 'function', - 'method' => 'drop', - 'args' => [ - $schema . '.' . $functionName, - ], - 'test-not-exists' => $schema . '.' . $functionName, +$tests[] = [ + 'manager' => 'function', + 'method' => 'drop', + 'args' => [ + $schema . '.' . $functionName, ], + 'test-not-exists' => $schema . '.' . $functionName, +]; - // Tests des triggers - [ - 'manager' => 'function', - 'method' => 'create', - 'args' => [ - $triggerFunction, - ], - ], - [ - 'manager' => 'trigger', - 'method' => 'create', - 'args' => [ - $trigger - ], - 'test-exists' => $schema.'.'.$triggerName.'1', - ], - [ - 'manager' => 'trigger', - 'method' => 'rename', - 'args' => [ - $triggerName.'1', - $triggerName, - $schema.'.'.$tableName - ], - 'test-not-exists' => $schema.'.'.$triggerName.'1', - 'test-exists' => $schema.'.'.$triggerName, +// Tests des triggers +$tests[] = [ + 'manager' => 'function', + 'method' => 'create', + 'args' => [ + $triggerFunction, ], - [ - 'manager' => 'trigger', - 'method' => 'disable', - 'args' => [ - [ - 'name' => $triggerName, - 'table' => $tableName, - 'schema' => $schema, - ] - ], +]; +$tests[] = [ + 'manager' => 'trigger', + 'method' => 'create', + 'args' => [ + $trigger, ], - [ - 'manager' => 'trigger', - 'method' => 'enable', - 'args' => [ - [ - 'name' => $triggerName, - 'table' => $tableName, - 'schema' => $schema, - ] + 'test-exists' => $schema . '.' . $triggerName . '1', +]; +$tests[] = [ + 'manager' => 'trigger', + 'method' => 'rename', + 'args' => [ + $triggerName . '1', + $triggerName, + $schema . '.' . $tableName, + ], + 'test-not-exists' => $schema . '.' . $triggerName . '1', + 'test-exists' => $schema . '.' . $triggerName, +]; +$tests[] = [ + 'manager' => 'trigger', + 'method' => 'disable', + 'args' => [ + [ + 'name' => $triggerName, + 'table' => $tableName, + 'schema' => $schema, ], ], - [ - 'manager' => 'trigger', - 'method' => 'drop', - 'args' => [ - $triggerName, - $schema.'.'.$tableName +]; +$tests[] = [ + 'manager' => 'trigger', + 'method' => 'enable', + 'args' => [ + [ + 'name' => $triggerName, + 'table' => $tableName, + 'schema' => $schema, ], - 'test-not-exists' => $schema.'.'.$triggerName, ], - [ - 'manager' => 'function', - 'method' => 'drop', - 'args' => [ - $schema.'.append_lib_to_libelle', - ], +]; +$tests[] = [ + 'manager' => 'trigger', + 'method' => 'drop', + 'args' => [ + $triggerName, + $schema . '.' . $tableName, + ], + 'test-not-exists' => $schema . '.' . $triggerName, +]; +$tests[] = [ + 'manager' => 'function', + 'method' => 'drop', + 'args' => [ + $schema . '.append_lib_to_libelle', ], +]; - // Destruction de la table - [ - 'manager' => 'table', - 'method' => 'drop', - 'args' => [ - $schema . '.' . $tableName - ], - 'test-not-exists' => $schema . '.' . $tableName, +// Destruction de la table +$tests[] = [ + 'manager' => 'table', + 'method' => 'drop', + 'args' => [ + $schema . '.' . $tableName, ], + 'test-not-exists' => $schema . '.' . $tableName, +]; - // destruction du schéma - [ +// destruction du schéma +if ($schema != 'public') { + $tests[] = [ 'manager' => 'schema', 'method' => 'drop', 'args' => [ $schema, ], 'test-not-exists' => $schema, - ], -]; \ No newline at end of file + ]; +} + +return $tests; \ No newline at end of file