From 836a4d899522628660fd936cae548a3634cb40cb Mon Sep 17 00:00:00 2001 From: Gregorio Chiko Putra Date: Thu, 31 Aug 2017 15:54:50 +0700 Subject: [PATCH] Modify Model set functions to abstract, create Access interactions, do test. --- App/Models/Access.php | 146 +++++++++++++++++++++++++++++++++++++ Core/Model.php | 10 +++ _tests/unit/AccessTest.php | 36 +++++++++ 3 files changed, 192 insertions(+) create mode 100644 App/Models/Access.php create mode 100644 _tests/unit/AccessTest.php diff --git a/App/Models/Access.php b/App/Models/Access.php new file mode 100644 index 0000000..9e5a2c6 --- /dev/null +++ b/App/Models/Access.php @@ -0,0 +1,146 @@ +createTable( + 'user', + [ + 'id int(3) NOT NULL AUTO_INCREMENT', + 'username varchar(25) NOT NULL', + 'password char(13)', + 'registered_at timestamp DEFAULT CURRENT_TIMESTAMP', + 'PRIMARY KEY (id)' + ] + ); + } + + public function showAll() + { + try { + $db = static::connectDB(); + + $sql = "SELECT id, username, registered_at FROM user"; + + if ($stmt = $db->query($sql)) { + $result = $stmt->fetchAll(\PDO::FETCH_ASSOC); + return true; + } + return false; + } catch (PDOException $e) { + echo $e->getMessage(); + } + } + + public function showSingle($id) + { + try { + $db = static::connectDB(); + + $sql = "SELECT id, username, registered_at FROM user WHERE id = ?"; + + $query = $db->prepare($sql); + + if ($query->execute([$id])) { + if ($query->rowCount() === 1) { + $result = $query->fetchAll(\PDO::FETCH_ASSOC); + return true; + } + } + return false; + } catch (PDOException $e) { + echo $e->getMessage(); + } + } + + public function entry($args) + { + if (count($args)) { + $keys = '`'.implode('`, `', array_keys($args)).'`'; + + $x = 1; + $values = ''; + foreach ($args as $field) { + $values .= '?'; + if ($x < count($args)) { + $values .= ', '; + } + $x++; + } + + try { + $db = static::connectDB(); + + $sql = "INSERT INTO user ({$keys}) VALUES ({$values})"; + + $query = $db->prepare($sql); + + $x = 1; + foreach ($args as $value) { + $query->bindValue($x, $value); + $x++; + } + + if ($query->execute()) { + return true; + } + return false; + } catch (PDOException $e) { + echo $e->getMessage(); + } + } + } + + public function update($args, $id) + { + if (count($args)) { + $keys = array_keys($args); + + $fields = []; + foreach ($keys as $key) { + $fields[] = $key.' = ?'; + } + + try { + $db = static::connectDB(); + + $sql = "UPDATE user SET ".implode(', ', $fields)." WHERE id = ?"; + + $query = $db->prepare($sql); + $x = 1; + foreach ($args as $value) { + $query->bindValue($x, $value); + $x++; + } + $query->bindValue($x, $id); + + if ($query->execute()) { + return true; + } + return false; + } catch (PDOException $e) { + echo $e->getMessage(); + } + } + } + + public function delete($id) + { + try { + $db = static::connectDB(); + + $sql = "DELETE FROM user WHERE id = ?"; + + $query = $db->prepare($sql); + + if ($query->execute([$id])) { + return true; + } + return false; + } catch (PDOException $e) { + echo $e->getMessage(); + } + } +} diff --git a/Core/Model.php b/Core/Model.php index 360b2ab..9d330b7 100644 --- a/Core/Model.php +++ b/Core/Model.php @@ -47,4 +47,14 @@ abstract class Model } return false; } + + abstract public function showAll(); + + abstract public function showSingle($id); + + abstract public function entry($args); + + abstract public function update($args, $id); + + abstract public function delete($id); } diff --git a/_tests/unit/AccessTest.php b/_tests/unit/AccessTest.php new file mode 100644 index 0000000..0e0c49d --- /dev/null +++ b/_tests/unit/AccessTest.php @@ -0,0 +1,36 @@ +assertTrue($access->showAll()); + + $args = [ + 'username' => 'gregorio', + 'password' => 'iniada13charc', + ]; + + $this->assertTrue($access->entry($args)); + + $args = [ + 'username' => 'masihgregorio', + 'password' => 'inijuga3belas' + ]; + + $this->assertTrue($access->update($args, 1)); + + $this->assertTrue($access->showSingle(1)); + + $this->assertTrue($access->delete(1)); + + $this->assertTrue($access->dropTable('user')); + } +}