Add PostgreSQL Support (#3)

Reviewed-on: #3
This commit was merged in pull request #3.
This commit is contained in:
2024-06-21 13:46:41 +00:00
parent 330e272187
commit 124426fa12
61 changed files with 2290 additions and 223 deletions

View File

@@ -2,7 +2,7 @@
namespace Test\Integration\SQLite;
use BitBadger\PDODocument\{Count, Field};
use BitBadger\PDODocument\{Count, DocumentException, Field};
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
@@ -44,4 +44,17 @@ class CountTest extends TestCase
$count = Count::byFields(ThrowawayDb::TABLE, [Field::BT('value', 'aardvark', 'apple')]);
$this->assertEquals(1, $count, 'There should have been 1 matching document');
}
public function testByContainsFails(): void
{
$this->expectException(DocumentException::class);
Count::byContains('', []);
}
#[TestDox('By JSON Path fails')]
public function testByJsonPathFails(): void
{
$this->expectException(DocumentException::class);
Count::byJsonPath('', '');
}
}

View File

@@ -2,7 +2,7 @@
namespace Test\Integration\SQLite;
use BitBadger\PDODocument\{Custom, Definition, DocumentException};
use BitBadger\PDODocument\{Custom, Definition, DocumentException, DocumentIndex};
use BitBadger\PDODocument\Mapper\ExistsMapper;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
@@ -41,7 +41,7 @@ class DefinitionTest extends TestCase
[':name' => $name], new ExistsMapper());
}
public function testEnsureTableSucceeds()
public function testEnsureTableSucceeds(): void
{
$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');
@@ -50,11 +50,17 @@ class DefinitionTest extends TestCase
$this->assertTrue($this->itExists('idx_ensured_key'), 'The key index should now exist');
}
public function testEnsureFieldIndexSucceeds()
public function testEnsureFieldIndexSucceeds(): void
{
$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');
}
public function testEnsureDocumentIndexFails(): void
{
$this->expectException(DocumentException::class);
Definition::ensureDocumentIndex('nope', DocumentIndex::Full);
}
}

View File

@@ -2,7 +2,7 @@
namespace Test\Integration\SQLite;
use BitBadger\PDODocument\{Count, Delete, Field};
use BitBadger\PDODocument\{Count, Delete, DocumentException, Field};
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
@@ -56,4 +56,17 @@ class DeleteTest extends TestCase
Delete::byFields(ThrowawayDb::TABLE, [Field::EQ('value', 'crimson')]);
$this->assertEquals(5, Count::all(ThrowawayDb::TABLE), 'There should have been 5 documents remaining');
}
public function testByContainsFails(): void
{
$this->expectException(DocumentException::class);
Delete::byContains('', []);
}
#[TestDox('By JSON Path fails')]
public function testByJsonPathFails(): void
{
$this->expectException(DocumentException::class);
Delete::byJsonPath('', '');
}
}

View File

@@ -2,7 +2,7 @@
namespace Test\Integration\SQLite;
use BitBadger\PDODocument\{Exists, Field};
use BitBadger\PDODocument\{DocumentException, Exists, Field};
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
@@ -51,4 +51,18 @@ class ExistsTest extends TestCase
$this->assertFalse(Exists::byFields(ThrowawayDb::TABLE, [Field::LT('nothing', 'none')]),
'There should not have been any existing documents');
}
public function testByContainsFails(): void
{
$this->expectException(DocumentException::class);
Exists::byContains('', []);
}
#[TestDox('By JSON Path fails')]
public function testByJsonPathFails(): void
{
$this->expectException(DocumentException::class);
Exists::byJsonPath('', '');
}
}

View File

@@ -2,7 +2,7 @@
namespace Test\Integration\SQLite;
use BitBadger\PDODocument\{Custom, Document, Field, Find};
use BitBadger\PDODocument\{Custom, Document, DocumentException, Field, Find};
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
use Test\Integration\TestDocument;
@@ -82,11 +82,22 @@ class FindTest extends TestCase
{
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::GT('num_value', 100)], TestDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned');
$count = 0;
foreach ($docs->items() as $ignored) $count++;
$this->assertFalse($docs->hasItems(), 'There should have been no documents in the list');
}
public function testByContainsFails(): void
{
$this->expectException(DocumentException::class);
Find::byContains('', [], TestDocument::class);
}
#[TestDox('By JSON Path fails')]
public function testByJsonPathFails(): void
{
$this->expectException(DocumentException::class);
Find::byJsonPath('', '', TestDocument::class);
}
public function testFirstByFieldsSucceedsWhenADocumentIsFound(): void
{
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'another')], TestDocument::class);
@@ -106,4 +117,17 @@ class FindTest extends TestCase
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'absent')], TestDocument::class);
$this->assertFalse($doc, 'There should not have been a document returned');
}
public function testFirstByContainsFails(): void
{
$this->expectException(DocumentException::class);
Find::firstByContains('', [], TestDocument::class);
}
#[TestDox('First by JSON Path fails')]
public function testFirstByJsonPathFails(): void
{
$this->expectException(DocumentException::class);
Find::firstByJsonPath('', '', TestDocument::class);
}
}

