Use Option for single doc queries
This commit is contained in:
@@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user