diff --git a/.phpunit.result.cache b/.phpunit.result.cache new file mode 100644 index 0000000..4917ec4 --- /dev/null +++ b/.phpunit.result.cache @@ -0,0 +1 @@ +{"version":1,"defects":{"CreateTest::testTable":5},"times":{"CreateTest::testTable":0.041,"DeleteTest::testAll":0.049,"DeleteTest::testWhere":0.07,"DeleteTest::testLike":0.052,"InsertTest::testInsert":0.048,"MySqlTest::testConstructor with data set #0":0.015,"MySqlTest::testConstructor with data set #1":0.002,"MySqlTest::testQueryAndFetch":0.005,"SelectTest::testAll":0.007,"SelectTest::testWhere":0.003,"SelectTest::testLike":0.003}} \ No newline at end of file diff --git a/src/Example.php b/src/Example.php deleted file mode 100644 index 6a1f845..0000000 --- a/src/Example.php +++ /dev/null @@ -1,6 +0,0 @@ -MySql = $_MySql; + } + + public function Table(string $tableName, array $valuesName, array $valuesType) + { + $sql = $this->generateSql($tableName, $valuesName, $valuesType); + + return $this->MySql->query($sql); + } + + protected function generateSql(string $tableName, array $valuesName, array $valuesType) + { + $valuesName = $this->removeSpaces($valuesName); + + $sql = "CREATE TABLE "; + $sql .= $tableName . " ( "; + foreach ($valuesName as $key => $value) { + $sql .= $value . " " . $valuesType[$key] . " "; + + $sql .= $key === array_key_last($valuesName) ? ");" : ", "; + } + + return $sql; + } + + protected function removeSpaces($valueToCheck) + { + return str_replace(" ", "", $valueToCheck); + } +} diff --git a/src/classes/MySql.php b/src/classes/MySql.php index 4016062..26c214c 100644 --- a/src/classes/MySql.php +++ b/src/classes/MySql.php @@ -9,7 +9,7 @@ class MySql private string $password; private string $database; - protected \mysqli $mysqli; + public \mysqli $mysqli; function __construct( string $_hostname, diff --git a/tests/CreateTest.php b/tests/CreateTest.php new file mode 100644 index 0000000..8101577 --- /dev/null +++ b/tests/CreateTest.php @@ -0,0 +1,34 @@ +MySql = new MySql("localhost", "root", "MySql", "world", $state); + $this->Create = new Create($this->MySql); + } + + public function testTable() + { + //Drop table, case exists + $this->dropTestTable(); + + $this->assertTrue($this->Create->Table("Test", ["Value 1", "Value 2"], ["varchar(20)", "varchar(20)"])); + + $this->assertFalse($this->Create->Table("Test", ["Value 1", "Value 2"], ["varchar(20)", "varchar(20)"])); + } + + public function dropTestTable() + { + $this->MySql->query("DROP TABLE Test"); + } +} diff --git a/tests/InsertTest.php b/tests/InsertTest.php index fbdb86c..178b480 100644 --- a/tests/InsertTest.php +++ b/tests/InsertTest.php @@ -27,8 +27,6 @@ public function testInsert() $this->assertFalse($this->Insert->Insert("NoTable", [], [])); //Delete the Temporary Table - $this->MySql->queryAndFetch("DROP TABLE temptable"); - - + $this->MySql->queryAndFetch("DROP TABLE temptable"); } }