250 lines
11 KiB
PHP
250 lines
11 KiB
PHP
|
<?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();
|
||
|
});
|
||
|
});
|