Commit 47423484 authored by Laurent Lecluse's avatar Laurent Lecluse
Browse files

tests schémas OK

parent 2322e9f0
Loading
Loading
Loading
Loading
Loading
+109 −2
Original line number Diff line number Diff line
@@ -11,6 +11,10 @@ abstract class AbstractBddTestCase extends TestCase

    protected Bdd $bdd;

    private array $calc;



    protected function setUp(): void
    {
        $this->protocole = require(__DIR__ . '/protocole/' . $this->bddName . '.php');
@@ -24,15 +28,27 @@ abstract class AbstractBddTestCase extends TestCase
        $manager = $this->bdd->manager($action['manager']);

        try {
            call_user_func_array([$manager, $action['method']], $action['args']);
            $result = call_user_func_array([$manager, $action['method']], $action['args'] ?? []);

            $tested = false;
            if (isset($action['test-exists'])) {
                $exists = call_user_func_array([$manager, 'exists'], [$action['test-exists']]);
                $this->assertTrue($exists);
            }elseif (isset($action['test-not-exists'])){
                $tested = true;
            }
            if (isset($action['test-not-exists'])) {
                $exists = call_user_func_array([$manager, 'exists'], [$action['test-not-exists']]);
                $this->assertFalse($exists);
                $tested = true;
            }
            if (isset($action['expected'])) {
                if (is_array($action['expected'])){
                    $this->assertArrayEquals($action['expected'], $result, true);
                }else{
                    $this->assertEquals($action['expected'], $result);
                }
            }
            if (!$tested) {
                $this->assertTrue(true);
            }
        } catch (\Exception $e) {
@@ -50,4 +66,95 @@ abstract class AbstractBddTestCase extends TestCase
        }
    }



    public function assertArrayEquals(array $a1, array $a2, bool $strict = false, string $path = ''): bool
    {
        if ('' === $path) {
            $this->calc = $a1;
        }

        $k1 = array_keys($a1);
        $k2 = array_keys($a2);

        $diff = array_diff($k1, $k2);
        $hasInt = false;
        foreach ($diff as $d) {
            if (is_int($d)) {
                $hasInt = true;
            }
        }
        if (($strict || $hasInt) && !empty($diff)) {
            $kl = implode(',', $k1);
            return $this->error('Les tableaux n\'ont pas les mêmes clés (' . $path . ') : ' . $kl . ' en trop');
        }

        if (!empty(array_diff($k2, $k1))) {
            $kl = implode(',', array_diff($k2, $k1));
            return $this->error('Les tableaux n\'ont pas les mêmes cés (' . $path . ') : ' . $kl . ' manquants');
        }

        foreach ($k1 as $k) {
            if (!isset($a2[$k])) continue;

            $p = $path . '/' . $k;
            $a1Type = getType($a1[$k]);
            $a2Type = getType($a2[$k]);
            if ($a1Type != $a2Type) {
                return $this->error('Des valeurs ne sont pas du même type (' . $p . ') : ' . $a2Type . ' attendu pour ' . $a1Type . ' calculé');
            }
            if (is_array($a1[$k])) {
                if (!$this->assertArrayEquals($a1[$k], $a2[$k], $strict, $p)) {
                    return $this->error('Des sous-tableaux sont différentes (' . $p . ')');
                }
            } else {
                if ($a1[$k] !== $a2[$k]) {
                    return $this->error('Des valeurs sont différentes (' . $p . ') : ' . $a2[$k] . ' attendu pour ' . $a1[$k] . ' calculé');
                }
            }
        }

        $this->assertEquals(true, true);
        return true;
    }



    private function error(string $message): bool
    {
        echo 'Données calculées :' . "\n";
        echo $this->arrayExport($this->calc);
        $this->assertNotTrue(true, $message);

        return false;
    }



    public function arrayExport($var, string $indent = ""): string
    {
        switch (gettype($var)) {
            case "array":
                $indexed = array_keys($var) === range(0, count($var) - 1);
                $r = [];
                $maxKeyLen = 0;
                foreach ($var as $key => $value) {
                    $key = $this->arrayExport($key);
                    $keyLen = strlen($key);
                    if ($keyLen > $maxKeyLen) $maxKeyLen = $keyLen;
                }
                foreach ($var as $key => $value) {
                    $key = $this->arrayExport($key);
                    $r[] = "$indent    "
                        . ($indexed ? "" : str_pad($key, $maxKeyLen, ' ') . " => ")
                        . $this->arrayExport($value, "$indent    ");
                }

                return "[\n" . implode(",\n", $r) . ",\n" . $indent . "]";
            case "boolean":
                return $var ? "TRUE" : "FALSE";
            default:
                return var_export($var, true);
        }
    }
}
+12 −1
Original line number Diff line number Diff line
@@ -9,7 +9,18 @@ return [
        'args'    => [
            ['name' => $schema],
        ],
        'test-not-exists' => $schema,
    ],

    [
        'manager'  => 'schema',
        'method'   => 'getList',
        'expected' => [],
    ],

    [
        'manager'  => 'schema',
        'method'   => 'get',
        'expected' => [],
    ],

    [
+23 −1
Original line number Diff line number Diff line
@@ -7,11 +7,33 @@ return [
        'manager'     => 'schema',
        'method'      => 'create',
        'args'        => [
            ['name' => $schema],
            ['name' => $schema . '1'],
        ],
        'test-exists' => $schema . '1',
    ],

    [
        'manager'     => 'schema',
        'method'      => 'rename',
        'args'        => [
            $schema . '1',
            $schema,
        ],
        'test-exists' => $schema,
    ],

    [
        'manager'  => 'schema',
        'method'   => 'getList',
        'expected' => [$schema],
    ],

    [
        'manager'  => 'schema',
        'method'   => 'get',
        'expected' => [$schema => ['name' => $schema]],
    ],

    [
        'manager'         => 'schema',
        'method'          => 'drop',