Changes for beta10 (#5)

- Add In/InArray support
- Add ORDER BY support for `Find` functions
- Update dependencies
- Implement fixes identified via static analysis

Reviewed-on: #5
This commit was merged in pull request #5.
This commit is contained in:
2024-09-27 02:15:00 +00:00
parent 9e0e663811
commit d067f8983f
66 changed files with 1728 additions and 664 deletions

View File

@@ -0,0 +1,35 @@
<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
namespace Test\Integration;
/**
* A document with an array of values
*/
class ArrayDocument
{
/**
* @param string $id The ID of the document
* @param string[] $values The values for the document
*/
public function __construct(public string $id = '', public array $values = []) { }
/**
* A set of documents used for integration tests
*
* @return ArrayDocument[] Test documents for InArray tests
*/
public static function testDocuments(): array
{
return [
new ArrayDocument('first', ['a', 'b', 'c']),
new ArrayDocument('second', ['c', 'd', 'e']),
new ArrayDocument('third', ['x', 'y', 'z'])
];
}
}

View File

@@ -33,44 +33,49 @@ class CountTest extends TestCase
parent::tearDown();
}
#[TestDox('all() succeeds')]
public function testAllSucceeds(): void
{
$count = Count::all(ThrowawayDb::TABLE);
$this->assertEquals(5, $count, 'There should have been 5 matching documents');
}
#[TestDox('byFields() succeeds for a numeric range')]
public function testByFieldsSucceedsForANumericRange(): void
{
$count = Count::byFields(ThrowawayDb::TABLE, [Field::BT('num_value', 10, 20)]);
$count = Count::byFields(ThrowawayDb::TABLE, [Field::between('num_value', 10, 20)]);
$this->assertEquals(3, $count, 'There should have been 3 matching documents');
}
#[TestDox('byFields() succeeds for a non-numeric range')]
public function testByFieldsSucceedsForANonNumericRange(): void
{
$count = Count::byFields(ThrowawayDb::TABLE, [Field::BT('value', 'aardvark', 'apple')]);
$count = Count::byFields(ThrowawayDb::TABLE, [Field::between('value', 'aardvark', 'apple')]);
$this->assertEquals(1, $count, 'There should have been 1 matching document');
}
#[TestDox('byContains() succeeds when documents match')]
public function testByContainsSucceedsWhenDocumentsMatch(): void
{
$this->assertEquals(2, Count::byContains(ThrowawayDb::TABLE, ['value' => 'purple']),
'There should have been 2 matching documents');
}
#[TestDox('byContains() succeeds when no documents match')]
public function testByContainsSucceedsWhenNoDocumentsMatch(): void
{
$this->assertEquals(0, Count::byContains(ThrowawayDb::TABLE, ['value' => 'magenta']),
'There should have been no matching documents');
}
#[TestDox('By JSON Path succeeds when documents match')]
#[TestDox('byJsonPath() succeeds when documents match')]
public function testByJsonPathSucceedsWhenDocumentsMatch(): void
{
$this->assertEquals(2, Count::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ < 5)'),
'There should have been 2 matching documents');
}
#[TestDox('By JSON Path succeeds when no documents match')]
#[TestDox('byJsonPath() succeeds when no documents match')]
public function testByJsonPathSucceedsWhenNoDocumentsMatch(): void
{
$this->assertEquals(0, Count::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)'),

View File

@@ -34,7 +34,8 @@ class CustomTest extends TestCase
ThrowawayDb::destroy($this->dbName);
}
public function testRunQuerySucceedsWithAValidQuery()
#[TestDox('runQuery() succeeds with a valid query')]
public function testRunQuerySucceedsWithAValidQuery(): void
{
$stmt = &Custom::runQuery('SELECT data FROM ' . ThrowawayDb::TABLE . ' LIMIT 1', []);
try {
@@ -44,7 +45,8 @@ class CustomTest extends TestCase
}
}
public function testRunQueryFailsWithAnInvalidQuery()
#[TestDox('runQuery() fails with an invalid query')]
public function testRunQueryFailsWithAnInvalidQuery(): void
{
$this->expectException(DocumentException::class);
$stmt = &Custom::runQuery('GRAB stuff FROM over_there UNTIL done', []);
@@ -55,7 +57,8 @@ class CustomTest extends TestCase
}
}
public function testListSucceedsWhenDataIsFound()
#[TestDox('list() succeeds when data is found')]
public function testListSucceedsWhenDataIsFound(): void
{
$list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE), [], new DocumentMapper(TestDocument::class));
$this->assertNotNull($list, 'The document list should not be null');
@@ -64,7 +67,8 @@ class CustomTest extends TestCase
$this->assertEquals(5, $count, 'There should have been 5 documents in the list');
}
public function testListSucceedsWhenNoDataIsFound()
#[TestDox('list() succeeds when no data is found')]
public function testListSucceedsWhenNoDataIsFound(): void
{
$list = Custom::list(
Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE (data->>'num_value')::numeric > :value",
@@ -73,7 +77,8 @@ class CustomTest extends TestCase
$this->assertFalse($list->hasItems(), 'There should have been no documents in the list');
}
public function testArraySucceedsWhenDataIsFound()
#[TestDox('array() succeeds when data is found')]
public function testArraySucceedsWhenDataIsFound(): void
{
$array = Custom::array(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'sub' IS NOT NULL", [],
new DocumentMapper(TestDocument::class));
@@ -81,7 +86,8 @@ class CustomTest extends TestCase
$this->assertCount(2, $array, 'There should have been 2 documents in the array');
}
public function testArraySucceedsWhenNoDataIsFound()
#[TestDox('array() succeeds when no data is found')]
public function testArraySucceedsWhenNoDataIsFound(): void
{
$array = Custom::array(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'value' = :value",
[':value' => 'not there'], new DocumentMapper(TestDocument::class));
@@ -89,6 +95,7 @@ class CustomTest extends TestCase
$this->assertCount(0, $array, 'There should have been no documents in the array');
}
#[TestDox('single() succeeds when a row is found')]
public function testSingleSucceedsWhenARowIsFound(): void
{
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id", [':id' => 'one'],
@@ -97,6 +104,7 @@ class CustomTest extends TestCase
$this->assertEquals('one', $doc->get()->id, 'The incorrect document was returned');
}
#[TestDox('single() succeeds when a row is not found')]
public function testSingleSucceedsWhenARowIsNotFound(): void
{
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id",
@@ -104,14 +112,16 @@ class CustomTest extends TestCase
$this->assertTrue($doc->isNone(), 'There should not have been a document returned');
}
public function testNonQuerySucceedsWhenOperatingOnData()
#[TestDox('nonQuery() succeeds when operating on data')]
public function testNonQuerySucceedsWhenOperatingOnData(): void
{
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
$remaining = Count::all(ThrowawayDb::TABLE);
$this->assertEquals(0, $remaining, 'There should be no documents remaining in the table');
}
public function testNonQuerySucceedsWhenNoDataMatchesWhereClause()
#[TestDox('nonQuery() succeeds when no data matches WHERE clause')]
public function testNonQuerySucceedsWhenNoDataMatchesWhereClause(): void
{
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE . " WHERE (data->>'num_value')::numeric > :value",
[':value' => 100]);
@@ -119,7 +129,8 @@ class CustomTest extends TestCase
$this->assertEquals(5, $remaining, 'There should be 5 documents remaining in the table');
}
public function testScalarSucceeds()
#[TestDox('scalar() succeeds')]
public function testScalarSucceeds(): void
{
$value = Custom::scalar("SELECT 5 AS it", [], new CountMapper());
$this->assertEquals(5, $value, 'The scalar value was not returned correctly');

View File

@@ -47,6 +47,7 @@ class DefinitionTest extends TestCase
[':name' => $name], new ExistsMapper());
}
#[TestDox('ensureTable() succeeds')]
public function testEnsureTableSucceeds(): void
{
$this->assertFalse($this->itExists('ensured'), 'The table should not exist already');
@@ -56,6 +57,7 @@ class DefinitionTest extends TestCase
$this->assertTrue($this->itExists('idx_ensured_key'), 'The key index should now exist');
}
#[TestDox('ensureFieldIndex() succeeds')]
public function testEnsureFieldIndexSucceeds(): void
{
$this->assertFalse($this->itExists('idx_ensured_test'), 'The index should not exist already');
@@ -64,6 +66,7 @@ class DefinitionTest extends TestCase
$this->assertTrue($this->itExists('idx_ensured_test'), 'The index should now exist');
}
#[TestDox('ensureDocumentIndex() succeeds for Full')]
public function testEnsureDocumentIndexSucceedsForFull(): void
{
$docIdx = 'idx_' . ThrowawayDb::TABLE . '_document';
@@ -73,6 +76,7 @@ class DefinitionTest extends TestCase
$this->assertTrue($this->itExists($docIdx), 'The document index should now exist');
}
#[TestDox('ensureDocumentIndex() succeeds for Optimized')]
public function testEnsureDocumentIndexSucceedsForOptimized(): void
{
$docIdx = 'idx_' . ThrowawayDb::TABLE . '_document';

View File

@@ -33,7 +33,7 @@ class DeleteTest extends TestCase
parent::tearDown();
}
#[TestDox('By ID succeeds when a document is deleted')]
#[TestDox('byId() succeeds when a document is deleted')]
public function testByIdSucceedsWhenADocumentIsDeleted(): void
{
$this->assertEquals(5, Count::all(ThrowawayDb::TABLE), 'There should have been 5 documents to start');
@@ -41,7 +41,7 @@ class DeleteTest extends TestCase
$this->assertEquals(4, Count::all(ThrowawayDb::TABLE), 'There should have been 4 documents remaining');
}
#[TestDox('By ID succeeds when a document is not deleted')]
#[TestDox('byId() succeeds when a document is not deleted')]
public function testByIdSucceedsWhenADocumentIsNotDeleted(): void
{
$this->assertEquals(5, Count::all(ThrowawayDb::TABLE), 'There should have been 5 documents to start');
@@ -49,20 +49,23 @@ class DeleteTest extends TestCase
$this->assertEquals(5, Count::all(ThrowawayDb::TABLE), 'There should have been 5 documents remaining');
}
#[TestDox('byFields() succeeds when documents are deleted')]
public function testByFieldsSucceedsWhenDocumentsAreDeleted(): void
{
$this->assertEquals(5, Count::all(ThrowawayDb::TABLE), 'There should have been 5 documents to start');
Delete::byFields(ThrowawayDb::TABLE, [Field::NE('value', 'purple')]);
Delete::byFields(ThrowawayDb::TABLE, [Field::notEqual('value', 'purple')]);
$this->assertEquals(2, Count::all(ThrowawayDb::TABLE), 'There should have been 2 documents remaining');
}
#[TestDox('byFields() succeeds when documents are not deleted')]
public function testByFieldsSucceedsWhenDocumentsAreNotDeleted(): void
{
$this->assertEquals(5, Count::all(ThrowawayDb::TABLE), 'There should have been 5 documents to start');
Delete::byFields(ThrowawayDb::TABLE, [Field::EQ('value', 'crimson')]);
Delete::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'crimson')]);
$this->assertEquals(5, Count::all(ThrowawayDb::TABLE), 'There should have been 5 documents remaining');
}
#[TestDox('byContains() succeeds when documents are deleted')]
public function testByContainsSucceedsWhenDocumentsAreDeleted(): void
{
$this->assertEquals(5, Count::all(ThrowawayDb::TABLE), 'There should have been 5 documents to start');
@@ -70,6 +73,7 @@ class DeleteTest extends TestCase
$this->assertEquals(3, Count::all(ThrowawayDb::TABLE), 'There should have been 3 documents remaining');
}
#[TestDox('byContains() succeeds when documents are not deleted')]
public function testByContainsSucceedsWhenDocumentsAreNotDeleted(): void
{
$this->assertEquals(5, Count::all(ThrowawayDb::TABLE), 'There should have been 5 documents to start');
@@ -77,7 +81,7 @@ class DeleteTest extends TestCase
$this->assertEquals(5, Count::all(ThrowawayDb::TABLE), 'There should have been 5 documents remaining');
}
#[TestDox('By JSON Path succeeds when documents are deleted')]
#[TestDox('byJsonPath() succeeds when documents are deleted')]
public function testByJsonPathSucceedsWhenDocumentsAreDeleted(): void
{
$this->assertEquals(5, Count::all(ThrowawayDb::TABLE), 'There should have been 5 documents to start');
@@ -85,7 +89,7 @@ class DeleteTest extends TestCase
$this->assertEquals(1, Count::all(ThrowawayDb::TABLE), 'There should have been 1 document remaining');
}
#[TestDox('By JSON Path succeeds when documents are not deleted')]
#[TestDox('byJsonPath() succeeds when documents are not deleted')]
public function testByJsonPathSucceedsWhenDocumentsAreNotDeleted(): void
{
$this->assertEquals(5, Count::all(ThrowawayDb::TABLE), 'There should have been 5 documents to start');

View File

@@ -35,6 +35,7 @@ class DocumentListTest extends TestCase
parent::tearDown();
}
#[TestDox('create() succeeds')]
public function testCreateSucceeds(): void
{
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
@@ -43,6 +44,7 @@ class DocumentListTest extends TestCase
$list = null;
}
#[TestDox('items() succeeds')]
public function testItemsSucceeds(): void
{
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
@@ -57,6 +59,7 @@ class DocumentListTest extends TestCase
$this->assertEquals(5, $count, 'There should have been 5 documents returned');
}
#[TestDox('items() fails when already consumed')]
public function testItemsFailsWhenAlreadyConsumed(): void
{
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
@@ -69,6 +72,7 @@ class DocumentListTest extends TestCase
iterator_to_array($list->items());
}
#[TestDox('hasItems() succeeds with empty results')]
public function testHasItemsSucceedsWithEmptyResults(): void
{
$list = DocumentList::create(
@@ -78,6 +82,7 @@ class DocumentListTest extends TestCase
$this->assertFalse($list->hasItems(), 'There should be no items in the list');
}
#[TestDox('hasItems() succeeds with non-empty results')]
public function testHasItemsSucceedsWithNonEmptyResults(): void
{
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
@@ -90,6 +95,7 @@ class DocumentListTest extends TestCase
$this->assertFalse($list->hasItems(), 'There should be no remaining items in the list');
}
#[TestDox('map() succeeds')]
public function testMapSucceeds(): void
{
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
@@ -102,6 +108,7 @@ class DocumentListTest extends TestCase
}
}
#[TestDox('iter() succeeds')]
public function testIterSucceeds(): void
{
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
@@ -114,6 +121,7 @@ class DocumentListTest extends TestCase
'Iteration did not have the expected result');
}
#[TestDox('mapToArray() succeeds')]
public function testMapToArraySucceeds(): void
{
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],

