Skip to content
Snippets Groups Projects
Commit db71f830 authored by Laurent Lecluse's avatar Laurent Lecluse
Browse files

- Renforcement du système de tests automatisés.

- [Fix] Meilleure gestion de la méthode exists, lorsqu'on traite sur le schéma public en Postgresql
parent db2ee2f5
Branches
Tags 1.1.2
No related merge requests found
Pipeline #35244 passed
......@@ -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)) {
......
......@@ -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);
......
......@@ -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) {
......
......@@ -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();
}
}
......@@ -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;
}
......
<?php
$schema = 'bddadmintests';
//$schema = 'public';
$sequence = 'album_id_seq';
$sequence = $schema.'_album_id_seq';
$tableName = 'taux_remu';
......@@ -116,7 +117,6 @@ $index = [
];
$functionName = 'insert_into_taux_remu';
$function = [
'schema' => $schema,
......@@ -146,7 +146,7 @@ BEGIN
NEW.libelle := NEW.libelle || '_lib';
RETURN NEW;
END;
$$ LANGUAGE plpgsql;"
$$ LANGUAGE plpgsql;",
];
$trigger = [
......@@ -155,21 +155,25 @@ $trigger = [
'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 [
$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,68 +181,70 @@ 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
[
$tests[] = [
'manager' => 'sequence',
'method' => 'create',
'args' => [
['schema' => $schema, 'name' => $sequence],
],
'test-exists' => $schema . '.' . $sequence
],
'test-exists' => $schema . '.' . $sequence,
];
[
$tests[] = [
'manager' => 'sequence',
'method' => 'getList',
'expected' => [$schema . '.' . $sequence],
],
];
[
$tests[] = [
'manager' => 'sequence',
'method' => 'get',
'expected' => [$schema . '.' . $sequence => ['schema' => $schema, 'name' => $sequence]],
],
];
// destruction de la séquence
[
$tests[] = [
'manager' => 'sequence',
'method' => 'drop',
'args' => [
$schema . '.' . $sequence,
],
'test-not-exists' => $schema . '.' . $sequence,
],
];
// Création de la table
[
$tests[] = [
'manager' => 'table',
'method' => 'create',
'args' => [
$table
$table,
],
'test-exists' => $schema . '.' . $tableName . '1',
],
];
// renommage de la table
[
$tests[] = [
'manager' => 'table',
'method' => 'rename',
'args' => [
......@@ -247,20 +253,20 @@ return [
],
'test-exists' => $schema . '.' . $tableName,
'test-not-exists' => $schema . '.' . $tableName . '1',
],
];
// Test de création de vues
[
$tests[] = [
'manager' => 'view',
'method' => 'create',
'args' => [
$view
$view,
],
'test-exists' => $schema . '.' . $viewName . '1',
],
];
// renommage de vue
[
$tests[] = [
'manager' => 'view',
'method' => 'rename',
'args' => [
......@@ -269,31 +275,31 @@ return [
],
'test-not-exists' => $schema . '.' . $viewName . '1',
'test-exists' => $schema . '.' . $viewName,
],
];
// Test de suppression de vues
[
$tests[] = [
'manager' => 'view',
'method' => 'drop',
'args' => [
$schema . '.' . $viewName
$schema . '.' . $viewName,
],
'test-not-exists' => $schema . '.' . $viewName,
],
];
// Test de création de vues matérialisées
[
$tests[] = [
'manager' => 'materialized-view',
'method' => 'create',
'args' => [
$mview
$mview,
],
'test-exists' => $schema . '.' . $mviewName . '1',
],
];
// renommage de vue matérialisée
[
$tests[] = [
'manager' => 'materialized-view',
'method' => 'rename',
'args' => [
......@@ -302,21 +308,21 @@ return [
],
'test-not-exists' => $schema . '.' . $mviewName . '1',
'test-exists' => $schema . '.' . $mviewName,
],
];
// Test de suppression de vues matérialisées
[
$tests[] = [
'manager' => 'materialized-view',
'method' => 'drop',
'args' => [
$schema . '.' . $mviewName
$schema . '.' . $mviewName,
],
'test-not-exists' => $schema . '.' . $mviewName,
],
];
// Modifications sur la table
[
$tests[] = [
'manager' => 'table',
'method' => 'alter',
'args' => [
......@@ -324,10 +330,10 @@ return [
$tableModified,
],
],
];
// test d'application des modifs de table
[
$tests[] = [
'manager' => 'table',
'method' => 'get',
'args' => [
......@@ -336,52 +342,52 @@ return [
'expected' => [
$schema . '.' . $tableName => $tableModified,
],
],
];
// tests d'index
[
$tests[] = [
'manager' => 'index',
'method' => 'create',
'args' => [
$index
$index,
],
'test-exists' => $schema . '.' . $indexName . '1',
],
];
[
$tests[] = [
'manager' => 'index',
'method' => 'rename',
'args' => [
$schema . '.' . $indexName . '1',
$schema . '.' . $indexName
$schema . '.' . $indexName,
],
'test-exists' => $schema . '.' . $indexName,
'test-not-exists' => $schema . '.' . $indexName . '1',
],
];
[
$tests[] = [
'manager' => 'index',
'method' => 'drop',
'args' => [
$schema . '.' . $indexName
$schema . '.' . $indexName,
],
'test-not-exists' => $indexName,
],
];
// tests functions
[
$tests[] = [
'manager' => 'function',
'method' => 'create',
'args' => [
$function,
],
'test-exists' => $schema . '.' . $functionName . '1',
],
];
[
$tests[] = [
'manager' => 'function',
'method' => 'rename',
'args' => [
......@@ -390,46 +396,46 @@ return [
],
'test-not-exists' => $schema . '.' . $functionName . '1',
'test-exists' => $schema . '.' . $functionName,
],
];
[
$tests[] = [
'manager' => 'function',
'method' => 'drop',
'args' => [
$schema . '.' . $functionName,
],
'test-not-exists' => $schema . '.' . $functionName,
],
];
// Tests des triggers
[
$tests[] = [
'manager' => 'function',
'method' => 'create',
'args' => [
$triggerFunction,
],
],
[
];
$tests[] = [
'manager' => 'trigger',
'method' => 'create',
'args' => [
$trigger
$trigger,
],
'test-exists' => $schema . '.' . $triggerName . '1',
],
[
];
$tests[] = [
'manager' => 'trigger',
'method' => 'rename',
'args' => [
$triggerName . '1',
$triggerName,
$schema.'.'.$tableName
$schema . '.' . $tableName,
],
'test-not-exists' => $schema . '.' . $triggerName . '1',
'test-exists' => $schema . '.' . $triggerName,
],
[
];
$tests[] = [
'manager' => 'trigger',
'method' => 'disable',
'args' => [
......@@ -437,10 +443,10 @@ return [
'name' => $triggerName,
'table' => $tableName,
'schema' => $schema,
]
],
],
[
];
$tests[] = [
'manager' => 'trigger',
'method' => 'enable',
'args' => [
......@@ -448,44 +454,47 @@ return [
'name' => $triggerName,
'table' => $tableName,
'schema' => $schema,
]
],
],
[
];
$tests[] = [
'manager' => 'trigger',
'method' => 'drop',
'args' => [
$triggerName,
$schema.'.'.$tableName
$schema . '.' . $tableName,
],
'test-not-exists' => $schema . '.' . $triggerName,
],
[
];
$tests[] = [
'manager' => 'function',
'method' => 'drop',
'args' => [
$schema . '.append_lib_to_libelle',
],
],
];
// Destruction de la table
[
$tests[] = [
'manager' => 'table',
'method' => 'drop',
'args' => [
$schema . '.' . $tableName
$schema . '.' . $tableName,
],
'test-not-exists' => $schema . '.' . $tableName,
],
];
// destruction du schéma
[
if ($schema != 'public') {
$tests[] = [
'manager' => 'schema',
'method' => 'drop',
'args' => [
$schema,
],
'test-not-exists' => $schema,
],
];
}
return $tests;
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment