Migrate tests to Pest
This commit is contained in:
35
tests/Integration/ArrayDocument.php
Normal file
35
tests/Integration/ArrayDocument.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Test\Integration;
|
||||
|
||||
/**
|
||||
* A document with an array of values
|
||||
*/
|
||||
class ArrayDocument
|
||||
{
|
||||
/**
|
||||
* @param string $id The ID of the document
|
||||
* @param string[] $values The values for the document
|
||||
*/
|
||||
public function __construct(public string $id = '', public array $values = []) { }
|
||||
|
||||
/**
|
||||
* A set of documents used for integration tests
|
||||
*
|
||||
* @return ArrayDocument[] Test documents for InArray tests
|
||||
*/
|
||||
public static function testDocuments(): array
|
||||
{
|
||||
return [
|
||||
new ArrayDocument('first', ['a', 'b', 'c']),
|
||||
new ArrayDocument('second', ['c', 'd', 'e']),
|
||||
new ArrayDocument('third', ['x', 'y', 'z'])
|
||||
];
|
||||
}
|
||||
}
|
||||
17
tests/Integration/NumDocument.php
Normal file
17
tests/Integration/NumDocument.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Test\Integration;
|
||||
|
||||
/**
|
||||
* A test document with a numeric ID
|
||||
*/
|
||||
class NumDocument
|
||||
{
|
||||
public function __construct(public int $id = 0, public string $value = '') { }
|
||||
}
|
||||
60
tests/Integration/PgIntegrationTest.php
Normal file
60
tests/Integration/PgIntegrationTest.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
* @see https://github.com/Zaid-Ajaj/ThrowawayDb The origin concept
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Test\Integration;
|
||||
|
||||
use BitBadger\PDODocument\{Configuration, Custom, Delete, DocumentException, Field};
|
||||
use BitBadger\PDODocument\Mapper\ExistsMapper;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Test\Integration\PostgreSQL\ThrowawayDb;
|
||||
|
||||
/**
|
||||
* Integration Test Class wrapper for PostgreSQL integration tests
|
||||
*/
|
||||
class PgIntegrationTest extends TestCase
|
||||
{
|
||||
/** @var string Database name for throwaway database */
|
||||
static private string $dbName = '';
|
||||
|
||||
public static function setUpBeforeClass(): void
|
||||
{
|
||||
self::$dbName = ThrowawayDb::create(false);
|
||||
}
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
ThrowawayDb::loadData();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Delete::byFields(ThrowawayDb::TABLE, [ Field::exists(Configuration::$idField)]);
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public static function tearDownAfterClass(): void
|
||||
{
|
||||
ThrowawayDb::destroy(self::$dbName);
|
||||
self::$dbName = '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
protected function dbObjectExists(string $name): bool
|
||||
{
|
||||
return Custom::scalar('SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = :name)',
|
||||
[':name' => $name], new ExistsMapper());
|
||||
}
|
||||
}
|
||||
45
tests/Integration/PostgreSQL/CountTest.php
Normal file
45
tests/Integration/PostgreSQL/CountTest.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{Count, Field};
|
||||
use Test\Integration\PostgreSQL\ThrowawayDb;
|
||||
|
||||
pest()->group('integration', 'postgresql');
|
||||
|
||||
describe('::all()', function () {
|
||||
test('counts all documents', function () {
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byFields()', function () {
|
||||
test('counts for numeric range correctly', function () {
|
||||
expect(Count::byFields(ThrowawayDb::TABLE, [Field::between('num_value', 10, 20)]))->toBe(3);
|
||||
});
|
||||
test('counts for non-numeric range correctly', function () {
|
||||
expect(Count::byFields(ThrowawayDb::TABLE, [Field::between('value', 'aardvark', 'apple')]))->toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byContains()', function () {
|
||||
test('counts matching documents', function () {
|
||||
expect(Count::byContains(ThrowawayDb::TABLE, ['value' => 'purple']))->toBe(2);
|
||||
});
|
||||
test('returns 0 for no matching documents', function () {
|
||||
expect(Count::byContains(ThrowawayDb::TABLE, ['value' => 'magenta']))->toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byJsonPath()', function () {
|
||||
test('counts matching documents', function () {
|
||||
expect(Count::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ < 5)'))->toBe(2);
|
||||
});
|
||||
test('returns 0 for no matching documents', function () {
|
||||
expect(Count::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)'))->toBe(0);
|
||||
});
|
||||
});
|
||||
97
tests/Integration/PostgreSQL/CustomTest.php
Normal file
97
tests/Integration/PostgreSQL/CustomTest.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{Count, Custom, DocumentException, Query};
|
||||
use BitBadger\PDODocument\Mapper\{CountMapper, DocumentMapper};
|
||||
use Test\Integration\PostgreSQL\ThrowawayDb;
|
||||
use Test\Integration\TestDocument;
|
||||
|
||||
pest()->group('integration', 'postgresql');
|
||||
|
||||
describe('::runQuery()', function () {
|
||||
test('runs a valid query successfully', function () {
|
||||
$stmt = &Custom::runQuery('SELECT data FROM ' . ThrowawayDb::TABLE . ' LIMIT 1', []);
|
||||
try {
|
||||
expect($stmt)->not->toBeNull();
|
||||
} finally {
|
||||
$stmt = null;
|
||||
}
|
||||
});
|
||||
test('fails with an invalid query', function () {
|
||||
$stmt = null;
|
||||
try {
|
||||
expect(function () use (&$stmt) { $stmt = &Custom::runQuery('GRAB stuff FROM over_there UNTIL done', []); })
|
||||
->toThrow(DocumentException::class);
|
||||
} finally {
|
||||
$stmt = null;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('::list()', function () {
|
||||
test('returns non-empty list when data found', function () {
|
||||
$list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE), [], new DocumentMapper(TestDocument::class));
|
||||
expect($list)->not->toBeNull();
|
||||
$count = 0;
|
||||
foreach ($list->items as $ignored) $count++;
|
||||
expect($count)->toBe(5);
|
||||
});
|
||||
test('returns empty list when no data found', function () {
|
||||
expect(Custom::list(
|
||||
Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE (data->>'num_value')::numeric > :value",
|
||||
[':value' => 100], new DocumentMapper(TestDocument::class)))
|
||||
->not->toBeNull()
|
||||
->hasItems->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::array()', function () {
|
||||
test('returns non-empty array when data found', function () {
|
||||
expect(Custom::array(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'sub' IS NOT NULL", [],
|
||||
new DocumentMapper(TestDocument::class)))
|
||||
->not->toBeNull()
|
||||
->toHaveCount(2);
|
||||
});
|
||||
test('returns empty array when no data found', function () {
|
||||
expect(Custom::array(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'value' = :value",
|
||||
[':value' => 'not there'], new DocumentMapper(TestDocument::class)))
|
||||
->not->toBeNull()
|
||||
->toBeEmpty();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::single()', function () {
|
||||
test('returns a document when one is found', function () {
|
||||
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id", [':id' => 'one'],
|
||||
new DocumentMapper(TestDocument::class));
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('one');
|
||||
});
|
||||
test('returns no document when one is not found', function () {
|
||||
expect(Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id",
|
||||
[':id' => 'eighty'], new DocumentMapper(TestDocument::class)))
|
||||
->isNone->toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::nonQuery()', function () {
|
||||
test('works when documents match the WHERE clause', function () {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(0);
|
||||
});
|
||||
test('works when no documents match the WHERE clause', function () {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE . " WHERE (data->>'num_value')::numeric > :value",
|
||||
[':value' => 100]);
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::scalar()', function () {
|
||||
test('returns a scalar value', function () {
|
||||
expect(Custom::scalar("SELECT 5 AS it", [], new CountMapper()))->toBe(5);
|
||||
});
|
||||
});
|
||||
47
tests/Integration/PostgreSQL/DefinitionTest.php
Normal file
47
tests/Integration/PostgreSQL/DefinitionTest.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{Definition, DocumentIndex};
|
||||
|
||||
pest()->group('integration', 'postgresql');
|
||||
|
||||
describe('::ensureTable()', function () {
|
||||
test('creates a table', function () {
|
||||
expect($this->dbObjectExists('ensured'))->toBeFalse()
|
||||
->and($this->dbObjectExists('idx_ensured_key'))->toBeFalse();
|
||||
Definition::ensureTable('ensured');
|
||||
expect($this->dbObjectExists('ensured'))->toBeTrue()
|
||||
->and($this->dbObjectExists('idx_ensured_key'))->toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::ensureFieldIndex()', function () {
|
||||
test('creates an index', function () {
|
||||
expect($this->dbObjectExists('idx_ensured_test'))->toBeFalse();
|
||||
Definition::ensureTable('ensured');
|
||||
Definition::ensureFieldIndex('ensured', 'test', ['name', 'age']);
|
||||
expect($this->dbObjectExists('idx_ensured_test'))->toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::ensureDocumentIndex()', function () {
|
||||
test('creates a full index', function () {
|
||||
$docIdx = 'idx_doc_table_document';
|
||||
Definition::ensureTable('doc_table');
|
||||
expect($this->dbObjectExists($docIdx))->toBeFalse();
|
||||
Definition::ensureDocumentIndex('doc_table', DocumentIndex::Full);
|
||||
expect($this->dbObjectExists($docIdx))->toBeTrue();
|
||||
});
|
||||
test('creates an optimized index', function () {
|
||||
$docIdx = 'idx_doc_tbl_document';
|
||||
Definition::ensureTable('doc_tbl');
|
||||
expect($this->dbObjectExists($docIdx))->toBeFalse();
|
||||
Definition::ensureDocumentIndex('doc_tbl', DocumentIndex::Optimized);
|
||||
expect($this->dbObjectExists($docIdx))->toBeTrue();
|
||||
});
|
||||
});
|
||||
64
tests/Integration/PostgreSQL/DeleteTest.php
Normal file
64
tests/Integration/PostgreSQL/DeleteTest.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{Count, Delete, Field};
|
||||
use Test\Integration\PostgreSQL\ThrowawayDb;
|
||||
|
||||
pest()->group('integration', 'postgresql');
|
||||
|
||||
describe('::byId()', function () {
|
||||
test('deletes a document when ID is matched', function () {
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
|
||||
Delete::byId(ThrowawayDb::TABLE, 'four');
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(4);
|
||||
});
|
||||
test('does not delete a document when ID is not matched', function () {
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
|
||||
Delete::byId(ThrowawayDb::TABLE, 'negative four');
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byFields()', function () {
|
||||
test('deletes documents when fields match', function () {
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
|
||||
Delete::byFields(ThrowawayDb::TABLE, [Field::notEqual('value', 'purple')]);
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(2);
|
||||
});
|
||||
test('does not delete documents when fields are not matched', function () {
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
|
||||
Delete::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'crimson')]);
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byContains()', function () {
|
||||
test('deletes documents when containment matches', function () {
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
|
||||
Delete::byContains(ThrowawayDb::TABLE, ['value' => 'purple']);
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(3);
|
||||
});
|
||||
test('does not delete documents when containment is not matched', function () {
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
|
||||
Delete::byContains(ThrowawayDb::TABLE, ['target' => 'acquired']);
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byJsonPath()', function () {
|
||||
test('deletes documents when path matches', function () {
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
|
||||
Delete::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ <> 0)');
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(1);
|
||||
});
|
||||
test('does not delete documents when path is not matched', function () {
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
|
||||
Delete::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ < 0)');
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
|
||||
});
|
||||
});
|
||||
96
tests/Integration/PostgreSQL/DocumentListTest.php
Normal file
96
tests/Integration/PostgreSQL/DocumentListTest.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{DocumentException, DocumentList, Query};
|
||||
use BitBadger\PDODocument\Mapper\DocumentMapper;
|
||||
use Test\Integration\PostgreSQL\ThrowawayDb;
|
||||
use Test\Integration\TestDocument;
|
||||
|
||||
pest()->group('integration', 'postgresql');
|
||||
|
||||
describe('::create()', function () {
|
||||
test('creates a document list', function () {
|
||||
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
||||
new DocumentMapper(TestDocument::class));
|
||||
expect($list)->not->toBeNull();
|
||||
$list = null; // free database result
|
||||
});
|
||||
});
|
||||
|
||||
describe('->items', function () {
|
||||
test('enumerates items in the list', function () {
|
||||
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
||||
new DocumentMapper(TestDocument::class));
|
||||
expect($list)->not->toBeNull();
|
||||
$count = 0;
|
||||
foreach ($list->items as $item) {
|
||||
expect(['one', 'two', 'three', 'four', 'five'])->toContain($item->id);
|
||||
$count++;
|
||||
}
|
||||
expect($count)->toBe(5);
|
||||
});
|
||||
test('fails when the list is exhausted', function () {
|
||||
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
||||
new DocumentMapper(TestDocument::class));
|
||||
expect($list)->not->toBeNull()->hasItems->toBeTrue();
|
||||
$ignored = iterator_to_array($list->items);
|
||||
expect($list)->hasItems->toBeFalse()
|
||||
->and(fn () => iterator_to_array($list->items))->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
||||
|
||||
describe('->hasItems', function () {
|
||||
test('returns false when no items are in the list', function () {
|
||||
expect(DocumentList::create(
|
||||
Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE (data->>'num_value')::numeric < 0", [],
|
||||
new DocumentMapper(TestDocument::class)))
|
||||
->not->toBeNull()
|
||||
->hasItems->toBeFalse();
|
||||
});
|
||||
test('returns true when items are in the list', function () {
|
||||
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
||||
new DocumentMapper(TestDocument::class));
|
||||
expect($list)->not->toBeNull()->hasItems->toBeTrue();
|
||||
foreach ($list->items as $ignored) {
|
||||
expect($list)->hasItems->toBeTrue();
|
||||
}
|
||||
expect($list)->hasItems->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('->map()', function () {
|
||||
test('transforms the list', function () {
|
||||
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
||||
new DocumentMapper(TestDocument::class));
|
||||
expect($list)->not->toBeNull()->hasItems->toBeTrue();
|
||||
foreach ($list->map(fn($doc) => strrev($doc->id)) as $mapped) {
|
||||
expect(['eno', 'owt', 'eerht', 'ruof', 'evif'])->toContain($mapped);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('->iter()', function () {
|
||||
test('walks the list', function () {
|
||||
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
||||
new DocumentMapper(TestDocument::class));
|
||||
expect($list)->not->toBeNull()->hasItems->toBeTrue();
|
||||
$splats = [];
|
||||
$list->iter(function ($doc) use (&$splats) { $splats[] = str_repeat('*', strlen($doc->id)); });
|
||||
expect(implode(' ', $splats))->toBe('*** *** ***** **** ****');
|
||||
});
|
||||
});
|
||||
|
||||
describe('->mapToArray()', function () {
|
||||
test('creates an associative array', function () {
|
||||
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
||||
new DocumentMapper(TestDocument::class));
|
||||
expect($list)->not->toBeNull()->hasItems->toBeTrue()
|
||||
->and($list->mapToArray(fn($it) => $it->id, fn($it) => $it->value))
|
||||
->toBe(['one' => 'FIRST!', 'two' => 'another', 'three' => '', 'four' => 'purple', 'five' => 'purple']);
|
||||
});
|
||||
});
|
||||
249
tests/Integration/PostgreSQL/DocumentTest.php
Normal file
249
tests/Integration/PostgreSQL/DocumentTest.php
Normal file
@@ -0,0 +1,249 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{AutoId, Configuration, Custom, Document, DocumentException, Field, Find, Query};
|
||||
use BitBadger\PDODocument\Mapper\ArrayMapper;
|
||||
use Test\Integration\{NumDocument, SubDocument, TestDocument};
|
||||
use Test\Integration\PostgreSQL\ThrowawayDb;
|
||||
|
||||
pest()->group('integration', 'postgresql');
|
||||
|
||||
describe('::insert()', function () {
|
||||
test('inserts an array with no automatic ID', function () {
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 'turkey', 'sub' => ['foo' => 'gobble', 'bar' => 'gobble']]);
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
|
||||
expect($tryDoc)
|
||||
->isSome->toBeTrue()
|
||||
->and($tryDoc->value)
|
||||
->id->toBe('turkey')
|
||||
->num_value->toBe(0)
|
||||
->sub->not->toBeNull()
|
||||
->sub->foo->toBe('gobble')
|
||||
->sub->bar->toBe('gobble')
|
||||
->and($tryDoc->value->value)->toBe('');
|
||||
});
|
||||
test('inserts an array with auto-number ID, not provided', function () {
|
||||
Configuration::$autoId = AutoId::Number;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'new', 'num_value' => 8]);
|
||||
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper());
|
||||
expect($doc)->isSome->toBeTrue()
|
||||
->and(json_decode($doc->value['data']))
|
||||
->id->toBe(1);
|
||||
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'again', 'num_value' => 7]);
|
||||
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE " . Query::whereById(docId: 2),
|
||||
[':id' => 2], new ArrayMapper());
|
||||
expect($doc)->isSome->toBeTrue()
|
||||
->and(json_decode($doc->value['data']))
|
||||
->id->toBe(2);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an array with auto-number ID, provided', function () {
|
||||
Configuration::$autoId = AutoId::Number;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 7, 'value' => 'new', 'num_value' => 8]);
|
||||
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper());
|
||||
expect($doc)->isSome->toBeTrue()
|
||||
->and(json_decode($doc->value['data']))
|
||||
->id->toBe(7);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
|
||||
});
|
||||
test('inserts an array with auto-UUID ID, not provided', function () {
|
||||
Configuration::$autoId = AutoId::UUID;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => '', 'num_value' => 5]);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 5)], TestDocument::class);
|
||||
expect($doc)
|
||||
->isSome->toBeTrue()
|
||||
->and($doc->value)->id->not->toBeEmpty();
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an array with auto-UUID ID, provided', function () {
|
||||
Configuration::$autoId = AutoId::UUID;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
$uuid = AutoId::generateUUID();
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => $uuid, 'value' => 'uuid', 'num_value' => 12]);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 12)], TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()
|
||||
->and($doc->value)->id->toBe($uuid);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an array with auto-string ID, not provided', function () {
|
||||
Configuration::$autoId = AutoId::RandomString;
|
||||
Configuration::$idStringLength = 6;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => '', 'value' => 'new', 'num_value' => 8]);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 8)], TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()
|
||||
->and($doc->value)->id->toHaveLength(6);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
Configuration::$idStringLength = 16;
|
||||
}
|
||||
});
|
||||
test('inserts an array with auto-string ID, provided', function () {
|
||||
Configuration::$autoId = AutoId::RandomString;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 'my-key', 'value' => 'old', 'num_value' => 3]);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()
|
||||
->and($doc->value)->id->toBe('my-key');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an object with no automatic ID', function () {
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument('turkey', sub: new SubDocument('gobble', 'gobble')));
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
|
||||
expect($tryDoc)->isSome->toBeTrue();
|
||||
$doc = $tryDoc->value;
|
||||
expect($doc)
|
||||
->id->toBe('turkey')
|
||||
->num_value->toBe(0)
|
||||
->sub->not->toBeNull()
|
||||
->sub->foo->toBe('gobble')
|
||||
->sub->bar->toBe('gobble')
|
||||
->and($doc->value)->toBe('');
|
||||
});
|
||||
test('inserts an object with auto-number ID, not provided', function () {
|
||||
Configuration::$autoId = AutoId::Number;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
|
||||
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'taco'));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'taco')], NumDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()
|
||||
->and($doc->value)->id->toBe(1);
|
||||
|
||||
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'burrito'));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'burrito')], NumDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()
|
||||
->and($doc->value)->id->toBe(2);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an object with auto-number ID, provided', function () {
|
||||
Configuration::$autoId = AutoId::Number;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, new NumDocument(64, 'large'));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'large')], NumDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()
|
||||
->and($doc->value)->id->toBe(64);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an object with auto-UUID ID, not provided', function () {
|
||||
Configuration::$autoId = AutoId::UUID;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument(value: 'something', num_value: 9));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::exists('value')], TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()
|
||||
->and($doc->value)->id->not->toBeEmpty();
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an object with auto-UUID ID, provided', function () {
|
||||
Configuration::$autoId = AutoId::UUID;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
$uuid = AutoId::generateUUID();
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument($uuid, num_value: 14));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 14)], TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()
|
||||
->and($doc->value)->id->toBe($uuid);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an object with auto-string ID, not provided', function () {
|
||||
Configuration::$autoId = AutoId::RandomString;
|
||||
Configuration::$idStringLength = 40;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument(num_value: 55));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 55)], TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()
|
||||
->and($doc->value)->id->toHaveLength(40);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
Configuration::$idStringLength = 16;
|
||||
}
|
||||
});
|
||||
test('inserts an object with auto-string ID, provided', function () {
|
||||
Configuration::$autoId = AutoId::RandomString;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument('my-key', num_value: 3));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()
|
||||
->and($doc->value)->id->toBe('my-key');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('throws an exception for duplicate key', function () {
|
||||
expect(fn () => Document::insert(ThrowawayDb::TABLE, new TestDocument('one')))
|
||||
->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::save()', function () {
|
||||
test('inserts a new document', function () {
|
||||
Document::save(ThrowawayDb::TABLE, new TestDocument('test', sub: new SubDocument('a', 'b')));
|
||||
expect(Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class))->isSome->toBeTrue();
|
||||
});
|
||||
test('updates an existing document', function () {
|
||||
Document::save(ThrowawayDb::TABLE, new TestDocument('two', num_value: 44));
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
|
||||
expect($tryDoc)->isSome->toBeTrue()
|
||||
->and($tryDoc->value)
|
||||
->num_value->toBe(44)
|
||||
->sub->toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::update()', function () {
|
||||
test('replaces an existing document', function () {
|
||||
Document::update(ThrowawayDb::TABLE, 'one', new TestDocument('one', 'howdy', 8, new SubDocument('y', 'z')));
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
|
||||
expect($tryDoc)->isSome->toBeTrue();
|
||||
$doc = $tryDoc->value;
|
||||
expect($doc)
|
||||
->num_value->toBe(8)
|
||||
->sub->not->toBeNull()
|
||||
->sub->foo->toBe('y')
|
||||
->sub->bar->toBe('z')
|
||||
->and($doc->value)->toBe('howdy');
|
||||
});
|
||||
test('does nothing for a non-existent document', function () {
|
||||
Document::update(ThrowawayDb::TABLE, 'two-hundred', new TestDocument('200'));
|
||||
expect(Find::byId(ThrowawayDb::TABLE, 'two-hundred', TestDocument::class))->isNone->toBeTrue();
|
||||
});
|
||||
});
|
||||
48
tests/Integration/PostgreSQL/ExistsTest.php
Normal file
48
tests/Integration/PostgreSQL/ExistsTest.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{Exists, Field};
|
||||
use Test\Integration\PostgreSQL\ThrowawayDb;
|
||||
|
||||
pest()->group('integration', 'postgresql');
|
||||
|
||||
describe('::byId()', function () {
|
||||
test('returns true when a document exists', function () {
|
||||
expect(Exists::byId(ThrowawayDb::TABLE, 'three'))->toBeTrue();
|
||||
});
|
||||
test('returns false when a document does not exist', function () {
|
||||
expect(Exists::byId(ThrowawayDb::TABLE, 'seven'))->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byFields()', function () {
|
||||
test('returns true when matching documents exist', function () {
|
||||
expect(Exists::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 10)]))->toBeTrue();
|
||||
});
|
||||
test('returns false when no matching documents exist', function () {
|
||||
expect(Exists::byFields(ThrowawayDb::TABLE, [Field::less('nothing', 'none')]))->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byContains()', function () {
|
||||
test('returns true when matching documents exist', function () {
|
||||
expect(Exists::byContains(ThrowawayDb::TABLE, ['value' => 'purple']))->toBeTrue();
|
||||
});
|
||||
test('returns false when no matching documents exist', function () {
|
||||
expect(Exists::byContains(ThrowawayDb::TABLE, ['value' => 'violet']))->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byJsonPath()', function () {
|
||||
test('returns true when matching documents exist', function () {
|
||||
expect(Exists::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ == 10)'))->toBeTrue();
|
||||
});
|
||||
test('returns false when no matching documents exist', function () {
|
||||
expect(Exists::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ == 10.1)'))->toBeFalse();
|
||||
});
|
||||
});
|
||||
214
tests/Integration/PostgreSQL/FindTest.php
Normal file
214
tests/Integration/PostgreSQL/FindTest.php
Normal file
@@ -0,0 +1,214 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{Custom, Delete, Document, Field, FieldMatch, Find};
|
||||
use Test\Integration\{ArrayDocument, NumDocument, TestDocument};
|
||||
use Test\Integration\PostgreSQL\ThrowawayDb;
|
||||
|
||||
pest()->group('integration', 'postgresql');
|
||||
|
||||
describe('::all()', function () {
|
||||
test('retrieves data', function () {
|
||||
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class);
|
||||
expect($docs)->not->toBeNull();
|
||||
$count = 0;
|
||||
foreach ($docs->items as $ignored) $count++;
|
||||
expect($count)->toBe(5);
|
||||
});
|
||||
test('sorts data ascending', function () {
|
||||
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id')]);
|
||||
expect($docs)->not->toBeNull()
|
||||
->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))
|
||||
->toBe(['five', 'four', 'one', 'three', 'two']);
|
||||
});
|
||||
test('sorts data descending', function () {
|
||||
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id DESC')]);
|
||||
expect($docs)->not->toBeNull()
|
||||
->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))
|
||||
->toBe(['two', 'three', 'one', 'four', 'five']);
|
||||
});
|
||||
test('sorts data numerically', function () {
|
||||
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class,
|
||||
[Field::named('sub.foo NULLS LAST'), Field::named('n:num_value')]);
|
||||
expect($docs)->not->toBeNull()
|
||||
->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))
|
||||
->toBe(['two', 'four', 'one', 'three', 'five']);
|
||||
});
|
||||
test('retrieves empty results', function () {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
expect(Find::all(ThrowawayDb::TABLE, TestDocument::class))
|
||||
->not->toBeNull()
|
||||
->hasItems->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byId()', function () {
|
||||
test('retrieves a document via string ID', function () {
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('two');
|
||||
});
|
||||
test('retrieves a document via numeric ID', function () {
|
||||
Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absent')]);
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 18, 'value' => 'howdy']);
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 18, NumDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe(18);
|
||||
});
|
||||
test('returns None when a document is not found', function () {
|
||||
expect(Find::byId(ThrowawayDb::TABLE, 'seventy-five', TestDocument::class))->isNone->toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byFields()', function () {
|
||||
test('retrieves matching documents', function () {
|
||||
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('value', ['blue', 'purple']), Field::exists('sub')],
|
||||
TestDocument::class, FieldMatch::All);
|
||||
expect($docs)->not->toBeNull();
|
||||
$count = 0;
|
||||
foreach ($docs->items as $ignored) $count++;
|
||||
expect($count)->toBe(1);
|
||||
});
|
||||
test('retrieves ordered matching documents', function () {
|
||||
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], TestDocument::class,
|
||||
FieldMatch::All, [Field::named('id')]);
|
||||
expect($docs)->not->toBeNull()
|
||||
->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))->toBe(['five', 'four']);
|
||||
});
|
||||
test('retrieves documents matching a numeric IN clause', function () {
|
||||
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('num_value', [2, 4, 6, 8])], TestDocument::class);
|
||||
expect($docs)->not->toBeNull();
|
||||
$count = 0;
|
||||
foreach ($docs->items as $ignored) $count++;
|
||||
expect($count)->toBe(1);
|
||||
});
|
||||
test('returns an empty list when no matching documents are found', function () {
|
||||
expect(Find::byFields(ThrowawayDb::TABLE, [Field::greater('num_value', 100)], TestDocument::class))
|
||||
->not->toBeNull()
|
||||
->hasItems->toBeFalse();
|
||||
});
|
||||
test('retrieves documents matching an inArray condition', function () {
|
||||
Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absentField')]);
|
||||
foreach (ArrayDocument::testDocuments() as $doc) Document::insert(ThrowawayDb::TABLE, $doc);
|
||||
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::inArray('values', ThrowawayDb::TABLE, ['c'])],
|
||||
ArrayDocument::class);
|
||||
expect($docs)->not->toBeNull();
|
||||
$count = 0;
|
||||
foreach ($docs->items as $ignored) $count++;
|
||||
expect($count)->toBe(2);
|
||||
});
|
||||
test('returns an empty list when no documents match an inArray condition', function () {
|
||||
Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absentField')]);
|
||||
foreach (ArrayDocument::testDocuments() as $doc) Document::insert(ThrowawayDb::TABLE, $doc);
|
||||
expect(Find::byFields(ThrowawayDb::TABLE, [Field::inArray('values', ThrowawayDb::TABLE, ['j'])],
|
||||
ArrayDocument::class))
|
||||
->not->toBeNull()
|
||||
->hasItems->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byContains()', function () {
|
||||
test('retrieves matching documents', function () {
|
||||
$docs = Find::byContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class);
|
||||
expect($docs)->not->toBeNull();
|
||||
$count = 0;
|
||||
foreach ($docs->items as $ignored) $count++;
|
||||
expect($count)->toBe(2);
|
||||
});
|
||||
test('retrieves ordered matching documents', function () {
|
||||
$docs = Find::byContains(ThrowawayDb::TABLE, ['sub' => ['foo' => 'green']], TestDocument::class,
|
||||
[Field::named('value')]);
|
||||
expect($docs)
|
||||
->not->toBeNull()
|
||||
->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))->toBe(['two', 'four']);
|
||||
});
|
||||
test('returns an empty list when no documents match', function () {
|
||||
expect(Find::byContains(ThrowawayDb::TABLE, ['value' => 'indigo'], TestDocument::class))
|
||||
->not->toBeNull()
|
||||
->hasItems->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byJsonPath()', function () {
|
||||
test('retrieves matching documents', function () {
|
||||
$docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class);
|
||||
expect($docs)->not->toBeNull();
|
||||
$count = 0;
|
||||
foreach ($docs->items as $ignored) $count++;
|
||||
expect($count)->toBe(2);
|
||||
});
|
||||
test('retrieves ordered matching documents', function () {
|
||||
$docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class,
|
||||
[Field::named('id')]);
|
||||
expect($docs)->not->toBeNull()
|
||||
->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))->toBe(['five', 'four']);
|
||||
});
|
||||
test('returns an empty list when no documents match', function () {
|
||||
expect(Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)', TestDocument::class))
|
||||
->not->toBeNull()
|
||||
->hasItems->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::firstByFields()', function () {
|
||||
test('retrieves a matching document', function () {
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'another')], TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('two');
|
||||
});
|
||||
test('retrieves a document for multiple results', function () {
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and(['two', 'four'])->toContain($doc->value->id);
|
||||
});
|
||||
test('retrieves a document for multiple ordered results', function () {
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class,
|
||||
orderBy: [Field::named('n:num_value DESC')]);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('four');
|
||||
});
|
||||
test('returns None when no documents match', function () {
|
||||
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'absent')], TestDocument::class))
|
||||
->isNone->toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::firstByContains()', function () {
|
||||
test('retrieves a matching document', function () {
|
||||
$doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'FIRST!'], TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('one');
|
||||
});
|
||||
test('retrieves a document for multiple results', function () {
|
||||
$doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and(['four', 'five'])->toContain($doc->value->id);
|
||||
});
|
||||
test('retrieves a document for multiple ordered results', function () {
|
||||
$doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class,
|
||||
[Field::named('sub.bar NULLS FIRST')]);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('five');
|
||||
});
|
||||
test('returns None when no documents match', function () {
|
||||
expect(Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'indigo'], TestDocument::class))
|
||||
->isNone->toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::firstByJsonPath()', function () {
|
||||
test('retrieves a matching document', function () {
|
||||
$doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ == 10)', TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('two');
|
||||
});
|
||||
test('retrieves a document for multiple results', function () {
|
||||
$doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and(['four', 'five'])->toContain($doc->value->id);
|
||||
});
|
||||
test('retrieves a document for multiple ordered results', function () {
|
||||
$doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class,
|
||||
[Field::named('id DESC')]);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('four');
|
||||
});
|
||||
test('returns None when no documents match', function () {
|
||||
expect(Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)', TestDocument::class))
|
||||
->isNone->toBeTrue();
|
||||
});
|
||||
});
|
||||
71
tests/Integration/PostgreSQL/PatchTest.php
Normal file
71
tests/Integration/PostgreSQL/PatchTest.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{Count, Exists, Field, Find, Patch};
|
||||
use Test\Integration\PostgreSQL\ThrowawayDb;
|
||||
use Test\Integration\TestDocument;
|
||||
|
||||
pest()->group('integration', 'postgresql');
|
||||
|
||||
describe('::byId()', function () {
|
||||
test('updates an existing document', function () {
|
||||
Patch::byId(ThrowawayDb::TABLE, 'one', ['num_value' => 44]);
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->num_value->toBe(44);
|
||||
});
|
||||
test('does nothing when a document does not exist', function () {
|
||||
$id = 'forty-seven';
|
||||
expect(Exists::byId(ThrowawayDb::TABLE, $id))->toBeFalse();
|
||||
Patch::byId(ThrowawayDb::TABLE, $id, ['foo' => 'green']); // no exception = pass
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byFields()', function () {
|
||||
test('updates existing documents', function () {
|
||||
Patch::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], ['num_value' => 77]);
|
||||
expect(Count::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 77)]))->toBe(2);
|
||||
});
|
||||
test('does nothing when no matching documents exist', function () {
|
||||
$fields = [Field::equal('value', 'burgundy')];
|
||||
expect(Count::byFields(ThrowawayDb::TABLE, $fields))->toBe(0);
|
||||
Patch::byFields(ThrowawayDb::TABLE, $fields, ['foo' => 'green']); // no exception = pass
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byContains()', function () {
|
||||
test('updates existing documents', function () {
|
||||
Patch::byContains(ThrowawayDb::TABLE, ['value' => 'another'], ['num_value' => 12]);
|
||||
$tryDoc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'another'], TestDocument::class);
|
||||
expect($tryDoc)->isSome->toBeTrue()
|
||||
->and($tryDoc->value)
|
||||
->id->toBe('two')
|
||||
->num_value->toBe(12);
|
||||
});
|
||||
test('does nothing when no matching documents exist', function () {
|
||||
$criteria = ['value' => 'updated'];
|
||||
expect(Count::byContains(ThrowawayDb::TABLE, $criteria))->toBe(0);
|
||||
Patch::byContains(ThrowawayDb::TABLE, $criteria, ['sub.foo' => 'green']); // no exception = pass
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byJsonPath()', function () {
|
||||
test('updates existing documents', function () {
|
||||
Patch::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', ['value' => 'blue']);
|
||||
$docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class);
|
||||
expect($docs)->not->toBeNull()->hasItems->toBeTrue();
|
||||
foreach ($docs->items as $item) {
|
||||
expect(['four', 'five'])->toContain($item->id)
|
||||
->and($item->value)->toBe('blue');
|
||||
}
|
||||
});
|
||||
test('does nothing when no matching documents exist', function () {
|
||||
$path = '$.num_value ? (@ > 100)';
|
||||
expect(Count::byJsonPath(ThrowawayDb::TABLE, $path))->toBe(0);
|
||||
Patch::byJsonPath(ThrowawayDb::TABLE, $path, ['value' => 'blue']); // no exception = pass
|
||||
});
|
||||
});
|
||||
90
tests/Integration/PostgreSQL/RemoveFieldsTest.php
Normal file
90
tests/Integration/PostgreSQL/RemoveFieldsTest.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{Exists, Field, Find, RemoveFields};
|
||||
use Test\Integration\PostgreSQL\ThrowawayDb;
|
||||
use Test\Integration\TestDocument;
|
||||
|
||||
pest()->group('integration', 'postgresql');
|
||||
|
||||
describe('::byId()', function () {
|
||||
test('removes fields', function () {
|
||||
RemoveFields::byId(ThrowawayDb::TABLE, 'two', ['sub', 'value']);
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
|
||||
expect($tryDoc)->isSome->toBeTrue();
|
||||
$doc = $tryDoc->value;
|
||||
expect($doc)->sub->toBeNull()
|
||||
->and($doc->value)->toBeEmpty();
|
||||
});
|
||||
test('does nothing when the field to remove does not exist', function () {
|
||||
expect(Exists::byFields(ThrowawayDb::TABLE, [Field::exists('a_field_that_does_not_exist')]))->toBeFalse();
|
||||
RemoveFields::byId(ThrowawayDb::TABLE, 'one', ['a_field_that_does_not_exist']); // no exception = pass
|
||||
});
|
||||
test('does nothing when the document does not exist', function () {
|
||||
expect(Exists::byId(ThrowawayDb::TABLE, 'fifty'))->toBeFalse();
|
||||
RemoveFields::byId(ThrowawayDb::TABLE, 'fifty', ['sub']); // no exception = pass
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byFields()', function () {
|
||||
test('removes fields from matching documents', function () {
|
||||
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], ['sub']);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->sub->toBeNull();
|
||||
});
|
||||
test('does nothing when the field to remove does not exist', function () {
|
||||
expect(Exists::byFields(ThrowawayDb::TABLE, [Field::exists('nada')]))->toBeFalse();
|
||||
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], ['nada']); // no exception = pass
|
||||
});
|
||||
test('does nothing when no documents match', function () {
|
||||
expect(Exists::byFields(ThrowawayDb::TABLE, [Field::notEqual('missing', 'nope')]))->toBeFalse();
|
||||
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::notEqual('missing', 'nope')], ['value']); // no exn = pass
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byContains()', function () {
|
||||
test('removes fields from matching documents', function () {
|
||||
$criteria = ['sub' => ['foo' => 'green']];
|
||||
RemoveFields::byContains(ThrowawayDb::TABLE, $criteria, ['value']);
|
||||
$docs = Find::byContains(ThrowawayDb::TABLE, $criteria, TestDocument::class);
|
||||
expect($docs)->not->toBeNull()->hasItems->toBeTrue();
|
||||
foreach ($docs->items as $item) {
|
||||
expect(['two', 'four'])->toContain($item->id)
|
||||
->and($item->value)->toBeEmpty();
|
||||
}
|
||||
});
|
||||
test('does nothing when the field to remove does not exist', function () {
|
||||
expect(Exists::byFields(ThrowawayDb::TABLE, [Field::exists('invalid_field')]))->toBeFalse();
|
||||
RemoveFields::byContains(ThrowawayDb::TABLE, ['sub' => ['foo' => 'green']], ['invalid_field']); // no exn = pass
|
||||
});
|
||||
test('does nothing when no documents match', function () {
|
||||
expect(Exists::byContains(ThrowawayDb::TABLE, ['value' => 'substantial']))->toBeFalse();
|
||||
RemoveFields::byContains(ThrowawayDb::TABLE, ['value' => 'substantial'], ['num_value']); // no exception = pass
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byJsonPath()', function () {
|
||||
test('removes fields from matching documents', function () {
|
||||
$path = '$.value ? (@ == "purple")';
|
||||
RemoveFields::byJsonPath(ThrowawayDb::TABLE, $path, ['sub']);
|
||||
$docs = Find::byJsonPath(ThrowawayDb::TABLE, $path, TestDocument::class);
|
||||
expect($docs)->not->toBeNull()->hasItems->toBeTrue();
|
||||
foreach ($docs->items as $item) {
|
||||
expect(['four', 'five'])->toContain($item->id)
|
||||
->and($item->sub)->toBeNull();
|
||||
}
|
||||
});
|
||||
test('does nothing when the field to remove does not exist', function () {
|
||||
expect(Exists::byFields(ThrowawayDb::TABLE, [Field::exists('submarine')]))->toBeFalse();
|
||||
RemoveFields::byJsonPath(ThrowawayDb::TABLE, '$.value ? (@ == "purple")', ['submarine']); // no exception = pass
|
||||
});
|
||||
test('does nothing when no documents match', function () {
|
||||
expect(Exists::byJsonPath(ThrowawayDb::TABLE, '$.value ? (@ == "mauve")'))->toBeFalse();
|
||||
RemoveFields::byJsonPath(ThrowawayDb::TABLE, '$.value ? (@ == "mauve")', ['value']); // no exception = pass
|
||||
});
|
||||
});
|
||||
92
tests/Integration/PostgreSQL/ThrowawayDb.php
Normal file
92
tests/Integration/PostgreSQL/ThrowawayDb.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
* @see https://github.com/Zaid-Ajaj/ThrowawayDb The origin concept
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Test\Integration\PostgreSQL;
|
||||
|
||||
use BitBadger\PDODocument\{AutoId, Configuration, Custom, Definition, Document, DocumentException};
|
||||
use Random\RandomException;
|
||||
use Test\Integration\{SubDocument, TestDocument};
|
||||
|
||||
/**
|
||||
* Utilities to create and destroy a throwaway PostgreSQL database to use for testing
|
||||
*/
|
||||
class ThrowawayDb
|
||||
{
|
||||
/** @var string The table used for document manipulation */
|
||||
public const TABLE = 'test_table';
|
||||
|
||||
/**
|
||||
* Configure the document library for the given database (or the main PostgreSQL connection, if the database name
|
||||
* is not provided; this is used for creating and dropping databases)
|
||||
*
|
||||
* @param string|null $dbName The name of the database to configure (optional, defaults to env or "postgres")
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
private static function configure(?string $dbName = null): void
|
||||
{
|
||||
Configuration::useDSN(sprintf("pgsql:host=%s;dbname=%s", $_ENV['PDO_DOC_PGSQL_HOST'] ?? 'localhost',
|
||||
$dbName ?? $_ENV['PDO_DOC_PGSQL_DB'] ?? 'postgres'));
|
||||
Configuration::$username = $_ENV['PDO_DOC_PGSQL_USER'] ?? 'postgres';
|
||||
Configuration::$password = $_ENV['PDO_DOC_PGSQL_PASS'] ?? 'postgres';
|
||||
Configuration::resetPDO();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load data into the test table
|
||||
*
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function loadData(): void
|
||||
{
|
||||
Document::insert(self::TABLE, new TestDocument('one', 'FIRST!', 0));
|
||||
Document::insert(self::TABLE, new TestDocument('two', 'another', 10, new SubDocument('green', 'blue')));
|
||||
Document::insert(self::TABLE, new TestDocument('three', '', 4));
|
||||
Document::insert(self::TABLE, new TestDocument('four', 'purple', 17, new SubDocument('green', 'red')));
|
||||
Document::insert(self::TABLE, new TestDocument('five', 'purple', 18));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a throwaway PostgreSQL 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|RandomException If any is encountered
|
||||
*/
|
||||
public static function create(bool $withData = true): string
|
||||
{
|
||||
$dbName = 'throwaway_' . AutoId::generateRandom(10);
|
||||
self::configure();
|
||||
Custom::nonQuery("CREATE DATABASE $dbName WITH OWNER " . Configuration::$username, []);
|
||||
self::configure($dbName);
|
||||
|
||||
Definition::ensureTable(self::TABLE);
|
||||
|
||||
if ($withData) {
|
||||
self::loadData();
|
||||
}
|
||||
|
||||
return $dbName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy a throwaway PostgreSQL database
|
||||
*
|
||||
* @param string $dbName The name of the PostgreSQL database to be dropped
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function destroy(string $dbName): void
|
||||
{
|
||||
self::configure();
|
||||
Custom::nonQuery("DROP DATABASE IF EXISTS $dbName WITH (FORCE)", []);
|
||||
Configuration::useDSN('');
|
||||
Configuration::$username = null;
|
||||
Configuration::$password = null;
|
||||
Configuration::resetPDO();
|
||||
}
|
||||
}
|
||||
39
tests/Integration/SQLite/CountTest.php
Normal file
39
tests/Integration/SQLite/CountTest.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{Count, DocumentException, Field};
|
||||
use Test\Integration\SQLite\ThrowawayDb;
|
||||
|
||||
pest()->group('integration', 'sqlite');
|
||||
|
||||
describe('::all()', function () {
|
||||
test('counts all documents', function () {
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byFields()', function () {
|
||||
test('counts by numeric range', function () {
|
||||
expect(Count::byFields(ThrowawayDb::TABLE, [Field::between('num_value', 10, 20)]))->toBe(3);
|
||||
});
|
||||
test('counts by non-numeric range', function () {
|
||||
expect(Count::byFields(ThrowawayDb::TABLE, [Field::between('value', 'aardvark', 'apple')]))->toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byContains()', function () {
|
||||
test('throws an exception', function () {
|
||||
expect(fn () => Count::byContains('', []))->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byJsonPath()', function () {
|
||||
test('throws an exception', function () {
|
||||
expect(fn () => Count::byJsonPath('', ''))->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
||||
94
tests/Integration/SQLite/CustomTest.php
Normal file
94
tests/Integration/SQLite/CustomTest.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{Count, Custom, DocumentException, Query};
|
||||
use BitBadger\PDODocument\Mapper\{CountMapper, DocumentMapper};
|
||||
use Test\Integration\SQLite\ThrowawayDb;
|
||||
use Test\Integration\TestDocument;
|
||||
|
||||
pest()->group('integration', 'sqlite');
|
||||
|
||||
describe('::runQuery()', function () {
|
||||
test('runs a valid query successfully', function () {
|
||||
$stmt = &Custom::runQuery('SELECT data FROM ' . ThrowawayDb::TABLE . ' LIMIT 1', []);
|
||||
try {
|
||||
expect($stmt)->not->toBeNull();
|
||||
} finally {
|
||||
$stmt = null;
|
||||
}
|
||||
});
|
||||
test('fails with an invalid query', function () {
|
||||
$stmt = null;
|
||||
try {
|
||||
expect(function () use (&$stmt) { $stmt = &Custom::runQuery('GRAB stuff FROM over_there UNTIL done', []); })
|
||||
->toThrow(DocumentException::class);
|
||||
} finally {
|
||||
$stmt = null;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('::list()', function () {
|
||||
test('returns non-empty list when data is found', function () {
|
||||
$list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE), [], new DocumentMapper(TestDocument::class));
|
||||
expect($list)->not->toBeNull();
|
||||
$count = 0;
|
||||
foreach ($list->items as $ignored) $count++;
|
||||
expect($count)->toBe(5);
|
||||
});
|
||||
test('returns empty list when not data is found', function () {
|
||||
expect(Custom::list(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'num_value' > :value",
|
||||
[':value' => 100], new DocumentMapper(TestDocument::class)))
|
||||
->not->toBeNull()
|
||||
->hasItems->toBeFalse();
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
describe('::array()', function () {
|
||||
test('returns non-empty array when data is found', function () {
|
||||
expect(Custom::array(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'sub' IS NOT NULL", [],
|
||||
new DocumentMapper(TestDocument::class)))
|
||||
->not->toBeNull()->toHaveCount(2);
|
||||
});
|
||||
test('returns empty array when data is not found', function () {
|
||||
expect(Custom::array(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'value' = :value",
|
||||
[':value' => 'not there'], new DocumentMapper(TestDocument::class)))
|
||||
->not->toBeNull()->toBeEmpty();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::single()', function () {
|
||||
test('returns a document when one is found', function () {
|
||||
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id", [':id' => 'one'],
|
||||
new DocumentMapper(TestDocument::class));
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('one');
|
||||
});
|
||||
test('returns no document when none is found', function () {
|
||||
expect(Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id",
|
||||
[':id' => 'eighty'], new DocumentMapper(TestDocument::class)))
|
||||
->isNone->toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::nonQuery()', function () {
|
||||
test('works when documents match the WHERE clause', function () {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(0);
|
||||
});
|
||||
test('works when no documents match the WHERE clause', function () {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE . " WHERE data->>'num_value' > :value", [':value' => 100]);
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::scalar()', function () {
|
||||
test('returns a scalar value', function () {
|
||||
expect(Custom::scalar("SELECT 5 AS it", [], new CountMapper()))->toBe(5);
|
||||
});
|
||||
});
|
||||
36
tests/Integration/SQLite/DefinitionTest.php
Normal file
36
tests/Integration/SQLite/DefinitionTest.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{Definition, DocumentException, DocumentIndex};
|
||||
|
||||
pest()->group('integration', 'sqlite');
|
||||
|
||||
describe('::ensureTable()', function () {
|
||||
test('creates table and PK index', function () {
|
||||
expect($this->dbObjectExists('ensured'))->toBeFalse()
|
||||
->and($this->dbObjectExists('idx_ensured_key'))->toBeFalse();
|
||||
Definition::ensureTable('ensured');
|
||||
expect($this->dbObjectExists('ensured'))->toBeTrue()
|
||||
->and($this->dbObjectExists('idx_ensured_key'))->toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::ensureFieldIndex()', function () {
|
||||
test('creates an index', function () {
|
||||
expect($this->dbObjectExists('idx_ensured_test'))->toBeFalse();
|
||||
Definition::ensureTable('ensured');
|
||||
Definition::ensureFieldIndex('ensured', 'test', ['name', 'age']);
|
||||
expect($this->dbObjectExists('idx_ensured_test'))->toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::ensureDocumentIndex()', function () {
|
||||
test('throws an exception', function () {
|
||||
expect(fn () => Definition::ensureDocumentIndex('', DocumentIndex::Full))->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
||||
50
tests/Integration/SQLite/DeleteTest.php
Normal file
50
tests/Integration/SQLite/DeleteTest.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{Count, Delete, DocumentException, Field};
|
||||
use Test\Integration\SQLite\ThrowawayDb;
|
||||
|
||||
pest()->group('integration', 'sqlite');
|
||||
|
||||
describe('::byId()', function () {
|
||||
test('deletes a document when one exists', function () {
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
|
||||
Delete::byId(ThrowawayDb::TABLE, 'four');
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(4);
|
||||
});
|
||||
test('does nothing when the document does not exist', function () {
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
|
||||
Delete::byId(ThrowawayDb::TABLE, 'negative four');
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byFields()', function () {
|
||||
test('deletes matching documents', function () {
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
|
||||
Delete::byFields(ThrowawayDb::TABLE, [Field::notEqual('value', 'purple')]);
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(2);
|
||||
});
|
||||
test('does nothing when no documents match', function () {
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
|
||||
Delete::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'crimson')]);
|
||||
expect(Count::all(ThrowawayDb::TABLE))->toBe(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byContains()', function () {
|
||||
test('throws an exception', function () {
|
||||
expect(fn () => Delete::byContains('', []))->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byJsonPath()', function () {
|
||||
test('throws an exception', function () {
|
||||
expect(fn () => Delete::byJsonPath('', ''))->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
||||
94
tests/Integration/SQLite/DocumentListTest.php
Normal file
94
tests/Integration/SQLite/DocumentListTest.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{DocumentException, DocumentList, Query};
|
||||
use BitBadger\PDODocument\Mapper\DocumentMapper;
|
||||
use Test\Integration\SQLite\ThrowawayDb;
|
||||
use Test\Integration\TestDocument;
|
||||
|
||||
pest()->group('integration', 'sqlite');
|
||||
|
||||
describe('::create()', function () {
|
||||
test('creates a document list', function () {
|
||||
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
||||
new DocumentMapper(TestDocument::class));
|
||||
expect($list)->not->toBeNull();
|
||||
$list = null; // free database result
|
||||
});
|
||||
});
|
||||
|
||||
describe('->items', function () {
|
||||
test('enumerates items in the list', function () {
|
||||
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
||||
new DocumentMapper(TestDocument::class));
|
||||
expect($list)->not->toBeNull();
|
||||
$count = 0;
|
||||
foreach ($list->items as $item) {
|
||||
expect(['one', 'two', 'three', 'four', 'five'])->toContain($item->id);
|
||||
$count++;
|
||||
}
|
||||
expect($count)->toBe(5);
|
||||
});
|
||||
test('fails when the list is exhausted', function () {
|
||||
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
||||
new DocumentMapper(TestDocument::class));
|
||||
expect($list)->not->toBeNull()->hasItems->toBeTrue();
|
||||
$ignored = iterator_to_array($list->items);
|
||||
expect($list)->hasItems->toBeFalse()
|
||||
->and(fn () => iterator_to_array($list->items))->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
||||
|
||||
describe('->hasItems', function () {
|
||||
test('returns true when items exist', function () {
|
||||
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
||||
new DocumentMapper(TestDocument::class));
|
||||
expect($list)->not->toBeNull()->hasItems->toBeTrue();
|
||||
foreach ($list->items as $ignored) {
|
||||
expect($list)->hasItems->toBeTrue();
|
||||
}
|
||||
expect($list)->hasItems->toBeFalse();
|
||||
});
|
||||
test('returns false when no items exist', function () {
|
||||
expect(DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'num_value' < 0", [],
|
||||
new DocumentMapper(TestDocument::class)))
|
||||
->not->toBeNull()->hasItems->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('->map()', function () {
|
||||
test('transforms the list', function () {
|
||||
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
||||
new DocumentMapper(TestDocument::class));
|
||||
expect($list)->not->toBeNull()->hasItems->toBeTrue();
|
||||
foreach ($list->map(fn($doc) => strrev($doc->id)) as $mapped) {
|
||||
expect(['eno', 'owt', 'eerht', 'ruof', 'evif'])->toContain($mapped);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('->iter()', function () {
|
||||
test('walks the list', function () {
|
||||
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
||||
new DocumentMapper(TestDocument::class));
|
||||
expect($list)->not->toBeNull()->hasItems->toBeTrue();
|
||||
$splats = [];
|
||||
$list->iter(function ($doc) use (&$splats) { $splats[] = str_repeat('*', strlen($doc->id)); });
|
||||
expect(implode(' ', $splats))->toBe('*** *** ***** **** ****');
|
||||
});
|
||||
});
|
||||
|
||||
describe('->mapToArray()', function () {
|
||||
test('creates an associative array', function () {
|
||||
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
|
||||
new DocumentMapper(TestDocument::class));
|
||||
expect($list)->not->toBeNull()->hasItems->toBeTrue()
|
||||
->and($list->mapToArray(fn($it) => $it->id, fn($it) => $it->value))
|
||||
->toBe(['one' => 'FIRST!', 'two' => 'another', 'three' => '', 'four' => 'purple', 'five' => 'purple']);
|
||||
});
|
||||
});
|
||||
231
tests/Integration/SQLite/DocumentTest.php
Normal file
231
tests/Integration/SQLite/DocumentTest.php
Normal file
@@ -0,0 +1,231 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{AutoId, Configuration, Custom, Document, DocumentException, Exists, Field, Find};
|
||||
use BitBadger\PDODocument\Mapper\ArrayMapper;
|
||||
use Test\Integration\{NumDocument, SubDocument, TestDocument};
|
||||
use Test\Integration\SQLite\ThrowawayDb;
|
||||
|
||||
pest()->group('integration', 'sqlite');
|
||||
|
||||
describe('::insert()', function () {
|
||||
test('inserts an array with no automatic ID', function () {
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 'turkey', 'sub' => ['foo' => 'gobble', 'bar' => 'gobble']]);
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
|
||||
expect($tryDoc)->isSome->toBeTrue()
|
||||
->and($tryDoc->value)
|
||||
->id->toBe('turkey')
|
||||
->num_value->toBe(0)
|
||||
->sub->not->toBeNull()
|
||||
->sub->foo->toBe('gobble')
|
||||
->sub->bar->toBe('gobble')
|
||||
->and($tryDoc->value->value)->toBeEmpty();
|
||||
});
|
||||
test('inserts an array with auto-number ID, not provided', function () {
|
||||
Configuration::$autoId = AutoId::Number;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'new', 'num_value' => 8]);
|
||||
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper());
|
||||
expect($doc)->isSome->toBeTrue()
|
||||
->and(json_decode($doc->value['data']))->id->toBe(1);
|
||||
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'again', 'num_value' => 7]);
|
||||
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = 2", [],
|
||||
new ArrayMapper());
|
||||
expect($doc)->isSome->toBeTrue()
|
||||
->and(json_decode($doc->value['data']))->id->toBe(2);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an array with auto-number ID, provided', function () {
|
||||
Configuration::$autoId = AutoId::Number;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 7, 'value' => 'new', 'num_value' => 8]);
|
||||
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper());
|
||||
expect($doc)->isSome->toBeTrue()
|
||||
->and(json_decode($doc->value['data']))->id->toBe(7);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an array with auto-UUID ID, not provided', function () {
|
||||
Configuration::$autoId = AutoId::UUID;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => '', 'num_value' => 5]);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 5)], TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->not->toBeEmpty();
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an array with auto-UUID ID, provided', function () {
|
||||
Configuration::$autoId = AutoId::UUID;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
$uuid = AutoId::generateUUID();
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => $uuid, 'value' => 'uuid', 'num_value' => 12]);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 12)], TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe($uuid);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an array with auto-string ID, not provided', function () {
|
||||
Configuration::$autoId = AutoId::RandomString;
|
||||
Configuration::$idStringLength = 6;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => '', 'value' => 'new', 'num_value' => 8]);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 8)], TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toHaveLength(6);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
Configuration::$idStringLength = 16;
|
||||
}
|
||||
});
|
||||
test('inserts an array with auto-string ID, provided', function () {
|
||||
Configuration::$autoId = AutoId::RandomString;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 'my-key', 'value' => 'old', 'num_value' => 3]);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('my-key');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an object with no automatic ID', function () {
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument('turkey', sub: new SubDocument('gobble', 'gobble')));
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
|
||||
expect($tryDoc)->isSome->toBeTrue()
|
||||
->and($tryDoc->value)
|
||||
->id->toBe('turkey')
|
||||
->num_value->toBe(0)
|
||||
->sub->not->toBeNull()
|
||||
->sub->foo->toBe('gobble')
|
||||
->sub->bar->toBe('gobble')
|
||||
->and($tryDoc->value->value)->toBeEmpty();
|
||||
});
|
||||
test('inserts an object with auto-number ID, not provided', function () {
|
||||
Configuration::$autoId = AutoId::Number;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
|
||||
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'taco'));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'taco')], NumDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe(1);
|
||||
|
||||
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'burrito'));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'burrito')], NumDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe(2);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an object with auto-number ID, provided', function () {
|
||||
Configuration::$autoId = AutoId::Number;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, new NumDocument(64, 'large'));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'large')], NumDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe(64);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an object with auto-UUID ID, not provided', function () {
|
||||
Configuration::$autoId = AutoId::UUID;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument(value: 'something', num_value: 9));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::exists('value')], TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->not->toBeEmpty();
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an object with auto-UUID ID, provided', function () {
|
||||
Configuration::$autoId = AutoId::UUID;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
$uuid = AutoId::generateUUID();
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument($uuid, num_value: 14));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 14)], TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe($uuid);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('inserts an object with auto-string ID, not provided', function () {
|
||||
Configuration::$autoId = AutoId::RandomString;
|
||||
Configuration::$idStringLength = 40;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument(num_value: 55));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 55)], TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toHaveLength(40);
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
Configuration::$idStringLength = 16;
|
||||
}
|
||||
});
|
||||
test('inserts an object with auto-string ID, provided', function () {
|
||||
Configuration::$autoId = AutoId::RandomString;
|
||||
try {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument('my-key', num_value: 3));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('my-key');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
});
|
||||
test('throws an exception for duplicate key', function () {
|
||||
expect(fn () => Document::insert(ThrowawayDb::TABLE, new TestDocument('one')))
|
||||
->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::save()', function () {
|
||||
test('inserts a new document', function () {
|
||||
Document::save(ThrowawayDb::TABLE, new TestDocument('test', sub: new SubDocument('a', 'b')));
|
||||
expect(Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class))->isSome->toBeTrue();
|
||||
});
|
||||
test('updates an existing document', function () {
|
||||
Document::save(ThrowawayDb::TABLE, new TestDocument('two', num_value: 44));
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
|
||||
expect($tryDoc)->isSome->toBeTrue()
|
||||
->and($tryDoc->value)
|
||||
->num_value->toBe(44)
|
||||
->sub->toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::update()', function () {
|
||||
test('replaces an existing document', function () {
|
||||
Document::update(ThrowawayDb::TABLE, 'one', new TestDocument('one', 'howdy', 8, new SubDocument('y', 'z')));
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
|
||||
expect($tryDoc)->isSome->toBeTrue()
|
||||
->and($tryDoc->value)
|
||||
->num_value->toBe(8)
|
||||
->sub->not->toBeNull()
|
||||
->sub->foo->toBe('y')
|
||||
->sub->bar->toBe('z')
|
||||
->and($tryDoc->value->value)->toBe('howdy');
|
||||
});
|
||||
test('does nothing for a non-existent document', function () {
|
||||
expect(Exists::byId(ThrowawayDb::TABLE, 'two-hundred'))->toBeFalse();
|
||||
Document::update(ThrowawayDb::TABLE, 'two-hundred', new TestDocument('200'));
|
||||
expect(Find::byId(ThrowawayDb::TABLE, 'two-hundred', TestDocument::class))->isNone->toBeTrue();
|
||||
});
|
||||
});
|
||||
42
tests/Integration/SQLite/ExistsTest.php
Normal file
42
tests/Integration/SQLite/ExistsTest.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{DocumentException, Exists, Field};
|
||||
use Test\Integration\SQLite\ThrowawayDb;
|
||||
|
||||
pest()->group('integration', 'sqlite');
|
||||
|
||||
describe('::byId()', function () {
|
||||
test('returns true when a document exists', function () {
|
||||
expect(Exists::byId(ThrowawayDb::TABLE, 'three'))->toBeTrue();
|
||||
});
|
||||
test('returns false when no document exists', function () {
|
||||
expect(Exists::byId(ThrowawayDb::TABLE, 'seven'))->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byFields()', function () {
|
||||
test('returns true when matching documents exist', function () {
|
||||
expect(Exists::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 10)]))->toBeTrue();
|
||||
});
|
||||
test('returns false when no matching documents exist', function () {
|
||||
expect(Exists::byFields(ThrowawayDb::TABLE, [Field::less('nothing', 'none')]))->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byContains()', function () {
|
||||
test('throws an exception', function () {
|
||||
expect(fn () => Exists::byContains('', []))->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byJsonPath()', function () {
|
||||
test('throws an exception', function () {
|
||||
expect(fn () => Exists::byJsonPath('', ''))->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
||||
151
tests/Integration/SQLite/FindTest.php
Normal file
151
tests/Integration/SQLite/FindTest.php
Normal file
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{Custom, Delete, Document, DocumentException, Field, FieldMatch, Find};
|
||||
use Test\Integration\{ArrayDocument, TestDocument};
|
||||
use Test\Integration\SQLite\ThrowawayDb;
|
||||
|
||||
pest()->group('integration', 'sqlite');
|
||||
|
||||
describe('::all()', function () {
|
||||
test('retrieves data', function () {
|
||||
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class);
|
||||
expect($docs)->not->toBeNull();
|
||||
$count = 0;
|
||||
foreach ($docs->items as $ignored) $count++;
|
||||
expect($count)->toBe(5);
|
||||
});
|
||||
test('sorts data ascending', function () {
|
||||
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id')]);
|
||||
expect($docs)->not->toBeNull()
|
||||
->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))
|
||||
->toBe(['five', 'four', 'one', 'three', 'two']);
|
||||
});
|
||||
test('sorts data descending', function () {
|
||||
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id DESC')]);
|
||||
expect($docs)->not->toBeNull()
|
||||
->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))
|
||||
->toBe(['two', 'three', 'one', 'four', 'five']);
|
||||
});
|
||||
test('sorts data numerically', function () {
|
||||
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class,
|
||||
[Field::named('sub.foo NULLS LAST'), Field::named('n:num_value')]);
|
||||
expect($docs)->not->toBeNull()
|
||||
->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))
|
||||
->toBe(['two', 'four', 'one', 'three', 'five']);
|
||||
});
|
||||
test('returns an empty list when no data exists', function () {
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
expect(Find::all(ThrowawayDb::TABLE, TestDocument::class))
|
||||
->not->toBeNull()->hasItems->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byId()', function () {
|
||||
test('returns a document when it exists', function () {
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('two');
|
||||
});
|
||||
test('returns a document with a numeric ID', function () {
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 18, 'value' => 'howdy']);
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 18, TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('18');
|
||||
});
|
||||
test('returns None when no document exists', function () {
|
||||
expect(Find::byId(ThrowawayDb::TABLE, 'seventy-five', TestDocument::class))->isNone->toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byFields()', function () {
|
||||
test('returns matching documents', function () {
|
||||
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('value', ['blue', 'purple']), Field::exists('sub')],
|
||||
TestDocument::class, FieldMatch::All);
|
||||
expect($docs)->not->toBeNull();
|
||||
$count = 0;
|
||||
foreach ($docs->items as $ignored) $count++;
|
||||
expect($count)->toBe(1);
|
||||
});
|
||||
test('returns ordered matching documents', function () {
|
||||
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], TestDocument::class,
|
||||
FieldMatch::All, [Field::named('id')]);
|
||||
expect($docs)->not->toBeNull()
|
||||
->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))->toBe(['five', 'four']);
|
||||
});
|
||||
test('returns documents matching numeric IN clause', function () {
|
||||
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('num_value', [2, 4, 6, 8])], TestDocument::class);
|
||||
expect($docs)->not->toBeNull();
|
||||
$count = 0;
|
||||
foreach ($docs->items as $ignored) $count++;
|
||||
expect($count)->toBe(1);
|
||||
});
|
||||
test('returns empty list when no documents match', function () {
|
||||
expect(Find::byFields(ThrowawayDb::TABLE, [Field::greater('num_value', 100)], TestDocument::class))
|
||||
->not->toBeNull()->hasItems->toBeFalse();
|
||||
});
|
||||
test('returns matching documents for inArray comparison', function () {
|
||||
Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absentField')]);
|
||||
foreach (ArrayDocument::testDocuments() as $doc) Document::insert(ThrowawayDb::TABLE, $doc);
|
||||
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::inArray('values', ThrowawayDb::TABLE, ['c'])],
|
||||
ArrayDocument::class);
|
||||
expect($docs)->not->toBeNull();
|
||||
$count = 0;
|
||||
foreach ($docs->items as $ignored) $count++;
|
||||
expect($count)->toBe(2);
|
||||
});
|
||||
test('returns empty list when no documents match inArray comparison', function () {
|
||||
Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absentField')]);
|
||||
foreach (ArrayDocument::testDocuments() as $doc) Document::insert(ThrowawayDb::TABLE, $doc);
|
||||
expect(Find::byFields(ThrowawayDb::TABLE, [Field::inArray('values', ThrowawayDb::TABLE, ['j'])],
|
||||
ArrayDocument::class))
|
||||
->not->toBeNull()->hasItems->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byContains()', function () {
|
||||
test('throws an exception', function () {
|
||||
expect(fn () => Find::byContains('', [], TestDocument::class))->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byJsonPath()', function () {
|
||||
test('throws an exception', function () {
|
||||
expect(fn () => Find::byJsonPath('', '', TestDocument::class))->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::firstByFields()', function () {
|
||||
test('returns a matching document', function () {
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'another')], TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('two');
|
||||
});
|
||||
test('returns one of several matching documents', function () {
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and(['two', 'four'])->toContain($doc->value->id);
|
||||
});
|
||||
test('returns first of ordered matching documents', function () {
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class,
|
||||
orderBy: [Field::named('n:num_value DESC')]);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->id->toBe('four');
|
||||
});
|
||||
test('returns None when no documents match', function () {
|
||||
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'absent')], TestDocument::class))
|
||||
->isNone->toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
describe('::firstByContains()', function () {
|
||||
test('throws an exception', function () {
|
||||
expect(fn () => Find::firstByContains('', [], TestDocument::class))->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::firstByJsonPath()', function () {
|
||||
test('throws an exception', function () {
|
||||
expect(fn () => Find::firstByJsonPath('', '', TestDocument::class))->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
||||
48
tests/Integration/SQLite/PatchTest.php
Normal file
48
tests/Integration/SQLite/PatchTest.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{Count, DocumentException, Exists, Field, Find, Patch};
|
||||
use Test\Integration\SQLite\ThrowawayDb;
|
||||
use Test\Integration\TestDocument;
|
||||
|
||||
pest()->group('integration', 'sqlite');
|
||||
|
||||
describe('::byId()', function () {
|
||||
test('updates an existing document', function () {
|
||||
Patch::byId(ThrowawayDb::TABLE, 'one', ['num_value' => 44]);
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->num_value->toBe(44);
|
||||
});
|
||||
test('does nothing when no document exists', function () {
|
||||
expect(Exists::byId(ThrowawayDb::TABLE, 'forty-seven'))->toBeFalse();
|
||||
Patch::byId(ThrowawayDb::TABLE, 'forty-seven', ['foo' => 'green']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byFields()', function () {
|
||||
test('updates matching documents', function () {
|
||||
Patch::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], ['num_value' => 77]);
|
||||
expect(Count::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 77)]))->toBe(2);
|
||||
});
|
||||
test('does nothing when no documents match', function () {
|
||||
expect(Exists::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'burgundy')]))->toBeFalse();
|
||||
Patch::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'burgundy')], ['foo' => 'green']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byContains()', function () {
|
||||
test('throws an exception', function () {
|
||||
expect(fn () => Patch::byContains('', [], []))->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byJsonPath()', function () {
|
||||
test('throws an exception', function () {
|
||||
expect(fn () => Patch::byJsonPath('', '', []))->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
||||
59
tests/Integration/SQLite/RemoveFieldsTest.php
Normal file
59
tests/Integration/SQLite/RemoveFieldsTest.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{DocumentException, Exists, Field, Find, RemoveFields};
|
||||
use Test\Integration\SQLite\ThrowawayDb;
|
||||
use Test\Integration\TestDocument;
|
||||
|
||||
pest()->group('integration', 'sqlite');
|
||||
|
||||
describe('::byId()', function () {
|
||||
test('updates an existing document', function () {
|
||||
RemoveFields::byId(ThrowawayDb::TABLE, 'two', ['sub', 'value']);
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
|
||||
expect($tryDoc)->isSome->toBeTrue()
|
||||
->and($tryDoc->value)->sub->toBeNull()
|
||||
->and($tryDoc->value->value)->toBeEmpty();
|
||||
});
|
||||
test('does nothing when the field to remove does not exist', function () {
|
||||
expect(Exists::byFields(ThrowawayDb::TABLE, [Field::exists('a_field_that_does_not_exist')]))->toBeFalse();
|
||||
RemoveFields::byId(ThrowawayDb::TABLE, 'one', ['a_field_that_does_not_exist']);
|
||||
});
|
||||
test('does nothing when the document does not exist', function () {
|
||||
expect(Exists::byId(ThrowawayDb::TABLE, 'fifty'))->toBeFalse();
|
||||
RemoveFields::byId(ThrowawayDb::TABLE, 'fifty', ['sub']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byFields()', function () {
|
||||
test('updates matching documents', function () {
|
||||
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], ['sub']);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], TestDocument::class);
|
||||
expect($doc)->isSome->toBeTrue()->and($doc->value)->sub->toBeNull();
|
||||
});
|
||||
test('does nothing when the field to remove does not exist', function () {
|
||||
expect(Exists::byFields(ThrowawayDb::TABLE, [Field::exists('nada')]))->toBeFalse();
|
||||
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], ['nada']);
|
||||
});
|
||||
test('does nothing when no documents match', function () {
|
||||
expect(Exists::byFields(ThrowawayDb::TABLE, [Field::notEqual('missing', 'nope')]))->toBeFalse();
|
||||
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::notEqual('missing', 'nope')], ['value']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byContains()', function () {
|
||||
test('throws an exception', function () {
|
||||
expect(fn () => RemoveFields::byContains('', [], []))->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
||||
|
||||
describe('::byJsonPath()', function () {
|
||||
test('throws an exception', function () {
|
||||
expect(fn () => RemoveFields::byJsonPath('', '', []))->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
||||
70
tests/Integration/SQLite/ThrowawayDb.php
Normal file
70
tests/Integration/SQLite/ThrowawayDb.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
* @see https://github.com/Zaid-Ajaj/ThrowawayDb The origin concept
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Test\Integration\SQLite;
|
||||
|
||||
use BitBadger\PDODocument\{AutoId, Configuration, Definition, Document, DocumentException};
|
||||
use Random\RandomException;
|
||||
use Test\Integration\{SubDocument, TestDocument};
|
||||
|
||||
/**
|
||||
* Utilities to create and destroy a throwaway SQLite database to use for testing
|
||||
*/
|
||||
class ThrowawayDb
|
||||
{
|
||||
/** @var string The table used for document manipulation */
|
||||
public const TABLE = 'test_table';
|
||||
|
||||
/**
|
||||
* Load data into the test table
|
||||
*
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function loadData(): void
|
||||
{
|
||||
Document::insert(self::TABLE, new TestDocument('one', 'FIRST!', 0));
|
||||
Document::insert(self::TABLE, new TestDocument('two', 'another', 10, new SubDocument('green', 'blue')));
|
||||
Document::insert(self::TABLE, new TestDocument('three', '', 4));
|
||||
Document::insert(self::TABLE, new TestDocument('four', 'purple', 17, new SubDocument('green', 'red')));
|
||||
Document::insert(self::TABLE, new TestDocument('five', 'purple', 18));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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|RandomException If any is encountered
|
||||
*/
|
||||
public static function create(bool $withData = true): string
|
||||
{
|
||||
$fileName = sprintf('throwaway-%s.db', AutoId::generateRandom(10));
|
||||
Configuration::useDSN("sqlite:./$fileName");
|
||||
Configuration::resetPDO();
|
||||
|
||||
Definition::ensureTable(self::TABLE);
|
||||
|
||||
if ($withData) {
|
||||
self::loadData();
|
||||
}
|
||||
|
||||
return $fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy a throwaway SQLite database
|
||||
*
|
||||
* @param string $fileName The name of the SQLite database to be deleted
|
||||
*/
|
||||
public static function destroy(string $fileName): void
|
||||
{
|
||||
Configuration::resetPDO();
|
||||
if (file_exists("./$fileName")) unlink("./$fileName");
|
||||
}
|
||||
}
|
||||
60
tests/Integration/SQLiteIntegrationTest.php
Normal file
60
tests/Integration/SQLiteIntegrationTest.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
* @see https://github.com/Zaid-Ajaj/ThrowawayDb The origin concept
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Test\Integration;
|
||||
|
||||
use BitBadger\PDODocument\{Configuration, Custom, Delete, DocumentException, Field};
|
||||
use BitBadger\PDODocument\Mapper\ExistsMapper;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Test\Integration\SQLite\ThrowawayDb;
|
||||
|
||||
/**
|
||||
* Integration Test Class wrapper for SQLite integration tests
|
||||
*/
|
||||
class SQLiteIntegrationTest extends TestCase
|
||||
{
|
||||
/** @var string Database name for throwaway database */
|
||||
static private string $dbName = '';
|
||||
|
||||
public static function setUpBeforeClass(): void
|
||||
{
|
||||
self::$dbName = ThrowawayDb::create(false);
|
||||
}
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
ThrowawayDb::loadData();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Delete::byFields(ThrowawayDb::TABLE, [ Field::exists(Configuration::$idField)]);
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public static function tearDownAfterClass(): void
|
||||
{
|
||||
ThrowawayDb::destroy(self::$dbName);
|
||||
self::$dbName = '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
protected function dbObjectExists(string $name): bool
|
||||
{
|
||||
return Custom::scalar('SELECT EXISTS (SELECT 1 FROM sqlite_master WHERE name = :name)',
|
||||
[':name' => $name], new ExistsMapper());
|
||||
}
|
||||
}
|
||||
17
tests/Integration/SubDocument.php
Normal file
17
tests/Integration/SubDocument.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Test\Integration;
|
||||
|
||||
/**
|
||||
* A sub-document for testing
|
||||
*/
|
||||
class SubDocument
|
||||
{
|
||||
public function __construct(public string $foo = '', public string $bar = '') { }
|
||||
}
|
||||
15
tests/Integration/TestDocument.php
Normal file
15
tests/Integration/TestDocument.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Test\Integration;
|
||||
|
||||
class TestDocument
|
||||
{
|
||||
public function __construct(public string $id = '', public string $value = '', public int $num_value = 0,
|
||||
public ?SubDocument $sub = null) { }
|
||||
}
|
||||
Reference in New Issue
Block a user