From 9fba3781d6f3ab22012fd5ec335c40ecdec703b8 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Fri, 7 Jun 2024 23:06:31 -0400 Subject: [PATCH] Add SQLite Patch integration tests - Use multiple-class use statements --- src/Custom.php | 4 +- src/Mapper/ExistsMapper.php | 4 +- src/Query/Count.php | 3 +- src/Query/Definition.php | 4 +- src/Query/Delete.php | 3 +- src/Query/Exists.php | 3 +- src/Query/Find.php | 3 +- src/Query/Patch.php | 6 +-- src/Query/RemoveFields.php | 6 +-- tests/integration/sqlite/CustomTest.php | 17 ++---- tests/integration/sqlite/DefinitionTest.php | 58 ++++++++++++++++++++ tests/integration/sqlite/PatchTest.php | 59 +++++++++++++++++++++ tests/unit/ConfigurationTest.php | 4 +- tests/unit/FieldTest.php | 3 +- tests/unit/Mapper/DocumentMapperTest.php | 3 +- tests/unit/Mapper/ExistsMapperTest.php | 4 +- tests/unit/ParametersTest.php | 8 +-- tests/unit/Query/DefinitionTest.php | 4 +- tests/unit/Query/PatchTest.php | 5 +- tests/unit/Query/RemoveFieldsTest.php | 6 +-- tests/unit/QueryTest.php | 3 +- 21 files changed, 143 insertions(+), 67 deletions(-) create mode 100644 tests/integration/sqlite/DefinitionTest.php create mode 100644 tests/integration/sqlite/PatchTest.php diff --git a/src/Custom.php b/src/Custom.php index 740868b..00494e0 100644 --- a/src/Custom.php +++ b/src/Custom.php @@ -27,7 +27,9 @@ class Custom $stmt = Configuration::dbConn()->prepare($query); } catch (PDOException $ex) { $keyword = explode(' ', $query, 2)[0]; - throw new DocumentException("Error executing $keyword statement: " . Configuration::dbConn()->errorCode(), + throw new DocumentException( + sprintf("Error executing %s statement: [%s] %s", $keyword, Configuration::dbConn()->errorCode(), + Configuration::dbConn()->errorInfo()[2]), previous: $ex); } foreach ($parameters as $key => $value) { diff --git a/src/Mapper/ExistsMapper.php b/src/Mapper/ExistsMapper.php index 4f7ec2c..7668318 100644 --- a/src/Mapper/ExistsMapper.php +++ b/src/Mapper/ExistsMapper.php @@ -2,9 +2,7 @@ namespace BitBadger\PDODocument\Mapper; -use BitBadger\PDODocument\Configuration; -use BitBadger\PDODocument\DocumentException; -use BitBadger\PDODocument\Mode; +use BitBadger\PDODocument\{Configuration, DocumentException, Mode}; /** * Map an EXISTS result to a boolean value diff --git a/src/Query/Count.php b/src/Query/Count.php index cc112f0..87c47e3 100644 --- a/src/Query/Count.php +++ b/src/Query/Count.php @@ -2,8 +2,7 @@ namespace BitBadger\PDODocument\Query; -use BitBadger\PDODocument\Field; -use BitBadger\PDODocument\Query; +use BitBadger\PDODocument\{Field, Query}; /** * Queries for counting documents diff --git a/src/Query/Definition.php b/src/Query/Definition.php index 4399310..6e584f5 100644 --- a/src/Query/Definition.php +++ b/src/Query/Definition.php @@ -2,9 +2,7 @@ namespace BitBadger\PDODocument\Query; -use BitBadger\PDODocument\Configuration; -use BitBadger\PDODocument\DocumentException; -use BitBadger\PDODocument\Mode; +use BitBadger\PDODocument\{Configuration, DocumentException, Mode}; /** * Queries to define tables and indexes diff --git a/src/Query/Delete.php b/src/Query/Delete.php index d171bb7..84bd2a0 100644 --- a/src/Query/Delete.php +++ b/src/Query/Delete.php @@ -2,8 +2,7 @@ namespace BitBadger\PDODocument\Query; -use BitBadger\PDODocument\Field; -use BitBadger\PDODocument\Query; +use BitBadger\PDODocument\{Field, Query}; /** * Queries to delete documents diff --git a/src/Query/Exists.php b/src/Query/Exists.php index ba6d47d..a7750b1 100644 --- a/src/Query/Exists.php +++ b/src/Query/Exists.php @@ -2,8 +2,7 @@ namespace BitBadger\PDODocument\Query; -use BitBadger\PDODocument\Field; -use BitBadger\PDODocument\Query; +use BitBadger\PDODocument\{Field, Query}; /** * Queries to determine document existence diff --git a/src/Query/Find.php b/src/Query/Find.php index dcc08be..2e65385 100644 --- a/src/Query/Find.php +++ b/src/Query/Find.php @@ -2,8 +2,7 @@ namespace BitBadger\PDODocument\Query; -use BitBadger\PDODocument\Field; -use BitBadger\PDODocument\Query; +use BitBadger\PDODocument\{Field, Query}; /** * Queries for retrieving documents diff --git a/src/Query/Patch.php b/src/Query/Patch.php index 7f63253..6b1ab76 100644 --- a/src/Query/Patch.php +++ b/src/Query/Patch.php @@ -2,11 +2,7 @@ namespace BitBadger\PDODocument\Query; -use BitBadger\PDODocument\Configuration; -use BitBadger\PDODocument\DocumentException; -use BitBadger\PDODocument\Field; -use BitBadger\PDODocument\Mode; -use BitBadger\PDODocument\Query; +use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode, Query}; /** * Queries to perform partial updates on documents diff --git a/src/Query/RemoveFields.php b/src/Query/RemoveFields.php index 46c1bcb..0e3fea0 100644 --- a/src/Query/RemoveFields.php +++ b/src/Query/RemoveFields.php @@ -2,11 +2,7 @@ namespace BitBadger\PDODocument\Query; -use BitBadger\PDODocument\Configuration; -use BitBadger\PDODocument\DocumentException; -use BitBadger\PDODocument\Field; -use BitBadger\PDODocument\Mode; -use BitBadger\PDODocument\Query; +use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode, Query}; /** * Queries to remove fields from documents diff --git a/tests/integration/sqlite/CustomTest.php b/tests/integration/sqlite/CustomTest.php index 3155e72..31c1d74 100644 --- a/tests/integration/sqlite/CustomTest.php +++ b/tests/integration/sqlite/CustomTest.php @@ -2,13 +2,8 @@ namespace Test\Integration\SQLite; -use BitBadger\PDODocument\Count; -use BitBadger\PDODocument\Custom; -use BitBadger\PDODocument\DocumentException; -use BitBadger\PDODocument\Mapper\CountMapper; -use BitBadger\PDODocument\Mapper\DocumentMapper; -use BitBadger\PDODocument\Mapper\StringMapper; -use BitBadger\PDODocument\Query; +use BitBadger\PDODocument\{Count, Custom, DocumentException, Query}; +use BitBadger\PDODocument\Mapper\{CountMapper, DocumentMapper}; use PHPUnit\Framework\TestCase; use Test\Integration\TestDocument; @@ -105,12 +100,8 @@ class CustomTest extends TestCase public function testNonQuerySucceedsWhenOperatingOnData() { Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); - try { - $remaining = Count::all(ThrowawayDb::TABLE); - $this->assertEquals(0, $remaining, 'There should be no documents remaining in the table'); - } finally { - $this->dbName = ThrowawayDb::exchange($this->dbName); - } + $remaining = Count::all(ThrowawayDb::TABLE); + $this->assertEquals(0, $remaining, 'There should be no documents remaining in the table'); } public function testNonQuerySucceedsWhenNoDataMatchesWhereClause() diff --git a/tests/integration/sqlite/DefinitionTest.php b/tests/integration/sqlite/DefinitionTest.php new file mode 100644 index 0000000..44f248e --- /dev/null +++ b/tests/integration/sqlite/DefinitionTest.php @@ -0,0 +1,58 @@ +dbName = ThrowawayDb::create(withData: false); + } + + protected function tearDown(): void + { + ThrowawayDb::destroy($this->dbName); + parent::tearDown(); + } + + /** + * Does the given named object exist in the database? + * + * @param string $name The name of the object whose existence should be verified + * @return bool True if the object exists, false if not + * @throws DocumentException If any is encountered + */ + private function itExists(string $name): bool + { + return Custom::scalar('SELECT EXISTS (SELECT 1 FROM sqlite_master WHERE name = :name)', + [':name' => $name], new ExistsMapper()); + } + + public function testEnsureTableSucceeds() + { + $this->assertFalse($this->itExists('ensured'), 'The table should not exist already'); + $this->assertFalse($this->itExists('idx_ensured_key'), 'The key index should not exist already'); + Definition::ensureTable('ensured'); + $this->assertTrue($this->itExists('ensured'), 'The table should now exist'); + $this->assertTrue($this->itExists('idx_ensured_key'), 'The key index should now exist'); + } + + public function testEnsureFieldIndexSucceeds() + { + $this->assertFalse($this->itExists('idx_ensured_test'), 'The index should not exist already'); + Definition::ensureTable('ensured'); + Definition::ensureFieldIndex('ensured', 'test', ['name', 'age']); + $this->assertTrue($this->itExists('idx_ensured_test'), 'The index should now exist'); + } +} diff --git a/tests/integration/sqlite/PatchTest.php b/tests/integration/sqlite/PatchTest.php new file mode 100644 index 0000000..b891501 --- /dev/null +++ b/tests/integration/sqlite/PatchTest.php @@ -0,0 +1,59 @@ +dbName = ThrowawayDb::create(); + } + + protected function tearDown(): void + { + ThrowawayDb::destroy($this->dbName); + parent::tearDown(); + } + + #[TestDox('By ID succeeds when a document is updated')] + public function testByIdSucceedsWhenADocumentIsUpdated(): void + { + Patch::byId(ThrowawayDb::TABLE, 'one', ['num_value' => 44]); + $doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class); + $this->assertNotFalse($doc, 'There should have been a document returned'); + $this->assertEquals(44, $doc->num_value, 'The updated document is not correct'); + } + + #[TestDox('By ID succeeds when no document is updated')] + public function testByIdSucceedsWhenNoDocumentIsUpdated(): void + { + Patch::byId(ThrowawayDb::TABLE, 'forty-seven', ['foo' => 'green']); + $this->assertTrue(true, 'The above not throwing an exception is the test'); + } + + + public function testByFieldsSucceedsWhenADocumentIsUpdated(): void + { + Patch::byFields(ThrowawayDb::TABLE, [Field::EQ('value', 'purple')], ['num_value' => 77]); + $after = Count::byFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 77)]); + $this->assertEquals(2, $after, 'There should have been 2 documents updated'); + } + + public function testByFieldsSucceedsWhenNoDocumentIsUpdated(): void + { + Patch::byFields(ThrowawayDb::TABLE, [Field::EQ('value', 'burgundy')], ['foo' => 'green']); + $this->assertTrue(true, 'The above not throwing an exception is the test'); + } +} diff --git a/tests/unit/ConfigurationTest.php b/tests/unit/ConfigurationTest.php index 6ff0c09..1416ceb 100644 --- a/tests/unit/ConfigurationTest.php +++ b/tests/unit/ConfigurationTest.php @@ -2,8 +2,7 @@ namespace Test\Unit; -use BitBadger\PDODocument\Configuration; -use BitBadger\PDODocument\DocumentException; +use BitBadger\PDODocument\{Configuration, DocumentException}; use PHPUnit\Framework\Attributes\TestDox; use PHPUnit\Framework\TestCase; @@ -34,6 +33,7 @@ class ConfigurationTest extends TestCase public function testDbConnFailsWhenNoDSNSpecified(): void { $this->expectException(DocumentException::class); + Configuration::$pdoDSN = ''; Configuration::dbConn(); } } diff --git a/tests/unit/FieldTest.php b/tests/unit/FieldTest.php index 8294534..f2f76e3 100644 --- a/tests/unit/FieldTest.php +++ b/tests/unit/FieldTest.php @@ -2,8 +2,7 @@ namespace Test\Unit; -use BitBadger\PDODocument\Field; -use BitBadger\PDODocument\Op; +use BitBadger\PDODocument\{Field, Op}; use PHPUnit\Framework\Attributes\TestDox; use PHPUnit\Framework\TestCase; diff --git a/tests/unit/Mapper/DocumentMapperTest.php b/tests/unit/Mapper/DocumentMapperTest.php index 2fa3af5..5213312 100644 --- a/tests/unit/Mapper/DocumentMapperTest.php +++ b/tests/unit/Mapper/DocumentMapperTest.php @@ -2,8 +2,7 @@ namespace Test\Unit\Mapper; -use BitBadger\PDODocument\DocumentException; -use BitBadger\PDODocument\Field; +use BitBadger\PDODocument\{DocumentException, Field}; use BitBadger\PDODocument\Mapper\DocumentMapper; use PHPUnit\Framework\Attributes\TestDox; use PHPUnit\Framework\TestCase; diff --git a/tests/unit/Mapper/ExistsMapperTest.php b/tests/unit/Mapper/ExistsMapperTest.php index ce68908..1dbaf02 100644 --- a/tests/unit/Mapper/ExistsMapperTest.php +++ b/tests/unit/Mapper/ExistsMapperTest.php @@ -2,10 +2,8 @@ namespace Test\Unit\Mapper; -use BitBadger\PDODocument\Configuration; -use BitBadger\PDODocument\DocumentException; +use BitBadger\PDODocument\{Configuration, DocumentException, Mode}; use BitBadger\PDODocument\Mapper\ExistsMapper; -use BitBadger\PDODocument\Mode; use PHPUnit\Framework\Attributes\TestDox; use PHPUnit\Framework\TestCase; diff --git a/tests/unit/ParametersTest.php b/tests/unit/ParametersTest.php index 1bf0ec0..be9d236 100644 --- a/tests/unit/ParametersTest.php +++ b/tests/unit/ParametersTest.php @@ -1,12 +1,8 @@