From 96d0232d787bfbb4f7c0e2456b8eeb1c68891e0d Mon Sep 17 00:00:00 2001 From: Aldarien Date: Mon, 22 Apr 2024 11:56:36 -0400 Subject: [PATCH 1/5] Added testing --- .gitignore | 2 + Dockerfile | 22 +++++++++ compose.yml | 5 ++ composer.json | 4 +- phpunit.xml | 28 +++++++++++ src/Database/Connection.php | 22 ++++++++- src/Exception/Database/InvalidQuery.php | 14 ++++++ tests/ConnectionTest.php | 65 +++++++++++++++++++++++++ tests/MySQLTest.php | 32 ++++++++++++ tests/PostgreSQLTest.php | 32 ++++++++++++ tests/ResultSetTest.php | 26 ++++++++++ tests/SQLiteTest.php | 20 ++++++++ tests/TransactionTest.php | 19 ++++++++ 13 files changed, 287 insertions(+), 4 deletions(-) create mode 100644 Dockerfile create mode 100644 compose.yml create mode 100644 phpunit.xml create mode 100644 src/Exception/Database/InvalidQuery.php create mode 100644 tests/ConnectionTest.php create mode 100644 tests/MySQLTest.php create mode 100644 tests/PostgreSQLTest.php create mode 100644 tests/ResultSetTest.php create mode 100644 tests/SQLiteTest.php create mode 100644 tests/TransactionTest.php diff --git a/.gitignore b/.gitignore index eed7ff6..9b6cffc 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ # PHPStorm **/.idea/ + +**/.cache/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3e14266 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,22 @@ +FROM composer:lts as deps +WORKDIR /app +RUN --mount=type=bind,source=./composer.json,target=composer.json \ + --mount=type=bind,source=./composer.lock,target=composer.lock \ + --mount=type=cache,target=/tmp/cache \ + composer install --no-interaction + +FROM php:8-cli as base +WORKDIR /app +RUN apt-get update && \ + apt-get install -yq --no-install-recommends libsqlite3-dev && \ + rm -rf /var/lib/apt/lists/* && \ + docker-php-ext-install pdo pdo_sqlite +COPY ./src /app/src + +FROM base as dev +COPY ./tests /app/tests +RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini" +COPY --from=deps /app/vendor/ /app/vendor + +FROM dev as test +ENTRYPOINT [ "./vendor/bin/phpunit" ] diff --git a/compose.yml b/compose.yml new file mode 100644 index 0000000..97f3091 --- /dev/null +++ b/compose.yml @@ -0,0 +1,5 @@ +services: + database: + build: . + volumes: + - ./:/app diff --git a/composer.json b/composer.json index 85a3f71..e415bf2 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,7 @@ { "name": "provm/database", "type": "library", + "version": "1.2.0", "authors": [ { "name": "Aldarien", @@ -12,8 +13,7 @@ "ext-pdo": "*" }, "require-dev": { - "phpunit/phpunit": "^10.0", - "kint-php/kint": "^5.0" + "phpunit/phpunit": "^10.0" }, "autoload": { "psr-4": { diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..fcc4184 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,28 @@ + + + + + tests + + + + + + src + + + diff --git a/src/Database/Connection.php b/src/Database/Connection.php index 934eba4..ab67de3 100644 --- a/src/Database/Connection.php +++ b/src/Database/Connection.php @@ -3,6 +3,7 @@ namespace ProVM\Database; use PDO; use ProVM\Concept\Database; +use ProVM\Exception\Database\InvalidQuery; class Connection implements Database\Connection { @@ -50,11 +51,19 @@ class Connection implements Database\Connection public function query(string $query): Database\ResultSet { - return new ResultSet($this->connect()->query($query)); + $statement = $this->connect()->query($query); + if ($statement === false) { + throw new InvalidQuery($query); + } + return new ResultSet($statement); } public function prepare(string $query): Database\ResultSet { - return new ResultSet($this->connect()->prepare($query)); + $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 { @@ -65,4 +74,13 @@ class Connection implements Database\Connection } 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(); + } } diff --git a/src/Exception/Database/InvalidQuery.php b/src/Exception/Database/InvalidQuery.php new file mode 100644 index 0000000..4f9b48c --- /dev/null +++ b/src/Exception/Database/InvalidQuery.php @@ -0,0 +1,14 @@ +pdo = new PDO('sqlite::memory:'); + $query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)"; + $this->pdo->query($query); + } + protected function tearDown(): void + { + unset($this->pdo); + } + + public function testConnection() + { + $host = "memory"; + + $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() + { + $host = "memory"; + + $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)"; + $connection->query($query); + $query = "SELECT * FROM test_table"; + $result = $connection->query($query); + $this->assertInstanceOf(Concept\Database\ResultSet::class, $result); + } + public function testPrepare() + { + $host = "memory"; + + $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)"; + $connection->query($query); + $query = "SELECT * FROM test_table"; + $result = $connection->prepare($query); + $this->assertInstanceOf(Concept\Database\ResultSet::class, $result); + } +} diff --git a/tests/MySQLTest.php b/tests/MySQLTest.php new file mode 100644 index 0000000..c02ede0 --- /dev/null +++ b/tests/MySQLTest.php @@ -0,0 +1,32 @@ +setHost($host); + $database->setPort($port); + $database->setName($name); + $database->setUser($user); + $database->setPassword($pass); + + $this->assertEquals($host, $database->getHost()); + $this->assertEquals($port, $database->getPort()); + $this->assertEquals($name, $database->getName()); + $this->assertEquals($user, $database->getUser()); + $this->assertEquals($pass, $database->getPassword()); + $this->assertTrue($database->needsUser()); + $this->assertEquals($dsn, $database->getDsn()); + } +} diff --git a/tests/PostgreSQLTest.php b/tests/PostgreSQLTest.php new file mode 100644 index 0000000..993005c --- /dev/null +++ b/tests/PostgreSQLTest.php @@ -0,0 +1,32 @@ +setHost($host); + $database->setPort($port); + $database->setName($name); + $database->setUser($user); + $database->setPassword($pass); + + $this->assertEquals($host, $database->getHost()); + $this->assertEquals($port, $database->getPort()); + $this->assertEquals($name, $database->getName()); + $this->assertEquals($user, $database->getUser()); + $this->assertEquals($pass, $database->getPassword()); + $this->assertFalse($database->needsUser()); + $this->assertEquals($dsn, $database->getDsn()); + } +} diff --git a/tests/ResultSetTest.php b/tests/ResultSetTest.php new file mode 100644 index 0000000..b64b4bf --- /dev/null +++ b/tests/ResultSetTest.php @@ -0,0 +1,26 @@ +getMockBuilder(PDOStatement::class)->getMock(); + $statement->method('execute')->willReturn(true); + $statement->method('fetch')->willReturn($result1); + $statement->method('fetchAll')->willReturn($result2); + $statement->method('rowCount')->willReturn(2); + + $resultSet = new ResultSet($statement); + + $resultSet->execute(['foo' => 'bar']); + $this->assertTrue(true); + $this->assertEquals($result1, $resultSet->fetchFirst()); + $this->assertEquals($result2, $resultSet->fetchAll()); + } +} diff --git a/tests/SQLiteTest.php b/tests/SQLiteTest.php new file mode 100644 index 0000000..b1d5820 --- /dev/null +++ b/tests/SQLiteTest.php @@ -0,0 +1,20 @@ +setHost($host); + + $this->assertEquals($host, $database->getHost()); + $this->assertFalse($database->needsUser()); + $this->assertEquals($dsn, $database->getDsn()); + } +} diff --git a/tests/TransactionTest.php b/tests/TransactionTest.php new file mode 100644 index 0000000..ec2ff0c --- /dev/null +++ b/tests/TransactionTest.php @@ -0,0 +1,19 @@ +createMock(ProVM\Concept\Database\Connection::class); + $transaction = new Transaction($connection); + $transaction->begin(); + $this->assertTrue(true); + $transaction->commit(); + $this->assertTrue(true); + $transaction->begin(); + $transaction->rollback(); + $this->assertTrue(true); + } +} -- 2.49.0 From 5014cca5be90a133e65d4ab13976cd0079352803 Mon Sep 17 00:00:00 2001 From: aldarien Date: Tue, 9 Jul 2024 03:50:45 +0000 Subject: [PATCH 2/5] Update 'Readme.md' composer repository: path -> url --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index f2c9f89..1cd1ae4 100644 --- a/Readme.md +++ b/Readme.md @@ -21,7 +21,7 @@ Database Abstraction Layer ... { "type": "git", - "path": "https://git.provm.cl/ProVM/database.git" + "url": "https://git.provm.cl/ProVM/database.git" } ... ], -- 2.49.0 From ed45901de2b4db135fc1748a110c8b694b012754 Mon Sep 17 00:00:00 2001 From: Aldarien Date: Tue, 30 Sep 2025 17:10:47 -0300 Subject: [PATCH 3/5] PHPUnit --- .gitignore | 1 + phpunit.xml | 22 ++++++++++------------ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 9b6cffc..7ce6e9b 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ **/.idea/ **/.cache/ +**/.phpunit.cache/ diff --git a/phpunit.xml b/phpunit.xml index fcc4184..d19d6d9 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,28 +1,26 @@ + failOnWarning="true"> tests - + - src + src - + -- 2.49.0 From d2c37d0aad58475a330ae361677df6f386142f1c Mon Sep 17 00:00:00 2001 From: Aldarien Date: Tue, 30 Sep 2025 17:15:39 -0300 Subject: [PATCH 4/5] Namespaces changes --- composer.json | 2 +- src/Concept/Database/Connection.php | 15 --- src/Database/Connection.php | 86 -------------- src/Database/Transaction.php | 40 ------- src/{Concept => Define}/Database.php | 12 +- src/Define/Database/Connection.php | 40 +++++++ .../Database/ResultSet.php | 7 +- .../Database/Transaction.php | 2 +- src/Exception/Database.php | 4 +- src/Exception/Database/BlankResult.php | 4 +- src/Exception/Database/InvalidQuery.php | 4 +- src/{Implement => Ideal}/Database.php | 16 +-- src/Implement/Connection.php | 111 ++++++++++++++++++ src/{Database => Implement}/MySQL.php | 4 +- src/{Database => Implement}/PostgreSQL.php | 4 +- src/{Database => Implement}/ResultSet.php | 38 ++++-- src/{Database => Implement}/SQLite.php | 4 +- src/Implement/Transaction.php | 39 ++++++ tests/ConnectionTest.php | 62 +++++----- tests/MySQLTest.php | 2 +- tests/PostgreSQLTest.php | 2 +- tests/ResultSetTest.php | 2 +- tests/SQLiteTest.php | 2 +- tests/TransactionTest.php | 4 +- 24 files changed, 289 insertions(+), 217 deletions(-) delete mode 100644 src/Concept/Database/Connection.php delete mode 100644 src/Database/Connection.php delete mode 100644 src/Database/Transaction.php rename src/{Concept => Define}/Database.php (50%) create mode 100644 src/Define/Database/Connection.php rename src/{Concept => Define}/Database/ResultSet.php (57%) rename src/{Concept => Define}/Database/Transaction.php (80%) rename src/{Implement => Ideal}/Database.php (70%) create mode 100644 src/Implement/Connection.php rename src/{Database => Implement}/MySQL.php (86%) rename src/{Database => Implement}/PostgreSQL.php (87%) rename src/{Database => Implement}/ResultSet.php (63%) rename src/{Database => Implement}/SQLite.php (69%) create mode 100644 src/Implement/Transaction.php diff --git a/composer.json b/composer.json index e415bf2..af00d84 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ }, "autoload": { "psr-4": { - "ProVM\\": "src/" + "Database\\": "src/" } } } diff --git a/src/Concept/Database/Connection.php b/src/Concept/Database/Connection.php deleted file mode 100644 index 705400d..0000000 --- a/src/Concept/Database/Connection.php +++ /dev/null @@ -1,15 +0,0 @@ -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(); - } -} diff --git a/src/Database/Transaction.php b/src/Database/Transaction.php deleted file mode 100644 index 3181903..0000000 --- a/src/Database/Transaction.php +++ /dev/null @@ -1,40 +0,0 @@ -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(); - } -} diff --git a/src/Concept/Database.php b/src/Define/Database.php similarity index 50% rename from src/Concept/Database.php rename to src/Define/Database.php index bc9f2a9..75f9804 100644 --- a/src/Concept/Database.php +++ b/src/Define/Database.php @@ -1,5 +1,5 @@ password; } - public function setHost(string $host): Concept\Database + public function setHost(string $host): Define\Database { $this->host = $host; return $this; } - public function setPort(int $port): Concept\Database + public function setPort(int $port): Define\Database { $this->port = $port; return $this; } - public function setName(string $name): Concept\Database + public function setName(string $name): Define\Database { $this->name = $name; return $this; } - public function setUser(string $username): Concept\Database + public function setUser(string $username): Define\Database { $this->user = $username; return $this; } - public function setPassword(string $password): Concept\Database + public function setPassword(string $password): Define\Database { $this->password = $password; return $this; diff --git a/src/Implement/Connection.php b/src/Implement/Connection.php new file mode 100644 index 0000000..b599d7a --- /dev/null +++ b/src/Implement/Connection.php @@ -0,0 +1,111 @@ +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(); + } +} diff --git a/src/Database/MySQL.php b/src/Implement/MySQL.php similarity index 86% rename from src/Database/MySQL.php rename to src/Implement/MySQL.php index a4e009f..546366c 100644 --- a/src/Database/MySQL.php +++ b/src/Implement/MySQL.php @@ -1,7 +1,7 @@ statement; } - protected function setStatement(PDOStatement $statement): ResultSet + protected function setStatement(PDOStatement $statement): self { $this->statement = $statement; return $this; } - public function execute(array $data): Database\ResultSet + public function execute(array $data): self { $this->statement->execute($data); return $this; } + /** + * @return PDOStatement + * @throws BlankResult + */ protected function checkResults(): PDOStatement { if ($this->getStatement()->rowCount() === 0) { @@ -38,18 +42,38 @@ class ResultSet implements Database\ResultSet } return $this->getStatement(); } + + /** + * @return array + * @throws BlankResult + */ public function fetchFirst(): array { return $this->checkResults()->fetch(PDO::FETCH_ASSOC); } + + /** + * @return array + * @throws BlankResult + */ public function fetchAll(): array { return $this->checkResults()->fetchAll(PDO::FETCH_ASSOC); } + + /** + * @return object + * @throws BlankResult + */ public function fetchFirstAsObject(): object { return $this->checkResults()->fetch(PDO::FETCH_OBJ); } + + /** + * @return array + * @throws BlankResult + */ public function fetchAllAsObjects(): array { return $this->checkResults()->fetchAll(PDO::FETCH_OBJ); diff --git a/src/Database/SQLite.php b/src/Implement/SQLite.php similarity index 69% rename from src/Database/SQLite.php rename to src/Implement/SQLite.php index f8ab299..e43481c 100644 --- a/src/Database/SQLite.php +++ b/src/Implement/SQLite.php @@ -1,7 +1,7 @@ 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(); + } +} diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index fe4919f..9286f22 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -1,65 +1,61 @@ pdo = new PDO('sqlite::memory:'); - $query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)"; + $this->pdo = new PDO("sqlite:{$this->host}"); + $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); } protected function tearDown(): void { 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() { - $host = "memory"; - - $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()); + $connection = $this->getConnection(); + $this->assertEquals($this->pdo, $connection->getPDO()); } public function testQuery() { - $host = "memory"; - - $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); + $connection = $this->getConnection(); $query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)"; $connection->query($query); $query = "SELECT * FROM test_table"; - $result = $connection->query($query); - $this->assertInstanceOf(Concept\Database\ResultSet::class, $result); + $connection->query($query); + $this->assertTrue(true); } public function testPrepare() { - $host = "memory"; - - $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); + $connection = $this->getConnection(); $query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)"; $connection->query($query); $query = "SELECT * FROM test_table"; - $result = $connection->prepare($query); - $this->assertInstanceOf(Concept\Database\ResultSet::class, $result); + $connection->prepare($query); + $this->assertTrue(true); } } diff --git a/tests/MySQLTest.php b/tests/MySQLTest.php index c02ede0..cce4c82 100644 --- a/tests/MySQLTest.php +++ b/tests/MySQLTest.php @@ -1,6 +1,6 @@ createMock(ProVM\Concept\Database\Connection::class); + $connection = $this->createMock(Database\Define\Database\Connection::class); $transaction = new Transaction($connection); $transaction->begin(); $this->assertTrue(true); -- 2.49.0 From 3f51ab05ac6acdbc2aad8d5d7fbde556f49bb192 Mon Sep 17 00:00:00 2001 From: Aldarien Date: Tue, 30 Sep 2025 17:28:15 -0300 Subject: [PATCH 5/5] Query Builder --- composer.json | 50 ++++++++++++++++++------------ src/Define/Database/Connection.php | 5 ++- src/Implement/Connection.php | 10 ++++++ 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/composer.json b/composer.json index af00d84..0252a19 100644 --- a/composer.json +++ b/composer.json @@ -1,23 +1,33 @@ { - "name": "provm/database", - "type": "library", - "version": "1.2.0", - "authors": [ - { - "name": "Aldarien", - "email": "aldarien85@gmail.com" - } - ], - "require": { - "php": ">=8", - "ext-pdo": "*" - }, - "require-dev": { - "phpunit/phpunit": "^10.0" - }, - "autoload": { - "psr-4": { - "Database\\": "src/" - } + "name": "provm/database", + "type": "library", + "version": "1.2.0", + "authors": [ + { + "name": "Aldarien", + "email": "aldarien85@gmail.com" } + ], + "require": { + "php": ">=8", + "ext-pdo": "*", + "provm/query_builder": "^3.0" + }, + "require-dev": { + "phpunit/phpunit": "^10.0" + }, + "autoload": { + "psr-4": { + "Database\\": "src/" + } + }, + "config": { + "sort-packages": true + }, + "repositories": [ + { + "type": "vcs", + "url": "https://git.provm.cl/ProVM/query_builder.git" + } + ] } diff --git a/src/Define/Database/Connection.php b/src/Define/Database/Connection.php index 8896788..829c43a 100644 --- a/src/Define/Database/Connection.php +++ b/src/Define/Database/Connection.php @@ -1,9 +1,10 @@ database = $database; return $this; } + protected Define\Query\Builder $builder; + public function queryBuilder(): Define\Query\Builder + { + return $this->builder; + } + public function setBuilder(Define\Query\Builder $builder): self + { + $this->builder = $builder; + return $this; + } protected PDO $pdo; public function connect(): self -- 2.49.0