Namespaces changes
This commit is contained in:
@ -17,7 +17,7 @@
|
|||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"ProVM\\": "src/"
|
"Database\\": "src/"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,15 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Concept\Database;
|
|
||||||
|
|
||||||
use ProVM\Concept\Database;
|
|
||||||
|
|
||||||
interface Connection
|
|
||||||
{
|
|
||||||
public function connect(): \PDO;
|
|
||||||
|
|
||||||
public function transaction(): Transaction;
|
|
||||||
|
|
||||||
public function query(string $query): ResultSet;
|
|
||||||
public function prepare(string $query): ResultSet;
|
|
||||||
public function execute(string $query, ?array $data = null): ResultSet;
|
|
||||||
}
|
|
@ -1,86 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Database;
|
|
||||||
|
|
||||||
use PDO;
|
|
||||||
use ProVM\Concept\Database;
|
|
||||||
use ProVM\Exception\Database\InvalidQuery;
|
|
||||||
|
|
||||||
class Connection implements Database\Connection
|
|
||||||
{
|
|
||||||
public function __construct(Database $database)
|
|
||||||
{
|
|
||||||
$this->setDatabase($database);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Database $database;
|
|
||||||
|
|
||||||
protected function getDatabase(): Database
|
|
||||||
{
|
|
||||||
return $this->database;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function setDatabase(Database $database): Database\Connection
|
|
||||||
{
|
|
||||||
$this->database = $database;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected PDO $pdo;
|
|
||||||
public function connect(): PDO
|
|
||||||
{
|
|
||||||
if (!isset($this->pdo)) {
|
|
||||||
$dsn = $this->getDatabase()->getDsn();
|
|
||||||
if ($this->getDatabase()->needsUser()) {
|
|
||||||
$this->pdo = new PDO($dsn, $this->getDatabase()->getUser(), $this->getDatabase()->getPassword());
|
|
||||||
} else {
|
|
||||||
$this->pdo = new PDO($dsn);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $this->pdo;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Database\Transaction $transaction;
|
|
||||||
|
|
||||||
public function transaction(): Database\Transaction
|
|
||||||
{
|
|
||||||
if (!isset($this->transaction)) {
|
|
||||||
$this->transaction = new Transaction($this);
|
|
||||||
}
|
|
||||||
return $this->transaction;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function query(string $query): Database\ResultSet
|
|
||||||
{
|
|
||||||
$statement = $this->connect()->query($query);
|
|
||||||
if ($statement === false) {
|
|
||||||
throw new InvalidQuery($query);
|
|
||||||
}
|
|
||||||
return new ResultSet($statement);
|
|
||||||
}
|
|
||||||
public function prepare(string $query): Database\ResultSet
|
|
||||||
{
|
|
||||||
$statement = $this->connect()->prepare($query);
|
|
||||||
if ($statement === false) {
|
|
||||||
throw new InvalidQuery($query);
|
|
||||||
}
|
|
||||||
return new ResultSet($statement);
|
|
||||||
}
|
|
||||||
public function execute(string $query, ?array $data = null): Database\ResultSet
|
|
||||||
{
|
|
||||||
if ($data !== null) {
|
|
||||||
$rs = $this->prepare($query);
|
|
||||||
$rs->execute($data);
|
|
||||||
return $rs;
|
|
||||||
}
|
|
||||||
return $this->query($query);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function fetchOne(string $query, ?array $data = null): array
|
|
||||||
{
|
|
||||||
return $this->execute($query, $data)->fetchFirst();
|
|
||||||
}
|
|
||||||
public function fetchMany(string $query, ?array $data = null): array
|
|
||||||
{
|
|
||||||
return $this->execute($query, $data)->fetchAll();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,40 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Database;
|
|
||||||
|
|
||||||
use PDO;
|
|
||||||
use ProVM\Concept;
|
|
||||||
|
|
||||||
class Transaction implements Concept\Database\Transaction
|
|
||||||
{
|
|
||||||
public function __construct(Concept\Database\Connection $connection)
|
|
||||||
{
|
|
||||||
$this->setConnection($connection);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Concept\Database\Connection $connection;
|
|
||||||
|
|
||||||
public function getConnection(): Concept\Database\Connection
|
|
||||||
{
|
|
||||||
return $this->connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setConnection(Concept\Database\Connection $connection): Concept\Database\Transaction
|
|
||||||
{
|
|
||||||
$this->connection = $connection;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function begin(): Concept\Database\Transaction
|
|
||||||
{
|
|
||||||
$this->getConnection()->connect()->beginTransaction();
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
public function commit(): void
|
|
||||||
{
|
|
||||||
$this->getConnection()->connect()->commit();
|
|
||||||
}
|
|
||||||
public function rollBack(): void
|
|
||||||
{
|
|
||||||
$this->getConnection()->connect()->rollBack();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Concept;
|
namespace Database\Define;
|
||||||
|
|
||||||
interface Database
|
interface Database
|
||||||
{
|
{
|
||||||
@ -9,10 +9,10 @@ interface Database
|
|||||||
public function getUser(): string;
|
public function getUser(): string;
|
||||||
public function getPassword(): string;
|
public function getPassword(): string;
|
||||||
public function needsUser(): bool;
|
public function needsUser(): bool;
|
||||||
public function setHost(string $host): Database;
|
public function setHost(string $host): self;
|
||||||
public function setPort(int $port): Database;
|
public function setPort(int $port): self;
|
||||||
public function setName(string $name): Database;
|
public function setName(string $name): self;
|
||||||
public function setUser(string $username): Database;
|
public function setUser(string $username): self;
|
||||||
public function setPassword(string $password): Database;
|
public function setPassword(string $password): self;
|
||||||
public function getDsn(): string;
|
public function getDsn(): string;
|
||||||
}
|
}
|
40
src/Define/Database/Connection.php
Normal file
40
src/Define/Database/Connection.php
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Define\Database;
|
||||||
|
|
||||||
|
use Database\Exception\Database\InvalidQuery;
|
||||||
|
use PDO;
|
||||||
|
use PDOException;
|
||||||
|
|
||||||
|
interface Connection
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return self
|
||||||
|
* @throws PDOException
|
||||||
|
*/
|
||||||
|
public function connect(): self;
|
||||||
|
public function getPDO(): PDO;
|
||||||
|
|
||||||
|
public function transaction(): Transaction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $query
|
||||||
|
* @return ResultSet
|
||||||
|
* @throws InvalidQuery
|
||||||
|
*/
|
||||||
|
public function query(string $query): ResultSet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $query
|
||||||
|
* @return ResultSet
|
||||||
|
* @throws InvalidQuery
|
||||||
|
*/
|
||||||
|
public function prepare(string $query): ResultSet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $query
|
||||||
|
* @param array|null $data
|
||||||
|
* @return ResultSet
|
||||||
|
* @throws InvalidQuery
|
||||||
|
*/
|
||||||
|
public function execute(string $query, ?array $data = null): ResultSet;
|
||||||
|
}
|
@ -1,9 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Concept\Database;
|
namespace Database\Define\Database;
|
||||||
|
|
||||||
|
use PDOStatement;
|
||||||
|
|
||||||
interface ResultSet
|
interface ResultSet
|
||||||
{
|
{
|
||||||
public function execute(array $data): ResultSet;
|
public function getStatement(): PDOStatement;
|
||||||
|
public function execute(array $data): self;
|
||||||
|
|
||||||
public function fetchFirst(): array;
|
public function fetchFirst(): array;
|
||||||
public function fetchAll(): array;
|
public function fetchAll(): array;
|
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Concept\Database;
|
namespace Database\Define\Database;
|
||||||
|
|
||||||
interface Transaction
|
interface Transaction
|
||||||
{
|
{
|
@ -1,12 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Exception;
|
namespace Database\Exception;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
abstract class Database extends Exception
|
abstract class Database extends Exception
|
||||||
{
|
{
|
||||||
const BASE_CODE = 600;
|
const BASE_CODE = 1000;
|
||||||
|
|
||||||
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null)
|
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null)
|
||||||
{
|
{
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Exception\Database;
|
namespace Database\Exception\Database;
|
||||||
|
|
||||||
use Throwable;
|
use Throwable;
|
||||||
use ProVM\Exception\Database;
|
use Database\Exception\Database;
|
||||||
|
|
||||||
class BlankResult extends Database
|
class BlankResult extends Database
|
||||||
{
|
{
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Exception\Database;
|
namespace Database\Exception\Database;
|
||||||
|
|
||||||
use Throwable;
|
use Throwable;
|
||||||
use ProVM\Exception;
|
use Database\Exception;
|
||||||
|
|
||||||
class InvalidQuery extends Exception\Database
|
class InvalidQuery extends Exception\Database
|
||||||
{
|
{
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Implement;
|
namespace Database\Ideal;
|
||||||
|
|
||||||
use ProVM\Concept;
|
use Database\Define;
|
||||||
|
|
||||||
abstract class Database implements Concept\Database
|
abstract class Database implements Define\Database
|
||||||
{
|
{
|
||||||
protected string $host;
|
protected string $host;
|
||||||
protected int $port;
|
protected int $port;
|
||||||
@ -32,27 +32,27 @@ abstract class Database implements Concept\Database
|
|||||||
return $this->password;
|
return $this->password;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setHost(string $host): Concept\Database
|
public function setHost(string $host): Define\Database
|
||||||
{
|
{
|
||||||
$this->host = $host;
|
$this->host = $host;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
public function setPort(int $port): Concept\Database
|
public function setPort(int $port): Define\Database
|
||||||
{
|
{
|
||||||
$this->port = $port;
|
$this->port = $port;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
public function setName(string $name): Concept\Database
|
public function setName(string $name): Define\Database
|
||||||
{
|
{
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
public function setUser(string $username): Concept\Database
|
public function setUser(string $username): Define\Database
|
||||||
{
|
{
|
||||||
$this->user = $username;
|
$this->user = $username;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
public function setPassword(string $password): Concept\Database
|
public function setPassword(string $password): Define\Database
|
||||||
{
|
{
|
||||||
$this->password = $password;
|
$this->password = $password;
|
||||||
return $this;
|
return $this;
|
111
src/Implement/Connection.php
Normal file
111
src/Implement/Connection.php
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Implement;
|
||||||
|
|
||||||
|
use PDO;
|
||||||
|
use PDOException;
|
||||||
|
use Database\Define;
|
||||||
|
use Database\Exception\Database\InvalidQuery;
|
||||||
|
|
||||||
|
class Connection implements Define\Database\Connection
|
||||||
|
{
|
||||||
|
public function __construct(Define\Database $database)
|
||||||
|
{
|
||||||
|
$this->setDatabase($database);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Define\Database $database;
|
||||||
|
|
||||||
|
protected function getDatabase(): Define\Database
|
||||||
|
{
|
||||||
|
return $this->database;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setDatabase(Define\Database $database): self
|
||||||
|
{
|
||||||
|
$this->database = $database;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected PDO $pdo;
|
||||||
|
public function connect(): self
|
||||||
|
{
|
||||||
|
if (!isset($this->pdo)) {
|
||||||
|
$dsn = $this->getDatabase()->getDsn();
|
||||||
|
if ($this->getDatabase()->needsUser()) {
|
||||||
|
$this->pdo = new PDO($dsn, $this->getDatabase()->getUser(), $this->getDatabase()->getPassword());
|
||||||
|
} else {
|
||||||
|
$this->pdo = new PDO($dsn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function getPDO(): PDO
|
||||||
|
{
|
||||||
|
return $this->connect()->pdo;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Define\Database\Transaction $transaction;
|
||||||
|
|
||||||
|
public function transaction(): Define\Database\Transaction
|
||||||
|
{
|
||||||
|
if (!isset($this->transaction)) {
|
||||||
|
$this->transaction = new Transaction($this);
|
||||||
|
}
|
||||||
|
return $this->transaction;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function query(string $query): Define\Database\ResultSet
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$statement = $this->getPDO()->query($query);
|
||||||
|
} catch (PDOException $exception) {
|
||||||
|
throw new InvalidQuery($query, $exception);
|
||||||
|
}
|
||||||
|
if ($statement === false) {
|
||||||
|
throw new InvalidQuery($query);
|
||||||
|
}
|
||||||
|
return new ResultSet($statement);
|
||||||
|
}
|
||||||
|
public function prepare(string $query): Define\Database\ResultSet
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$statement = $this->getPDO()->prepare($query);
|
||||||
|
} catch (PDOException $exception) {
|
||||||
|
throw new InvalidQuery($query, $exception);
|
||||||
|
}
|
||||||
|
if ($statement === false) {
|
||||||
|
throw new InvalidQuery($query);
|
||||||
|
}
|
||||||
|
return new ResultSet($statement);
|
||||||
|
}
|
||||||
|
public function execute(string $query, ?array $data = null): Define\Database\ResultSet
|
||||||
|
{
|
||||||
|
if ($data !== null) {
|
||||||
|
return $this->prepare($query)
|
||||||
|
->execute($data);
|
||||||
|
}
|
||||||
|
return $this->query($query);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $query
|
||||||
|
* @param array|null $data
|
||||||
|
* @return array
|
||||||
|
* @throws InvalidQuery
|
||||||
|
*/
|
||||||
|
public function fetchOne(string $query, ?array $data = null): array
|
||||||
|
{
|
||||||
|
return $this->execute($query, $data)->fetchFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $query
|
||||||
|
* @param array|null $data
|
||||||
|
* @return array
|
||||||
|
* @throws InvalidQuery
|
||||||
|
*/
|
||||||
|
public function fetchMany(string $query, ?array $data = null): array
|
||||||
|
{
|
||||||
|
return $this->execute($query, $data)->fetchAll();
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Database;
|
namespace Database\Implement;
|
||||||
|
|
||||||
use ProVM\Implement\Database;
|
use Database\Ideal\Database;
|
||||||
|
|
||||||
class MySQL extends Database
|
class MySQL extends Database
|
||||||
{
|
{
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Database;
|
namespace Database\Implement;
|
||||||
|
|
||||||
use ProVM\Implement\Database;
|
use Database\Ideal\Database;
|
||||||
|
|
||||||
class PostgreSQL extends Database
|
class PostgreSQL extends Database
|
||||||
{
|
{
|
@ -1,12 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Database;
|
namespace Database\Implement;
|
||||||
|
|
||||||
use PDO;
|
use PDO;
|
||||||
use PDOStatement;
|
use PDOStatement;
|
||||||
use ProVM\Concept\Database;
|
use Database\Define;
|
||||||
use ProVM\Exception\Database\BlankResult;
|
use Database\Exception\Database\BlankResult;
|
||||||
|
|
||||||
class ResultSet implements Database\ResultSet
|
class ResultSet implements Define\Database\ResultSet
|
||||||
{
|
{
|
||||||
public function __construct(PDOStatement $statement)
|
public function __construct(PDOStatement $statement)
|
||||||
{
|
{
|
||||||
@ -15,22 +15,26 @@ class ResultSet implements Database\ResultSet
|
|||||||
|
|
||||||
protected PDOStatement $statement;
|
protected PDOStatement $statement;
|
||||||
|
|
||||||
protected function getStatement(): PDOStatement
|
public function getStatement(): PDOStatement
|
||||||
{
|
{
|
||||||
return $this->statement;
|
return $this->statement;
|
||||||
}
|
}
|
||||||
protected function setStatement(PDOStatement $statement): ResultSet
|
protected function setStatement(PDOStatement $statement): self
|
||||||
{
|
{
|
||||||
$this->statement = $statement;
|
$this->statement = $statement;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function execute(array $data): Database\ResultSet
|
public function execute(array $data): self
|
||||||
{
|
{
|
||||||
$this->statement->execute($data);
|
$this->statement->execute($data);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return PDOStatement
|
||||||
|
* @throws BlankResult
|
||||||
|
*/
|
||||||
protected function checkResults(): PDOStatement
|
protected function checkResults(): PDOStatement
|
||||||
{
|
{
|
||||||
if ($this->getStatement()->rowCount() === 0) {
|
if ($this->getStatement()->rowCount() === 0) {
|
||||||
@ -38,18 +42,38 @@ class ResultSet implements Database\ResultSet
|
|||||||
}
|
}
|
||||||
return $this->getStatement();
|
return $this->getStatement();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
* @throws BlankResult
|
||||||
|
*/
|
||||||
public function fetchFirst(): array
|
public function fetchFirst(): array
|
||||||
{
|
{
|
||||||
return $this->checkResults()->fetch(PDO::FETCH_ASSOC);
|
return $this->checkResults()->fetch(PDO::FETCH_ASSOC);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
* @throws BlankResult
|
||||||
|
*/
|
||||||
public function fetchAll(): array
|
public function fetchAll(): array
|
||||||
{
|
{
|
||||||
return $this->checkResults()->fetchAll(PDO::FETCH_ASSOC);
|
return $this->checkResults()->fetchAll(PDO::FETCH_ASSOC);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return object
|
||||||
|
* @throws BlankResult
|
||||||
|
*/
|
||||||
public function fetchFirstAsObject(): object
|
public function fetchFirstAsObject(): object
|
||||||
{
|
{
|
||||||
return $this->checkResults()->fetch(PDO::FETCH_OBJ);
|
return $this->checkResults()->fetch(PDO::FETCH_OBJ);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
* @throws BlankResult
|
||||||
|
*/
|
||||||
public function fetchAllAsObjects(): array
|
public function fetchAllAsObjects(): array
|
||||||
{
|
{
|
||||||
return $this->checkResults()->fetchAll(PDO::FETCH_OBJ);
|
return $this->checkResults()->fetchAll(PDO::FETCH_OBJ);
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Database;
|
namespace Database\Implement;
|
||||||
|
|
||||||
use ProVM\Implement\Database;
|
use Database\Ideal\Database;
|
||||||
|
|
||||||
class SQLite extends Database
|
class SQLite extends Database
|
||||||
{
|
{
|
39
src/Implement/Transaction.php
Normal file
39
src/Implement/Transaction.php
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Implement;
|
||||||
|
|
||||||
|
use Database\Define;
|
||||||
|
|
||||||
|
class Transaction implements Define\Database\Transaction
|
||||||
|
{
|
||||||
|
public function __construct(Define\Database\Connection $connection)
|
||||||
|
{
|
||||||
|
$this->setConnection($connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Define\Database\Connection $connection;
|
||||||
|
|
||||||
|
public function getConnection(): Define\Database\Connection
|
||||||
|
{
|
||||||
|
return $this->connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setConnection(Define\Database\Connection $connection): self
|
||||||
|
{
|
||||||
|
$this->connection = $connection;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function begin(): self
|
||||||
|
{
|
||||||
|
$this->getConnection()->getPDO()->beginTransaction();
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function commit(): void
|
||||||
|
{
|
||||||
|
$this->getConnection()->getPDO()->commit();
|
||||||
|
}
|
||||||
|
public function rollBack(): void
|
||||||
|
{
|
||||||
|
$this->getConnection()->getPDO()->rollBack();
|
||||||
|
}
|
||||||
|
}
|
@ -1,65 +1,61 @@
|
|||||||
<?php
|
<?php
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
use ProVM\Database\Connection;
|
use Database\Implement\Connection;
|
||||||
use ProVM\Concept;
|
use Database\Define;
|
||||||
|
|
||||||
class ConnectionTest extends TestCase
|
class ConnectionTest extends TestCase
|
||||||
{
|
{
|
||||||
|
protected string $host = '/tmp/test.db3';
|
||||||
|
protected string $tableName = 'test_table';
|
||||||
protected PDO $pdo;
|
protected PDO $pdo;
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->pdo = new PDO('sqlite::memory:');
|
$this->pdo = new PDO("sqlite:{$this->host}");
|
||||||
$query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)";
|
$query = "CREATE TABLE IF NOT EXISTS {$this->tableName} (id INTEGER PRIMARY KEY, test TEXT)";
|
||||||
|
$this->pdo->query($query);
|
||||||
|
$query = "INSERT INTO {$this->tableName} (test) VALUES ('test')";
|
||||||
$this->pdo->query($query);
|
$this->pdo->query($query);
|
||||||
}
|
}
|
||||||
protected function tearDown(): void
|
protected function tearDown(): void
|
||||||
{
|
{
|
||||||
unset($this->pdo);
|
unset($this->pdo);
|
||||||
|
unlink($this->host);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getConnection(): Connection
|
||||||
|
{
|
||||||
|
$host = $this->host;
|
||||||
|
|
||||||
|
$database = $this->getMockBuilder(Define\Database::class)->getMock();
|
||||||
|
$database->method('getHost')->willReturn($host);
|
||||||
|
$database->method('getDsn')->willReturn("sqlite:{$host}");
|
||||||
|
$database->method('needsUser')->willReturn(false);
|
||||||
|
|
||||||
|
return new Connection($database);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testConnection()
|
public function testConnection()
|
||||||
{
|
{
|
||||||
$host = "memory";
|
$connection = $this->getConnection();
|
||||||
|
$this->assertEquals($this->pdo, $connection->getPDO());
|
||||||
$database = $this->getMockBuilder(Concept\Database::class)->getMock();
|
|
||||||
$database->method('getHost')->willReturn($host);
|
|
||||||
$database->method('getDsn')->willReturn("sqlite::{$host}");
|
|
||||||
$database->method('needsUser')->willReturn(false);
|
|
||||||
|
|
||||||
$connection = new Connection($database);
|
|
||||||
$this->assertEquals($this->pdo, $connection->connect());
|
|
||||||
}
|
}
|
||||||
public function testQuery()
|
public function testQuery()
|
||||||
{
|
{
|
||||||
$host = "memory";
|
$connection = $this->getConnection();
|
||||||
|
|
||||||
$database = $this->getMockBuilder(Concept\Database::class)->getMock();
|
|
||||||
$database->method('getHost')->willReturn($host);
|
|
||||||
$database->method('getDsn')->willReturn("sqlite::{$host}");
|
|
||||||
$database->method('needsUser')->willReturn(false);
|
|
||||||
|
|
||||||
$connection = new Connection($database);
|
|
||||||
$query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)";
|
$query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)";
|
||||||
$connection->query($query);
|
$connection->query($query);
|
||||||
$query = "SELECT * FROM test_table";
|
$query = "SELECT * FROM test_table";
|
||||||
$result = $connection->query($query);
|
$connection->query($query);
|
||||||
$this->assertInstanceOf(Concept\Database\ResultSet::class, $result);
|
$this->assertTrue(true);
|
||||||
}
|
}
|
||||||
public function testPrepare()
|
public function testPrepare()
|
||||||
{
|
{
|
||||||
$host = "memory";
|
$connection = $this->getConnection();
|
||||||
|
|
||||||
$database = $this->getMockBuilder(Concept\Database::class)->getMock();
|
|
||||||
$database->method('getHost')->willReturn($host);
|
|
||||||
$database->method('getDsn')->willReturn("sqlite::{$host}");
|
|
||||||
$database->method('needsUser')->willReturn(false);
|
|
||||||
|
|
||||||
$connection = new Connection($database);
|
|
||||||
$query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)";
|
$query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)";
|
||||||
$connection->query($query);
|
$connection->query($query);
|
||||||
$query = "SELECT * FROM test_table";
|
$query = "SELECT * FROM test_table";
|
||||||
$result = $connection->prepare($query);
|
$connection->prepare($query);
|
||||||
$this->assertInstanceOf(Concept\Database\ResultSet::class, $result);
|
$this->assertTrue(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use ProVM\Database\MySQL;
|
use Database\Implement\MySQL;
|
||||||
|
|
||||||
class MySQLTest extends TestCase
|
class MySQLTest extends TestCase
|
||||||
{
|
{
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use ProVM\Database\PostgreSQL;
|
use Database\Implement\PostgreSQL;
|
||||||
|
|
||||||
class PostgreSQLTest extends TestCase
|
class PostgreSQLTest extends TestCase
|
||||||
{
|
{
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
use ProVM\Database\ResultSet;
|
use Database\Implement\ResultSet;
|
||||||
|
|
||||||
class ResultSetTest extends TestCase
|
class ResultSetTest extends TestCase
|
||||||
{
|
{
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use ProVM\Database\SQLite;
|
use Database\Implement\SQLite;
|
||||||
|
|
||||||
class SQLiteTest extends TestCase
|
class SQLiteTest extends TestCase
|
||||||
{
|
{
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use ProVM\Database\Transaction;
|
use Database\Implement\Transaction;
|
||||||
|
|
||||||
class TransactionTest extends TestCase
|
class TransactionTest extends TestCase
|
||||||
{
|
{
|
||||||
public function testTransaction()
|
public function testTransaction()
|
||||||
{
|
{
|
||||||
$connection = $this->createMock(ProVM\Concept\Database\Connection::class);
|
$connection = $this->createMock(Database\Define\Database\Connection::class);
|
||||||
$transaction = new Transaction($connection);
|
$transaction = new Transaction($connection);
|
||||||
$transaction->begin();
|
$transaction->begin();
|
||||||
$this->assertTrue(true);
|
$this->assertTrue(true);
|
||||||
|
Reference in New Issue
Block a user