From 731bdfcf82ded8560efecd0c67e2ae9b2acc6b04 Mon Sep 17 00:00:00 2001 From: absurdo Date: Fri, 27 Oct 2023 23:12:08 +0200 Subject: [PATCH] Added wtable in other file --- src/WPDO.php | 32 ++++++++--------------------- src/WTable.php | 19 +++++++++++++++++ tests/WPDOTest.php | 51 ++++++++++++++++++++++++++++++++++------------ 3 files changed, 65 insertions(+), 37 deletions(-) create mode 100644 src/WTable.php diff --git a/src/WPDO.php b/src/WPDO.php index af39391..9a0b1a1 100644 --- a/src/WPDO.php +++ b/src/WPDO.php @@ -2,21 +2,6 @@ namespace PhangoApp\WPDO; - -class WTable { - - public $table; - public $fields; - - public function __construct($table, $fields) { - - $this->name=$table; - $this->fields=$fields; - - } - -} - class WPDO { static public $host_db=''; @@ -49,19 +34,21 @@ class WPDO { echo 'Cannot connect to the mysqldb'; - exit(); + return false; } $this->conn=new \PDO(WPDO::$driver.':host='.WPDO::$host_db.';dbname='.WPDO::$db, WPDO::$login_db, WPDO::$pass_db); + return true; + } public function query($query, $values) { $this->sth=$this->conn->prepare($query); - $this->sth->execute([$values]); + //$this->sth->execute([$values]); return $this->sth->execute($values); @@ -74,8 +61,6 @@ class WPDO { $query='select '.$str_fields.' from '.$this->table->name.' '.$where_sql; $this->sth=$this->conn->prepare($query); - - $this->sth->execute($values); return $this->sth->execute($values); @@ -128,7 +113,7 @@ class WPDO { } public function check_fields($fields) { - + if($fields[0]!='*') { $final_fields=[]; @@ -142,19 +127,17 @@ class WPDO { } } - - $str_fields=implode(', ', $final_fields); } else { - $str_fields='*'; + $final_fields=['*']; } if(count($final_fields)==0) { - throw new Exception('No valid fields selected in '.$this->table->name); + throw new \Exception('No valid fields selected in '.$this->table->name.' Valid fields are '.implode(', ', $this->table->fields).' . Fields passed are: '.implode(', ', $fields)); } @@ -163,3 +146,4 @@ class WPDO { } } + diff --git a/src/WTable.php b/src/WTable.php new file mode 100644 index 0000000..d3f8972 --- /dev/null +++ b/src/WTable.php @@ -0,0 +1,19 @@ +name=$table; + $this->fields=$fields; + + } + +} + diff --git a/tests/WPDOTest.php b/tests/WPDOTest.php index 1a29400..d2adb26 100644 --- a/tests/WPDOTest.php +++ b/tests/WPDOTest.php @@ -1,6 +1,7 @@ connect(); - final class WPDOTest extends TestCase { + public function testConnect() + { + + global $pdo; + + $table=new WTable('table_test', ['name', 'last_name', 'type']); + + $pdo=new WPDO($table); + + $this->assertTrue($pdo->connect()); + + } + public function testCreateTable() { global $pdo; + + $sql_table="DROP TABLE IF EXISTS table_test; + CREATE TABLE table_test ( + `name` VARCHAR(255) NOT NULL default '', + `last_name` VARCHAR(255) NOT NULL default '', + `type` INT NOT NULL + );"; - $this->assertTrue($pdo->query($sql_table)); + $this->assertTrue($pdo->query($sql_table, [])); } + /** + * @depends testCreateTable + */ + + public function testInsert() { + + global $pdo; + + $this->assertEquals('table_test', $pdo->table->name); + + $this->assertTrue($pdo->insert(['name', 'last_name', 'type'], ['first', 'last', '1'])); + + } + /** * @depends testCreateTable */ @@ -37,7 +62,7 @@ final class WPDOTest extends TestCase { { global $pdo; - $this->assertTrue($pdo->query('drop table table_test')); + $this->assertTrue($pdo->query('drop table table_test', [])); }