Migrate tests to Pest (#8)

Reviewed-on: #8
This commit was merged in pull request #8.
This commit is contained in:
2024-11-17 21:30:53 +00:00
parent df436c9ef4
commit 529e823955
93 changed files with 5725 additions and 5186 deletions

View 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);
});
});

View 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 () {
expect(Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id", [':id' => 'one'],
new DocumentMapper(TestDocument::class)))
->isSome()->toBeTrue()->get()->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);
});
});

View 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);
});
});

View 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);
});
});

View 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']);
});
});

View 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->get())
->id->toBe('turkey')
->num_value->toBe(0)
->sub->not->toBeNull()
->sub->foo->toBe('gobble')
->sub->bar->toBe('gobble')
->and($tryDoc->get()->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->get()['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->get()['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->get()['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]);
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 5)], TestDocument::class))
->isSome()->toBeTrue()->get()->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]);
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 12)], TestDocument::class))
->isSome()->toBeTrue()->get()->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]);
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 8)], TestDocument::class))
->isSome()->toBeTrue()->get()->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]);
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class))
->isSome()->toBeTrue()->get()->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->get())
->id->toBe('turkey')
->num_value->toBe(0)
->sub->not->toBeNull()
->sub->foo->toBe('gobble')
->sub->bar->toBe('gobble')
->and($tryDoc->get()->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'));
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'taco')], NumDocument::class))
->isSome()->toBeTrue()->get()->id->toBe(1);
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'burrito'));
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'burrito')], NumDocument::class))
->isSome()->toBeTrue()->get()->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'));
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'large')], NumDocument::class))
->isSome()->toBeTrue()->get()->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));
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::exists('value')], TestDocument::class))
->isSome()->toBeTrue()->get()->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));
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 14)], TestDocument::class))
->isSome()->toBeTrue()->get()->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));
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 55)], TestDocument::class))
->isSome()->toBeTrue()->get()->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));
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class))
->isSome()->toBeTrue()->get()->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->get())
->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->get())
->num_value->toBe(8)
->sub->not->toBeNull()
->sub->foo->toBe('y')
->sub->bar->toBe('z')
->and($tryDoc->get()->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();
});
});

View 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);
});
});

View 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 () {
expect(Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class))
->isSome()->toBeTrue()->get()->id->toBe('two');
});
test('returns a document with a numeric ID', function () {
Document::insert(ThrowawayDb::TABLE, ['id' => 18, 'value' => 'howdy']);
expect(Find::byId(ThrowawayDb::TABLE, 18, TestDocument::class))
->isSome()->toBeTrue()->get()->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 () {
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'another')], TestDocument::class))
->isSome()->toBeTrue()->get()->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->get()->id);
});
test('returns first of ordered matching documents', function () {
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class,
orderBy: [Field::named('n:num_value DESC')]))
->isSome()->toBeTrue()->get()->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);
});
});

View 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]);
expect(Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class))
->isSome()->toBeTrue()->get()->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);
});
});

View 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->get())->sub->toBeNull()
->and($tryDoc->get()->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']);
expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], TestDocument::class))
->isSome()->toBeTrue()->get()->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);
});
});

View 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");
}
}