View File

@@ -2,7 +2,7 @@
namespace Test\Integration\SQLite;
use BitBadger\PDODocument\{Count, Field, Find, Patch};
use BitBadger\PDODocument\{Count, DocumentException, Field, Find, Patch};
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
use Test\Integration\TestDocument;
@@ -44,7 +44,6 @@ class PatchTest extends TestCase
$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]);
@@ -57,4 +56,17 @@ class PatchTest extends TestCase
Patch::byFields(ThrowawayDb::TABLE, [Field::EQ('value', 'burgundy')], ['foo' => 'green']);
$this->assertTrue(true, 'The above not throwing an exception is the test');
}
public function testByContainsFails(): void
{
$this->expectException(DocumentException::class);
Patch::byContains('', [], []);
}
#[TestDox('By JSON Path fails')]
public function testByJsonPathFails(): void
{
$this->expectException(DocumentException::class);
Patch::byJsonPath('', '', []);
}
}

View File

@@ -2,7 +2,7 @@
namespace Test\Integration\SQLite;
use BitBadger\PDODocument\{Field, Find, RemoveFields};
use BitBadger\PDODocument\{DocumentException, Field, Find, RemoveFields};
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
use Test\Integration\TestDocument;
@@ -71,4 +71,17 @@ class RemoveFieldsTest extends TestCase
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::NE('missing', 'nope')], ['value']);
$this->assertTrue(true, 'The above not throwing an exception is the test');
}
public function testByContainsFails(): void
{
$this->expectException(DocumentException::class);
RemoveFields::byContains('', [], []);
}
#[TestDox('By JSON Path fails')]
public function testByJsonPathFails(): void
{
$this->expectException(DocumentException::class);
RemoveFields::byJsonPath('', '', []);
}
}

View File

@@ -2,13 +2,9 @@
namespace Test\Integration\SQLite;
use BitBadger\PDODocument\Configuration;
use BitBadger\PDODocument\Definition;
use BitBadger\PDODocument\Document;
use BitBadger\PDODocument\DocumentException;
use BitBadger\PDODocument\Mode;
use Test\Integration\SubDocument;
use Test\Integration\TestDocument;
use BitBadger\PDODocument\{AutoId, Configuration, Definition, Document, DocumentException, Mode};
use Random\RandomException;
use Test\Integration\{SubDocument, TestDocument};
/**
* Utilities to create and destroy a throwaway SQLite database to use for testing
@@ -16,18 +12,18 @@ use Test\Integration\TestDocument;
class ThrowawayDb
{
/** @var string The table used for document manipulation */
public const string TABLE = "test_table";
public const TABLE = "test_table";
/**
* Create a throwaway SQLite database
*
* @param bool $withData Whether to initialize this database with data (optional; defaults to `true`)
* @return string The name of the database (use to pass to `destroy` function at end of test)
* @throws DocumentException If any is encountered
* @throws DocumentException|RandomException If any is encountered
*/
public static function create(bool $withData = true): string
{
$fileName = sprintf('throwaway-%s-%d.db', date('His'), rand(10, 99));
$fileName = sprintf('throwaway-%s.db', AutoId::generateRandom(10));
Configuration::$pdoDSN = "sqlite:./$fileName";
Configuration::$mode = Mode::SQLite;
Configuration::resetPDO();
@@ -52,20 +48,6 @@ class ThrowawayDb
public static function destroy(string $fileName): void
{
Configuration::resetPDO();
unlink("./$fileName");
}
/**
* Destroy the given throwaway database and create another
*
* @param string $fileName The name of the database to be destroyed
* @param bool $withData Whether to initialize the database with data (optional; defaults to `true`)
* @return string The name of the new database
* @throws DocumentException If any is encountered
*/
public static function exchange(string $fileName, bool $withData = true): string
{
self::destroy($fileName);
return self::create($withData);
if (file_exists("./$fileName")) unlink("./$fileName");
}
}