* @license MIT */ declare(strict_types=1); use BitBadger\PDODocument\{AutoId, Configuration, Count, Custom, Delete, Document, DocumentException, DocumentList, Exists, Field, Find, Query}; use BitBadger\PDODocument\Mapper\{ArrayMapper}; use Test\Integration\NumDocument; use Test\Integration\SQLite\ThrowawayDb; use Test\Integration\SubDocument; use Test\Integration\TestDocument; 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(); }); });