View File

@@ -35,7 +35,7 @@ class DocumentTest extends TestCase
parent::tearDown();
}
#[TestDox('Insert succeeds for array no auto ID')]
#[TestDox('insert() succeeds for array no auto ID')]
public function testInsertSucceedsForArrayNoAutoId(): void
{
Document::insert(ThrowawayDb::TABLE, ['id' => 'turkey', 'sub' => ['foo' => 'gobble', 'bar' => 'gobble']]);
@@ -50,7 +50,7 @@ class DocumentTest extends TestCase
$this->assertEquals('gobble', $doc->sub->bar, 'The sub-document bar property was incorrect');
}
#[TestDox('Insert succeeds for array with auto number ID not provided')]
#[TestDox('insert() succeeds for array with auto number ID not provided')]
public function testInsertSucceedsForArrayWithAutoNumberIdNotProvided(): void
{
Configuration::$autoId = AutoId::Number;
@@ -74,7 +74,7 @@ class DocumentTest extends TestCase
}
}
#[TestDox('Insert succeeds for array with auto number ID with ID provided')]
#[TestDox('insert() succeeds for array with auto number ID with ID provided')]
public function testInsertSucceedsForArrayWithAutoNumberIdWithIdProvided(): void
{
Configuration::$autoId = AutoId::Number;
@@ -90,14 +90,14 @@ class DocumentTest extends TestCase
}
}
#[TestDox('Insert succeeds for array with auto UUID ID not provided')]
#[TestDox('insert() succeeds for array with auto UUID ID not provided')]
public function testInsertSucceedsForArrayWithAutoUuidIdNotProvided(): void
{
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::EQ('num_value', 5)], TestDocument::class);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 5)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertNotEmpty($doc->get()->id, 'The ID should have been auto-generated');
} finally {
@@ -105,7 +105,7 @@ class DocumentTest extends TestCase
}
}
#[TestDox('Insert succeeds for array with auto UUID ID with ID provided')]
#[TestDox('insert() succeeds for array with auto UUID ID with ID provided')]
public function testInsertSucceedsForArrayWithAutoUuidIdWithIdProvided(): void
{
Configuration::$autoId = AutoId::UUID;
@@ -113,7 +113,7 @@ class DocumentTest extends TestCase
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::EQ('num_value', 12)], TestDocument::class);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 12)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertEquals($uuid, $doc->get()->id, 'The ID should not have been changed');
} finally {
@@ -121,7 +121,7 @@ class DocumentTest extends TestCase
}
}
#[TestDox('Insert succeeds for array with auto string ID not provided')]
#[TestDox('insert() succeeds for array with auto string ID not provided')]
public function testInsertSucceedsForArrayWithAutoStringIdNotProvided(): void
{
Configuration::$autoId = AutoId::RandomString;
@@ -129,7 +129,7 @@ class DocumentTest extends TestCase
try {
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);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 8)], TestDocument::class);
$this->assertTrue($doc->isSome(), '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');
@@ -139,14 +139,14 @@ class DocumentTest extends TestCase
}
}
#[TestDox('Insert succeeds for array with auto string ID with ID provided')]
#[TestDox('insert() succeeds for array with auto string ID with ID provided')]
public function testInsertSucceedsForArrayWithAutoStringIdWithIdProvided(): void
{
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::EQ('num_value', 3)], TestDocument::class);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertEquals('my-key', $doc->get()->id, 'The ID should not have been changed');
} finally {
@@ -154,7 +154,7 @@ class DocumentTest extends TestCase
}
}
#[TestDox('Insert succeeds for object no auto ID')]
#[TestDox('insert() succeeds for object no auto ID')]
public function testInsertSucceedsForObjectNoAutoId(): void
{
Document::insert(ThrowawayDb::TABLE, new TestDocument('turkey', sub: new SubDocument('gobble', 'gobble')));
@@ -169,7 +169,7 @@ class DocumentTest extends TestCase
$this->assertEquals('gobble', $doc->sub->bar, 'The sub-document bar property was incorrect');
}
#[TestDox('Insert succeeds for object with auto number ID not provided')]
#[TestDox('insert() succeeds for object with auto number ID not provided')]
public function testInsertSucceedsForObjectWithAutoNumberIdNotProvided(): void
{
Configuration::$autoId = AutoId::Number;
@@ -177,12 +177,12 @@ class DocumentTest extends TestCase
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'taco'));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'taco')], NumDocument::class);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'taco')], NumDocument::class);
$this->assertTrue($doc->isSome(), '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);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'burrito')], NumDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertEquals(2, $doc->get()->id, 'The ID 2 should have been auto-generated');
} finally {
@@ -190,14 +190,14 @@ class DocumentTest extends TestCase
}
}
#[TestDox('Insert succeeds for object with auto number ID with ID provided')]
#[TestDox('insert() succeeds for object with auto number ID with ID provided')]
public function testInsertSucceedsForObjectWithAutoNumberIdWithIdProvided(): void
{
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::EQ('value', 'large')], NumDocument::class);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'large')], NumDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertEquals(64, $doc->get()->id, 'The ID 64 should have been stored');
} finally {
@@ -205,14 +205,14 @@ class DocumentTest extends TestCase
}
}
#[TestDox('Insert succeeds for object with auto UUID ID not provided')]
#[TestDox('insert() succeeds for object with auto UUID ID not provided')]
public function testInsertSucceedsForObjectWithAutoUuidIdNotProvided(): void
{
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::EX('value')], TestDocument::class);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::exists('value')], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertNotEmpty($doc->get()->id, 'The ID should have been auto-generated');
} finally {
@@ -220,7 +220,7 @@ class DocumentTest extends TestCase
}
}
#[TestDox('Insert succeeds for object with auto UUID ID with ID provided')]
#[TestDox('insert() succeeds for object with auto UUID ID with ID provided')]
public function testInsertSucceedsForObjectWithAutoUuidIdWithIdProvided(): void
{
Configuration::$autoId = AutoId::UUID;
@@ -228,7 +228,7 @@ class DocumentTest extends TestCase
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::EQ('num_value', 14)], TestDocument::class);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 14)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertEquals($uuid, $doc->get()->id, 'The ID should not have been changed');
} finally {
@@ -236,7 +236,7 @@ class DocumentTest extends TestCase
}
}
#[TestDox('Insert succeeds for object with auto string ID not provided')]
#[TestDox('insert() succeeds for object with auto string ID not provided')]
public function testInsertSucceedsForObjectWithAutoStringIdNotProvided(): void
{
Configuration::$autoId = AutoId::RandomString;
@@ -244,7 +244,7 @@ class DocumentTest extends TestCase
try {
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);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 55)], TestDocument::class);
$this->assertTrue($doc->isSome(), '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');
@@ -254,14 +254,14 @@ class DocumentTest extends TestCase
}
}
#[TestDox('Insert succeeds for object with auto string ID with ID provided')]
#[TestDox('insert() succeeds for object with auto string ID with ID provided')]
public function testInsertSucceedsForObjectWithAutoStringIdWithIdProvided(): void
{
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::EQ('num_value', 3)], TestDocument::class);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertEquals('my-key', $doc->get()->id, 'The ID should not have been changed');
} finally {
@@ -269,12 +269,14 @@ class DocumentTest extends TestCase
}
}
#[TestDox('insert() fails for duplicate key')]
public function testInsertFailsForDuplicateKey(): void
{
$this->expectException(DocumentException::class);
Document::insert(ThrowawayDb::TABLE, new TestDocument('one'));
}
#[TestDox('save() succeeds when a document is inserted')]
public function testSaveSucceedsWhenADocumentIsInserted(): void
{
Document::save(ThrowawayDb::TABLE, new TestDocument('test', sub: new SubDocument('a', 'b')));
@@ -282,6 +284,7 @@ class DocumentTest extends TestCase
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
}
#[TestDox('save() succeeds when a document is updated')]
public function testSaveSucceedsWhenADocumentIsUpdated(): void
{
Document::save(ThrowawayDb::TABLE, new TestDocument('two', num_value: 44));
@@ -292,6 +295,7 @@ class DocumentTest extends TestCase
$this->assertNull($doc->sub, 'The sub-document should have been null');
}
#[TestDox('update() succeeds when replacing a document')]
public function testUpdateSucceedsWhenReplacingADocument(): void
{
Document::update(ThrowawayDb::TABLE, 'one', new TestDocument('one', 'howdy', 8, new SubDocument('y', 'z')));
@@ -305,6 +309,7 @@ class DocumentTest extends TestCase
$this->assertEquals('z', $doc->sub->bar, 'The sub-document bar property was incorrect');
}
#[TestDox('update() succeeds when no document is replaced')]
public function testUpdateSucceedsWhenNoDocumentIsReplaced(): void
{
Document::update(ThrowawayDb::TABLE, 'two-hundred', new TestDocument('200'));

View File

@@ -33,51 +33,55 @@ class ExistsTest extends TestCase
parent::tearDown();
}
#[TestDox('By ID succeeds when a document exists')]
#[TestDox('byId() succeeds when a document exists')]
public function testByIdSucceedsWhenADocumentExists(): void
{
$this->assertTrue(Exists::byId(ThrowawayDb::TABLE, 'three'), 'There should have been an existing document');
}
#[TestDox('By ID succeeds when a document does not exist')]
#[TestDox('byId() succeeds when a document does not exist')]
public function testByIdSucceedsWhenADocumentDoesNotExist(): void
{
$this->assertFalse(Exists::byId(ThrowawayDb::TABLE, 'seven'),
'There should not have been an existing document');
}
#[TestDox('byFields() succeeds when documents exist')]
public function testByFieldsSucceedsWhenDocumentsExist(): void
{
$this->assertTrue(Exists::byFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 10)]),
$this->assertTrue(Exists::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 10)]),
'There should have been existing documents');
}
#[TestDox('byFields() succeeds when no matching documents exist')]
public function testByFieldsSucceedsWhenNoMatchingDocumentsExist(): void
{
$this->assertFalse(Exists::byFields(ThrowawayDb::TABLE, [Field::LT('nothing', 'none')]),
$this->assertFalse(Exists::byFields(ThrowawayDb::TABLE, [Field::less('nothing', 'none')]),
'There should not have been any existing documents');
}
#[TestDox('byContains() succeeds when documents exist')]
public function testByContainsSucceedsWhenDocumentsExist(): void
{
$this->assertTrue(Exists::byContains(ThrowawayDb::TABLE, ['value' => 'purple']),
'There should have been existing documents');
}
#[TestDox('byContains() succeeds when no matching documents exist')]
public function testByContainsSucceedsWhenNoMatchingDocumentsExist(): void
{
$this->assertFalse(Exists::byContains(ThrowawayDb::TABLE, ['value' => 'violet']),
'There should not have been existing documents');
}
#[TestDox('By JSON Path succeeds when documents exist')]
#[TestDox('byJsonPath() succeeds when documents exist')]
public function testByJsonPathSucceedsWhenDocumentsExist(): void
{
$this->assertTrue(Exists::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ == 10)'),
'There should have been existing documents');
}
#[TestDox('By JSON Path succeeds when no matching documents exist')]
#[TestDox('byJsonPath() succeeds when no matching documents exist')]
public function testByJsonPathSucceedsWhenNoMatchingDocumentsExist(): void
{
$this->assertFalse(Exists::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ == 10.1)'),

View File

@@ -8,10 +8,10 @@ declare(strict_types=1);
namespace Test\Integration\PostgreSQL;
use BitBadger\PDODocument\{Custom, Delete, Document, Field, Find};
use BitBadger\PDODocument\{Custom, Delete, Document, Field, FieldMatch, Find};
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
use Test\Integration\{NumDocument, TestDocument};
use Test\Integration\{ArrayDocument, NumDocument, TestDocument};
/**
* PostgreSQL integration tests for the Find class
@@ -34,6 +34,7 @@ class FindTest extends TestCase
parent::tearDown();
}
#[TestDox('all() succeeds when there is data')]
public function testAllSucceedsWhenThereIsData(): void
{
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class);
@@ -43,6 +44,35 @@ class FindTest extends TestCase
$this->assertEquals(5, $count, 'There should have been 5 documents in the list');
}
#[TestDox('all() succeeds when ordering data ascending')]
public function testAllSucceedsWhenOrderingDataAscending(): void
{
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id')]);
$this->assertNotNull($docs, 'There should have been a document list returned');
$ids = iterator_to_array($docs->map(fn ($it) => $it->id), false);
$this->assertEquals(['five', 'four', 'one', 'three', 'two'], $ids, 'The documents were not ordered correctly');
}
#[TestDox('all() succeeds when ordering data descending')]
public function testAllSucceedsWhenOrderingDataDescending(): void
{
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id DESC')]);
$this->assertNotNull($docs, 'There should have been a document list returned');
$ids = iterator_to_array($docs->map(fn ($it) => $it->id), false);
$this->assertEquals(['two', 'three', 'one', 'four', 'five'], $ids, 'The documents were not ordered correctly');
}
#[TestDox('all() succeeds when ordering data numerically')]
public function testAllSucceedsWhenOrderingDataNumerically(): void
{
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class,
[Field::named('sub.foo NULLS LAST'), Field::named('n:num_value')]);
$this->assertNotNull($docs, 'There should have been a document list returned');
$ids = iterator_to_array($docs->map(fn ($it) => $it->id), false);
$this->assertEquals(['two', 'four', 'one', 'three', 'five'], $ids, 'The documents were not ordered correctly');
}
#[TestDox('all() succeeds when there is no data')]
public function testAllSucceedsWhenThereIsNoData(): void
{
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
@@ -51,7 +81,7 @@ class FindTest extends TestCase
$this->assertFalse($docs->hasItems(), 'There should have been no documents in the list');
}
#[TestDox('By ID succeeds when a document is found')]
#[TestDox('byId() succeeds when a document is found')]
public function testByIdSucceedsWhenADocumentIsFound(): void
{
$doc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
@@ -59,39 +89,87 @@ class FindTest extends TestCase
$this->assertEquals('two', $doc->get()->id, 'An incorrect document was returned');
}
#[TestDox('By ID succeeds when a document is found with numeric ID')]
#[TestDox('byId() succeeds when a document is found with numeric ID')]
public function testByIdSucceedsWhenADocumentIsFoundWithNumericId(): void
{
Delete::byFields(ThrowawayDb::TABLE, [Field::NEX('absent')]);
Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absent')]);
Document::insert(ThrowawayDb::TABLE, ['id' => 18, 'value' => 'howdy']);
$doc = Find::byId(ThrowawayDb::TABLE, 18, NumDocument::class);
$this->assertTrue($doc->isSome(), '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')]
#[TestDox('byId() succeeds when a document is not found')]
public function testByIdSucceedsWhenADocumentIsNotFound(): void
{
$doc = Find::byId(ThrowawayDb::TABLE, 'seventy-five', TestDocument::class);
$this->assertTrue($doc->isNone(), 'There should not have been a document returned');
}
#[TestDox('byFields() succeeds when documents are found')]
public function testByFieldsSucceedsWhenDocumentsAreFound(): void
{
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::GT('num_value', 15)], TestDocument::class);
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('value', ['blue', 'purple']), Field::exists('sub')],
TestDocument::class, FieldMatch::All);
$this->assertNotNull($docs, 'There should have been a document list returned');
$count = 0;
foreach ($docs->items() as $ignored) $count++;
$this->assertEquals(1, $count, 'There should have been 1 document in the list');
}
#[TestDox('byFields() succeeds when documents are found and ordered')]
public function testByFieldsSucceedsWhenDocumentsAreFoundAndOrdered(): void
{
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], TestDocument::class,
FieldMatch::All, [Field::named('id')]);
$this->assertNotNull($docs, 'There should have been a document list returned');
$ids = iterator_to_array($docs->map(fn ($it) => $it->id), false);
$this->assertEquals(['five', 'four'], $ids, 'The documents were not ordered correctly');
}
#[TestDox('byFields() succeeds when documents are found using IN with numeric field')]
public function testByFieldsSucceedsWhenDocumentsAreFoundUsingInWithNumericField(): void
{
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('num_value', [2, 4, 6, 8])], TestDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned');
$count = 0;
foreach ($docs->items() as $ignored) $count++;
$this->assertEquals(1, $count, 'There should have been 1 document in the list');
}
#[TestDox('byFields() succeeds when no documents are found')]
public function testByFieldsSucceedsWhenNoDocumentsAreFound(): void
{
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::greater('num_value', 100)], TestDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned');
$this->assertFalse($docs->hasItems(), 'There should have been no documents in the list');
}
#[TestDox('byFields() succeeds for inArray when matching documents exist')]
public function testByFieldsSucceedsForInArrayWhenMatchingDocumentsExist(): void
{
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);
$this->assertNotNull($docs, 'There should have been a document list returned');
$count = 0;
foreach ($docs->items() as $ignored) $count++;
$this->assertEquals(2, $count, 'There should have been 2 documents in the list');
}
public function testByFieldsSucceedsWhenNoDocumentsAreFound(): void
#[TestDox('byFields() succeeds for inArray when no matching documents exist')]
public function testByFieldsSucceedsForInArrayWhenNoMatchingDocumentsExist(): void
{
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::GT('num_value', 100)], TestDocument::class);
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, ['j'])],
ArrayDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned');
$this->assertFalse($docs->hasItems(), 'There should have been no documents in the list');
}
#[TestDox('byContains() succeeds when documents are found')]
public function testByContainsSucceedsWhenDocumentsAreFound(): void
{
$docs = Find::byContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class);
@@ -101,6 +179,17 @@ class FindTest extends TestCase
$this->assertEquals(2, $count, 'There should have been 2 documents in the list');
}
#[TestDox('byContains() succeeds when documents are found and ordered')]
public function testByContainsSucceedsWhenDocumentsAreFoundAndOrdered(): void
{
$docs = Find::byContains(ThrowawayDb::TABLE, ['sub' => ['foo' => 'green']], TestDocument::class,
[Field::named('value')]);
$this->assertNotNull($docs, 'There should have been a document list returned');
$ids = iterator_to_array($docs->map(fn ($it) => $it->id), false);
$this->assertEquals(['two', 'four'], $ids, 'The documents were not ordered correctly');
}
#[TestDox('byContains() succeeds when no documents are found')]
public function testByContainsSucceedsWhenNoDocumentsAreFound(): void
{
$docs = Find::byContains(ThrowawayDb::TABLE, ['value' => 'indigo'], TestDocument::class);
@@ -108,7 +197,7 @@ class FindTest extends TestCase
$this->assertFalse($docs->hasItems(), 'The document list should be empty');
}
#[TestDox('By JSON Path succeeds when documents are found')]
#[TestDox('byJsonPath() succeeds when documents are found')]
public function testByJsonPathSucceedsWhenDocumentsAreFound(): void
{
$docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class);
@@ -118,7 +207,17 @@ class FindTest extends TestCase
$this->assertEquals(2, $count, 'There should have been 2 documents in the list');
}
#[TestDox('By JSON Path succeeds when no documents are found')]
#[TestDox('byJsonPath() succeeds when documents are found and ordered')]
public function testByJsonPathSucceedsWhenDocumentsAreFoundAndOrdered(): void
{
$docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class,
[Field::named('id')]);
$this->assertNotNull($docs, 'There should have been a document list returned');
$ids = iterator_to_array($docs->map(fn ($it) => $it->id), false);
$this->assertEquals(['five', 'four'], $ids, 'The documents were not ordered correctly');
}
#[TestDox('byJsonPath() succeeds when no documents are found')]
public function testByJsonPathSucceedsWhenNoDocumentsAreFound(): void
{
$docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)', TestDocument::class);
@@ -126,26 +225,39 @@ class FindTest extends TestCase
$this->assertFalse($docs->hasItems(), 'The document list should be empty');
}
#[TestDox('firstByFields() succeeds when a document is found')]
public function testFirstByFieldsSucceedsWhenADocumentIsFound(): void
{
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'another')], TestDocument::class);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'another')], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertEquals('two', $doc->get()->id, 'The incorrect document was returned');
}
#[TestDox('firstByFields() succeeds when multiple documents are found')]
public function testFirstByFieldsSucceedsWhenMultipleDocumentsAreFound(): void
{
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('sub.foo', 'green')], TestDocument::class);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertContains($doc->get()->id, ['two', 'four'], 'An incorrect document was returned');
}
#[TestDox('firstByFields() succeeds when multiple ordered documents are found')]
public function testFirstByFieldsSucceedsWhenMultipleOrderedDocumentsAreFound(): void
{
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class,
orderBy: [Field::named('n:num_value DESC')]);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertEquals('four', $doc->get()->id, 'The incorrect document was returned');
}
#[TestDox('firstByFields() succeeds when a document is not found')]
public function testFirstByFieldsSucceedsWhenADocumentIsNotFound(): void
{
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'absent')], TestDocument::class);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'absent')], TestDocument::class);
$this->assertTrue($doc->isNone(), 'There should not have been a document returned');
}
#[TestDox('firstByContains() succeeds when a document is found')]
public function testFirstByContainsSucceedsWhenADocumentIsFound(): void
{
$doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'FIRST!'], TestDocument::class);
@@ -153,6 +265,7 @@ class FindTest extends TestCase
$this->assertEquals('one', $doc->get()->id, 'The incorrect document was returned');
}
#[TestDox('firstByContains() succeeds when multiple documents are found')]
public function testFirstByContainsSucceedsWhenMultipleDocumentsAreFound(): void
{
$doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class);
@@ -160,13 +273,23 @@ class FindTest extends TestCase
$this->assertContains($doc->get()->id, ['four', 'five'], 'An incorrect document was returned');
}
#[TestDox('firstByContains() succeeds when multiple ordered documents are found')]
public function testFirstByContainsSucceedsWhenMultipleOrderedDocumentsAreFound(): void
{
$doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class,
[Field::named('sub.bar NULLS FIRST')]);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertEquals('five', $doc->get()->id, 'The incorrect document was returned');
}
#[TestDox('firstByContains() succeeds when a document is not found')]
public function testFirstByContainsSucceedsWhenADocumentIsNotFound(): void
{
$doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'indigo'], TestDocument::class);
$this->assertTrue($doc->isNone(), 'There should not have been a document returned');
}
#[TestDox('First by JSON Path succeeds when a document is found')]
#[TestDox('firstByJsonPath() succeeds when a document is found')]
public function testFirstByJsonPathSucceedsWhenADocumentIsFound(): void
{
$doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ == 10)', TestDocument::class);
@@ -174,7 +297,7 @@ class FindTest extends TestCase
$this->assertEquals('two', $doc->get()->id, 'The incorrect document was returned');
}
#[TestDox('First by JSON Path succeeds when multiple documents are found')]
#[TestDox('firstByJsonPath() succeeds when multiple documents are found')]
public function testFirstByJsonPathSucceedsWhenMultipleDocumentsAreFound(): void
{
$doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class);
@@ -182,7 +305,16 @@ class FindTest extends TestCase
$this->assertContains($doc->get()->id, ['four', 'five'], 'An incorrect document was returned');
}
#[TestDox('First by JSON Path succeeds when a document is not found')]
#[TestDox('firstByJsonPath() succeeds when multiple ordered documents are found')]
public function testFirstByJsonPathSucceedsWhenMultipleOrderedDocumentsAreFound(): void
{
$doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class,
[Field::named('id DESC')]);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertEquals('four', $doc->get()->id, 'The incorrect document was returned');
}
#[TestDox('firstByJsonPath() succeeds when a document is not found')]
public function testFirstByJsonPathSucceedsWhenADocumentIsNotFound(): void
{
$doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)', TestDocument::class);

View File

@@ -34,7 +34,7 @@ class PatchTest extends TestCase
parent::tearDown();
}
#[TestDox('By ID succeeds when a document is updated')]
#[TestDox('byId() succeeds when a document is updated')]
public function testByIdSucceedsWhenADocumentIsUpdated(): void
{
Patch::byId(ThrowawayDb::TABLE, 'one', ['num_value' => 44]);
@@ -43,7 +43,7 @@ class PatchTest extends TestCase
$this->assertEquals(44, $doc->get()->num_value, 'The updated document is not correct');
}
#[TestDox('By ID succeeds when no document is updated')]
#[TestDox('byId() succeeds when no document is updated')]
public function testByIdSucceedsWhenNoDocumentIsUpdated(): void
{
$id = 'forty-seven';
@@ -52,21 +52,24 @@ class PatchTest extends TestCase
$this->assertTrue(true, 'The above not throwing an exception is the test');
}
#[TestDox('byFields() succeeds when a document is updated')]
public function testByFieldsSucceedsWhenADocumentIsUpdated(): void
{
Patch::byFields(ThrowawayDb::TABLE, [Field::EQ('value', 'purple')], ['num_value' => 77]);
$after = Count::byFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 77)]);
Patch::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], ['num_value' => 77]);
$after = Count::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 77)]);
$this->assertEquals(2, $after, 'There should have been 2 documents updated');
}
#[TestDox('byFields() succeeds when no document is updated')]
public function testByFieldsSucceedsWhenNoDocumentIsUpdated(): void
{
$fields = [Field::EQ('value', 'burgundy')];
$fields = [Field::equal('value', 'burgundy')];
$this->assertEquals(0, Count::byFields(ThrowawayDb::TABLE, $fields), 'There should be no matching documents');
Patch::byFields(ThrowawayDb::TABLE, $fields, ['foo' => 'green']);
$this->assertTrue(true, 'The above not throwing an exception is the test');
}
#[TestDox('byContains() succeeds when documents are updated')]
public function testByContainsSucceedsWhenDocumentsAreUpdated(): void
{
Patch::byContains(ThrowawayDb::TABLE, ['value' => 'another'], ['num_value' => 12]);
@@ -77,6 +80,7 @@ class PatchTest extends TestCase
$this->assertEquals(12, $doc->num_value, 'The document was not patched');
}
#[TestDox('byContains() succeeds when no documents are updated')]
public function testByContainsSucceedsWhenNoDocumentsAreUpdated(): void
{
$criteria = ['value' => 'updated'];
@@ -86,7 +90,7 @@ class PatchTest extends TestCase
$this->assertTrue(true, 'The above not throwing an exception is the test');
}
#[TestDox('By JSON Path succeeds when documents are updated')]
#[TestDox('byJsonPath() succeeds when documents are updated')]
public function testByJsonPathSucceedsWhenDocumentsAreUpdated(): void
{
Patch::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', ['value' => 'blue']);
@@ -99,7 +103,7 @@ class PatchTest extends TestCase
}
}
#[TestDox('By JSON Path succeeds when documents are not updated')]
#[TestDox('byJsonPath() succeeds when documents are not updated')]
public function testByJsonPathSucceedsWhenDocumentsAreNotUpdated(): void
{
$path = '$.num_value ? (@ > 100)';

View File

@@ -34,7 +34,7 @@ class RemoveFieldsTest extends TestCase
parent::tearDown();
}
#[TestDox('By ID succeeds when fields are removed')]
#[TestDox('byId() succeeds when fields are removed')]
public function testByIdSucceedsWhenFieldsAreRemoved(): void
{
RemoveFields::byId(ThrowawayDb::TABLE, 'two', ['sub', 'value']);
@@ -45,40 +45,44 @@ class RemoveFieldsTest extends TestCase
$this->assertNull($doc->sub, 'Sub-document should have been null');
}
#[TestDox('By ID succeeds when a field is not removed')]
#[TestDox('byId() succeeds when a field is not removed')]
public function testByIdSucceedsWhenAFieldIsNotRemoved(): void
{
RemoveFields::byId(ThrowawayDb::TABLE, 'one', ['a_field_that_does_not_exist']);
$this->assertTrue(true, 'The above not throwing an exception is the test');
}
#[TestDox('By ID succeeds when no document is matched')]
#[TestDox('byId() succeeds when no document is matched')]
public function testByIdSucceedsWhenNoDocumentIsMatched(): void
{
RemoveFields::byId(ThrowawayDb::TABLE, 'fifty', ['sub']);
$this->assertTrue(true, 'The above not throwing an exception is the test');
}
#[TestDox('byFields() succeeds when a field is removed')]
public function testByFieldsSucceedsWhenAFieldIsRemoved(): void
{
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 17)], ['sub']);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 17)], TestDocument::class);
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], ['sub']);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertNull($doc->get()->sub, 'Sub-document should have been null');
}
#[TestDox('byFields() succeeds when a field is not removed')]
public function testByFieldsSucceedsWhenAFieldIsNotRemoved(): void
{
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 17)], ['nada']);
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], ['nada']);
$this->assertTrue(true, 'The above not throwing an exception is the test');
}
#[TestDox('byFields() succeeds when no document is matched')]
public function testByFieldsSucceedsWhenNoDocumentIsMatched(): void
{
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::NE('missing', 'nope')], ['value']);
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::notEqual('missing', 'nope')], ['value']);
$this->assertTrue(true, 'The above not throwing an exception is the test');
}
#[TestDox('byContains() succeeds when a field is removed')]
public function testByContainsSucceedsWhenAFieldIsRemoved(): void
{
$criteria = ['sub' => ['foo' => 'green']];
@@ -92,19 +96,21 @@ class RemoveFieldsTest extends TestCase
}
}
#[TestDox('byContains() succeeds when a field is not removed')]
public function testByContainsSucceedsWhenAFieldIsNotRemoved(): void
{
RemoveFields::byContains(ThrowawayDb::TABLE, ['sub' => ['foo' => 'green']], ['invalid_field']);
$this->assertTrue(true, 'The above not throwing an exception is the test');
}
#[TestDox('byContains() succeeds when no document is matched')]
public function testByContainsSucceedsWhenNoDocumentIsMatched(): void
{
RemoveFields::byContains(ThrowawayDb::TABLE, ['value' => 'substantial'], ['num_value']);
$this->assertTrue(true, 'The above not throwing an exception is the test');
}
#[TestDox('By JSON Path succeeds when a field is removed')]
#[TestDox('byJsonPath() succeeds when a field is removed')]
public function testByJsonPathSucceedsWhenAFieldIsRemoved(): void
{
$path = '$.value ? (@ == "purple")';
@@ -118,14 +124,14 @@ class RemoveFieldsTest extends TestCase
}
}
#[TestDox('By JSON Path succeeds when a field is not removed')]
#[TestDox('byJsonPath() succeeds when a field is not removed')]
public function testByJsonPathSucceedsWhenAFieldIsNotRemoved(): void
{
RemoveFields::byJsonPath(ThrowawayDb::TABLE, '$.value ? (@ == "purple")', ['submarine']);
$this->assertTrue(true, 'The above not throwing an exception is the test');
}
#[TestDox('By JSON Path succeeds when no document is matched')]
#[TestDox('byJsonPath() succeeds when no document is matched')]
public function testByJsonPathSucceedsWhenNoDocumentIsMatched(): void
{
RemoveFields::byJsonPath(ThrowawayDb::TABLE, '$.value ? (@ == "mauve")', ['value']);

View File

@@ -33,31 +33,35 @@ class CountTest extends TestCase
parent::tearDown();
}
#[TestDox('all() succeeds')]
public function testAllSucceeds(): void
{
$count = Count::all(ThrowawayDb::TABLE);
$this->assertEquals(5, $count, 'There should have been 5 matching documents');
}
#[TestDox('byFields() succeeds for a numeric range')]
public function testByFieldsSucceedsForANumericRange(): void
{
$count = Count::byFields(ThrowawayDb::TABLE, [Field::BT('num_value', 10, 20)]);
$count = Count::byFields(ThrowawayDb::TABLE, [Field::between('num_value', 10, 20)]);
$this->assertEquals(3, $count, 'There should have been 3 matching documents');
}
#[TestDox('byFields() succeeds for a non-numeric range')]
public function testByFieldsSucceedsForANonNumericRange(): void
{
$count = Count::byFields(ThrowawayDb::TABLE, [Field::BT('value', 'aardvark', 'apple')]);
$count = Count::byFields(ThrowawayDb::TABLE, [Field::between('value', 'aardvark', 'apple')]);
$this->assertEquals(1, $count, 'There should have been 1 matching document');
}
#[TestDox('byContains() fails')]
public function testByContainsFails(): void
{
$this->expectException(DocumentException::class);
Count::byContains('', []);
}
#[TestDox('By JSON Path fails')]
#[TestDox('byJsonPath() fails')]
public function testByJsonPathFails(): void
{
$this->expectException(DocumentException::class);

View File

@@ -34,7 +34,8 @@ class CustomTest extends TestCase
ThrowawayDb::destroy($this->dbName);
}
public function testRunQuerySucceedsWithAValidQuery()
#[TestDox('runQuery() succeeds with a valid query')]
public function testRunQuerySucceedsWithAValidQuery(): void
{
$stmt = &Custom::runQuery('SELECT data FROM ' . ThrowawayDb::TABLE . ' LIMIT 1', []);
try {
@@ -44,7 +45,8 @@ class CustomTest extends TestCase
}
}
public function testRunQueryFailsWithAnInvalidQuery()
#[TestDox('runQuery() fails with an invalid query')]
public function testRunQueryFailsWithAnInvalidQuery(): void
{
$this->expectException(DocumentException::class);
$stmt = &Custom::runQuery('GRAB stuff FROM over_there UNTIL done', []);
@@ -55,7 +57,8 @@ class CustomTest extends TestCase
}
}
public function testListSucceedsWhenDataIsFound()
#[TestDox('list() succeeds when data is found')]
public function testListSucceedsWhenDataIsFound(): void
{
$list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE), [], new DocumentMapper(TestDocument::class));
$this->assertNotNull($list, 'The document list should not be null');
@@ -64,7 +67,8 @@ class CustomTest extends TestCase
$this->assertEquals(5, $count, 'There should have been 5 documents in the list');
}
public function testListSucceedsWhenNoDataIsFound()
#[TestDox('list() succeeds when no data is found')]
public function testListSucceedsWhenNoDataIsFound(): void
{
$list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'num_value' > :value",
[':value' => 100], new DocumentMapper(TestDocument::class));
@@ -72,7 +76,8 @@ class CustomTest extends TestCase
$this->assertFalse($list->hasItems(), 'There should have been no documents in the list');
}
public function testArraySucceedsWhenDataIsFound()
#[TestDox('array() succeeds when data is found')]
public function testArraySucceedsWhenDataIsFound(): void
{
$array = Custom::array(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'sub' IS NOT NULL", [],
new DocumentMapper(TestDocument::class));
@@ -80,7 +85,8 @@ class CustomTest extends TestCase
$this->assertCount(2, $array, 'There should have been 2 documents in the array');
}
public function testArraySucceedsWhenNoDataIsFound()
#[TestDox('array() succeeds when no data is found')]
public function testArraySucceedsWhenNoDataIsFound(): void
{
$array = Custom::array(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'value' = :value",
[':value' => 'not there'], new DocumentMapper(TestDocument::class));
@@ -88,6 +94,7 @@ class CustomTest extends TestCase
$this->assertCount(0, $array, 'There should have been no documents in the array');
}
#[TestDox('single() succeeds when a row is found')]
public function testSingleSucceedsWhenARowIsFound(): void
{
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id", [':id' => 'one'],
@@ -96,6 +103,7 @@ class CustomTest extends TestCase
$this->assertEquals('one', $doc->get()->id, 'The incorrect document was returned');
}
#[TestDox('single() succeeds when a row is not found')]
public function testSingleSucceedsWhenARowIsNotFound(): void
{
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id",
@@ -103,21 +111,24 @@ class CustomTest extends TestCase
$this->assertTrue($doc->isNone(), 'There should not have been a document returned');
}
public function testNonQuerySucceedsWhenOperatingOnData()
#[TestDox('nonQuery() succeeds when operating on data')]
public function testNonQuerySucceedsWhenOperatingOnData(): void
{
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
$remaining = Count::all(ThrowawayDb::TABLE);
$this->assertEquals(0, $remaining, 'There should be no documents remaining in the table');
}
public function testNonQuerySucceedsWhenNoDataMatchesWhereClause()
#[TestDox('nonQuery() succeeds when no data matches WHERE clause')]
public function testNonQuerySucceedsWhenNoDataMatchesWhereClause(): void
{
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE . " WHERE data->>'num_value' > :value", [':value' => 100]);
$remaining = Count::all(ThrowawayDb::TABLE);
$this->assertEquals(5, $remaining, 'There should be 5 documents remaining in the table');
}
public function testScalarSucceeds()
#[TestDox('scalar() succeeds')]
public function testScalarSucceeds(): void
{
$value = Custom::scalar("SELECT 5 AS it", [], new CountMapper());
$this->assertEquals(5, $value, 'The scalar value was not returned correctly');

View File

@@ -47,6 +47,7 @@ class DefinitionTest extends TestCase
[':name' => $name], new ExistsMapper());
}
#[TestDox('ensureTable() succeeds')]
public function testEnsureTableSucceeds(): void
{
$this->assertFalse($this->itExists('ensured'), 'The table should not exist already');
@@ -56,6 +57,7 @@ class DefinitionTest extends TestCase
$this->assertTrue($this->itExists('idx_ensured_key'), 'The key index should now exist');
}
#[TestDox('ensureFieldIndex() succeeds')]
public function testEnsureFieldIndexSucceeds(): void
{
$this->assertFalse($this->itExists('idx_ensured_test'), 'The index should not exist already');
@@ -64,6 +66,7 @@ class DefinitionTest extends TestCase
$this->assertTrue($this->itExists('idx_ensured_test'), 'The index should now exist');
}
#[TestDox('ensureDocumentIndex() fails')]
public function testEnsureDocumentIndexFails(): void
{
$this->expectException(DocumentException::class);

View File

@@ -33,7 +33,7 @@ class DeleteTest extends TestCase
parent::tearDown();
}
#[TestDox('By ID succeeds when a document is deleted')]
#[TestDox('byId() succeeds when a document is deleted')]
public function testByIdSucceedsWhenADocumentIsDeleted(): void
{
$this->assertEquals(5, Count::all(ThrowawayDb::TABLE), 'There should have been 5 documents to start');
@@ -41,7 +41,7 @@ class DeleteTest extends TestCase
$this->assertEquals(4, Count::all(ThrowawayDb::TABLE), 'There should have been 4 documents remaining');
}
#[TestDox('By ID succeeds when a document is not deleted')]
#[TestDox('byId() succeeds when a document is not deleted')]
public function testByIdSucceedsWhenADocumentIsNotDeleted(): void
{
$this->assertEquals(5, Count::all(ThrowawayDb::TABLE), 'There should have been 5 documents to start');
@@ -49,27 +49,30 @@ class DeleteTest extends TestCase
$this->assertEquals(5, Count::all(ThrowawayDb::TABLE), 'There should have been 5 documents remaining');
}
#[TestDox('byFields() succeeds when documents are deleted')]
public function testByFieldsSucceedsWhenDocumentsAreDeleted(): void
{
$this->assertEquals(5, Count::all(ThrowawayDb::TABLE), 'There should have been 5 documents to start');
Delete::byFields(ThrowawayDb::TABLE, [Field::NE('value', 'purple')]);
Delete::byFields(ThrowawayDb::TABLE, [Field::notEqual('value', 'purple')]);
$this->assertEquals(2, Count::all(ThrowawayDb::TABLE), 'There should have been 2 documents remaining');
}
#[TestDox('byFields() succeeds when documents are not deleted')]
public function testByFieldsSucceedsWhenDocumentsAreNotDeleted(): void
{
$this->assertEquals(5, Count::all(ThrowawayDb::TABLE), 'There should have been 5 documents to start');
Delete::byFields(ThrowawayDb::TABLE, [Field::EQ('value', 'crimson')]);
Delete::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'crimson')]);
$this->assertEquals(5, Count::all(ThrowawayDb::TABLE), 'There should have been 5 documents remaining');
}
#[TestDox('byContains() fails')]
public function testByContainsFails(): void
{
$this->expectException(DocumentException::class);
Delete::byContains('', []);
}
#[TestDox('By JSON Path fails')]
#[TestDox('byJsonPath() fails')]
public function testByJsonPathFails(): void
{
$this->expectException(DocumentException::class);

View File

@@ -35,6 +35,7 @@ class DocumentListTest extends TestCase
parent::tearDown();
}
#[TestDox('create() succeeds')]
public function testCreateSucceeds(): void
{
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
@@ -43,6 +44,7 @@ class DocumentListTest extends TestCase
$list = null;
}
#[TestDox('items() succeeds')]
public function testItemsSucceeds(): void
{
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
@@ -57,6 +59,7 @@ class DocumentListTest extends TestCase
$this->assertEquals(5, $count, 'There should have been 5 documents returned');
}
#[TestDox('items() fails when already consumed')]
public function testItemsFailsWhenAlreadyConsumed(): void
{
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
@@ -69,6 +72,7 @@ class DocumentListTest extends TestCase
iterator_to_array($list->items());
}
#[TestDox('hasItems() succeeds with empty results')]
public function testHasItemsSucceedsWithEmptyResults(): void
{
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'num_value' < 0", [],
@@ -77,6 +81,7 @@ class DocumentListTest extends TestCase
$this->assertFalse($list->hasItems(), 'There should be no items in the list');
}
#[TestDox('hasItems() succeeds with non-empty results')]
public function testHasItemsSucceedsWithNonEmptyResults(): void
{
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
@@ -88,6 +93,8 @@ class DocumentListTest extends TestCase
}
$this->assertFalse($list->hasItems(), 'There should be no remaining items in the list');
}
#[TestDox('map() succeeds')]
public function testMapSucceeds(): void
{
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
@@ -100,6 +107,7 @@ class DocumentListTest extends TestCase
}
}
#[TestDox('iter() succeeds')]
public function testIterSucceeds(): void
{
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],
@@ -112,6 +120,7 @@ class DocumentListTest extends TestCase
'Iteration did not have the expected result');
}
#[TestDox('mapToArray() succeeds')]
public function testMapToArraySucceeds(): void
{
$list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [],

View File

@@ -35,7 +35,7 @@ class DocumentTest extends TestCase
parent::tearDown();
}
#[TestDox('Insert succeeds for array no auto ID')]
#[TestDox('insert() succeeds for array no auto ID')]
public function testInsertSucceedsForArrayNoAutoId(): void
{
Document::insert(ThrowawayDb::TABLE, ['id' => 'turkey', 'sub' => ['foo' => 'gobble', 'bar' => 'gobble']]);
@@ -50,7 +50,7 @@ class DocumentTest extends TestCase
$this->assertEquals('gobble', $doc->sub->bar, 'The sub-document bar property was incorrect');
}
#[TestDox('Insert succeeds for array with auto number ID not provided')]
#[TestDox('insert() succeeds for array with auto number ID not provided')]
public function testInsertSucceedsForArrayWithAutoNumberIdNotProvided(): void
{
Configuration::$autoId = AutoId::Number;
@@ -74,7 +74,7 @@ class DocumentTest extends TestCase
}
}
#[TestDox('Insert succeeds for array with auto number ID with ID provided')]
#[TestDox('insert() succeeds for array with auto number ID with ID provided')]
public function testInsertSucceedsForArrayWithAutoNumberIdWithIdProvided(): void
{
Configuration::$autoId = AutoId::Number;
@@ -90,14 +90,14 @@ class DocumentTest extends TestCase
}
}
#[TestDox('Insert succeeds for array with auto UUID ID not provided')]
#[TestDox('insert() succeeds for array with auto UUID ID not provided')]
public function testInsertSucceedsForArrayWithAutoUuidIdNotProvided(): void
{
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::EQ('num_value', 5)], TestDocument::class);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 5)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertNotEmpty($doc->get()->id, 'The ID should have been auto-generated');
} finally {
@@ -105,7 +105,7 @@ class DocumentTest extends TestCase
}
}
#[TestDox('Insert succeeds for array with auto UUID ID with ID provided')]
#[TestDox('insert() succeeds for array with auto UUID ID with ID provided')]
public function testInsertSucceedsForArrayWithAutoUuidIdWithIdProvided(): void
{
Configuration::$autoId = AutoId::UUID;
@@ -113,7 +113,7 @@ class DocumentTest extends TestCase
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::EQ('num_value', 12)], TestDocument::class);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 12)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertEquals($uuid, $doc->get()->id, 'The ID should not have been changed');
} finally {
@@ -121,7 +121,7 @@ class DocumentTest extends TestCase
}
}
#[TestDox('Insert succeeds for array with auto string ID not provided')]
#[TestDox('insert() succeeds for array with auto string ID not provided')]
public function testInsertSucceedsForArrayWithAutoStringIdNotProvided(): void
{
Configuration::$autoId = AutoId::RandomString;
@@ -129,7 +129,7 @@ class DocumentTest extends TestCase
try {
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);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 8)], TestDocument::class);
$this->assertTrue($doc->isSome(), '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');
@@ -139,14 +139,14 @@ class DocumentTest extends TestCase
}
}
#[TestDox('Insert succeeds for array with auto string ID with ID provided')]
#[TestDox('insert() succeeds for array with auto string ID with ID provided')]
public function testInsertSucceedsForArrayWithAutoStringIdWithIdProvided(): void
{
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::EQ('num_value', 3)], TestDocument::class);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertEquals('my-key', $doc->get()->id, 'The ID should not have been changed');
} finally {
@@ -154,7 +154,7 @@ class DocumentTest extends TestCase
}
}
#[TestDox('Insert succeeds for object no auto ID')]
#[TestDox('insert() succeeds for object no auto ID')]
public function testInsertSucceedsForObjectNoAutoId(): void
{
Document::insert(ThrowawayDb::TABLE, new TestDocument('turkey', sub: new SubDocument('gobble', 'gobble')));
@@ -169,7 +169,7 @@ class DocumentTest extends TestCase
$this->assertEquals('gobble', $doc->sub->bar, 'The sub-document bar property was incorrect');
}
#[TestDox('Insert succeeds for object with auto number ID not provided')]
#[TestDox('insert() succeeds for object with auto number ID not provided')]
public function testInsertSucceedsForObjectWithAutoNumberIdNotProvided(): void
{
Configuration::$autoId = AutoId::Number;
@@ -177,12 +177,12 @@ class DocumentTest extends TestCase
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'taco'));
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'taco')], NumDocument::class);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'taco')], NumDocument::class);
$this->assertTrue($doc->isSome(), '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);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'burrito')], NumDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertEquals(2, $doc->get()->id, 'The ID 2 should have been auto-generated');
} finally {
@@ -190,14 +190,14 @@ class DocumentTest extends TestCase
}
}
#[TestDox('Insert succeeds for object with auto number ID with ID provided')]
#[TestDox('insert() succeeds for object with auto number ID with ID provided')]
public function testInsertSucceedsForObjectWithAutoNumberIdWithIdProvided(): void
{
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::EQ('value', 'large')], NumDocument::class);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'large')], NumDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertEquals(64, $doc->get()->id, 'The ID 64 should have been stored');
} finally {
@@ -205,14 +205,14 @@ class DocumentTest extends TestCase
}
}
#[TestDox('Insert succeeds for object with auto UUID ID not provided')]
#[TestDox('insert() succeeds for object with auto UUID ID not provided')]
public function testInsertSucceedsForObjectWithAutoUuidIdNotProvided(): void
{
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::EX('value')], TestDocument::class);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::exists('value')], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertNotEmpty($doc->get()->id, 'The ID should have been auto-generated');
} finally {
@@ -220,7 +220,7 @@ class DocumentTest extends TestCase
}
}
#[TestDox('Insert succeeds for object with auto UUID ID with ID provided')]
#[TestDox('insert() succeeds for object with auto UUID ID with ID provided')]
public function testInsertSucceedsForObjectWithAutoUuidIdWithIdProvided(): void
{
Configuration::$autoId = AutoId::UUID;
@@ -228,7 +228,7 @@ class DocumentTest extends TestCase
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::EQ('num_value', 14)], TestDocument::class);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 14)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertEquals($uuid, $doc->get()->id, 'The ID should not have been changed');
} finally {
@@ -236,7 +236,7 @@ class DocumentTest extends TestCase
}
}
#[TestDox('Insert succeeds for object with auto string ID not provided')]
#[TestDox('insert() succeeds for object with auto string ID not provided')]
public function testInsertSucceedsForObjectWithAutoStringIdNotProvided(): void
{
Configuration::$autoId = AutoId::RandomString;
@@ -244,7 +244,7 @@ class DocumentTest extends TestCase
try {
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);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 55)], TestDocument::class);
$this->assertTrue($doc->isSome(), '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');
@@ -254,14 +254,14 @@ class DocumentTest extends TestCase
}
}
#[TestDox('Insert succeeds for object with auto string ID with ID provided')]
#[TestDox('insert() succeeds for object with auto string ID with ID provided')]
public function testInsertSucceedsForObjectWithAutoStringIdWithIdProvided(): void
{
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::EQ('num_value', 3)], TestDocument::class);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertEquals('my-key', $doc->get()->id, 'The ID should not have been changed');
} finally {
@@ -269,12 +269,14 @@ class DocumentTest extends TestCase
}
}
#[TestDox('insert() fails for duplicate key')]
public function testInsertFailsForDuplicateKey(): void
{
$this->expectException(DocumentException::class);
Document::insert(ThrowawayDb::TABLE, new TestDocument('one'));
}
#[TestDox('save() succeeds when a document is inserted')]
public function testSaveSucceedsWhenADocumentIsInserted(): void
{
Document::save(ThrowawayDb::TABLE, new TestDocument('test', sub: new SubDocument('a', 'b')));
@@ -282,6 +284,7 @@ class DocumentTest extends TestCase
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
}
#[TestDox('save() succeeds when a document is updated')]
public function testSaveSucceedsWhenADocumentIsUpdated(): void
{
Document::save(ThrowawayDb::TABLE, new TestDocument('two', num_value: 44));
@@ -292,6 +295,7 @@ class DocumentTest extends TestCase
$this->assertNull($doc->sub, 'The sub-document should have been null');
}
#[TestDox('update() succeeds when replacing a document')]
public function testUpdateSucceedsWhenReplacingADocument(): void
{
Document::update(ThrowawayDb::TABLE, 'one', new TestDocument('one', 'howdy', 8, new SubDocument('y', 'z')));
@@ -305,6 +309,7 @@ class DocumentTest extends TestCase
$this->assertEquals('z', $doc->sub->bar, 'The sub-document bar property was incorrect');
}
#[TestDox('update() succeeds when no document is replaced')]
public function testUpdateSucceedsWhenNoDocumentIsReplaced(): void
{
Document::update(ThrowawayDb::TABLE, 'two-hundred', new TestDocument('200'));

View File

@@ -33,38 +33,41 @@ class ExistsTest extends TestCase
parent::tearDown();
}
#[TestDox('By ID succeeds when a document exists')]
#[TestDox('byId() succeeds when a document exists')]
public function testByIdSucceedsWhenADocumentExists(): void
{
$this->assertTrue(Exists::byId(ThrowawayDb::TABLE, 'three'), 'There should have been an existing document');
}
#[TestDox('By ID succeeds when a document does not exist')]
#[TestDox('byId() succeeds when a document does not exist')]
public function testByIdSucceedsWhenADocumentDoesNotExist(): void
{
$this->assertFalse(Exists::byId(ThrowawayDb::TABLE, 'seven'),
'There should not have been an existing document');
}
#[TestDox('byFields() succeeds when documents exist')]
public function testByFieldsSucceedsWhenDocumentsExist(): void
{
$this->assertTrue(Exists::byFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 10)]),
$this->assertTrue(Exists::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 10)]),
'There should have been existing documents');
}
#[TestDox('byFields() succeeds when no matching documents exist')]
public function testByFieldsSucceedsWhenNoMatchingDocumentsExist(): void
{
$this->assertFalse(Exists::byFields(ThrowawayDb::TABLE, [Field::LT('nothing', 'none')]),
$this->assertFalse(Exists::byFields(ThrowawayDb::TABLE, [Field::less('nothing', 'none')]),
'There should not have been any existing documents');
}
#[TestDox('byContains() fails')]
public function testByContainsFails(): void
{
$this->expectException(DocumentException::class);
Exists::byContains('', []);
}
#[TestDox('By JSON Path fails')]
#[TestDox('byJsonPath() fails')]
public function testByJsonPathFails(): void
{
$this->expectException(DocumentException::class);

View File

@@ -8,9 +8,10 @@ declare(strict_types=1);
namespace Test\Integration\SQLite;
use BitBadger\PDODocument\{Custom, Document, DocumentException, Field, Find};
use BitBadger\PDODocument\{Custom, Delete, Document, DocumentException, Field, FieldMatch, Find};
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
use Test\Integration\ArrayDocument;
use Test\Integration\TestDocument;
/**
@@ -34,6 +35,7 @@ class FindTest extends TestCase
parent::tearDown();
}
#[TestDox('all() succeeds when there is data')]
public function testAllSucceedsWhenThereIsData(): void
{
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class);
@@ -43,6 +45,35 @@ class FindTest extends TestCase
$this->assertEquals(5, $count, 'There should have been 5 documents in the list');
}
#[TestDox('all() succeeds when ordering data ascending')]
public function testAllSucceedsWhenOrderingDataAscending(): void
{
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id')]);
$this->assertNotNull($docs, 'There should have been a document list returned');
$ids = iterator_to_array($docs->map(fn ($it) => $it->id), false);
$this->assertEquals(['five', 'four', 'one', 'three', 'two'], $ids, 'The documents were not ordered correctly');
}
#[TestDox('all() succeeds when ordering data descending')]
public function testAllSucceedsWhenOrderingDataDescending(): void
{
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id DESC')]);
$this->assertNotNull($docs, 'There should have been a document list returned');
$ids = iterator_to_array($docs->map(fn ($it) => $it->id), false);
$this->assertEquals(['two', 'three', 'one', 'four', 'five'], $ids, 'The documents were not ordered correctly');
}
#[TestDox('all() succeeds when ordering data numerically')]
public function testAllSucceedsWhenOrderingDataNumerically(): void
{
$docs = Find::all(ThrowawayDb::TABLE, TestDocument::class,
[Field::named('sub.foo NULLS LAST'), Field::named('n:num_value')]);
$this->assertNotNull($docs, 'There should have been a document list returned');
$ids = iterator_to_array($docs->map(fn ($it) => $it->id), false);
$this->assertEquals(['two', 'four', 'one', 'three', 'five'], $ids, 'The documents were not ordered correctly');
}
#[TestDox('all() succeeds when there is no data')]
public function testAllSucceedsWhenThereIsNoData(): void
{
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
@@ -51,7 +82,7 @@ class FindTest extends TestCase
$this->assertFalse($docs->hasItems(), 'There should have been no documents in the list');
}
#[TestDox('By ID succeeds when a document is found')]
#[TestDox('byId() succeeds when a document is found')]
public function testByIdSucceedsWhenADocumentIsFound(): void
{
$doc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class);
@@ -59,7 +90,7 @@ class FindTest extends TestCase
$this->assertEquals('two', $doc->get()->id, 'An incorrect document was returned');
}
#[TestDox('By ID succeeds when a document is found with numeric ID')]
#[TestDox('byId() succeeds when a document is found with numeric ID')]
public function testByIdSucceedsWhenADocumentIsFoundWithNumericId(): void
{
Document::insert(ThrowawayDb::TABLE, ['id' => 18, 'value' => 'howdy']);
@@ -68,69 +99,130 @@ class FindTest extends TestCase
$this->assertEquals('18', $doc->get()->id, 'An incorrect document was returned');
}
#[TestDox('By ID succeeds when a document is not found')]
#[TestDox('byId() succeeds when a document is not found')]
public function testByIdSucceedsWhenADocumentIsNotFound(): void
{
$doc = Find::byId(ThrowawayDb::TABLE, 'seventy-five', TestDocument::class);
$this->assertTrue($doc->isNone(), 'There should not have been a document returned');
}
#[TestDox('byFields() succeeds when documents are found')]
public function testByFieldsSucceedsWhenDocumentsAreFound(): void
{
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::GT('num_value', 15)], TestDocument::class);
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('value', ['blue', 'purple']), Field::exists('sub')],
TestDocument::class, FieldMatch::All);
$this->assertNotNull($docs, 'There should have been a document list returned');
$count = 0;
foreach ($docs->items() as $ignored) $count++;
$this->assertEquals(1, $count, 'There should have been 1 document in the list');
}
#[TestDox('byFields() succeeds when documents are found and ordered')]
public function testByFieldsSucceedsWhenDocumentsAreFoundAndOrdered(): void
{
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], TestDocument::class,
FieldMatch::All, [Field::named('id')]);
$this->assertNotNull($docs, 'There should have been a document list returned');
$ids = iterator_to_array($docs->map(fn ($it) => $it->id), false);
$this->assertEquals(['five', 'four'], $ids, 'The documents were not ordered correctly');
}
#[TestDox('byFields() succeeds when documents are found using IN with numeric field')]
public function testByFieldsSucceedsWhenDocumentsAreFoundUsingInWithNumericField(): void
{
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('num_value', [2, 4, 6, 8])], TestDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned');
$count = 0;
foreach ($docs->items() as $ignored) $count++;
$this->assertEquals(1, $count, 'There should have been 1 document in the list');
}
#[TestDox('byFields() succeeds when no documents are found')]
public function testByFieldsSucceedsWhenNoDocumentsAreFound(): void
{
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::greater('num_value', 100)], TestDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned');
$this->assertFalse($docs->hasItems(), 'There should have been no documents in the list');
}
#[TestDox('byFields() succeeds for inArray when matching documents exist')]
public function testByFieldsSucceedsForInArrayWhenMatchingDocumentsExist(): void
{
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);
$this->assertNotNull($docs, 'There should have been a document list returned');
$count = 0;
foreach ($docs->items() as $ignored) $count++;
$this->assertEquals(2, $count, 'There should have been 2 documents in the list');
}
public function testByFieldsSucceedsWhenNoDocumentsAreFound(): void
#[TestDox('byFields() succeeds for inArray when no matching documents exist')]
public function testByFieldsSucceedsForInArrayWhenNoMatchingDocumentsExist(): void
{
$docs = Find::byFields(ThrowawayDb::TABLE, [Field::GT('num_value', 100)], TestDocument::class);
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, ['j'])],
ArrayDocument::class);
$this->assertNotNull($docs, 'There should have been a document list returned');
$this->assertFalse($docs->hasItems(), 'There should have been no documents in the list');
}
#[TestDox('byContains() fails')]
public function testByContainsFails(): void
{
$this->expectException(DocumentException::class);
Find::byContains('', [], TestDocument::class);
}
#[TestDox('By JSON Path fails')]
#[TestDox('byJsonPath() fails')]
public function testByJsonPathFails(): void
{
$this->expectException(DocumentException::class);
Find::byJsonPath('', '', TestDocument::class);
}
#[TestDox('firstByFields() succeeds when a document is found')]
public function testFirstByFieldsSucceedsWhenADocumentIsFound(): void
{
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'another')], TestDocument::class);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'another')], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertEquals('two', $doc->get()->id, 'The incorrect document was returned');
}
#[TestDox('firstByFields() succeeds when multiple documents are found')]
public function testFirstByFieldsSucceedsWhenMultipleDocumentsAreFound(): void
{
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('sub.foo', 'green')], TestDocument::class);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertContains($doc->get()->id, ['two', 'four'], 'An incorrect document was returned');
}
#[TestDox('firstByFields() succeeds when multiple ordered documents are found')]
public function testFirstByFieldsSucceedsWhenMultipleOrderedDocumentsAreFound(): void
{
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class,
orderBy: [Field::named('n:num_value DESC')]);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertEquals('four', $doc->get()->id, 'The incorrect document was returned');
}
#[TestDox('firstByFields() succeeds when a document is not found')]
public function testFirstByFieldsSucceedsWhenADocumentIsNotFound(): void
{
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'absent')], TestDocument::class);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'absent')], TestDocument::class);
$this->assertTrue($doc->isNone(), 'There should not have been a document returned');
}
#[TestDox('firstByContains() fails')]
public function testFirstByContainsFails(): void
{
$this->expectException(DocumentException::class);
Find::firstByContains('', [], TestDocument::class);
}
#[TestDox('First by JSON Path fails')]
#[TestDox('firstByJsonPath() fails')]
public function testFirstByJsonPathFails(): void
{
$this->expectException(DocumentException::class);

View File

@@ -34,7 +34,7 @@ class PatchTest extends TestCase
parent::tearDown();
}
#[TestDox('By ID succeeds when a document is updated')]
#[TestDox('byId() succeeds when a document is updated')]
public function testByIdSucceedsWhenADocumentIsUpdated(): void
{
Patch::byId(ThrowawayDb::TABLE, 'one', ['num_value' => 44]);
@@ -43,33 +43,36 @@ class PatchTest extends TestCase
$this->assertEquals(44, $doc->get()->num_value, 'The updated document is not correct');
}
#[TestDox('By ID succeeds when no document is updated')]
#[TestDox('byId() succeeds when no document is updated')]
public function testByIdSucceedsWhenNoDocumentIsUpdated(): void
{
Patch::byId(ThrowawayDb::TABLE, 'forty-seven', ['foo' => 'green']);
$this->assertTrue(true, 'The above not throwing an exception is the test');
}
#[TestDox('byFields() succeeds when a document is updated')]
public function testByFieldsSucceedsWhenADocumentIsUpdated(): void
{
Patch::byFields(ThrowawayDb::TABLE, [Field::EQ('value', 'purple')], ['num_value' => 77]);
$after = Count::byFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 77)]);
Patch::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], ['num_value' => 77]);
$after = Count::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 77)]);
$this->assertEquals(2, $after, 'There should have been 2 documents updated');
}
#[TestDox('byFields() succeeds when no document is updated')]
public function testByFieldsSucceedsWhenNoDocumentIsUpdated(): void
{
Patch::byFields(ThrowawayDb::TABLE, [Field::EQ('value', 'burgundy')], ['foo' => 'green']);
Patch::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'burgundy')], ['foo' => 'green']);
$this->assertTrue(true, 'The above not throwing an exception is the test');
}
#[TestDox('byContains() fails')]
public function testByContainsFails(): void
{
$this->expectException(DocumentException::class);
Patch::byContains('', [], []);
}
#[TestDox('By JSON Path fails')]
#[TestDox('byJsonPath() fails')]
public function testByJsonPathFails(): void
{
$this->expectException(DocumentException::class);

View File

@@ -34,7 +34,7 @@ class RemoveFieldsTest extends TestCase
parent::tearDown();
}
#[TestDox('By ID succeeds when fields are removed')]
#[TestDox('byId() succeeds when fields are removed')]
public function testByIdSucceedsWhenFieldsAreRemoved(): void
{
RemoveFields::byId(ThrowawayDb::TABLE, 'two', ['sub', 'value']);
@@ -45,47 +45,51 @@ class RemoveFieldsTest extends TestCase
$this->assertNull($doc->sub, 'Sub-document should have been null');
}
#[TestDox('By ID succeeds when a field is not removed')]
#[TestDox('byId() succeeds when a field is not removed')]
public function testByIdSucceedsWhenAFieldIsNotRemoved(): void
{
RemoveFields::byId(ThrowawayDb::TABLE, 'one', ['a_field_that_does_not_exist']);
$this->assertTrue(true, 'The above not throwing an exception is the test');
}
#[TestDox('By ID succeeds when no document is matched')]
#[TestDox('byId() succeeds when no document is matched')]
public function testByIdSucceedsWhenNoDocumentIsMatched(): void
{
RemoveFields::byId(ThrowawayDb::TABLE, 'fifty', ['sub']);
$this->assertTrue(true, 'The above not throwing an exception is the test');
}
#[TestDox('byFields() succeeds when a field is removed')]
public function testByFieldsSucceedsWhenAFieldIsRemoved(): void
{
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 17)], ['sub']);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 17)], TestDocument::class);
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], ['sub']);
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], TestDocument::class);
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
$this->assertNull($doc->get()->sub, 'Sub-document should have been null');
}
#[TestDox('byFields() succeeds when a field is not removed')]
public function testByFieldsSucceedsWhenAFieldIsNotRemoved(): void
{
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 17)], ['nada']);
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], ['nada']);
$this->assertTrue(true, 'The above not throwing an exception is the test');
}
#[TestDox('byFields() succeeds when no document is matched')]
public function testByFieldsSucceedsWhenNoDocumentIsMatched(): void
{
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::NE('missing', 'nope')], ['value']);
RemoveFields::byFields(ThrowawayDb::TABLE, [Field::notEqual('missing', 'nope')], ['value']);
$this->assertTrue(true, 'The above not throwing an exception is the test');
}
#[TestDox('byContains() fails')]
public function testByContainsFails(): void
{
$this->expectException(DocumentException::class);
RemoveFields::byContains('', [], []);
}
#[TestDox('By JSON Path fails')]
#[TestDox('byJsonPath() fails')]
public function testByJsonPathFails(): void
{
$this->expectException(DocumentException::class);