diff --git a/composer.json b/composer.json index 140f868..5d6e1e5 100644 --- a/composer.json +++ b/composer.json @@ -39,9 +39,9 @@ "autoload-dev": { "psr-4": { "Test\\": "./tests", - "Test\\Integration\\": "./tests/integration", + "Test\\Integration\\": "./tests/Integration", "Test\\Integration\\PostgreSQL\\": "./tests/Integration/PostgreSQL", - "Test\\Integration\\SQLite\\": "./tests/integration/sqlite" + "Test\\Integration\\SQLite\\": "./tests/Integration/SQLite" } }, "archive": { diff --git a/tests/integration/ArrayDocument.php b/tests/Integration/ArrayDocument.php similarity index 100% rename from tests/integration/ArrayDocument.php rename to tests/Integration/ArrayDocument.php diff --git a/tests/integration/NumDocument.php b/tests/Integration/NumDocument.php similarity index 100% rename from tests/integration/NumDocument.php rename to tests/Integration/NumDocument.php diff --git a/tests/Integration/SQLite/DocumentTest.php b/tests/Integration/SQLite/DocumentTest.php index ac66802..5395409 100644 --- a/tests/Integration/SQLite/DocumentTest.php +++ b/tests/Integration/SQLite/DocumentTest.php @@ -6,23 +6,10 @@ declare(strict_types=1); -use BitBadger\PDODocument\{AutoId, - Configuration, - Count, - Custom, - Delete, - Document, - DocumentException, - DocumentList, - Exists, - Field, - Find, - Query}; -use BitBadger\PDODocument\Mapper\{ArrayMapper}; -use Test\Integration\NumDocument; +use BitBadger\PDODocument\{AutoId, Configuration, Custom, Document, DocumentException, Exists, Field, Find}; +use BitBadger\PDODocument\Mapper\ArrayMapper; +use Test\Integration\{NumDocument, SubDocument, TestDocument}; use Test\Integration\SQLite\ThrowawayDb; -use Test\Integration\SubDocument; -use Test\Integration\TestDocument; pest()->group('integration', 'sqlite'); diff --git a/tests/Integration/SQLite/ExistsTest.php b/tests/Integration/SQLite/ExistsTest.php new file mode 100644 index 0000000..3d04bd9 --- /dev/null +++ b/tests/Integration/SQLite/ExistsTest.php @@ -0,0 +1,42 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{DocumentException, Exists, Field}; +use Test\Integration\SQLite\ThrowawayDb; + +pest()->group('integration', 'sqlite'); + +describe('::byId()', function () { + test('returns true when a document exists', function () { + expect(Exists::byId(ThrowawayDb::TABLE, 'three'))->toBeTrue(); + }); + test('returns false when no document exists', function () { + expect(Exists::byId(ThrowawayDb::TABLE, 'seven'))->toBeFalse(); + }); +}); + +describe('::byFields()', function () { + test('returns true when matching documents exist', function () { + expect(Exists::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 10)]))->toBeTrue(); + }); + test('returns false when no matching documents exist', function () { + expect(Exists::byFields(ThrowawayDb::TABLE, [Field::less('nothing', 'none')]))->toBeFalse(); + }); +}); + +describe('::byContains()', function () { + test('throws an exception', function () { + expect(fn () => Exists::byContains('', []))->toThrow(DocumentException::class); + }); +}); + +describe('::byJsonPath()', function () { + test('throws an exception', function () { + expect(fn () => Exists::byJsonPath('', ''))->toThrow(DocumentException::class); + }); +}); diff --git a/tests/Integration/SQLite/FindTest.php b/tests/Integration/SQLite/FindTest.php new file mode 100644 index 0000000..0e5bda4 --- /dev/null +++ b/tests/Integration/SQLite/FindTest.php @@ -0,0 +1,151 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Custom, Delete, Document, DocumentException, Field, FieldMatch, Find}; +use Test\Integration\{ArrayDocument, TestDocument}; +use Test\Integration\SQLite\ThrowawayDb; + +pest()->group('integration', 'sqlite'); + +describe('::all()', function () { + test('retrieves data', function () { + $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class); + expect($docs)->not->toBeNull(); + $count = 0; + foreach ($docs->items() as $ignored) $count++; + expect($count)->toBe(5); + }); + test('sorts data ascending', function () { + $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id')]); + expect($docs)->not->toBeNull() + ->and(iterator_to_array($docs->map(fn ($it) => $it->id), false)) + ->toBe(['five', 'four', 'one', 'three', 'two']); + }); + test('sorts data descending', function () { + $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, [Field::named('id DESC')]); + expect($docs)->not->toBeNull() + ->and(iterator_to_array($docs->map(fn ($it) => $it->id), false)) + ->toBe(['two', 'three', 'one', 'four', 'five']); + }); + test('sorts data numerically', function () { + $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class, + [Field::named('sub.foo NULLS LAST'), Field::named('n:num_value')]); + expect($docs)->not->toBeNull() + ->and(iterator_to_array($docs->map(fn ($it) => $it->id), false)) + ->toBe(['two', 'four', 'one', 'three', 'five']); + }); + test('returns an empty list when no data exists', function () { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + expect(Find::all(ThrowawayDb::TABLE, TestDocument::class)) + ->not->toBeNull()->hasItems()->toBeFalse(); + }); +}); + +describe('::byId()', function () { + test('returns a document when it exists', function () { + expect(Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class)) + ->isSome()->toBeTrue()->get()->id->toBe('two'); + }); + test('returns a document with a numeric ID', function () { + Document::insert(ThrowawayDb::TABLE, ['id' => 18, 'value' => 'howdy']); + expect(Find::byId(ThrowawayDb::TABLE, 18, TestDocument::class)) + ->isSome()->toBeTrue()->get()->id->toBe('18'); + }); + test('returns None when no document exists', function () { + expect(Find::byId(ThrowawayDb::TABLE, 'seventy-five', TestDocument::class))->isNone()->toBeTrue(); + }); +}); + +describe('::byFields()', function () { + test('returns matching documents', function () { + $docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('value', ['blue', 'purple']), Field::exists('sub')], + TestDocument::class, FieldMatch::All); + expect($docs)->not->toBeNull(); + $count = 0; + foreach ($docs->items() as $ignored) $count++; + expect($count)->toBe(1); + }); + test('returns ordered matching documents', function () { + $docs = Find::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], TestDocument::class, + FieldMatch::All, [Field::named('id')]); + expect($docs)->not->toBeNull() + ->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))->toBe(['five', 'four']); + }); + test('returns documents matching numeric IN clause', function () { + $docs = Find::byFields(ThrowawayDb::TABLE, [Field::in('num_value', [2, 4, 6, 8])], TestDocument::class); + expect($docs)->not->toBeNull(); + $count = 0; + foreach ($docs->items() as $ignored) $count++; + expect($count)->toBe(1); + }); + test('returns empty list when no documents match', function () { + expect(Find::byFields(ThrowawayDb::TABLE, [Field::greater('num_value', 100)], TestDocument::class)) + ->not->toBeNull()->hasItems()->toBeFalse(); + }); + test('returns matching documents for inArray comparison', function () { + 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); + expect($docs)->not->toBeNull(); + $count = 0; + foreach ($docs->items() as $ignored) $count++; + expect($count)->toBe(2); + }); + test('returns empty list when no documents match inArray comparison', function () { + Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absentField')]); + foreach (ArrayDocument::testDocuments() as $doc) Document::insert(ThrowawayDb::TABLE, $doc); + expect(Find::byFields(ThrowawayDb::TABLE, [Field::inArray('values', ThrowawayDb::TABLE, ['j'])], + ArrayDocument::class)) + ->not->toBeNull()->hasItems()->toBeFalse(); + }); +}); + +describe('::byContains()', function () { + test('throws an exception', function () { + expect(fn () => Find::byContains('', [], TestDocument::class))->toThrow(DocumentException::class); + }); +}); + +describe('::byJsonPath()', function () { + test('throws an exception', function () { + expect(fn () => Find::byJsonPath('', '', TestDocument::class))->toThrow(DocumentException::class); + }); +}); + +describe('::firstByFields()', function () { + test('returns a matching document', function () { + expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'another')], TestDocument::class)) + ->isSome()->toBeTrue()->get()->id->toBe('two'); + }); + test('returns one of several matching documents', function () { + $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class); + expect($doc)->isSome()->toBeTrue()->and(['two', 'four'])->toContain($doc->get()->id); + }); + test('returns first of ordered matching documents', function () { + expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('sub.foo', 'green')], TestDocument::class, + orderBy: [Field::named('n:num_value DESC')])) + ->isSome()->toBeTrue()->get()->id->toBe('four'); + }); + test('returns None when no documents match', function () { + expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'absent')], TestDocument::class)) + ->isNone()->toBeTrue(); + }); +}); + +describe('::firstByContains()', function () { + test('throws an exception', function () { + expect(fn () => Find::firstByContains('', [], TestDocument::class))->toThrow(DocumentException::class); + }); +}); + +describe('::firstByJsonPath()', function () { + test('throws an exception', function () { + expect(fn () => Find::firstByJsonPath('', '', TestDocument::class))->toThrow(DocumentException::class); + }); +}); diff --git a/tests/Integration/SQLite/PatchTest.php b/tests/Integration/SQLite/PatchTest.php new file mode 100644 index 0000000..426e121 --- /dev/null +++ b/tests/Integration/SQLite/PatchTest.php @@ -0,0 +1,48 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Count, DocumentException, Exists, Field, Find, Patch}; +use Test\Integration\SQLite\ThrowawayDb; +use Test\Integration\TestDocument; + +pest()->group('integration', 'sqlite'); + +describe('::byId()', function () { + test('updates an existing document', function () { + Patch::byId(ThrowawayDb::TABLE, 'one', ['num_value' => 44]); + expect(Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class)) + ->isSome()->toBeTrue()->get()->num_value->toBe(44); + }); + test('does nothing when no document exists', function () { + expect(Exists::byId(ThrowawayDb::TABLE, 'forty-seven'))->toBeFalse(); + Patch::byId(ThrowawayDb::TABLE, 'forty-seven', ['foo' => 'green']); + }); +}); + +describe('::byFields()', function () { + test('updates matching documents', function () { + Patch::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], ['num_value' => 77]); + expect(Count::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 77)]))->toBe(2); + }); + test('does nothing when no documents match', function () { + expect(Exists::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'burgundy')]))->toBeFalse(); + Patch::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'burgundy')], ['foo' => 'green']); + }); +}); + +describe('::byContains()', function () { + test('throws an exception', function () { + expect(fn () => Patch::byContains('', [], []))->toThrow(DocumentException::class); + }); +}); + +describe('::byJsonPath()', function () { + test('throws an exception', function () { + expect(fn () => Patch::byJsonPath('', '', []))->toThrow(DocumentException::class); + }); +}); diff --git a/tests/Integration/SQLite/RemoveFieldsTest.php b/tests/Integration/SQLite/RemoveFieldsTest.php new file mode 100644 index 0000000..6e683d9 --- /dev/null +++ b/tests/Integration/SQLite/RemoveFieldsTest.php @@ -0,0 +1,59 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{DocumentException, Exists, Field, Find, RemoveFields}; +use Test\Integration\SQLite\ThrowawayDb; +use Test\Integration\TestDocument; + +pest()->group('integration', 'sqlite'); + +describe('::byId()', function () { + test('updates an existing document', function () { + RemoveFields::byId(ThrowawayDb::TABLE, 'two', ['sub', 'value']); + $tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class); + expect($tryDoc)->isSome()->toBeTrue() + ->and($tryDoc->get())->sub->toBeNull() + ->and($tryDoc->get()->value)->toBeEmpty(); + }); + test('does nothing when the field to remove does not exist', function () { + expect(Exists::byFields(ThrowawayDb::TABLE, [Field::exists('a_field_that_does_not_exist')]))->toBeFalse(); + RemoveFields::byId(ThrowawayDb::TABLE, 'one', ['a_field_that_does_not_exist']); + }); + test('does nothing when the document does not exist', function () { + expect(Exists::byId(ThrowawayDb::TABLE, 'fifty'))->toBeFalse(); + RemoveFields::byId(ThrowawayDb::TABLE, 'fifty', ['sub']); + }); +}); + +describe('::byFields()', function () { + test('updates matching documents', function () { + RemoveFields::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], ['sub']); + expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], TestDocument::class)) + ->isSome()->toBeTrue()->get()->sub->toBeNull(); + }); + test('does nothing when the field to remove does not exist', function () { + expect(Exists::byFields(ThrowawayDb::TABLE, [Field::exists('nada')]))->toBeFalse(); + RemoveFields::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 17)], ['nada']); + }); + test('does nothing when no documents match', function () { + expect(Exists::byFields(ThrowawayDb::TABLE, [Field::notEqual('missing', 'nope')]))->toBeFalse(); + RemoveFields::byFields(ThrowawayDb::TABLE, [Field::notEqual('missing', 'nope')], ['value']); + }); +}); + +describe('::byContains()', function () { + test('throws an exception', function () { + expect(fn () => RemoveFields::byContains('', [], []))->toThrow(DocumentException::class); + }); +}); + +describe('::byJsonPath()', function () { + test('throws an exception', function () { + expect(fn () => RemoveFields::byJsonPath('', '', []))->toThrow(DocumentException::class); + }); +}); diff --git a/tests/integration/sqlite/ThrowawayDb.php b/tests/Integration/SQLite/ThrowawayDb.php similarity index 100% rename from tests/integration/sqlite/ThrowawayDb.php rename to tests/Integration/SQLite/ThrowawayDb.php diff --git a/tests/integration/SubDocument.php b/tests/Integration/SubDocument.php similarity index 100% rename from tests/integration/SubDocument.php rename to tests/Integration/SubDocument.php diff --git a/tests/integration/TestDocument.php b/tests/Integration/TestDocument.php similarity index 100% rename from tests/integration/TestDocument.php rename to tests/Integration/TestDocument.php diff --git a/tests/integration/sqlite/ExistsTest.php b/tests/integration/sqlite/ExistsTest.php deleted file mode 100644 index 3d99d65..0000000 --- a/tests/integration/sqlite/ExistsTest.php +++ /dev/null @@ -1,77 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Integration\SQLite; - -use BitBadger\PDODocument\{DocumentException, Exists, Field}; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * SQLite integration tests for the Exists class - */ -#[TestDox('Exists (SQLite integration)')] -class ExistsTest extends TestCase -{ - /** @var string Database name for throwaway database */ - private string $dbName; - - protected function setUp(): void - { - parent::setUp(); - $this->dbName = ThrowawayDb::create(); - } - - protected function tearDown(): void - { - ThrowawayDb::destroy($this->dbName); - parent::tearDown(); - } - - #[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('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::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::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('byJsonPath() fails')] - public function testByJsonPathFails(): void - { - $this->expectException(DocumentException::class); - Exists::byJsonPath('', ''); - } -} - diff --git a/tests/integration/sqlite/FindTest.php b/tests/integration/sqlite/FindTest.php deleted file mode 100644 index 8f7a42d..0000000 --- a/tests/integration/sqlite/FindTest.php +++ /dev/null @@ -1,231 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Integration\SQLite; - -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; - -/** - * SQLite integration tests for the Find class - */ -#[TestDox('Find (SQLite integration)')] -class FindTest extends TestCase -{ - /** @var string Database name for throwaway database */ - private string $dbName; - - protected function setUp(): void - { - parent::setUp(); - $this->dbName = ThrowawayDb::create(); - } - - protected function tearDown(): void - { - ThrowawayDb::destroy($this->dbName); - parent::tearDown(); - } - - #[TestDox('all() succeeds when there is data')] - public function testAllSucceedsWhenThereIsData(): void - { - $docs = Find::all(ThrowawayDb::TABLE, TestDocument::class); - $this->assertNotNull($docs, 'There should have been a document list returned'); - $count = 0; - foreach ($docs->items() as $ignored) $count++; - $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, []); - $docs = Find::all(ThrowawayDb::TABLE, 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('byId() succeeds when a document is found')] - public function testByIdSucceedsWhenADocumentIsFound(): void - { - $doc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class); - $this->assertTrue($doc->isSome(), 'There should have been a document returned'); - $this->assertEquals('two', $doc->get()->id, 'An incorrect document was returned'); - } - - #[TestDox('byId() succeeds when a document is found with numeric ID')] - public function testByIdSucceedsWhenADocumentIsFoundWithNumericId(): void - { - Document::insert(ThrowawayDb::TABLE, ['id' => 18, 'value' => 'howdy']); - $doc = Find::byId(ThrowawayDb::TABLE, 18, TestDocument::class); - $this->assertTrue($doc->isSome(), 'There should have been a document returned'); - $this->assertEquals('18', $doc->get()->id, 'An incorrect document was returned'); - } - - #[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::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'); - } - - #[TestDox('byFields() succeeds for inArray when no matching documents exist')] - public function testByFieldsSucceedsForInArrayWhenNoMatchingDocumentsExist(): 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, ['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('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::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::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::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('firstByJsonPath() fails')] - public function testFirstByJsonPathFails(): void - { - $this->expectException(DocumentException::class); - Find::firstByJsonPath('', '', TestDocument::class); - } -} diff --git a/tests/integration/sqlite/PatchTest.php b/tests/integration/sqlite/PatchTest.php deleted file mode 100644 index 0a31936..0000000 --- a/tests/integration/sqlite/PatchTest.php +++ /dev/null @@ -1,81 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Integration\SQLite; - -use BitBadger\PDODocument\{Count, DocumentException, Field, Find, Patch}; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; -use Test\Integration\TestDocument; - -/** - * SQLite integration tests for the Patch class - */ -#[TestDox('Patch (SQLite integration)')] -class PatchTest extends TestCase -{ - /** @var string Database name for throwaway database */ - private string $dbName; - - protected function setUp(): void - { - parent::setUp(); - $this->dbName = ThrowawayDb::create(); - } - - protected function tearDown(): void - { - ThrowawayDb::destroy($this->dbName); - parent::tearDown(); - } - - #[TestDox('byId() succeeds when a document is updated')] - public function testByIdSucceedsWhenADocumentIsUpdated(): void - { - Patch::byId(ThrowawayDb::TABLE, 'one', ['num_value' => 44]); - $doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class); - $this->assertTrue($doc->isSome(), 'There should have been a document returned'); - $this->assertEquals(44, $doc->get()->num_value, 'The updated document is not correct'); - } - - #[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::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::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('byJsonPath() fails')] - public function testByJsonPathFails(): void - { - $this->expectException(DocumentException::class); - Patch::byJsonPath('', '', []); - } -} diff --git a/tests/integration/sqlite/RemoveFieldsTest.php b/tests/integration/sqlite/RemoveFieldsTest.php deleted file mode 100644 index a0c46d8..0000000 --- a/tests/integration/sqlite/RemoveFieldsTest.php +++ /dev/null @@ -1,98 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Integration\SQLite; - -use BitBadger\PDODocument\{DocumentException, Field, Find, RemoveFields}; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; -use Test\Integration\TestDocument; - -/** - * SQLite integration tests for the RemoveFields class - */ -#[TestDox('Remove Fields (SQLite integration)')] -class RemoveFieldsTest extends TestCase -{ - /** @var string Database name for throwaway database */ - private string $dbName; - - protected function setUp(): void - { - parent::setUp(); - $this->dbName = ThrowawayDb::create(); - } - - protected function tearDown(): void - { - ThrowawayDb::destroy($this->dbName); - parent::tearDown(); - } - - #[TestDox('byId() succeeds when fields are removed')] - public function testByIdSucceedsWhenFieldsAreRemoved(): void - { - RemoveFields::byId(ThrowawayDb::TABLE, 'two', ['sub', 'value']); - $tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class); - $this->assertTrue($tryDoc->isSome(), 'There should have been a document returned'); - $doc = $tryDoc->get(); - $this->assertEquals('', $doc->value, 'Value should have been blank (its default value)'); - $this->assertNull($doc->sub, 'Sub-document should have been null'); - } - - #[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('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::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::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::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('byJsonPath() fails')] - public function testByJsonPathFails(): void - { - $this->expectException(DocumentException::class); - RemoveFields::byJsonPath('', '', []); - } -}