Skip to content

Commit

Permalink
v1.1.0 - Add Create Table
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDevick committed Oct 31, 2021
1 parent 02f276e commit 0c4944d
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 10 deletions.
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -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}}
6 changes: 0 additions & 6 deletions src/Example.php

This file was deleted.

40 changes: 40 additions & 0 deletions src/classes/Create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace DinoDev\MySql\Classes;

class Create extends MySql
{
protected MySql $MySql;

public function __construct(MySql $_MySql)
{
$this->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);
}
}
2 changes: 1 addition & 1 deletion src/classes/MySql.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class MySql
private string $password;
private string $database;

protected \mysqli $mysqli;
public \mysqli $mysqli;

function __construct(
string $_hostname,
Expand Down
34 changes: 34 additions & 0 deletions tests/CreateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use PHPUnit\Framework\TestCase;
use DinoDev\MySql\Classes\MySql;
use DinoDev\MySql\Classes\Create;

require_once __DIR__ . "/../vendor/autoload.php";

class CreateTest extends TestCase
{
public MySql $MySql;
public Create $Create;

protected function setUp(): void
{
$this->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");
}
}
4 changes: 1 addition & 3 deletions tests/InsertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

0 comments on commit 0c4944d

Please sign in to comment.