Use Option for single doc queries
This commit is contained in:
@@ -87,15 +87,15 @@ class CustomTest extends TestCase
|
||||
{
|
||||
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id", [':id' => 'one'],
|
||||
new DocumentMapper(TestDocument::class));
|
||||
$this->assertNotNull($doc, 'There should have been a document returned');
|
||||
$this->assertEquals('one', $doc->id, 'The incorrect document was returned');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals('one', $doc->get()->id, 'The incorrect document was returned');
|
||||
}
|
||||
|
||||
public function testSingleSucceedsWhenARowIsNotFound(): void
|
||||
{
|
||||
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id",
|
||||
[':id' => 'eighty'], new DocumentMapper(TestDocument::class));
|
||||
$this->assertFalse($doc, 'There should not have been a document returned');
|
||||
$this->assertTrue($doc->isEmpty(), 'There should not have been a document returned');
|
||||
}
|
||||
|
||||
public function testNonQuerySucceedsWhenOperatingOnData()
|
||||
|
||||
@@ -33,8 +33,9 @@ class DocumentTest extends TestCase
|
||||
public function testInsertSucceedsForArrayNoAutoId(): void
|
||||
{
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 'turkey', 'sub' => ['foo' => 'gobble', 'bar' => 'gobble']]);
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document inserted');
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
|
||||
$this->assertTrue($tryDoc->isDefined(), 'There should have been a document inserted');
|
||||
$doc = $tryDoc->get();
|
||||
$this->assertEquals('turkey', $doc->id, 'The ID was incorrect');
|
||||
$this->assertEquals('', $doc->value, 'The value was incorrect');
|
||||
$this->assertEquals(0, $doc->num_value, 'The numeric value was incorrect');
|
||||
@@ -52,15 +53,15 @@ class DocumentTest extends TestCase
|
||||
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'new', 'num_value' => 8]);
|
||||
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper());
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$obj = json_decode($doc['data']);
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$obj = json_decode($doc->get()['data']);
|
||||
$this->assertEquals(1, $obj->id, 'The ID 1 should have been auto-generated');
|
||||
|
||||
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());
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$obj = json_decode($doc['data']);
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$obj = json_decode($doc->get()['data']);
|
||||
$this->assertEquals(2, $obj->id, 'The ID 2 should have been auto-generated');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
@@ -75,8 +76,8 @@ class DocumentTest extends TestCase
|
||||
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());
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$obj = json_decode($doc['data']);
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$obj = json_decode($doc->get()['data']);
|
||||
$this->assertEquals(7, $obj->id, 'The ID 7 should have been stored');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
@@ -91,8 +92,8 @@ class DocumentTest extends TestCase
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => '', 'num_value' => 5]);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 5)], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertNotEmpty($doc->id, 'The ID should have been auto-generated');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertNotEmpty($doc->get()->id, 'The ID should have been auto-generated');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
@@ -107,8 +108,8 @@ class DocumentTest extends TestCase
|
||||
$uuid = AutoId::generateUUID();
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => $uuid, 'value' => 'uuid', 'num_value' => 12]);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 12)], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals($uuid, $doc->id, 'The ID should not have been changed');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals($uuid, $doc->get()->id, 'The ID should not have been changed');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
@@ -123,8 +124,9 @@ class DocumentTest extends TestCase
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => '', 'value' => 'new', 'num_value' => 8]);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 8)], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals(6, strlen($doc->id), 'The ID should have been auto-generated and had 6 characters');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals(6, strlen($doc->get()->id),
|
||||
'The ID should have been auto-generated and had 6 characters');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
Configuration::$idStringLength = 16;
|
||||
@@ -139,8 +141,8 @@ class DocumentTest extends TestCase
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 'my-key', 'value' => 'old', 'num_value' => 3]);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 3)], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals('my-key', $doc->id, 'The ID should not have been changed');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals('my-key', $doc->get()->id, 'The ID should not have been changed');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
@@ -150,8 +152,9 @@ class DocumentTest extends TestCase
|
||||
public function testInsertSucceedsForObjectNoAutoId(): void
|
||||
{
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument('turkey', sub: new SubDocument('gobble', 'gobble')));
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document inserted');
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
|
||||
$this->assertTrue($tryDoc->isDefined(), 'There should have been a document inserted');
|
||||
$doc = $tryDoc->get();
|
||||
$this->assertEquals('turkey', $doc->id, 'The ID was incorrect');
|
||||
$this->assertEquals('', $doc->value, 'The value was incorrect');
|
||||
$this->assertEquals(0, $doc->num_value, 'The numeric value was incorrect');
|
||||
@@ -169,13 +172,13 @@ class DocumentTest extends TestCase
|
||||
|
||||
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'taco'));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'taco')], NumDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals(1, $doc->id, 'The ID 1 should have been auto-generated');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals(1, $doc->get()->id, 'The ID 1 should have been auto-generated');
|
||||
|
||||
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'burrito'));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'burrito')], NumDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals(2, $doc->id, 'The ID 2 should have been auto-generated');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals(2, $doc->get()->id, 'The ID 2 should have been auto-generated');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
@@ -189,8 +192,8 @@ class DocumentTest extends TestCase
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, new NumDocument(64, 'large'));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'large')], NumDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals(64, $doc->id, 'The ID 64 should have been stored');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals(64, $doc->get()->id, 'The ID 64 should have been stored');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
@@ -204,8 +207,8 @@ class DocumentTest extends TestCase
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument(value: 'something', num_value: 9));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EX('value')], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertNotEmpty($doc->id, 'The ID should have been auto-generated');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertNotEmpty($doc->get()->id, 'The ID should have been auto-generated');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
@@ -220,8 +223,8 @@ class DocumentTest extends TestCase
|
||||
$uuid = AutoId::generateUUID();
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument($uuid, num_value: 14));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 14)], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals($uuid, $doc->id, 'The ID should not have been changed');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals($uuid, $doc->get()->id, 'The ID should not have been changed');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
@@ -236,8 +239,9 @@ class DocumentTest extends TestCase
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument(num_value: 55));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 55)], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals(40, strlen($doc->id), 'The ID should have been auto-generated and had 40 characters');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals(40, strlen($doc->get()->id),
|
||||
'The ID should have been auto-generated and had 40 characters');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
Configuration::$idStringLength = 16;
|
||||
@@ -252,8 +256,8 @@ class DocumentTest extends TestCase
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument('my-key', num_value: 3));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 3)], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals('my-key', $doc->id, 'The ID should not have been changed');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals('my-key', $doc->get()->id, 'The ID should not have been changed');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
@@ -269,14 +273,15 @@ class DocumentTest extends TestCase
|
||||
{
|
||||
Document::save(ThrowawayDb::TABLE, new TestDocument('test', sub: new SubDocument('a', 'b')));
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
}
|
||||
|
||||
public function testSaveSucceedsWhenADocumentIsUpdated(): void
|
||||
{
|
||||
Document::save(ThrowawayDb::TABLE, new TestDocument('two', num_value: 44));
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
|
||||
$this->assertTrue($tryDoc->isDefined(), 'There should have been a document returned');
|
||||
$doc = $tryDoc->get();
|
||||
$this->assertEquals(44, $doc->num_value, 'The numeric value was not updated');
|
||||
$this->assertNull($doc->sub, 'The sub-document should have been null');
|
||||
}
|
||||
@@ -284,8 +289,9 @@ class DocumentTest extends TestCase
|
||||
public function testUpdateSucceedsWhenReplacingADocument(): void
|
||||
{
|
||||
Document::update(ThrowawayDb::TABLE, 'one', new TestDocument('one', 'howdy', 8, new SubDocument('y', 'z')));
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
|
||||
$this->assertNotFalse($tryDoc->isDefined(), 'There should have been a document returned');
|
||||
$doc = $tryDoc->get();
|
||||
$this->assertEquals('howdy', $doc->value, 'The value was incorrect');
|
||||
$this->assertEquals(8, $doc->num_value, 'The numeric value was incorrect');
|
||||
$this->assertNotNull($doc->sub, 'The sub-document should not have been null');
|
||||
@@ -297,6 +303,6 @@ class DocumentTest extends TestCase
|
||||
{
|
||||
Document::update(ThrowawayDb::TABLE, 'two-hundred', new TestDocument('200'));
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'two-hundred', TestDocument::class);
|
||||
$this->assertFalse($doc, 'There should not have been a document returned');
|
||||
$this->assertTrue($doc->isEmpty(), 'There should not have been a document returned');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,8 +49,8 @@ class FindTest extends TestCase
|
||||
public function testByIdSucceedsWhenADocumentIsFound(): void
|
||||
{
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals('two', $doc->id, 'An incorrect document was returned');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals('two', $doc->get()->id, 'An incorrect document was returned');
|
||||
}
|
||||
|
||||
#[TestDox('By ID succeeds when a document is found with numeric ID')]
|
||||
@@ -59,15 +59,15 @@ class FindTest extends TestCase
|
||||
Delete::byFields(ThrowawayDb::TABLE, [Field::NEX('absent')]);
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 18, 'value' => 'howdy']);
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 18, NumDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals(18, $doc->id, 'An incorrect document was returned');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals(18, $doc->get()->id, 'An incorrect document was returned');
|
||||
}
|
||||
|
||||
#[TestDox('By ID succeeds when a document is not found')]
|
||||
public function testByIdSucceedsWhenADocumentIsNotFound(): void
|
||||
{
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'seventy-five', TestDocument::class);
|
||||
$this->assertFalse($doc, 'There should not have been a document returned');
|
||||
$this->assertTrue($doc->isEmpty(), 'There should not have been a document returned');
|
||||
}
|
||||
|
||||
public function testByFieldsSucceedsWhenDocumentsAreFound(): void
|
||||
@@ -123,62 +123,63 @@ class FindTest extends TestCase
|
||||
public function testFirstByFieldsSucceedsWhenADocumentIsFound(): void
|
||||
{
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'another')], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals('two', $doc->id, 'The incorrect document was returned');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals('two', $doc->get()->id, 'The incorrect document was returned');
|
||||
}
|
||||
|
||||
public function testFirstByFieldsSucceedsWhenMultipleDocumentsAreFound(): void
|
||||
{
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('sub.foo', 'green')], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertContains($doc->id, ['two', 'four'], 'An incorrect document was returned');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertContains($doc->get()->id, ['two', 'four'], 'An incorrect document was returned');
|
||||
}
|
||||
|
||||
public function testFirstByFieldsSucceedsWhenADocumentIsNotFound(): void
|
||||
{
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'absent')], TestDocument::class);
|
||||
$this->assertFalse($doc, 'There should not have been a document returned');
|
||||
$this->assertTrue($doc->isEmpty(), 'There should not have been a document returned');
|
||||
}
|
||||
|
||||
public function testFirstByContainsSucceedsWhenADocumentIsFound(): void
|
||||
{
|
||||
$doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'FIRST!'], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals('one', $doc->id, 'The incorrect document was returned');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals('one', $doc->get()->id, 'The incorrect document was returned');
|
||||
}
|
||||
|
||||
public function testFirstByContainsSucceedsWhenMultipleDocumentsAreFound(): void
|
||||
{
|
||||
$doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertContains($doc->id, ['four', 'five'], 'An incorrect document was returned');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertContains($doc->get()->id, ['four', 'five'], 'An incorrect document was returned');
|
||||
}
|
||||
|
||||
public function testFirstByContainsSucceedsWhenADocumentIsNotFound(): void
|
||||
{
|
||||
$doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'indigo'], TestDocument::class);
|
||||
$this->assertFalse($doc, 'There should not have been a document returned');
|
||||
$this->assertTrue($doc->isEmpty(), 'There should not have been a document returned');
|
||||
}
|
||||
|
||||
#[TestDox('First by JSON Path succeeds when a document is found')]
|
||||
public function testFirstByJsonPathSucceedsWhenADocumentIsFound(): void
|
||||
{
|
||||
$doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ == 10)', TestDocument::class);
|
||||
$this->assertEquals('two', $doc->id, 'The incorrect document was returned');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals('two', $doc->get()->id, 'The incorrect document was returned');
|
||||
}
|
||||
|
||||
#[TestDox('First by JSON Path succeeds when multiple documents are found')]
|
||||
public function testFirstByJsonPathSucceedsWhenMultipleDocumentsAreFound(): void
|
||||
{
|
||||
$doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertContains($doc->id, ['four', 'five'], 'An incorrect document was returned');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertContains($doc->get()->id, ['four', 'five'], 'An incorrect document was returned');
|
||||
}
|
||||
|
||||
#[TestDox('First by JSON Path succeeds when a document is not found')]
|
||||
public function testFirstByJsonPathSucceedsWhenADocumentIsNotFound(): void
|
||||
{
|
||||
$doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)', TestDocument::class);
|
||||
$this->assertFalse($doc, 'There should not have been a document returned');
|
||||
$this->assertTrue($doc->isEmpty(), 'There should not have been a document returned');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,8 +33,8 @@ class PatchTest extends TestCase
|
||||
{
|
||||
Patch::byId(ThrowawayDb::TABLE, 'one', ['num_value' => 44]);
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals(44, $doc->num_value, 'The updated document is not correct');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals(44, $doc->get()->num_value, 'The updated document is not correct');
|
||||
}
|
||||
|
||||
#[TestDox('By ID succeeds when no document is updated')]
|
||||
@@ -64,8 +64,9 @@ class PatchTest extends TestCase
|
||||
public function testByContainsSucceedsWhenDocumentsAreUpdated(): void
|
||||
{
|
||||
Patch::byContains(ThrowawayDb::TABLE, ['value' => 'another'], ['num_value' => 12]);
|
||||
$doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'another'], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$tryDoc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'another'], TestDocument::class);
|
||||
$this->assertNotFalse($tryDoc->isDefined(), 'There should have been a document returned');
|
||||
$doc = $tryDoc->get();
|
||||
$this->assertEquals('two', $doc->id, 'An incorrect document was returned');
|
||||
$this->assertEquals(12, $doc->num_value, 'The document was not patched');
|
||||
}
|
||||
|
||||
@@ -32,8 +32,9 @@ class RemoveFieldsTest extends TestCase
|
||||
public function testByIdSucceedsWhenFieldsAreRemoved(): void
|
||||
{
|
||||
RemoveFields::byId(ThrowawayDb::TABLE, 'two', ['sub', 'value']);
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
|
||||
$this->assertNotFalse($tryDoc->isDefined(), 'There should have been a document returned');
|
||||
$doc = $tryDoc->get();
|
||||
$this->assertEquals('', $doc->value, 'Value should have been blank (its default value)');
|
||||
$this->assertNull($doc->sub, 'Sub-document should have been null');
|
||||
}
|
||||
@@ -56,8 +57,8 @@ class RemoveFieldsTest extends TestCase
|
||||
{
|
||||
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 17)], ['sub']);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 17)], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertNull($doc->sub, 'Sub-document should have been null');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertNull($doc->get()->sub, 'Sub-document should have been null');
|
||||
}
|
||||
|
||||
public function testByFieldsSucceedsWhenAFieldIsNotRemoved(): void
|
||||
|
||||
@@ -86,15 +86,15 @@ class CustomTest extends TestCase
|
||||
{
|
||||
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id", [':id' => 'one'],
|
||||
new DocumentMapper(TestDocument::class));
|
||||
$this->assertNotNull($doc, 'There should have been a document returned');
|
||||
$this->assertEquals('one', $doc->id, 'The incorrect document was returned');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals('one', $doc->get()->id, 'The incorrect document was returned');
|
||||
}
|
||||
|
||||
public function testSingleSucceedsWhenARowIsNotFound(): void
|
||||
{
|
||||
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id",
|
||||
[':id' => 'eighty'], new DocumentMapper(TestDocument::class));
|
||||
$this->assertFalse($doc, 'There should not have been a document returned');
|
||||
$this->assertTrue($doc->isEmpty(), 'There should not have been a document returned');
|
||||
}
|
||||
|
||||
public function testNonQuerySucceedsWhenOperatingOnData()
|
||||
|
||||
@@ -33,8 +33,9 @@ class DocumentTest extends TestCase
|
||||
public function testInsertSucceedsForArrayNoAutoId(): void
|
||||
{
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 'turkey', 'sub' => ['foo' => 'gobble', 'bar' => 'gobble']]);
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document inserted');
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
|
||||
$this->assertTrue($tryDoc->isDefined(), 'There should have been a document inserted');
|
||||
$doc = $tryDoc->get();
|
||||
$this->assertEquals('turkey', $doc->id, 'The ID was incorrect');
|
||||
$this->assertEquals('', $doc->value, 'The value was incorrect');
|
||||
$this->assertEquals(0, $doc->num_value, 'The numeric value was incorrect');
|
||||
@@ -52,15 +53,15 @@ class DocumentTest extends TestCase
|
||||
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'new', 'num_value' => 8]);
|
||||
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper());
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$obj = json_decode($doc['data']);
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$obj = json_decode($doc->get()['data']);
|
||||
$this->assertEquals(1, $obj->id, 'The ID 1 should have been auto-generated');
|
||||
|
||||
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());
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$obj = json_decode($doc['data']);
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$obj = json_decode($doc->get()['data']);
|
||||
$this->assertEquals(2, $obj->id, 'The ID 2 should have been auto-generated');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
@@ -75,8 +76,8 @@ class DocumentTest extends TestCase
|
||||
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());
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$obj = json_decode($doc['data']);
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$obj = json_decode($doc->get()['data']);
|
||||
$this->assertEquals(7, $obj->id, 'The ID 7 should have been stored');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
@@ -91,8 +92,8 @@ class DocumentTest extends TestCase
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => '', 'num_value' => 5]);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 5)], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertNotEmpty($doc->id, 'The ID should have been auto-generated');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertNotEmpty($doc->get()->id, 'The ID should have been auto-generated');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
@@ -107,8 +108,8 @@ class DocumentTest extends TestCase
|
||||
$uuid = AutoId::generateUUID();
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => $uuid, 'value' => 'uuid', 'num_value' => 12]);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 12)], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals($uuid, $doc->id, 'The ID should not have been changed');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals($uuid, $doc->get()->id, 'The ID should not have been changed');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
@@ -123,8 +124,9 @@ class DocumentTest extends TestCase
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => '', 'value' => 'new', 'num_value' => 8]);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 8)], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals(6, strlen($doc->id), 'The ID should have been auto-generated and had 6 characters');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals(6, strlen($doc->get()->id),
|
||||
'The ID should have been auto-generated and had 6 characters');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
Configuration::$idStringLength = 16;
|
||||
@@ -139,8 +141,8 @@ class DocumentTest extends TestCase
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 'my-key', 'value' => 'old', 'num_value' => 3]);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 3)], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals('my-key', $doc->id, 'The ID should not have been changed');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals('my-key', $doc->get()->id, 'The ID should not have been changed');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
@@ -150,8 +152,9 @@ class DocumentTest extends TestCase
|
||||
public function testInsertSucceedsForObjectNoAutoId(): void
|
||||
{
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument('turkey', sub: new SubDocument('gobble', 'gobble')));
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document inserted');
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
|
||||
$this->assertNotFalse($tryDoc->isDefined(), 'There should have been a document inserted');
|
||||
$doc = $tryDoc->get();
|
||||
$this->assertEquals('turkey', $doc->id, 'The ID was incorrect');
|
||||
$this->assertEquals('', $doc->value, 'The value was incorrect');
|
||||
$this->assertEquals(0, $doc->num_value, 'The numeric value was incorrect');
|
||||
@@ -169,13 +172,13 @@ class DocumentTest extends TestCase
|
||||
|
||||
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'taco'));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'taco')], NumDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals(1, $doc->id, 'The ID 1 should have been auto-generated');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals(1, $doc->get()->id, 'The ID 1 should have been auto-generated');
|
||||
|
||||
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'burrito'));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'burrito')], NumDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals(2, $doc->id, 'The ID 2 should have been auto-generated');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals(2, $doc->get()->id, 'The ID 2 should have been auto-generated');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
@@ -189,8 +192,8 @@ class DocumentTest extends TestCase
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, new NumDocument(64, 'large'));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'large')], NumDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals(64, $doc->id, 'The ID 64 should have been stored');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals(64, $doc->get()->id, 'The ID 64 should have been stored');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
@@ -204,8 +207,8 @@ class DocumentTest extends TestCase
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument(value: 'something', num_value: 9));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EX('value')], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertNotEmpty($doc->id, 'The ID should have been auto-generated');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertNotEmpty($doc->get()->id, 'The ID should have been auto-generated');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
@@ -220,8 +223,8 @@ class DocumentTest extends TestCase
|
||||
$uuid = AutoId::generateUUID();
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument($uuid, num_value: 14));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 14)], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals($uuid, $doc->id, 'The ID should not have been changed');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals($uuid, $doc->get()->id, 'The ID should not have been changed');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
@@ -236,8 +239,9 @@ class DocumentTest extends TestCase
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument(num_value: 55));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 55)], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals(40, strlen($doc->id), 'The ID should have been auto-generated and had 40 characters');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals(40, strlen($doc->get()->id),
|
||||
'The ID should have been auto-generated and had 40 characters');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
Configuration::$idStringLength = 16;
|
||||
@@ -252,8 +256,8 @@ class DocumentTest extends TestCase
|
||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
||||
Document::insert(ThrowawayDb::TABLE, new TestDocument('my-key', num_value: 3));
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 3)], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals('my-key', $doc->id, 'The ID should not have been changed');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals('my-key', $doc->get()->id, 'The ID should not have been changed');
|
||||
} finally {
|
||||
Configuration::$autoId = AutoId::None;
|
||||
}
|
||||
@@ -269,14 +273,15 @@ class DocumentTest extends TestCase
|
||||
{
|
||||
Document::save(ThrowawayDb::TABLE, new TestDocument('test', sub: new SubDocument('a', 'b')));
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
}
|
||||
|
||||
public function testSaveSucceedsWhenADocumentIsUpdated(): void
|
||||
{
|
||||
Document::save(ThrowawayDb::TABLE, new TestDocument('two', num_value: 44));
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
|
||||
$this->assertTrue($tryDoc->isDefined(), 'There should have been a document returned');
|
||||
$doc = $tryDoc->get();
|
||||
$this->assertEquals(44, $doc->num_value, 'The numeric value was not updated');
|
||||
$this->assertNull($doc->sub, 'The sub-document should have been null');
|
||||
}
|
||||
@@ -284,8 +289,9 @@ class DocumentTest extends TestCase
|
||||
public function testUpdateSucceedsWhenReplacingADocument(): void
|
||||
{
|
||||
Document::update(ThrowawayDb::TABLE, 'one', new TestDocument('one', 'howdy', 8, new SubDocument('y', 'z')));
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
|
||||
$this->assertTrue($tryDoc->isDefined(), 'There should have been a document returned');
|
||||
$doc = $tryDoc->get();
|
||||
$this->assertEquals('howdy', $doc->value, 'The value was incorrect');
|
||||
$this->assertEquals(8, $doc->num_value, 'The numeric value was incorrect');
|
||||
$this->assertNotNull($doc->sub, 'The sub-document should not have been null');
|
||||
@@ -297,6 +303,6 @@ class DocumentTest extends TestCase
|
||||
{
|
||||
Document::update(ThrowawayDb::TABLE, 'two-hundred', new TestDocument('200'));
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'two-hundred', TestDocument::class);
|
||||
$this->assertFalse($doc, 'There should not have been a document returned');
|
||||
$this->assertTrue($doc->isEmpty(), 'There should not have been a document returned');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,8 +49,8 @@ class FindTest extends TestCase
|
||||
public function testByIdSucceedsWhenADocumentIsFound(): void
|
||||
{
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals('two', $doc->id, 'An incorrect document was returned');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals('two', $doc->get()->id, 'An incorrect document was returned');
|
||||
}
|
||||
|
||||
#[TestDox('By ID succeeds when a document is found with numeric ID')]
|
||||
@@ -58,15 +58,15 @@ class FindTest extends TestCase
|
||||
{
|
||||
Document::insert(ThrowawayDb::TABLE, ['id' => 18, 'value' => 'howdy']);
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 18, TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals('18', $doc->id, 'An incorrect document was returned');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals('18', $doc->get()->id, 'An incorrect document was returned');
|
||||
}
|
||||
|
||||
#[TestDox('By ID succeeds when a document is not found')]
|
||||
public function testByIdSucceedsWhenADocumentIsNotFound(): void
|
||||
{
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'seventy-five', TestDocument::class);
|
||||
$this->assertFalse($doc, 'There should not have been a document returned');
|
||||
$this->assertTrue($doc->isEmpty(), 'There should not have been a document returned');
|
||||
}
|
||||
|
||||
public function testByFieldsSucceedsWhenDocumentsAreFound(): void
|
||||
@@ -101,21 +101,21 @@ class FindTest extends TestCase
|
||||
public function testFirstByFieldsSucceedsWhenADocumentIsFound(): void
|
||||
{
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'another')], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals('two', $doc->id, 'The incorrect document was returned');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals('two', $doc->get()->id, 'The incorrect document was returned');
|
||||
}
|
||||
|
||||
public function testFirstByFieldsSucceedsWhenMultipleDocumentsAreFound(): void
|
||||
{
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('sub.foo', 'green')], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertContains($doc->id, ['two', 'four'], 'An incorrect document was returned');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertContains($doc->get()->id, ['two', 'four'], 'An incorrect document was returned');
|
||||
}
|
||||
|
||||
public function testFirstByFieldsSucceedsWhenADocumentIsNotFound(): void
|
||||
{
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'absent')], TestDocument::class);
|
||||
$this->assertFalse($doc, 'There should not have been a document returned');
|
||||
$this->assertTrue($doc->isEmpty(), 'There should not have been a document returned');
|
||||
}
|
||||
|
||||
public function testFirstByContainsFails(): void
|
||||
|
||||
@@ -33,8 +33,8 @@ class PatchTest extends TestCase
|
||||
{
|
||||
Patch::byId(ThrowawayDb::TABLE, 'one', ['num_value' => 44]);
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertEquals(44, $doc->num_value, 'The updated document is not correct');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertEquals(44, $doc->get()->num_value, 'The updated document is not correct');
|
||||
}
|
||||
|
||||
#[TestDox('By ID succeeds when no document is updated')]
|
||||
|
||||
@@ -32,8 +32,9 @@ class RemoveFieldsTest extends TestCase
|
||||
public function testByIdSucceedsWhenFieldsAreRemoved(): void
|
||||
{
|
||||
RemoveFields::byId(ThrowawayDb::TABLE, 'two', ['sub', 'value']);
|
||||
$doc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
|
||||
$this->assertTrue($tryDoc->isDefined(), 'There should have been a document returned');
|
||||
$doc = $tryDoc->get();
|
||||
$this->assertEquals('', $doc->value, 'Value should have been blank (its default value)');
|
||||
$this->assertNull($doc->sub, 'Sub-document should have been null');
|
||||
}
|
||||
@@ -56,8 +57,8 @@ class RemoveFieldsTest extends TestCase
|
||||
{
|
||||
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 17)], ['sub']);
|
||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 17)], TestDocument::class);
|
||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
||||
$this->assertNull($doc->sub, 'Sub-document should have been null');
|
||||
$this->assertTrue($doc->isDefined(), 'There should have been a document returned');
|
||||
$this->assertNull($doc->get()->sub, 'Sub-document should have been null');
|
||||
}
|
||||
|
||||
public function testByFieldsSucceedsWhenAFieldIsNotRemoved(): void
|
||||
|
||||
Reference in New Issue
Block a user