From a5f2a5e77197dfa30d779e0d5404eeb7b01b9c3d Mon Sep 17 00:00:00 2001 From: Austin Collier Date: Fri, 5 Jan 2024 16:02:30 -0700 Subject: [PATCH] Unit tests and such --- README.md | 4 +- flight/core/Loader.php | 2 + .../{Pdo_Wrapper.php => PdoWrapper.php} | 4 +- tests/PdoWrapperTest.php | 111 ++++++++++++++++++ 4 files changed, 118 insertions(+), 3 deletions(-) rename flight/database/{Pdo_Wrapper.php => PdoWrapper.php} (97%) create mode 100644 tests/PdoWrapperTest.php diff --git a/README.md b/README.md index f4bf25e..fd41cad 100644 --- a/README.md +++ b/README.md @@ -386,7 +386,7 @@ query your database. ```php // Register the PDO helper class -Flight::register('db', \flight\database\Pdo_Wrapper::class, ['mysql:host=localhost;dbname=cool_db_name', 'user', 'pass', [ +Flight::register('db', \flight\database\PdoWrapper::class, ['mysql:host=localhost;dbname=cool_db_name', 'user', 'pass', [ PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'utf8mb4\'', PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_STRINGIFY_FETCHES => false, @@ -412,6 +412,8 @@ Flight::route('/users', function () { // Special IN() syntax to help out (make sure IN is in caps) $users = Flight::db()->fetchAll('SELECT * FROM users WHERE id IN (?)', [[1,2,3,4,5]]); + // you could also do this + $users = Flight::db()->fetchAll('SELECT * FROM users WHERE id IN (?)', [ '1,2,3,4,5']); // Insert a new user Flight::db()->runQuery("INSERT INTO users (name, email) VALUES (?, ?)", ['Bob', 'bob@example.com']); diff --git a/flight/core/Loader.php b/flight/core/Loader.php index c438445..d262e74 100644 --- a/flight/core/Loader.php +++ b/flight/core/Loader.php @@ -204,6 +204,8 @@ class Loader /** * Autoloads classes. + * + * Classes are not allowed to have underscores in their names. * * @param string $class Class name */ diff --git a/flight/database/Pdo_Wrapper.php b/flight/database/PdoWrapper.php similarity index 97% rename from flight/database/Pdo_Wrapper.php rename to flight/database/PdoWrapper.php index 7628d11..ac87aff 100644 --- a/flight/database/Pdo_Wrapper.php +++ b/flight/database/PdoWrapper.php @@ -5,7 +5,7 @@ namespace flight\database; use PDO; use PDOStatement; -class Pdo_Wrapper extends PDO { +class PdoWrapper extends PDO { /** * How you create the connection for the database @@ -15,7 +15,7 @@ class Pdo_Wrapper extends PDO { * @param string $password - Ex: 'password' * @param array $options - PDO options you can pass in */ - public function __construct(string $dsn, string $username, string $password, array $options = []) { + public function __construct(string $dsn, ?string $username = null, ?string $password = null, array $options = []) { parent::__construct($dsn, $username, $password, $options); } diff --git a/tests/PdoWrapperTest.php b/tests/PdoWrapperTest.php new file mode 100644 index 0000000..2e047a9 --- /dev/null +++ b/tests/PdoWrapperTest.php @@ -0,0 +1,111 @@ + + * @license MIT, http://flightphp.com/license + */ + + + +class PdoWrapperTest extends PHPUnit\Framework\TestCase +{ + /** + * @var Pdo_Wrapper + */ + private $pdo_wrapper; + + protected function setUp(): void + { + $this->pdo_wrapper = new PdoWrapper('sqlite::memory:'); + // create a test table and insert 3 rows of data + $this->pdo_wrapper->exec('CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)'); + $this->pdo_wrapper->exec('INSERT INTO test (name) VALUES ("one")'); + $this->pdo_wrapper->exec('INSERT INTO test (name) VALUES ("two")'); + $this->pdo_wrapper->exec('INSERT INTO test (name) VALUES ("three")'); + } + + protected function tearDown(): void + { + // delete the test table + $this->pdo_wrapper->exec('DROP TABLE test'); + } + + public function testRunQuerySelectAllStatement() { + $statement = $this->pdo_wrapper->runQuery('SELECT * FROM test'); + $this->assertInstanceOf(PDOStatement::class, $statement); + $this->assertCount(3, $statement->fetchAll()); + } + + public function testRunQuerySelectOneStatement() { + $statement = $this->pdo_wrapper->runQuery('SELECT * FROM test WHERE id = 1'); + $this->assertInstanceOf(PDOStatement::class, $statement); + $this->assertCount(1, $statement->fetchAll()); + } + + public function testRunQueryInsertStatement() { + $statement = $this->pdo_wrapper->runQuery('INSERT INTO test (name) VALUES ("four")'); + $this->assertInstanceOf(PDOStatement::class, $statement); + $this->assertEquals(1, $statement->rowCount()); + } + + public function testRunQueryUpdateStatement() { + $statement = $this->pdo_wrapper->runQuery('UPDATE test SET name = "something" WHERE name LIKE ?', ['%t%']); + $this->assertInstanceOf(PDOStatement::class, $statement); + $this->assertEquals(2, $statement->rowCount()); + } + + public function testRunQueryDeleteStatement() { + $statement = $this->pdo_wrapper->runQuery('DELETE FROM test WHERE name LIKE ?', ['%t%']); + $this->assertInstanceOf(PDOStatement::class, $statement); + $this->assertEquals(2, $statement->rowCount()); + } + + public function testFetchField() { + $id = $this->pdo_wrapper->fetchField('SELECT id FROM test WHERE name = ?', ['two']); + $this->assertEquals(2, $id); + } + + public function testFetchRow() { + $row = $this->pdo_wrapper->fetchRow('SELECT * FROM test WHERE name = ?', ['two']); + $this->assertEquals(2, $row['id']); + $this->assertEquals('two', $row['name']); + } + + public function testFetchAll() { + $rows = $this->pdo_wrapper->fetchAll('SELECT * FROM test'); + $this->assertCount(3, $rows); + $this->assertEquals(1, $rows[0]['id']); + $this->assertEquals('one', $rows[0]['name']); + $this->assertEquals(2, $rows[1]['id']); + $this->assertEquals('two', $rows[1]['name']); + $this->assertEquals(3, $rows[2]['id']); + $this->assertEquals('three', $rows[2]['name']); + } + + public function testFetchAllWithNamedParams() { + $rows = $this->pdo_wrapper->fetchAll('SELECT * FROM test WHERE name = :name', [ 'name' => 'two']); + $this->assertCount(1, $rows); + $this->assertEquals(2, $rows[0]['id']); + $this->assertEquals('two', $rows[0]['name']); + } + + public function testFetchAllWithInInt() { + $rows = $this->pdo_wrapper->fetchAll('SELECT id FROM test WHERE id IN(?)', [ [1,2 ]]); + $this->assertEquals(2, count($rows)); + } + + public function testFetchAllWithInString() { + $rows = $this->pdo_wrapper->fetchAll('SELECT id FROM test WHERE name IN(?)', [ ['one','two' ]]); + $this->assertEquals(2, count($rows)); + } + + public function testFetchAllWithInStringCommas() { + $rows = $this->pdo_wrapper->fetchAll('SELECT id FROM test WHERE id > ? AND name IN(?)', [ 0, 'one,two' ]); + $this->assertEquals(2, count($rows)); + } + +}