From 4b12be61610d09e5a3857b29e6b00064f1b7434b Mon Sep 17 00:00:00 2001 From: Angel Date: Tue, 30 Mar 2021 18:16:24 +0800 Subject: [PATCH] [feature] Medoo test case --- .gitignore | 8 +++++- composer.json | 13 ++++++++- phpunit.xml | 22 +++++++++++++++ tests/MedooTestCase.php | 62 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 phpunit.xml create mode 100644 tests/MedooTestCase.php diff --git a/.gitignore b/.gitignore index 2f1704f0..ad20d736 100644 --- a/.gitignore +++ b/.gitignore @@ -216,4 +216,10 @@ pip-log.txt .mr.developer.cfg #php-cs-fixer -/.php_cs \ No newline at end of file +/.php_cs + +#PHPUnit +/.phpunit.cache + +/composer.lock +/vendor \ No newline at end of file diff --git a/composer.json b/composer.json index 2bb0d08f..cd04fb10 100644 --- a/composer.json +++ b/composer.json @@ -16,6 +16,9 @@ "php": ">=5.4", "ext-pdo": "*" }, + "require-dev": { + "phpunit/phpunit": "^9.0" + }, "suggest": { "ext-pdo_mysql": "For MySQL or MariaDB database", "ext-pdo_sqlsrv": "For MSSQL database on both Window/Liunx platform", @@ -27,7 +30,15 @@ }, "autoload": { "psr-4": { - "Medoo\\": "/src" + "Medoo\\": "src/" } + }, + "autoload-dev": { + "psr-4": { + "Medoo\\Tests\\": "tests/" + } + }, + "scripts": { + "test": "phpunit" } } \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 00000000..67cb1aeb --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,22 @@ + + + + + ./tests + + + + + + src + + + diff --git a/tests/MedooTestCase.php b/tests/MedooTestCase.php new file mode 100644 index 00000000..a6da4b8c --- /dev/null +++ b/tests/MedooTestCase.php @@ -0,0 +1,62 @@ +database = new Medoo([ + 'testMode' => true + ]); + } + + public function typesProvider(): array + { + return [ + 'MySQL' => ['mysql'], + 'MSSQL' => ['mssql'], + 'SQLite' => ['sqlite'], + 'PostgreSQL' => ['pgsql'], + 'Oracle' => ['oracle'] + ]; + } + + public function setType($type) : void + { + $this->database->type = $type; + } + + public function expectedQuery($expected) : string + { + $identifier = [ + 'mysql' => '`$1`', + 'mssql' => '[$1]' + ]; + + return preg_replace( + '/"((?![_\d])[\p{N}\p{L}_]+)"/u', + $identifier[$this->database->type] ?? '"$1"', + $expected + ); + } + + public function assertQuery($expected, $query) : void + { + if (is_array($expected)) { + + $this->assertEquals( + $this->expectedQuery($expected[$this->database->type] ?? $expected['default']), + $query + ); + } + else { + $this->assertEquals($this->expectedQuery($expected), $query); + } + } +} \ No newline at end of file