From 1235bd41480cb2d52151d8d520d3f28ce8fd10c2 Mon Sep 17 00:00:00 2001 From: Gregorio Chiko Putra Date: Thu, 31 Aug 2017 13:21:38 +0700 Subject: [PATCH] Create database connection using configuration then do a test --- App/Config.php | 11 +++++++ Core/Model.php | 64 +++++++++++++++++++++++++++++++++++++++ _tests/unit/ModelTest.php | 18 +++++++++++ 3 files changed, 93 insertions(+) create mode 100644 App/Config.php create mode 100644 Core/Model.php create mode 100644 _tests/unit/ModelTest.php diff --git a/App/Config.php b/App/Config.php new file mode 100644 index 0000000..fc9c12b --- /dev/null +++ b/App/Config.php @@ -0,0 +1,11 @@ +setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + } catch (PDOException $e) { + echo $e->getMessage(); + } + } + + return $conn; + } + + public function createTable() + { + $sql = "CREATE TABLE IF NOT EXISTS pengumuman ( + id int(3) NOT NULL AUTO_INCREMENT, + category int(3) NOT NULL, + created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + expire_at timestamp NOT NULL, + creator int(3) NOT NULL, + edited_at timestamp, + editor int(3), + content varchar(255) NOT NULL, + + PRIMARY KEY (id) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"; + + $db = static::connectDB(); + $query = $db->prepare($sql); + + if ($query->execute()) { + return true; + } + return false; + } + + public function dropTable() + { + $sql = "DROP TABLE IF EXISTS pengumuman"; + + $db = static::connectDB(); + $query = $db->prepare($sql); + + if ($query->execute()) { + return true; + } + return false; + } +} diff --git a/_tests/unit/ModelTest.php b/_tests/unit/ModelTest.php new file mode 100644 index 0000000..3a31ded --- /dev/null +++ b/_tests/unit/ModelTest.php @@ -0,0 +1,18 @@ +assertTrue($model->createTable()); + $this->assertTrue($model->dropTable()); + } +}