diff --git a/composer.json b/composer.json index f25554f..5d6e1e5 100644 --- a/composer.json +++ b/composer.json @@ -25,9 +25,9 @@ "ext-pdo": "*" }, "require-dev": { - "phpunit/phpunit": "^11", "square/pjson": "^0.5.0", - "phpstan/phpstan": "^1.12" + "phpstan/phpstan": "^1.12", + "pestphp/pest": "^3.2" }, "autoload": { "psr-4": { @@ -39,13 +39,17 @@ "autoload-dev": { "psr-4": { "Test\\": "./tests", - "Test\\Unit\\": "./tests/unit", - "Test\\Integration\\": "./tests/integration", - "Test\\Integration\\PostgreSQL\\": "./tests/integration/postgresql", - "Test\\Integration\\SQLite\\": "./tests/integration/sqlite" + "Test\\Integration\\": "./tests/Integration", + "Test\\Integration\\PostgreSQL\\": "./tests/Integration/PostgreSQL", + "Test\\Integration\\SQLite\\": "./tests/Integration/SQLite" } }, "archive": { "exclude": [ "/tests", "/.gitattributes", "/.gitignore", "/.git", "/composer.lock" ] + }, + "config": { + "allow-plugins": { + "pestphp/pest-plugin": true + } } } diff --git a/composer.lock b/composer.lock index 75a8b3a..32331b9 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "ec1fce1531c57de8cb1300e3d1cf2a83", + "content-hash": "a2c56fcb72f4bb062370f85b100f3d31", "packages": [ { "name": "bit-badger/inspired-by-fsharp", @@ -105,17 +105,348 @@ ], "packages-dev": [ { - "name": "myclabs/deep-copy", - "version": "1.12.0", + "name": "brianium/paratest", + "version": "v7.6.0", "source": { "type": "git", - "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c" + "url": "https://github.com/paratestphp/paratest.git", + "reference": "68ff89a8de47d086588e391a516d2a5b5fde6254" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c", - "reference": "3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c", + "url": "https://api.github.com/repos/paratestphp/paratest/zipball/68ff89a8de47d086588e391a516d2a5b5fde6254", + "reference": "68ff89a8de47d086588e391a516d2a5b5fde6254", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-pcre": "*", + "ext-reflection": "*", + "ext-simplexml": "*", + "fidry/cpu-core-counter": "^1.2.0", + "jean85/pretty-package-versions": "^2.0.6", + "php": "~8.2.0 || ~8.3.0 || ~8.4.0", + "phpunit/php-code-coverage": "^11.0.7", + "phpunit/php-file-iterator": "^5.1.0", + "phpunit/php-timer": "^7.0.1", + "phpunit/phpunit": "^11.4.1", + "sebastian/environment": "^7.2.0", + "symfony/console": "^6.4.11 || ^7.1.5", + "symfony/process": "^6.4.8 || ^7.1.5" + }, + "require-dev": { + "doctrine/coding-standard": "^12.0.0", + "ext-pcov": "*", + "ext-posix": "*", + "phpstan/phpstan": "^1.12.6", + "phpstan/phpstan-deprecation-rules": "^1.2.1", + "phpstan/phpstan-phpunit": "^1.4.0", + "phpstan/phpstan-strict-rules": "^1.6.1", + "squizlabs/php_codesniffer": "^3.10.3", + "symfony/filesystem": "^6.4.9 || ^7.1.5" + }, + "bin": [ + "bin/paratest", + "bin/paratest_for_phpstorm" + ], + "type": "library", + "autoload": { + "psr-4": { + "ParaTest\\": [ + "src/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Brian Scaturro", + "email": "scaturrob@gmail.com", + "role": "Developer" + }, + { + "name": "Filippo Tessarotto", + "email": "zoeslam@gmail.com", + "role": "Developer" + } + ], + "description": "Parallel testing for PHP", + "homepage": "https://github.com/paratestphp/paratest", + "keywords": [ + "concurrent", + "parallel", + "phpunit", + "testing" + ], + "support": { + "issues": "https://github.com/paratestphp/paratest/issues", + "source": "https://github.com/paratestphp/paratest/tree/v7.6.0" + }, + "funding": [ + { + "url": "https://github.com/sponsors/Slamdunk", + "type": "github" + }, + { + "url": "https://paypal.me/filippotessarotto", + "type": "paypal" + } + ], + "time": "2024-10-15T12:38:31+00:00" + }, + { + "name": "doctrine/deprecations", + "version": "1.1.3", + "source": { + "type": "git", + "url": "https://github.com/doctrine/deprecations.git", + "reference": "dfbaa3c2d2e9a9df1118213f3b8b0c597bb99fab" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/deprecations/zipball/dfbaa3c2d2e9a9df1118213f3b8b0c597bb99fab", + "reference": "dfbaa3c2d2e9a9df1118213f3b8b0c597bb99fab", + "shasum": "" + }, + "require": { + "php": "^7.1 || ^8.0" + }, + "require-dev": { + "doctrine/coding-standard": "^9", + "phpstan/phpstan": "1.4.10 || 1.10.15", + "phpstan/phpstan-phpunit": "^1.0", + "phpunit/phpunit": "^7.5 || ^8.5 || ^9.5", + "psalm/plugin-phpunit": "0.18.4", + "psr/log": "^1 || ^2 || ^3", + "vimeo/psalm": "4.30.0 || 5.12.0" + }, + "suggest": { + "psr/log": "Allows logging deprecations via PSR-3 logger implementation" + }, + "type": "library", + "autoload": { + "psr-4": { + "Doctrine\\Deprecations\\": "lib/Doctrine/Deprecations" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "A small layer on top of trigger_error(E_USER_DEPRECATED) or PSR-3 logging with options to disable all deprecations or selectively for packages.", + "homepage": "https://www.doctrine-project.org/", + "support": { + "issues": "https://github.com/doctrine/deprecations/issues", + "source": "https://github.com/doctrine/deprecations/tree/1.1.3" + }, + "time": "2024-01-30T19:34:25+00:00" + }, + { + "name": "fidry/cpu-core-counter", + "version": "1.2.0", + "source": { + "type": "git", + "url": "https://github.com/theofidry/cpu-core-counter.git", + "reference": "8520451a140d3f46ac33042715115e290cf5785f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/theofidry/cpu-core-counter/zipball/8520451a140d3f46ac33042715115e290cf5785f", + "reference": "8520451a140d3f46ac33042715115e290cf5785f", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0" + }, + "require-dev": { + "fidry/makefile": "^0.2.0", + "fidry/php-cs-fixer-config": "^1.1.2", + "phpstan/extension-installer": "^1.2.0", + "phpstan/phpstan": "^1.9.2", + "phpstan/phpstan-deprecation-rules": "^1.0.0", + "phpstan/phpstan-phpunit": "^1.2.2", + "phpstan/phpstan-strict-rules": "^1.4.4", + "phpunit/phpunit": "^8.5.31 || ^9.5.26", + "webmozarts/strict-phpunit": "^7.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "Fidry\\CpuCoreCounter\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Théo FIDRY", + "email": "theo.fidry@gmail.com" + } + ], + "description": "Tiny utility to get the number of CPU cores.", + "keywords": [ + "CPU", + "core" + ], + "support": { + "issues": "https://github.com/theofidry/cpu-core-counter/issues", + "source": "https://github.com/theofidry/cpu-core-counter/tree/1.2.0" + }, + "funding": [ + { + "url": "https://github.com/theofidry", + "type": "github" + } + ], + "time": "2024-08-06T10:04:20+00:00" + }, + { + "name": "filp/whoops", + "version": "2.16.0", + "source": { + "type": "git", + "url": "https://github.com/filp/whoops.git", + "reference": "befcdc0e5dce67252aa6322d82424be928214fa2" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/filp/whoops/zipball/befcdc0e5dce67252aa6322d82424be928214fa2", + "reference": "befcdc0e5dce67252aa6322d82424be928214fa2", + "shasum": "" + }, + "require": { + "php": "^7.1 || ^8.0", + "psr/log": "^1.0.1 || ^2.0 || ^3.0" + }, + "require-dev": { + "mockery/mockery": "^1.0", + "phpunit/phpunit": "^7.5.20 || ^8.5.8 || ^9.3.3", + "symfony/var-dumper": "^4.0 || ^5.0" + }, + "suggest": { + "symfony/var-dumper": "Pretty print complex values better with var-dumper available", + "whoops/soap": "Formats errors as SOAP responses" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.7-dev" + } + }, + "autoload": { + "psr-4": { + "Whoops\\": "src/Whoops/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Filipe Dobreira", + "homepage": "https://github.com/filp", + "role": "Developer" + } + ], + "description": "php error handling for cool kids", + "homepage": "https://filp.github.io/whoops/", + "keywords": [ + "error", + "exception", + "handling", + "library", + "throwable", + "whoops" + ], + "support": { + "issues": "https://github.com/filp/whoops/issues", + "source": "https://github.com/filp/whoops/tree/2.16.0" + }, + "funding": [ + { + "url": "https://github.com/denis-sokolov", + "type": "github" + } + ], + "time": "2024-09-25T12:00:00+00:00" + }, + { + "name": "jean85/pretty-package-versions", + "version": "2.0.6", + "source": { + "type": "git", + "url": "https://github.com/Jean85/pretty-package-versions.git", + "reference": "f9fdd29ad8e6d024f52678b570e5593759b550b4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Jean85/pretty-package-versions/zipball/f9fdd29ad8e6d024f52678b570e5593759b550b4", + "reference": "f9fdd29ad8e6d024f52678b570e5593759b550b4", + "shasum": "" + }, + "require": { + "composer-runtime-api": "^2.0.0", + "php": "^7.1|^8.0" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^3.2", + "jean85/composer-provided-replaced-stub-package": "^1.0", + "phpstan/phpstan": "^1.4", + "phpunit/phpunit": "^7.5|^8.5|^9.4", + "vimeo/psalm": "^4.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.x-dev" + } + }, + "autoload": { + "psr-4": { + "Jean85\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Alessandro Lai", + "email": "alessandro.lai85@gmail.com" + } + ], + "description": "A library to get pretty versions strings of installed dependencies", + "keywords": [ + "composer", + "package", + "release", + "versions" + ], + "support": { + "issues": "https://github.com/Jean85/pretty-package-versions/issues", + "source": "https://github.com/Jean85/pretty-package-versions/tree/2.0.6" + }, + "time": "2024-03-08T09:58:59+00:00" + }, + { + "name": "myclabs/deep-copy", + "version": "1.12.1", + "source": { + "type": "git", + "url": "https://github.com/myclabs/DeepCopy.git", + "reference": "123267b2c49fbf30d78a7b2d333f6be754b94845" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/123267b2c49fbf30d78a7b2d333f6be754b94845", + "reference": "123267b2c49fbf30d78a7b2d333f6be754b94845", "shasum": "" }, "require": { @@ -154,7 +485,7 @@ ], "support": { "issues": "https://github.com/myclabs/DeepCopy/issues", - "source": "https://github.com/myclabs/DeepCopy/tree/1.12.0" + "source": "https://github.com/myclabs/DeepCopy/tree/1.12.1" }, "funding": [ { @@ -162,20 +493,20 @@ "type": "tidelift" } ], - "time": "2024-06-12T14:39:25+00:00" + "time": "2024-11-08T17:47:46+00:00" }, { "name": "nikic/php-parser", - "version": "v5.3.0", + "version": "v5.3.1", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "3abf7425cd284141dc5d8d14a9ee444de3345d1a" + "reference": "8eea230464783aa9671db8eea6f8c6ac5285794b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/3abf7425cd284141dc5d8d14a9ee444de3345d1a", - "reference": "3abf7425cd284141dc5d8d14a9ee444de3345d1a", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/8eea230464783aa9671db8eea6f8c6ac5285794b", + "reference": "8eea230464783aa9671db8eea6f8c6ac5285794b", "shasum": "" }, "require": { @@ -218,9 +549,517 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v5.3.0" + "source": "https://github.com/nikic/PHP-Parser/tree/v5.3.1" }, - "time": "2024-09-29T13:56:26+00:00" + "time": "2024-10-08T18:51:32+00:00" + }, + { + "name": "nunomaduro/collision", + "version": "v8.5.0", + "source": { + "type": "git", + "url": "https://github.com/nunomaduro/collision.git", + "reference": "f5c101b929c958e849a633283adff296ed5f38f5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/nunomaduro/collision/zipball/f5c101b929c958e849a633283adff296ed5f38f5", + "reference": "f5c101b929c958e849a633283adff296ed5f38f5", + "shasum": "" + }, + "require": { + "filp/whoops": "^2.16.0", + "nunomaduro/termwind": "^2.1.0", + "php": "^8.2.0", + "symfony/console": "^7.1.5" + }, + "conflict": { + "laravel/framework": "<11.0.0 || >=12.0.0", + "phpunit/phpunit": "<10.5.1 || >=12.0.0" + }, + "require-dev": { + "larastan/larastan": "^2.9.8", + "laravel/framework": "^11.28.0", + "laravel/pint": "^1.18.1", + "laravel/sail": "^1.36.0", + "laravel/sanctum": "^4.0.3", + "laravel/tinker": "^2.10.0", + "orchestra/testbench-core": "^9.5.3", + "pestphp/pest": "^2.36.0 || ^3.4.0", + "sebastian/environment": "^6.1.0 || ^7.2.0" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "NunoMaduro\\Collision\\Adapters\\Laravel\\CollisionServiceProvider" + ] + }, + "branch-alias": { + "dev-8.x": "8.x-dev" + } + }, + "autoload": { + "files": [ + "./src/Adapters/Phpunit/Autoload.php" + ], + "psr-4": { + "NunoMaduro\\Collision\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nuno Maduro", + "email": "enunomaduro@gmail.com" + } + ], + "description": "Cli error handling for console/command-line PHP applications.", + "keywords": [ + "artisan", + "cli", + "command-line", + "console", + "error", + "handling", + "laravel", + "laravel-zero", + "php", + "symfony" + ], + "support": { + "issues": "https://github.com/nunomaduro/collision/issues", + "source": "https://github.com/nunomaduro/collision" + }, + "funding": [ + { + "url": "https://www.paypal.com/paypalme/enunomaduro", + "type": "custom" + }, + { + "url": "https://github.com/nunomaduro", + "type": "github" + }, + { + "url": "https://www.patreon.com/nunomaduro", + "type": "patreon" + } + ], + "time": "2024-10-15T16:06:32+00:00" + }, + { + "name": "nunomaduro/termwind", + "version": "v2.2.0", + "source": { + "type": "git", + "url": "https://github.com/nunomaduro/termwind.git", + "reference": "42c84e4e8090766bbd6445d06cd6e57650626ea3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/nunomaduro/termwind/zipball/42c84e4e8090766bbd6445d06cd6e57650626ea3", + "reference": "42c84e4e8090766bbd6445d06cd6e57650626ea3", + "shasum": "" + }, + "require": { + "ext-mbstring": "*", + "php": "^8.2", + "symfony/console": "^7.1.5" + }, + "require-dev": { + "illuminate/console": "^11.28.0", + "laravel/pint": "^1.18.1", + "mockery/mockery": "^1.6.12", + "pestphp/pest": "^2.36.0", + "phpstan/phpstan": "^1.12.6", + "phpstan/phpstan-strict-rules": "^1.6.1", + "symfony/var-dumper": "^7.1.5", + "thecodingmachine/phpstan-strict-rules": "^1.0.0" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Termwind\\Laravel\\TermwindServiceProvider" + ] + }, + "branch-alias": { + "dev-2.x": "2.x-dev" + } + }, + "autoload": { + "files": [ + "src/Functions.php" + ], + "psr-4": { + "Termwind\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nuno Maduro", + "email": "enunomaduro@gmail.com" + } + ], + "description": "Its like Tailwind CSS, but for the console.", + "keywords": [ + "cli", + "console", + "css", + "package", + "php", + "style" + ], + "support": { + "issues": "https://github.com/nunomaduro/termwind/issues", + "source": "https://github.com/nunomaduro/termwind/tree/v2.2.0" + }, + "funding": [ + { + "url": "https://www.paypal.com/paypalme/enunomaduro", + "type": "custom" + }, + { + "url": "https://github.com/nunomaduro", + "type": "github" + }, + { + "url": "https://github.com/xiCO2k", + "type": "github" + } + ], + "time": "2024-10-15T16:15:16+00:00" + }, + { + "name": "pestphp/pest", + "version": "v3.5.1", + "source": { + "type": "git", + "url": "https://github.com/pestphp/pest.git", + "reference": "179d46ce97d52bcb3f791449ae94025c3f32e3e3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/pestphp/pest/zipball/179d46ce97d52bcb3f791449ae94025c3f32e3e3", + "reference": "179d46ce97d52bcb3f791449ae94025c3f32e3e3", + "shasum": "" + }, + "require": { + "brianium/paratest": "^7.6.0", + "nunomaduro/collision": "^8.5.0", + "nunomaduro/termwind": "^2.2.0", + "pestphp/pest-plugin": "^3.0.0", + "pestphp/pest-plugin-arch": "^3.0.0", + "pestphp/pest-plugin-mutate": "^3.0.5", + "php": "^8.2.0", + "phpunit/phpunit": "^11.4.3" + }, + "conflict": { + "filp/whoops": "<2.16.0", + "phpunit/phpunit": ">11.4.3", + "sebastian/exporter": "<6.0.0", + "webmozart/assert": "<1.11.0" + }, + "require-dev": { + "pestphp/pest-dev-tools": "^3.3.0", + "pestphp/pest-plugin-type-coverage": "^3.1.0", + "symfony/process": "^7.1.6" + }, + "bin": [ + "bin/pest" + ], + "type": "library", + "extra": { + "pest": { + "plugins": [ + "Pest\\Mutate\\Plugins\\Mutate", + "Pest\\Plugins\\Configuration", + "Pest\\Plugins\\Bail", + "Pest\\Plugins\\Cache", + "Pest\\Plugins\\Coverage", + "Pest\\Plugins\\Init", + "Pest\\Plugins\\Environment", + "Pest\\Plugins\\Help", + "Pest\\Plugins\\Memory", + "Pest\\Plugins\\Only", + "Pest\\Plugins\\Printer", + "Pest\\Plugins\\ProcessIsolation", + "Pest\\Plugins\\Profile", + "Pest\\Plugins\\Retry", + "Pest\\Plugins\\Snapshot", + "Pest\\Plugins\\Verbose", + "Pest\\Plugins\\Version", + "Pest\\Plugins\\Parallel" + ] + }, + "phpstan": { + "includes": [ + "extension.neon" + ] + } + }, + "autoload": { + "files": [ + "src/Functions.php", + "src/Pest.php" + ], + "psr-4": { + "Pest\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nuno Maduro", + "email": "enunomaduro@gmail.com" + } + ], + "description": "The elegant PHP Testing Framework.", + "keywords": [ + "framework", + "pest", + "php", + "test", + "testing", + "unit" + ], + "support": { + "issues": "https://github.com/pestphp/pest/issues", + "source": "https://github.com/pestphp/pest/tree/v3.5.1" + }, + "funding": [ + { + "url": "https://www.paypal.com/paypalme/enunomaduro", + "type": "custom" + }, + { + "url": "https://github.com/nunomaduro", + "type": "github" + } + ], + "time": "2024-10-31T16:12:45+00:00" + }, + { + "name": "pestphp/pest-plugin", + "version": "v3.0.0", + "source": { + "type": "git", + "url": "https://github.com/pestphp/pest-plugin.git", + "reference": "e79b26c65bc11c41093b10150c1341cc5cdbea83" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/pestphp/pest-plugin/zipball/e79b26c65bc11c41093b10150c1341cc5cdbea83", + "reference": "e79b26c65bc11c41093b10150c1341cc5cdbea83", + "shasum": "" + }, + "require": { + "composer-plugin-api": "^2.0.0", + "composer-runtime-api": "^2.2.2", + "php": "^8.2" + }, + "conflict": { + "pestphp/pest": "<3.0.0" + }, + "require-dev": { + "composer/composer": "^2.7.9", + "pestphp/pest": "^3.0.0", + "pestphp/pest-dev-tools": "^3.0.0" + }, + "type": "composer-plugin", + "extra": { + "class": "Pest\\Plugin\\Manager" + }, + "autoload": { + "psr-4": { + "Pest\\Plugin\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "The Pest plugin manager", + "keywords": [ + "framework", + "manager", + "pest", + "php", + "plugin", + "test", + "testing", + "unit" + ], + "support": { + "source": "https://github.com/pestphp/pest-plugin/tree/v3.0.0" + }, + "funding": [ + { + "url": "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=66BYDWAT92N6L", + "type": "custom" + }, + { + "url": "https://github.com/nunomaduro", + "type": "github" + }, + { + "url": "https://www.patreon.com/nunomaduro", + "type": "patreon" + } + ], + "time": "2024-09-08T23:21:41+00:00" + }, + { + "name": "pestphp/pest-plugin-arch", + "version": "v3.0.0", + "source": { + "type": "git", + "url": "https://github.com/pestphp/pest-plugin-arch.git", + "reference": "0a27e55a270cfe73d8cb70551b91002ee2cb64b0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/pestphp/pest-plugin-arch/zipball/0a27e55a270cfe73d8cb70551b91002ee2cb64b0", + "reference": "0a27e55a270cfe73d8cb70551b91002ee2cb64b0", + "shasum": "" + }, + "require": { + "pestphp/pest-plugin": "^3.0.0", + "php": "^8.2", + "ta-tikoma/phpunit-architecture-test": "^0.8.4" + }, + "require-dev": { + "pestphp/pest": "^3.0.0", + "pestphp/pest-dev-tools": "^3.0.0" + }, + "type": "library", + "extra": { + "pest": { + "plugins": [ + "Pest\\Arch\\Plugin" + ] + } + }, + "autoload": { + "files": [ + "src/Autoload.php" + ], + "psr-4": { + "Pest\\Arch\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "The Arch plugin for Pest PHP.", + "keywords": [ + "arch", + "architecture", + "framework", + "pest", + "php", + "plugin", + "test", + "testing", + "unit" + ], + "support": { + "source": "https://github.com/pestphp/pest-plugin-arch/tree/v3.0.0" + }, + "funding": [ + { + "url": "https://www.paypal.com/paypalme/enunomaduro", + "type": "custom" + }, + { + "url": "https://github.com/nunomaduro", + "type": "github" + } + ], + "time": "2024-09-08T23:23:55+00:00" + }, + { + "name": "pestphp/pest-plugin-mutate", + "version": "v3.0.5", + "source": { + "type": "git", + "url": "https://github.com/pestphp/pest-plugin-mutate.git", + "reference": "e10dbdc98c9e2f3890095b4fe2144f63a5717e08" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/pestphp/pest-plugin-mutate/zipball/e10dbdc98c9e2f3890095b4fe2144f63a5717e08", + "reference": "e10dbdc98c9e2f3890095b4fe2144f63a5717e08", + "shasum": "" + }, + "require": { + "nikic/php-parser": "^5.2.0", + "pestphp/pest-plugin": "^3.0.0", + "php": "^8.2", + "psr/simple-cache": "^3.0.0" + }, + "require-dev": { + "pestphp/pest": "^3.0.8", + "pestphp/pest-dev-tools": "^3.0.0", + "pestphp/pest-plugin-type-coverage": "^3.0.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Pest\\Mutate\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Sandro Gehri", + "email": "sandrogehri@gmail.com" + } + ], + "description": "Mutates your code to find untested cases", + "keywords": [ + "framework", + "mutate", + "mutation", + "pest", + "php", + "plugin", + "test", + "testing", + "unit" + ], + "support": { + "source": "https://github.com/pestphp/pest-plugin-mutate/tree/v3.0.5" + }, + "funding": [ + { + "url": "https://www.paypal.com/paypalme/enunomaduro", + "type": "custom" + }, + { + "url": "https://github.com/gehrisandro", + "type": "github" + }, + { + "url": "https://github.com/nunomaduro", + "type": "github" + } + ], + "time": "2024-09-22T07:54:40+00:00" }, { "name": "phar-io/manifest", @@ -341,17 +1180,239 @@ "time": "2022-02-21T01:04:05+00:00" }, { - "name": "phpstan/phpstan", - "version": "1.12.5", + "name": "phpdocumentor/reflection-common", + "version": "2.2.0", "source": { "type": "git", - "url": "https://github.com/phpstan/phpstan.git", - "reference": "7e6c6cb7cecb0a6254009a1a8a7d54ec99812b17" + "url": "https://github.com/phpDocumentor/ReflectionCommon.git", + "reference": "1d01c49d4ed62f25aa84a747ad35d5a16924662b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/7e6c6cb7cecb0a6254009a1a8a7d54ec99812b17", - "reference": "7e6c6cb7cecb0a6254009a1a8a7d54ec99812b17", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/1d01c49d4ed62f25aa84a747ad35d5a16924662b", + "reference": "1d01c49d4ed62f25aa84a747ad35d5a16924662b", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-2.x": "2.x-dev" + } + }, + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jaap van Otterdijk", + "email": "opensource@ijaap.nl" + } + ], + "description": "Common reflection classes used by phpdocumentor to reflect the code structure", + "homepage": "http://www.phpdoc.org", + "keywords": [ + "FQSEN", + "phpDocumentor", + "phpdoc", + "reflection", + "static analysis" + ], + "support": { + "issues": "https://github.com/phpDocumentor/ReflectionCommon/issues", + "source": "https://github.com/phpDocumentor/ReflectionCommon/tree/2.x" + }, + "time": "2020-06-27T09:03:43+00:00" + }, + { + "name": "phpdocumentor/reflection-docblock", + "version": "5.6.0", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", + "reference": "f3558a4c23426d12bffeaab463f8a8d8b681193c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/f3558a4c23426d12bffeaab463f8a8d8b681193c", + "reference": "f3558a4c23426d12bffeaab463f8a8d8b681193c", + "shasum": "" + }, + "require": { + "doctrine/deprecations": "^1.1", + "ext-filter": "*", + "php": "^7.4 || ^8.0", + "phpdocumentor/reflection-common": "^2.2", + "phpdocumentor/type-resolver": "^1.7", + "phpstan/phpdoc-parser": "^1.7|^2.0", + "webmozart/assert": "^1.9.1" + }, + "require-dev": { + "mockery/mockery": "~1.3.5 || ~1.6.0", + "phpstan/extension-installer": "^1.1", + "phpstan/phpstan": "^1.8", + "phpstan/phpstan-mockery": "^1.1", + "phpstan/phpstan-webmozart-assert": "^1.2", + "phpunit/phpunit": "^9.5", + "psalm/phar": "^5.26" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.x-dev" + } + }, + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mike van Riel", + "email": "me@mikevanriel.com" + }, + { + "name": "Jaap van Otterdijk", + "email": "opensource@ijaap.nl" + } + ], + "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", + "support": { + "issues": "https://github.com/phpDocumentor/ReflectionDocBlock/issues", + "source": "https://github.com/phpDocumentor/ReflectionDocBlock/tree/5.6.0" + }, + "time": "2024-11-12T11:25:25+00:00" + }, + { + "name": "phpdocumentor/type-resolver", + "version": "1.10.0", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/TypeResolver.git", + "reference": "679e3ce485b99e84c775d28e2e96fade9a7fb50a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/679e3ce485b99e84c775d28e2e96fade9a7fb50a", + "reference": "679e3ce485b99e84c775d28e2e96fade9a7fb50a", + "shasum": "" + }, + "require": { + "doctrine/deprecations": "^1.0", + "php": "^7.3 || ^8.0", + "phpdocumentor/reflection-common": "^2.0", + "phpstan/phpdoc-parser": "^1.18|^2.0" + }, + "require-dev": { + "ext-tokenizer": "*", + "phpbench/phpbench": "^1.2", + "phpstan/extension-installer": "^1.1", + "phpstan/phpstan": "^1.8", + "phpstan/phpstan-phpunit": "^1.1", + "phpunit/phpunit": "^9.5", + "rector/rector": "^0.13.9", + "vimeo/psalm": "^4.25" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-1.x": "1.x-dev" + } + }, + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mike van Riel", + "email": "me@mikevanriel.com" + } + ], + "description": "A PSR-5 based resolver of Class names, Types and Structural Element Names", + "support": { + "issues": "https://github.com/phpDocumentor/TypeResolver/issues", + "source": "https://github.com/phpDocumentor/TypeResolver/tree/1.10.0" + }, + "time": "2024-11-09T15:12:26+00:00" + }, + { + "name": "phpstan/phpdoc-parser", + "version": "2.0.0", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpdoc-parser.git", + "reference": "c00d78fb6b29658347f9d37ebe104bffadf36299" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/c00d78fb6b29658347f9d37ebe104bffadf36299", + "reference": "c00d78fb6b29658347f9d37ebe104bffadf36299", + "shasum": "" + }, + "require": { + "php": "^7.4 || ^8.0" + }, + "require-dev": { + "doctrine/annotations": "^2.0", + "nikic/php-parser": "^5.3.0", + "php-parallel-lint/php-parallel-lint": "^1.2", + "phpstan/extension-installer": "^1.0", + "phpstan/phpstan": "^2.0", + "phpstan/phpstan-phpunit": "^2.0", + "phpstan/phpstan-strict-rules": "^2.0", + "phpunit/phpunit": "^9.6", + "symfony/process": "^5.2" + }, + "type": "library", + "autoload": { + "psr-4": { + "PHPStan\\PhpDocParser\\": [ + "src/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPDoc parser with support for nullable, intersection and generic types", + "support": { + "issues": "https://github.com/phpstan/phpdoc-parser/issues", + "source": "https://github.com/phpstan/phpdoc-parser/tree/2.0.0" + }, + "time": "2024-10-13T11:29:49+00:00" + }, + { + "name": "phpstan/phpstan", + "version": "1.12.10", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "fc463b5d0fe906dcf19689be692c65c50406a071" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/fc463b5d0fe906dcf19689be692c65c50406a071", + "reference": "fc463b5d0fe906dcf19689be692c65c50406a071", "shasum": "" }, "require": { @@ -396,39 +1457,39 @@ "type": "github" } ], - "time": "2024-09-26T12:45:22+00:00" + "time": "2024-11-11T15:37:09+00:00" }, { "name": "phpunit/php-code-coverage", - "version": "11.0.6", + "version": "11.0.7", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "ebdffc9e09585dafa71b9bffcdb0a229d4704c45" + "reference": "f7f08030e8811582cc459871d28d6f5a1a4d35ca" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/ebdffc9e09585dafa71b9bffcdb0a229d4704c45", - "reference": "ebdffc9e09585dafa71b9bffcdb0a229d4704c45", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/f7f08030e8811582cc459871d28d6f5a1a4d35ca", + "reference": "f7f08030e8811582cc459871d28d6f5a1a4d35ca", "shasum": "" }, "require": { "ext-dom": "*", "ext-libxml": "*", "ext-xmlwriter": "*", - "nikic/php-parser": "^5.1.0", + "nikic/php-parser": "^5.3.1", "php": ">=8.2", - "phpunit/php-file-iterator": "^5.0.1", + "phpunit/php-file-iterator": "^5.1.0", "phpunit/php-text-template": "^4.0.1", "sebastian/code-unit-reverse-lookup": "^4.0.1", "sebastian/complexity": "^4.0.1", "sebastian/environment": "^7.2.0", "sebastian/lines-of-code": "^3.0.1", - "sebastian/version": "^5.0.1", + "sebastian/version": "^5.0.2", "theseer/tokenizer": "^1.2.3" }, "require-dev": { - "phpunit/phpunit": "^11.0" + "phpunit/phpunit": "^11.4.1" }, "suggest": { "ext-pcov": "PHP extension that provides line coverage", @@ -466,7 +1527,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", "security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/11.0.6" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/11.0.7" }, "funding": [ { @@ -474,7 +1535,7 @@ "type": "github" } ], - "time": "2024-08-22T04:37:56+00:00" + "time": "2024-10-09T06:21:38+00:00" }, { "name": "phpunit/php-file-iterator", @@ -723,16 +1784,16 @@ }, { "name": "phpunit/phpunit", - "version": "11.3.6", + "version": "11.4.3", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "d62c45a19c665bb872c2a47023a0baf41a98bb2b" + "reference": "e8e8ed1854de5d36c088ec1833beae40d2dedd76" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/d62c45a19c665bb872c2a47023a0baf41a98bb2b", - "reference": "d62c45a19c665bb872c2a47023a0baf41a98bb2b", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/e8e8ed1854de5d36c088ec1833beae40d2dedd76", + "reference": "e8e8ed1854de5d36c088ec1833beae40d2dedd76", "shasum": "" }, "require": { @@ -746,21 +1807,21 @@ "phar-io/manifest": "^2.0.4", "phar-io/version": "^3.2.1", "php": ">=8.2", - "phpunit/php-code-coverage": "^11.0.6", + "phpunit/php-code-coverage": "^11.0.7", "phpunit/php-file-iterator": "^5.1.0", "phpunit/php-invoker": "^5.0.1", "phpunit/php-text-template": "^4.0.1", "phpunit/php-timer": "^7.0.1", "sebastian/cli-parser": "^3.0.2", "sebastian/code-unit": "^3.0.1", - "sebastian/comparator": "^6.1.0", + "sebastian/comparator": "^6.1.1", "sebastian/diff": "^6.0.2", "sebastian/environment": "^7.2.0", "sebastian/exporter": "^6.1.3", "sebastian/global-state": "^7.0.2", "sebastian/object-enumerator": "^6.0.1", "sebastian/type": "^5.1.0", - "sebastian/version": "^5.0.1" + "sebastian/version": "^5.0.2" }, "suggest": { "ext-soap": "To be able to generate mocks based on WSDL files" @@ -771,7 +1832,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "11.3-dev" + "dev-main": "11.4-dev" } }, "autoload": { @@ -803,7 +1864,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/11.3.6" + "source": "https://github.com/sebastianbergmann/phpunit/tree/11.4.3" }, "funding": [ { @@ -819,7 +1880,161 @@ "type": "tidelift" } ], - "time": "2024-09-19T10:54:28+00:00" + "time": "2024-10-28T13:07:50+00:00" + }, + { + "name": "psr/container", + "version": "2.0.2", + "source": { + "type": "git", + "url": "https://github.com/php-fig/container.git", + "reference": "c71ecc56dfe541dbd90c5360474fbc405f8d5963" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/container/zipball/c71ecc56dfe541dbd90c5360474fbc405f8d5963", + "reference": "c71ecc56dfe541dbd90c5360474fbc405f8d5963", + "shasum": "" + }, + "require": { + "php": ">=7.4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Container\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common Container Interface (PHP FIG PSR-11)", + "homepage": "https://github.com/php-fig/container", + "keywords": [ + "PSR-11", + "container", + "container-interface", + "container-interop", + "psr" + ], + "support": { + "issues": "https://github.com/php-fig/container/issues", + "source": "https://github.com/php-fig/container/tree/2.0.2" + }, + "time": "2021-11-05T16:47:00+00:00" + }, + { + "name": "psr/log", + "version": "3.0.2", + "source": { + "type": "git", + "url": "https://github.com/php-fig/log.git", + "reference": "f16e1d5863e37f8d8c2a01719f5b34baa2b714d3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/log/zipball/f16e1d5863e37f8d8c2a01719f5b34baa2b714d3", + "reference": "f16e1d5863e37f8d8c2a01719f5b34baa2b714d3", + "shasum": "" + }, + "require": { + "php": ">=8.0.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Log\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common interface for logging libraries", + "homepage": "https://github.com/php-fig/log", + "keywords": [ + "log", + "psr", + "psr-3" + ], + "support": { + "source": "https://github.com/php-fig/log/tree/3.0.2" + }, + "time": "2024-09-11T13:17:53+00:00" + }, + { + "name": "psr/simple-cache", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/simple-cache.git", + "reference": "764e0b3939f5ca87cb904f570ef9be2d78a07865" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/simple-cache/zipball/764e0b3939f5ca87cb904f570ef9be2d78a07865", + "reference": "764e0b3939f5ca87cb904f570ef9be2d78a07865", + "shasum": "" + }, + "require": { + "php": ">=8.0.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\SimpleCache\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common interfaces for simple caching", + "keywords": [ + "cache", + "caching", + "psr", + "psr-16", + "simple-cache" + ], + "support": { + "source": "https://github.com/php-fig/simple-cache/tree/3.0.0" + }, + "time": "2021-10-29T13:26:27+00:00" }, { "name": "sebastian/cli-parser", @@ -993,16 +2208,16 @@ }, { "name": "sebastian/comparator", - "version": "6.1.0", + "version": "6.2.1", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/comparator.git", - "reference": "fa37b9e2ca618cb051d71b60120952ee8ca8b03d" + "reference": "43d129d6a0f81c78bee378b46688293eb7ea3739" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/fa37b9e2ca618cb051d71b60120952ee8ca8b03d", - "reference": "fa37b9e2ca618cb051d71b60120952ee8ca8b03d", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/43d129d6a0f81c78bee378b46688293eb7ea3739", + "reference": "43d129d6a0f81c78bee378b46688293eb7ea3739", "shasum": "" }, "require": { @@ -1013,12 +2228,12 @@ "sebastian/exporter": "^6.0" }, "require-dev": { - "phpunit/phpunit": "^11.3" + "phpunit/phpunit": "^11.4" }, "type": "library", "extra": { "branch-alias": { - "dev-main": "6.1-dev" + "dev-main": "6.2-dev" } }, "autoload": { @@ -1058,7 +2273,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/comparator/issues", "security": "https://github.com/sebastianbergmann/comparator/security/policy", - "source": "https://github.com/sebastianbergmann/comparator/tree/6.1.0" + "source": "https://github.com/sebastianbergmann/comparator/tree/6.2.1" }, "funding": [ { @@ -1066,7 +2281,7 @@ "type": "github" } ], - "time": "2024-09-11T15:42:56+00:00" + "time": "2024-10-31T05:30:08+00:00" }, { "name": "sebastian/complexity", @@ -1692,16 +2907,16 @@ }, { "name": "sebastian/version", - "version": "5.0.1", + "version": "5.0.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/version.git", - "reference": "45c9debb7d039ce9b97de2f749c2cf5832a06ac4" + "reference": "c687e3387b99f5b03b6caa64c74b63e2936ff874" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/45c9debb7d039ce9b97de2f749c2cf5832a06ac4", - "reference": "45c9debb7d039ce9b97de2f749c2cf5832a06ac4", + "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/c687e3387b99f5b03b6caa64c74b63e2936ff874", + "reference": "c687e3387b99f5b03b6caa64c74b63e2936ff874", "shasum": "" }, "require": { @@ -1734,7 +2949,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/version/issues", "security": "https://github.com/sebastianbergmann/version/security/policy", - "source": "https://github.com/sebastianbergmann/version/tree/5.0.1" + "source": "https://github.com/sebastianbergmann/version/tree/5.0.2" }, "funding": [ { @@ -1742,7 +2957,7 @@ "type": "github" } ], - "time": "2024-07-03T05:13:08+00:00" + "time": "2024-10-09T05:16:32+00:00" }, { "name": "square/pjson", @@ -1792,6 +3007,838 @@ }, "time": "2024-03-15T18:19:22+00:00" }, + { + "name": "symfony/console", + "version": "v7.2.0-RC1", + "source": { + "type": "git", + "url": "https://github.com/symfony/console.git", + "reference": "23c8aae6d764e2bae02d2a99f7532a7f6ed619cf" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/console/zipball/23c8aae6d764e2bae02d2a99f7532a7f6ed619cf", + "reference": "23c8aae6d764e2bae02d2a99f7532a7f6ed619cf", + "shasum": "" + }, + "require": { + "php": ">=8.2", + "symfony/polyfill-mbstring": "~1.0", + "symfony/service-contracts": "^2.5|^3", + "symfony/string": "^6.4|^7.0" + }, + "conflict": { + "symfony/dependency-injection": "<6.4", + "symfony/dotenv": "<6.4", + "symfony/event-dispatcher": "<6.4", + "symfony/lock": "<6.4", + "symfony/process": "<6.4" + }, + "provide": { + "psr/log-implementation": "1.0|2.0|3.0" + }, + "require-dev": { + "psr/log": "^1|^2|^3", + "symfony/config": "^6.4|^7.0", + "symfony/dependency-injection": "^6.4|^7.0", + "symfony/event-dispatcher": "^6.4|^7.0", + "symfony/http-foundation": "^6.4|^7.0", + "symfony/http-kernel": "^6.4|^7.0", + "symfony/lock": "^6.4|^7.0", + "symfony/messenger": "^6.4|^7.0", + "symfony/process": "^6.4|^7.0", + "symfony/stopwatch": "^6.4|^7.0", + "symfony/var-dumper": "^6.4|^7.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Console\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Eases the creation of beautiful and testable command line interfaces", + "homepage": "https://symfony.com", + "keywords": [ + "cli", + "command-line", + "console", + "terminal" + ], + "support": { + "source": "https://github.com/symfony/console/tree/v7.2.0-RC1" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-11-06T14:24:19+00:00" + }, + { + "name": "symfony/deprecation-contracts", + "version": "v3.5.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/deprecation-contracts.git", + "reference": "0e0d29ce1f20deffb4ab1b016a7257c4f1e789a1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/0e0d29ce1f20deffb4ab1b016a7257c4f1e789a1", + "reference": "0e0d29ce1f20deffb4ab1b016a7257c4f1e789a1", + "shasum": "" + }, + "require": { + "php": ">=8.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "3.5-dev" + }, + "thanks": { + "name": "symfony/contracts", + "url": "https://github.com/symfony/contracts" + } + }, + "autoload": { + "files": [ + "function.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "A generic function and convention to trigger deprecation notices", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/deprecation-contracts/tree/v3.5.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-04-18T09:32:20+00:00" + }, + { + "name": "symfony/finder", + "version": "v7.2.0-RC1", + "source": { + "type": "git", + "url": "https://github.com/symfony/finder.git", + "reference": "6de263e5868b9a137602dd1e33e4d48bfae99c49" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/finder/zipball/6de263e5868b9a137602dd1e33e4d48bfae99c49", + "reference": "6de263e5868b9a137602dd1e33e4d48bfae99c49", + "shasum": "" + }, + "require": { + "php": ">=8.2" + }, + "require-dev": { + "symfony/filesystem": "^6.4|^7.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Finder\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Finds files and directories via an intuitive fluent interface", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/finder/tree/v7.2.0-RC1" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-10-23T06:56:12+00:00" + }, + { + "name": "symfony/polyfill-ctype", + "version": "v1.31.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-ctype.git", + "reference": "a3cc8b044a6ea513310cbd48ef7333b384945638" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/a3cc8b044a6ea513310cbd48ef7333b384945638", + "reference": "a3cc8b044a6ea513310cbd48ef7333b384945638", + "shasum": "" + }, + "require": { + "php": ">=7.2" + }, + "provide": { + "ext-ctype": "*" + }, + "suggest": { + "ext-ctype": "For best performance" + }, + "type": "library", + "extra": { + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Ctype\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Gert de Pagter", + "email": "BackEndTea@gmail.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for ctype functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "ctype", + "polyfill", + "portable" + ], + "support": { + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.31.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-09-09T11:45:10+00:00" + }, + { + "name": "symfony/polyfill-intl-grapheme", + "version": "v1.31.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-intl-grapheme.git", + "reference": "b9123926e3b7bc2f98c02ad54f6a4b02b91a8abe" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-intl-grapheme/zipball/b9123926e3b7bc2f98c02ad54f6a4b02b91a8abe", + "reference": "b9123926e3b7bc2f98c02ad54f6a4b02b91a8abe", + "shasum": "" + }, + "require": { + "php": ">=7.2" + }, + "suggest": { + "ext-intl": "For best performance" + }, + "type": "library", + "extra": { + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Intl\\Grapheme\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for intl's grapheme_* functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "grapheme", + "intl", + "polyfill", + "portable", + "shim" + ], + "support": { + "source": "https://github.com/symfony/polyfill-intl-grapheme/tree/v1.31.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-09-09T11:45:10+00:00" + }, + { + "name": "symfony/polyfill-intl-normalizer", + "version": "v1.31.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-intl-normalizer.git", + "reference": "3833d7255cc303546435cb650316bff708a1c75c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-intl-normalizer/zipball/3833d7255cc303546435cb650316bff708a1c75c", + "reference": "3833d7255cc303546435cb650316bff708a1c75c", + "shasum": "" + }, + "require": { + "php": ">=7.2" + }, + "suggest": { + "ext-intl": "For best performance" + }, + "type": "library", + "extra": { + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Intl\\Normalizer\\": "" + }, + "classmap": [ + "Resources/stubs" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for intl's Normalizer class and related functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "intl", + "normalizer", + "polyfill", + "portable", + "shim" + ], + "support": { + "source": "https://github.com/symfony/polyfill-intl-normalizer/tree/v1.31.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-09-09T11:45:10+00:00" + }, + { + "name": "symfony/polyfill-mbstring", + "version": "v1.31.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-mbstring.git", + "reference": "85181ba99b2345b0ef10ce42ecac37612d9fd341" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/85181ba99b2345b0ef10ce42ecac37612d9fd341", + "reference": "85181ba99b2345b0ef10ce42ecac37612d9fd341", + "shasum": "" + }, + "require": { + "php": ">=7.2" + }, + "provide": { + "ext-mbstring": "*" + }, + "suggest": { + "ext-mbstring": "For best performance" + }, + "type": "library", + "extra": { + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Mbstring\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for the Mbstring extension", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "mbstring", + "polyfill", + "portable", + "shim" + ], + "support": { + "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.31.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-09-09T11:45:10+00:00" + }, + { + "name": "symfony/process", + "version": "v7.2.0-RC1", + "source": { + "type": "git", + "url": "https://github.com/symfony/process.git", + "reference": "d34b22ba9390ec19d2dd966c40aa9e8462f27a7e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/process/zipball/d34b22ba9390ec19d2dd966c40aa9e8462f27a7e", + "reference": "d34b22ba9390ec19d2dd966c40aa9e8462f27a7e", + "shasum": "" + }, + "require": { + "php": ">=8.2" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Process\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Executes commands in sub-processes", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/process/tree/v7.2.0-RC1" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-11-06T14:24:19+00:00" + }, + { + "name": "symfony/service-contracts", + "version": "v3.5.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/service-contracts.git", + "reference": "bd1d9e59a81d8fa4acdcea3f617c581f7475a80f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/service-contracts/zipball/bd1d9e59a81d8fa4acdcea3f617c581f7475a80f", + "reference": "bd1d9e59a81d8fa4acdcea3f617c581f7475a80f", + "shasum": "" + }, + "require": { + "php": ">=8.1", + "psr/container": "^1.1|^2.0", + "symfony/deprecation-contracts": "^2.5|^3" + }, + "conflict": { + "ext-psr": "<1.1|>=2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "3.5-dev" + }, + "thanks": { + "name": "symfony/contracts", + "url": "https://github.com/symfony/contracts" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Contracts\\Service\\": "" + }, + "exclude-from-classmap": [ + "/Test/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Generic abstractions related to writing services", + "homepage": "https://symfony.com", + "keywords": [ + "abstractions", + "contracts", + "decoupling", + "interfaces", + "interoperability", + "standards" + ], + "support": { + "source": "https://github.com/symfony/service-contracts/tree/v3.5.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-04-18T09:32:20+00:00" + }, + { + "name": "symfony/string", + "version": "v7.2.0-RC1", + "source": { + "type": "git", + "url": "https://github.com/symfony/string.git", + "reference": "446e0d146f991dde3e73f45f2c97a9faad773c82" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/string/zipball/446e0d146f991dde3e73f45f2c97a9faad773c82", + "reference": "446e0d146f991dde3e73f45f2c97a9faad773c82", + "shasum": "" + }, + "require": { + "php": ">=8.2", + "symfony/polyfill-ctype": "~1.8", + "symfony/polyfill-intl-grapheme": "~1.0", + "symfony/polyfill-intl-normalizer": "~1.0", + "symfony/polyfill-mbstring": "~1.0" + }, + "conflict": { + "symfony/translation-contracts": "<2.5" + }, + "require-dev": { + "symfony/emoji": "^7.1", + "symfony/error-handler": "^6.4|^7.0", + "symfony/http-client": "^6.4|^7.0", + "symfony/intl": "^6.4|^7.0", + "symfony/translation-contracts": "^2.5|^3.0", + "symfony/var-exporter": "^6.4|^7.0" + }, + "type": "library", + "autoload": { + "files": [ + "Resources/functions.php" + ], + "psr-4": { + "Symfony\\Component\\String\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Provides an object-oriented API to strings and deals with bytes, UTF-8 code points and grapheme clusters in a unified way", + "homepage": "https://symfony.com", + "keywords": [ + "grapheme", + "i18n", + "string", + "unicode", + "utf-8", + "utf8" + ], + "support": { + "source": "https://github.com/symfony/string/tree/v7.2.0-RC1" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-11-13T13:31:26+00:00" + }, + { + "name": "ta-tikoma/phpunit-architecture-test", + "version": "0.8.4", + "source": { + "type": "git", + "url": "https://github.com/ta-tikoma/phpunit-architecture-test.git", + "reference": "89f0dea1cb0f0d5744d3ec1764a286af5e006636" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/ta-tikoma/phpunit-architecture-test/zipball/89f0dea1cb0f0d5744d3ec1764a286af5e006636", + "reference": "89f0dea1cb0f0d5744d3ec1764a286af5e006636", + "shasum": "" + }, + "require": { + "nikic/php-parser": "^4.18.0 || ^5.0.0", + "php": "^8.1.0", + "phpdocumentor/reflection-docblock": "^5.3.0", + "phpunit/phpunit": "^10.5.5 || ^11.0.0", + "symfony/finder": "^6.4.0 || ^7.0.0" + }, + "require-dev": { + "laravel/pint": "^1.13.7", + "phpstan/phpstan": "^1.10.52" + }, + "type": "library", + "autoload": { + "psr-4": { + "PHPUnit\\Architecture\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ni Shi", + "email": "futik0ma011@gmail.com" + }, + { + "name": "Nuno Maduro", + "email": "enunomaduro@gmail.com" + } + ], + "description": "Methods for testing application architecture", + "keywords": [ + "architecture", + "phpunit", + "stucture", + "test", + "testing" + ], + "support": { + "issues": "https://github.com/ta-tikoma/phpunit-architecture-test/issues", + "source": "https://github.com/ta-tikoma/phpunit-architecture-test/tree/0.8.4" + }, + "time": "2024-01-05T14:10:56+00:00" + }, { "name": "theseer/tokenizer", "version": "1.2.3", @@ -1841,6 +3888,64 @@ } ], "time": "2024-03-03T12:36:25+00:00" + }, + { + "name": "webmozart/assert", + "version": "1.11.0", + "source": { + "type": "git", + "url": "https://github.com/webmozarts/assert.git", + "reference": "11cb2199493b2f8a3b53e7f19068fc6aac760991" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/webmozarts/assert/zipball/11cb2199493b2f8a3b53e7f19068fc6aac760991", + "reference": "11cb2199493b2f8a3b53e7f19068fc6aac760991", + "shasum": "" + }, + "require": { + "ext-ctype": "*", + "php": "^7.2 || ^8.0" + }, + "conflict": { + "phpstan/phpstan": "<0.12.20", + "vimeo/psalm": "<4.6.1 || 4.6.2" + }, + "require-dev": { + "phpunit/phpunit": "^8.5.13" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.10-dev" + } + }, + "autoload": { + "psr-4": { + "Webmozart\\Assert\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Bernhard Schussek", + "email": "bschussek@gmail.com" + } + ], + "description": "Assertions to validate method input/output with nice error messages.", + "keywords": [ + "assert", + "check", + "validate" + ], + "support": { + "issues": "https://github.com/webmozarts/assert/issues", + "source": "https://github.com/webmozarts/assert/tree/1.11.0" + }, + "time": "2022-06-03T18:03:27+00:00" } ], "aliases": [], diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..7d0904f --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,18 @@ + + + + + ./tests + + + + + ./app + ./src + + + diff --git a/src/DocumentException.php b/src/DocumentException.php index 5fc5fb9..0d500e2 100644 --- a/src/DocumentException.php +++ b/src/DocumentException.php @@ -9,12 +9,13 @@ declare(strict_types=1); namespace BitBadger\PDODocument; use Exception; +use Stringable; use Throwable; /** * Exceptions occurring during document processing */ -class DocumentException extends Exception +class DocumentException extends Exception implements Stringable { /** * Constructor 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/PgIntegrationTest.php b/tests/Integration/PgIntegrationTest.php new file mode 100644 index 0000000..5e53482 --- /dev/null +++ b/tests/Integration/PgIntegrationTest.php @@ -0,0 +1,60 @@ + + * @license MIT + * @see https://github.com/Zaid-Ajaj/ThrowawayDb The origin concept + */ + +declare(strict_types=1); + +namespace Test\Integration; + +use BitBadger\PDODocument\{Configuration, Custom, Delete, DocumentException, Field}; +use BitBadger\PDODocument\Mapper\ExistsMapper; +use PHPUnit\Framework\TestCase; +use Test\Integration\PostgreSQL\ThrowawayDb; + +/** + * Integration Test Class wrapper for PostgreSQL integration tests + */ +class PgIntegrationTest extends TestCase +{ + /** @var string Database name for throwaway database */ + static private string $dbName = ''; + + public static function setUpBeforeClass(): void + { + self::$dbName = ThrowawayDb::create(false); + } + + protected function setUp(): void + { + parent::setUp(); + ThrowawayDb::loadData(); + } + + protected function tearDown(): void + { + Delete::byFields(ThrowawayDb::TABLE, [ Field::exists(Configuration::$idField)]); + parent::tearDown(); + } + + public static function tearDownAfterClass(): void + { + ThrowawayDb::destroy(self::$dbName); + self::$dbName = ''; + } + + /** + * Does the given named object exist in the database? + * + * @param string $name The name of the object whose existence should be verified + * @return bool True if the object exists, false if not + * @throws DocumentException If any is encountered + */ + protected function dbObjectExists(string $name): bool + { + return Custom::scalar('SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = :name)', + [':name' => $name], new ExistsMapper()); + } +} diff --git a/tests/Integration/PostgreSQL/CountTest.php b/tests/Integration/PostgreSQL/CountTest.php new file mode 100644 index 0000000..56e4795 --- /dev/null +++ b/tests/Integration/PostgreSQL/CountTest.php @@ -0,0 +1,45 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Count, Field}; +use Test\Integration\PostgreSQL\ThrowawayDb; + +pest()->group('integration', 'postgresql'); + +describe('::all()', function () { + test('counts all documents', function () { + expect(Count::all(ThrowawayDb::TABLE))->toBe(5); + }); +}); + +describe('::byFields()', function () { + test('counts for numeric range correctly', function () { + expect(Count::byFields(ThrowawayDb::TABLE, [Field::between('num_value', 10, 20)]))->toBe(3); + }); + test('counts for non-numeric range correctly', function () { + expect(Count::byFields(ThrowawayDb::TABLE, [Field::between('value', 'aardvark', 'apple')]))->toBe(1); + }); +}); + +describe('::byContains()', function () { + test('counts matching documents', function () { + expect(Count::byContains(ThrowawayDb::TABLE, ['value' => 'purple']))->toBe(2); + }); + test('returns 0 for no matching documents', function () { + expect(Count::byContains(ThrowawayDb::TABLE, ['value' => 'magenta']))->toBe(0); + }); +}); + +describe('::byJsonPath()', function () { + test('counts matching documents', function () { + expect(Count::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ < 5)'))->toBe(2); + }); + test('returns 0 for no matching documents', function () { + expect(Count::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)'))->toBe(0); + }); +}); diff --git a/tests/Integration/PostgreSQL/CustomTest.php b/tests/Integration/PostgreSQL/CustomTest.php new file mode 100644 index 0000000..44f34a3 --- /dev/null +++ b/tests/Integration/PostgreSQL/CustomTest.php @@ -0,0 +1,98 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Count, Custom, DocumentException, Query}; +use BitBadger\PDODocument\Mapper\{CountMapper, DocumentMapper}; +use Test\Integration\PostgreSQL\ThrowawayDb; +use Test\Integration\TestDocument; + +pest()->group('integration', 'postgresql'); + +describe('::runQuery()', function () { + test('runs a valid query successfully', function () { + $stmt = &Custom::runQuery('SELECT data FROM ' . ThrowawayDb::TABLE . ' LIMIT 1', []); + try { + expect($stmt)->not->toBeNull(); + } finally { + $stmt = null; + } + }); + test('fails with an invalid query', function () { + $stmt = null; + try { + expect(function () use (&$stmt) { $stmt = &Custom::runQuery('GRAB stuff FROM over_there UNTIL done', []); }) + ->toThrow(DocumentException::class); + } finally { + $stmt = null; + } + }); +}); + +describe('::list()', function () { + test('returns non-empty list when data found', function () { + $list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE), [], new DocumentMapper(TestDocument::class)); + expect($list)->not->toBeNull(); + $count = 0; + foreach ($list->items() as $ignored) $count++; + expect($count)->toBe(5); + }); + test('returns empty list when no data found', function () { + expect(Custom::list( + Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE (data->>'num_value')::numeric > :value", + [':value' => 100], new DocumentMapper(TestDocument::class))) + ->not->toBeNull() + ->hasItems()->toBeFalse(); + }); +}); + +describe('::array()', function () { + test('returns non-empty array when data found', function () { + expect(Custom::array(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'sub' IS NOT NULL", [], + new DocumentMapper(TestDocument::class))) + ->not->toBeNull() + ->toHaveCount(2); + }); + test('returns empty array when no data found', function () { + expect(Custom::array(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'value' = :value", + [':value' => 'not there'], new DocumentMapper(TestDocument::class))) + ->not->toBeNull() + ->toBeEmpty(); + }); +}); + +describe('::single()', function () { + test('returns a document when one is found', function () { + expect(Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id", [':id' => 'one'], + new DocumentMapper(TestDocument::class))) + ->isSome()->toBeTrue() + ->get()->id->toBe('one'); + }); + test('returns no document when one is not found', function () { + expect(Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id", + [':id' => 'eighty'], new DocumentMapper(TestDocument::class))) + ->isNone()->toBeTrue(); + }); +}); + +describe('::nonQuery()', function () { + test('works when documents match the WHERE clause', function () { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + expect(Count::all(ThrowawayDb::TABLE))->toBe(0); + }); + test('works when no documents match the WHERE clause', function () { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE . " WHERE (data->>'num_value')::numeric > :value", + [':value' => 100]); + expect(Count::all(ThrowawayDb::TABLE))->toBe(5); + }); +}); + +describe('::scalar()', function () { + test('returns a scalar value', function () { + expect(Custom::scalar("SELECT 5 AS it", [], new CountMapper()))->toBe(5); + }); +}); diff --git a/tests/Integration/PostgreSQL/DefinitionTest.php b/tests/Integration/PostgreSQL/DefinitionTest.php new file mode 100644 index 0000000..e01eb27 --- /dev/null +++ b/tests/Integration/PostgreSQL/DefinitionTest.php @@ -0,0 +1,47 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Definition, DocumentIndex}; + +pest()->group('integration', 'postgresql'); + +describe('::ensureTable()', function () { + test('creates a table', function () { + expect($this->dbObjectExists('ensured'))->toBeFalse() + ->and($this->dbObjectExists('idx_ensured_key'))->toBeFalse(); + Definition::ensureTable('ensured'); + expect($this->dbObjectExists('ensured'))->toBeTrue() + ->and($this->dbObjectExists('idx_ensured_key'))->toBeTrue(); + }); +}); + +describe('::ensureFieldIndex()', function () { + test('creates an index', function () { + expect($this->dbObjectExists('idx_ensured_test'))->toBeFalse(); + Definition::ensureTable('ensured'); + Definition::ensureFieldIndex('ensured', 'test', ['name', 'age']); + expect($this->dbObjectExists('idx_ensured_test'))->toBeTrue(); + }); +}); + +describe('::ensureDocumentIndex()', function () { + test('creates a full index', function () { + $docIdx = 'idx_doc_table_document'; + Definition::ensureTable('doc_table'); + expect($this->dbObjectExists($docIdx))->toBeFalse(); + Definition::ensureDocumentIndex('doc_table', DocumentIndex::Full); + expect($this->dbObjectExists($docIdx))->toBeTrue(); + }); + test('creates an optimized index', function () { + $docIdx = 'idx_doc_tbl_document'; + Definition::ensureTable('doc_tbl'); + expect($this->dbObjectExists($docIdx))->toBeFalse(); + Definition::ensureDocumentIndex('doc_tbl', DocumentIndex::Optimized); + expect($this->dbObjectExists($docIdx))->toBeTrue(); + }); +}); diff --git a/tests/Integration/PostgreSQL/DeleteTest.php b/tests/Integration/PostgreSQL/DeleteTest.php new file mode 100644 index 0000000..6d007e5 --- /dev/null +++ b/tests/Integration/PostgreSQL/DeleteTest.php @@ -0,0 +1,64 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Count, Delete, Field}; +use Test\Integration\PostgreSQL\ThrowawayDb; + +pest()->group('integration', 'postgresql'); + +describe('::byId()', function () { + test('deletes a document when ID is matched', function () { + expect(Count::all(ThrowawayDb::TABLE))->toBe(5); + Delete::byId(ThrowawayDb::TABLE, 'four'); + expect(Count::all(ThrowawayDb::TABLE))->toBe(4); + }); + test('does not delete a document when ID is not matched', function () { + expect(Count::all(ThrowawayDb::TABLE))->toBe(5); + Delete::byId(ThrowawayDb::TABLE, 'negative four'); + expect(Count::all(ThrowawayDb::TABLE))->toBe(5); + }); +}); + +describe('::byFields()', function () { + test('deletes documents when fields match', function () { + expect(Count::all(ThrowawayDb::TABLE))->toBe(5); + Delete::byFields(ThrowawayDb::TABLE, [Field::notEqual('value', 'purple')]); + expect(Count::all(ThrowawayDb::TABLE))->toBe(2); + }); + test('does not delete documents when fields are not matched', function () { + expect(Count::all(ThrowawayDb::TABLE))->toBe(5); + Delete::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'crimson')]); + expect(Count::all(ThrowawayDb::TABLE))->toBe(5); + }); +}); + +describe('::byContains()', function () { + test('deletes documents when containment matches', function () { + expect(Count::all(ThrowawayDb::TABLE))->toBe(5); + Delete::byContains(ThrowawayDb::TABLE, ['value' => 'purple']); + expect(Count::all(ThrowawayDb::TABLE))->toBe(3); + }); + test('does not delete documents when containment is not matched', function () { + expect(Count::all(ThrowawayDb::TABLE))->toBe(5); + Delete::byContains(ThrowawayDb::TABLE, ['target' => 'acquired']); + expect(Count::all(ThrowawayDb::TABLE))->toBe(5); + }); +}); + +describe('::byJsonPath()', function () { + test('deletes documents when path matches', function () { + expect(Count::all(ThrowawayDb::TABLE))->toBe(5); + Delete::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ <> 0)'); + expect(Count::all(ThrowawayDb::TABLE))->toBe(1); + }); + test('does not delete documents when path is not matched', function () { + expect(Count::all(ThrowawayDb::TABLE))->toBe(5); + Delete::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ < 0)'); + expect(Count::all(ThrowawayDb::TABLE))->toBe(5); + }); +}); diff --git a/tests/Integration/PostgreSQL/DocumentListTest.php b/tests/Integration/PostgreSQL/DocumentListTest.php new file mode 100644 index 0000000..0a22fe2 --- /dev/null +++ b/tests/Integration/PostgreSQL/DocumentListTest.php @@ -0,0 +1,96 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{DocumentException, DocumentList, Query}; +use BitBadger\PDODocument\Mapper\DocumentMapper; +use Test\Integration\PostgreSQL\ThrowawayDb; +use Test\Integration\TestDocument; + +pest()->group('integration', 'postgresql'); + +describe('::create()', function () { + test('creates a document list', function () { + $list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [], + new DocumentMapper(TestDocument::class)); + expect($list)->not->toBeNull(); + $list = null; // free database result + }); +}); + +describe('->items()', function () { + test('enumerates items in the list', function () { + $list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [], + new DocumentMapper(TestDocument::class)); + expect($list)->not->toBeNull(); + $count = 0; + foreach ($list->items() as $item) { + expect(['one', 'two', 'three', 'four', 'five'])->toContain($item->id); + $count++; + } + expect($count)->toBe(5); + }); + test('fails when the list is exhausted', function () { + $list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [], + new DocumentMapper(TestDocument::class)); + expect($list)->not->toBeNull()->hasItems()->toBeTrue(); + $ignored = iterator_to_array($list->items()); + expect($list)->hasItems()->toBeFalse() + ->and(fn () => iterator_to_array($list->items()))->toThrow(DocumentException::class); + }); +}); + +describe('->hasItems()', function () { + test('returns false when no items are in the list', function () { + expect(DocumentList::create( + Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE (data->>'num_value')::numeric < 0", [], + new DocumentMapper(TestDocument::class))) + ->not->toBeNull() + ->hasItems()->toBeFalse(); + }); + test('returns true when items are in the list', function () { + $list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [], + new DocumentMapper(TestDocument::class)); + expect($list)->not->toBeNull()->hasItems()->toBeTrue(); + foreach ($list->items() as $ignored) { + expect($list)->hasItems()->toBeTrue(); + } + expect($list)->hasItems()->toBeFalse(); + }); +}); + +describe('->map()', function () { + test('transforms the list', function () { + $list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [], + new DocumentMapper(TestDocument::class)); + expect($list)->not->toBeNull()->hasItems()->toBeTrue(); + foreach ($list->map(fn($doc) => strrev($doc->id)) as $mapped) { + expect(['eno', 'owt', 'eerht', 'ruof', 'evif'])->toContain($mapped); + } + }); +}); + +describe('->iter()', function () { + test('walks the list', function () { + $list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [], + new DocumentMapper(TestDocument::class)); + expect($list)->not->toBeNull()->hasItems()->toBeTrue(); + $splats = []; + $list->iter(function ($doc) use (&$splats) { $splats[] = str_repeat('*', strlen($doc->id)); }); + expect(implode(' ', $splats))->toBe('*** *** ***** **** ****'); + }); +}); + +describe('->mapToArray()', function () { + test('creates an associative array', function () { + $list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [], + new DocumentMapper(TestDocument::class)); + expect($list)->not->toBeNull()->hasItems()->toBeTrue() + ->and($list->mapToArray(fn($it) => $it->id, fn($it) => $it->value)) + ->toBe(['one' => 'FIRST!', 'two' => 'another', 'three' => '', 'four' => 'purple', 'five' => 'purple']); + }); +}); diff --git a/tests/Integration/PostgreSQL/DocumentTest.php b/tests/Integration/PostgreSQL/DocumentTest.php new file mode 100644 index 0000000..8716c58 --- /dev/null +++ b/tests/Integration/PostgreSQL/DocumentTest.php @@ -0,0 +1,249 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{AutoId, Configuration, Custom, Document, DocumentException, Field, Find, Query}; +use BitBadger\PDODocument\Mapper\ArrayMapper; +use Test\Integration\{NumDocument, SubDocument, TestDocument}; +use Test\Integration\PostgreSQL\ThrowawayDb; + +pest()->group('integration', 'postgresql'); + +describe('::insert()', function () { + test('inserts an array with no automatic ID', function () { + Document::insert(ThrowawayDb::TABLE, ['id' => 'turkey', 'sub' => ['foo' => 'gobble', 'bar' => 'gobble']]); + $tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class); + expect($tryDoc) + ->isSome()->toBeTrue() + ->and($tryDoc->get()) + ->id->toBe('turkey') + ->value->toBe('') + ->num_value->toBe(0) + ->sub->not->toBeNull() + ->sub->foo->toBe('gobble') + ->sub->bar->toBe('gobble'); + }); + test('inserts an array with auto-number ID, not provided', function () { + Configuration::$autoId = AutoId::Number; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + + Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'new', 'num_value' => 8]); + $doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper()); + expect($doc)->isSome()->toBeTrue() + ->and(json_decode($doc->get()['data'])) + ->id->toBe(1); + + Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'again', 'num_value' => 7]); + $doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE " . Query::whereById(docId: 2), + [':id' => 2], new ArrayMapper()); + expect($doc)->isSome()->toBeTrue() + ->and(json_decode($doc->get()['data'])) + ->id->toBe(2); + } finally { + Configuration::$autoId = AutoId::None; + } + }); + test('inserts an array with auto-number ID, provided', function () { + Configuration::$autoId = AutoId::Number; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + Document::insert(ThrowawayDb::TABLE, ['id' => 7, 'value' => 'new', 'num_value' => 8]); + $doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper()); + expect($doc)->isSome()->toBeTrue() + ->and(json_decode($doc->get()['data'])) + ->id->toBe(7); + } finally { + Configuration::$autoId = AutoId::None; + } + + }); + test('inserts an array with auto-UUID ID, not provided', function () { + Configuration::$autoId = AutoId::UUID; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + Document::insert(ThrowawayDb::TABLE, ['id' => '', 'num_value' => 5]); + $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 5)], TestDocument::class); + expect($doc) + ->isSome()->toBeTrue() + ->and($doc->get())->id->not->toBeEmpty(); + } finally { + Configuration::$autoId = AutoId::None; + } + }); + test('inserts an array with auto-UUID ID, provided', function () { + Configuration::$autoId = AutoId::UUID; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + $uuid = AutoId::generateUUID(); + Document::insert(ThrowawayDb::TABLE, ['id' => $uuid, 'value' => 'uuid', 'num_value' => 12]); + $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 12)], TestDocument::class); + expect($doc)->isSome()->toBeTrue() + ->and($doc->get())->id->toBe($uuid); + } finally { + Configuration::$autoId = AutoId::None; + } + }); + test('inserts an array with auto-string ID, not provided', function () { + Configuration::$autoId = AutoId::RandomString; + Configuration::$idStringLength = 6; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + Document::insert(ThrowawayDb::TABLE, ['id' => '', 'value' => 'new', 'num_value' => 8]); + $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 8)], TestDocument::class); + expect($doc)->isSome()->toBeTrue() + ->and($doc->get())->id->toHaveLength(6); + } finally { + Configuration::$autoId = AutoId::None; + Configuration::$idStringLength = 16; + } + }); + test('inserts an array with auto-string ID, provided', function () { + Configuration::$autoId = AutoId::RandomString; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + Document::insert(ThrowawayDb::TABLE, ['id' => 'my-key', 'value' => 'old', 'num_value' => 3]); + $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class); + expect($doc)->isSome()->toBeTrue() + ->and($doc->get())->id->toBe('my-key'); + } finally { + Configuration::$autoId = AutoId::None; + } + }); + test('inserts an object with no automatic ID', function () { + Document::insert(ThrowawayDb::TABLE, new TestDocument('turkey', sub: new SubDocument('gobble', 'gobble'))); + $tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class); + expect($tryDoc)->isSome()->toBeTrue(); + $doc = $tryDoc->get(); + expect($doc) + ->id->toBe('turkey') + ->num_value->toBe(0) + ->sub->not->toBeNull() + ->sub->foo->toBe('gobble') + ->sub->bar->toBe('gobble') + ->and($doc->value)->toBe(''); + }); + test('inserts an object with auto-number ID, not provided', function () { + Configuration::$autoId = AutoId::Number; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + + Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'taco')); + $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'taco')], NumDocument::class); + expect($doc)->isSome()->toBeTrue() + ->and($doc->get())->id->toBe(1); + + Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'burrito')); + $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'burrito')], NumDocument::class); + expect($doc)->isSome()->toBeTrue() + ->and($doc->get())->id->toBe(2); + } finally { + Configuration::$autoId = AutoId::None; + } + }); + test('inserts an object with auto-number ID, provided', function () { + Configuration::$autoId = AutoId::Number; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + Document::insert(ThrowawayDb::TABLE, new NumDocument(64, 'large')); + $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'large')], NumDocument::class); + expect($doc)->isSome()->toBeTrue() + ->and($doc->get())->id->toBe(64); + } finally { + Configuration::$autoId = AutoId::None; + } + }); + test('inserts an object with auto-UUID ID, not provided', function () { + Configuration::$autoId = AutoId::UUID; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + Document::insert(ThrowawayDb::TABLE, new TestDocument(value: 'something', num_value: 9)); + $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::exists('value')], TestDocument::class); + expect($doc)->isSome()->toBeTrue() + ->and($doc->get())->id->not->toBeEmpty(); + } finally { + Configuration::$autoId = AutoId::None; + } + }); + test('inserts an object with auto-UUID ID, provided', function () { + Configuration::$autoId = AutoId::UUID; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + $uuid = AutoId::generateUUID(); + Document::insert(ThrowawayDb::TABLE, new TestDocument($uuid, num_value: 14)); + $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 14)], TestDocument::class); + expect($doc)->isSome()->toBeTrue() + ->and($doc->get())->id->toBe($uuid); + } finally { + Configuration::$autoId = AutoId::None; + } + }); + test('inserts an object with auto-string ID, not provided', function () { + Configuration::$autoId = AutoId::RandomString; + Configuration::$idStringLength = 40; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + Document::insert(ThrowawayDb::TABLE, new TestDocument(num_value: 55)); + $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 55)], TestDocument::class); + expect($doc)->isSome()->toBeTrue() + ->and($doc->get())->id->toHaveLength(40); + } finally { + Configuration::$autoId = AutoId::None; + Configuration::$idStringLength = 16; + } + }); + test('inserts an object with auto-string ID, provided', function () { + Configuration::$autoId = AutoId::RandomString; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + Document::insert(ThrowawayDb::TABLE, new TestDocument('my-key', num_value: 3)); + $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class); + expect($doc)->isSome()->toBeTrue() + ->and($doc->get())->id->toBe('my-key'); + } finally { + Configuration::$autoId = AutoId::None; + } + }); + test('throws an exception for duplicate key', function () { + expect(fn () => Document::insert(ThrowawayDb::TABLE, new TestDocument('one'))) + ->toThrow(DocumentException::class); + }); +}); + +describe('::save()', function () { + test('inserts a new document', function () { + Document::save(ThrowawayDb::TABLE, new TestDocument('test', sub: new SubDocument('a', 'b'))); + expect(Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class))->isSome()->toBeTrue(); + }); + test('updates an existing document', function () { + Document::save(ThrowawayDb::TABLE, new TestDocument('two', num_value: 44)); + $tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class); + expect($tryDoc)->isSome()->toBeTrue() + ->and($tryDoc->get()) + ->num_value->toBe(44) + ->sub->toBeNull(); + }); +}); + +describe('::update()', function () { + test('replaces an existing document', function () { + Document::update(ThrowawayDb::TABLE, 'one', new TestDocument('one', 'howdy', 8, new SubDocument('y', 'z'))); + $tryDoc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class); + expect($tryDoc)->isSome()->toBeTrue(); + $doc = $tryDoc->get(); + expect($doc) + ->num_value->toBe(8) + ->sub->not->toBeNull() + ->sub->foo->toBe('y') + ->sub->bar->toBe('z') + ->and($doc->value)->toBe('howdy'); + }); + test('does nothing for a non-existent document', function () { + Document::update(ThrowawayDb::TABLE, 'two-hundred', new TestDocument('200')); + expect(Find::byId(ThrowawayDb::TABLE, 'two-hundred', TestDocument::class))->isNone()->toBeTrue(); + }); +}); diff --git a/tests/Integration/PostgreSQL/ExistsTest.php b/tests/Integration/PostgreSQL/ExistsTest.php new file mode 100644 index 0000000..44efbc4 --- /dev/null +++ b/tests/Integration/PostgreSQL/ExistsTest.php @@ -0,0 +1,48 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Exists, Field}; +use Test\Integration\PostgreSQL\ThrowawayDb; + +pest()->group('integration', 'postgresql'); + +describe('::byId()', function () { + test('returns true when a document exists', function () { + expect(Exists::byId(ThrowawayDb::TABLE, 'three'))->toBeTrue(); + }); + test('returns false when a document does not exist', 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('returns true when matching documents exist', function () { + expect(Exists::byContains(ThrowawayDb::TABLE, ['value' => 'purple']))->toBeTrue(); + }); + test('returns false when no matching documents exist', function () { + expect(Exists::byContains(ThrowawayDb::TABLE, ['value' => 'violet']))->toBeFalse(); + }); +}); + +describe('::byJsonPath()', function () { + test('returns true when matching documents exist', function () { + expect(Exists::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ == 10)'))->toBeTrue(); + }); + test('returns false when no matching documents exist', function () { + expect(Exists::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ == 10.1)'))->toBeFalse(); + }); +}); diff --git a/tests/Integration/PostgreSQL/FindTest.php b/tests/Integration/PostgreSQL/FindTest.php new file mode 100644 index 0000000..f46ea28 --- /dev/null +++ b/tests/Integration/PostgreSQL/FindTest.php @@ -0,0 +1,216 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Custom, Delete, Document, Field, FieldMatch, Find}; +use Test\Integration\{ArrayDocument, NumDocument, TestDocument}; +use Test\Integration\PostgreSQL\ThrowawayDb; + +pest()->group('integration', 'postgresql'); + +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('retrieves empty results', function () { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + expect(Find::all(ThrowawayDb::TABLE, TestDocument::class)) + ->not->toBeNull() + ->hasItems()->toBeFalse(); + }); +}); + +describe('::byId()', function () { + test('retrieves a document via string ID', function () { + expect(Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class)) + ->isSome()->toBeTrue() + ->get()->id->toBe('two'); + }); + test('retrieves a document via numeric ID', function () { + Delete::byFields(ThrowawayDb::TABLE, [Field::notExists('absent')]); + Document::insert(ThrowawayDb::TABLE, ['id' => 18, 'value' => 'howdy']); + expect(Find::byId(ThrowawayDb::TABLE, 18, NumDocument::class)) + ->isSome()->toBeTrue() + ->get()->id->toBe(18); + }); + test('returns None when a document is not found', function () { + expect(Find::byId(ThrowawayDb::TABLE, 'seventy-five', TestDocument::class))->isNone()->toBeTrue(); + }); +}); + +describe('::byFields()', function () { + test('retrieves 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('retrieves 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('retrieves documents matching a 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 an empty list when no matching documents are found', function () { + expect(Find::byFields(ThrowawayDb::TABLE, [Field::greater('num_value', 100)], TestDocument::class)) + ->not->toBeNull() + ->hasItems()->toBeFalse(); + }); + test('retrieves documents matching an inArray condition', 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 an empty list when no documents match an inArray condition', 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('retrieves matching documents', function () { + $docs = Find::byContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class); + expect($docs)->not->toBeNull(); + $count = 0; + foreach ($docs->items() as $ignored) $count++; + expect($count)->toBe(2); + }); + test('retrieves ordered matching documents', function () { + $docs = Find::byContains(ThrowawayDb::TABLE, ['sub' => ['foo' => 'green']], TestDocument::class, + [Field::named('value')]); + expect($docs) + ->not->toBeNull() + ->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))->toBe(['two', 'four']); + }); + test('returns an empty list when no documents match', function () { + expect(Find::byContains(ThrowawayDb::TABLE, ['value' => 'indigo'], TestDocument::class)) + ->not->toBeNull() + ->hasItems()->toBeFalse(); + }); +}); + +describe('::byJsonPath()', function () { + test('retrieves matching documents', function () { + $docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class); + expect($docs)->not->toBeNull(); + $count = 0; + foreach ($docs->items() as $ignored) $count++; + expect($count)->toBe(2); + }); + test('retrieves ordered matching documents', function () { + $docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class, + [Field::named('id')]); + expect($docs)->not->toBeNull() + ->and(iterator_to_array($docs->map(fn ($it) => $it->id), false))->toBe(['five', 'four']); + }); + test('returns an empty list when no documents match', function () { + expect(Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)', TestDocument::class)) + ->not->toBeNull() + ->hasItems()->toBeFalse(); + }); +}); + +describe('::firstByFields()', function () { + test('retrieves a matching document', function () { + expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'another')], TestDocument::class)) + ->isSome()->toBeTrue()->get()->id->toBe('two'); + }); + test('retrieves a document for multiple results', 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('retrieves a document for multiple ordered results', 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('retrieves a matching document', function () { + expect(Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'FIRST!'], TestDocument::class)) + ->isSome()->toBeTrue()->get()->id->toBe('one'); + }); + test('retrieves a document for multiple results', function () { + $doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class); + expect($doc)->isSome()->toBeTrue()->and(['four', 'five'])->toContain($doc->get()->id); + }); + test('retrieves a document for multiple ordered results', function () { + expect(Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::class, + [Field::named('sub.bar NULLS FIRST')])) + ->isSome()->toBeTrue()->get()->id->toBe('five'); + }); + test('returns None when no documents match', function () { + expect(Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'indigo'], TestDocument::class)) + ->isNone()->toBeTrue(); + }); +}); + +describe('::firstByJsonPath()', function () { + test('retrieves a matching document', function () { + expect(Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ == 10)', TestDocument::class)) + ->isSome()->toBeTrue()->get()->id->toBe('two'); + }); + test('retrieves a document for multiple results', function () { + $doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class); + expect($doc)->isSome()->toBeTrue()->and(['four', 'five'])->toContain($doc->get()->id); + }); + test('retrieves a document for multiple ordered results', function () { + expect(Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class, + [Field::named('id DESC')])) + ->isSome()->toBeTrue()->get()->id->toBe('four'); + }); + test('returns None when no documents match', function () { + expect(Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)', TestDocument::class)) + ->isNone()->toBeTrue(); + }); +}); diff --git a/tests/Integration/PostgreSQL/PatchTest.php b/tests/Integration/PostgreSQL/PatchTest.php new file mode 100644 index 0000000..751bf6b --- /dev/null +++ b/tests/Integration/PostgreSQL/PatchTest.php @@ -0,0 +1,72 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Count, Exists, Field, Find, Patch}; +use Test\Integration\PostgreSQL\ThrowawayDb; +use Test\Integration\TestDocument; + +pest()->group('integration', 'postgresql'); + +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 a document does not exist', function () { + $id = 'forty-seven'; + expect(Exists::byId(ThrowawayDb::TABLE, $id))->toBeFalse(); + Patch::byId(ThrowawayDb::TABLE, $id, ['foo' => 'green']); // no exception = pass + }); +}); + +describe('::byFields()', function () { + test('updates existing 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 matching documents exist', function () { + $fields = [Field::equal('value', 'burgundy')]; + expect(Count::byFields(ThrowawayDb::TABLE, $fields))->toBe(0); + Patch::byFields(ThrowawayDb::TABLE, $fields, ['foo' => 'green']); // no exception = pass + }); +}); + +describe('::byContains()', function () { + test('updates existing documents', function () { + Patch::byContains(ThrowawayDb::TABLE, ['value' => 'another'], ['num_value' => 12]); + $tryDoc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'another'], TestDocument::class); + expect($tryDoc)->isSome()->toBeTrue() + ->and($tryDoc->get()) + ->id->toBe('two') + ->num_value->toBe(12); + }); + test('does nothing when no matching documents exist', function () { + $criteria = ['value' => 'updated']; + expect(Count::byContains(ThrowawayDb::TABLE, $criteria))->toBe(0); + Patch::byContains(ThrowawayDb::TABLE, $criteria, ['sub.foo' => 'green']); // no exception = pass + }); +}); + +describe('::byJsonPath()', function () { + test('updates existing documents', function () { + Patch::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', ['value' => 'blue']); + $docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class); + expect($docs)->not->toBeNull()->hasItems()->toBeTrue(); + foreach ($docs->items() as $item) { + expect(['four', 'five'])->toContain($item->id) + ->and($item->value)->toBe('blue'); + } + }); + test('does nothing when no matching documents exist', function () { + $path = '$.num_value ? (@ > 100)'; + expect(Count::byJsonPath(ThrowawayDb::TABLE, $path))->toBe(0); + Patch::byJsonPath(ThrowawayDb::TABLE, $path, ['value' => 'blue']); // no exception = pass + }); +}); diff --git a/tests/Integration/PostgreSQL/RemoveFieldsTest.php b/tests/Integration/PostgreSQL/RemoveFieldsTest.php new file mode 100644 index 0000000..8047644 --- /dev/null +++ b/tests/Integration/PostgreSQL/RemoveFieldsTest.php @@ -0,0 +1,91 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Exists, Field, Find, RemoveFields}; +use Test\Integration\PostgreSQL\ThrowawayDb; +use Test\Integration\TestDocument; + +pest()->group('integration', 'postgresql'); + +describe('::byId()', function () { + test('removes fields', function () { + RemoveFields::byId(ThrowawayDb::TABLE, 'two', ['sub', 'value']); + $tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class); + expect($tryDoc)->isSome()->toBeTrue(); + $doc = $tryDoc->get(); + expect($doc)->sub->toBeNull() + ->and($doc->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']); // no exception = pass + }); + test('does nothing when the document does not exist', function () { + expect(Exists::byId(ThrowawayDb::TABLE, 'fifty'))->toBeFalse(); + RemoveFields::byId(ThrowawayDb::TABLE, 'fifty', ['sub']); // no exception = pass + }); +}); + +describe('::byFields()', function () { + test('removes fields from 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']); // no exception = pass + }); + 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']); // no exn = pass + }); +}); + +describe('::byContains()', function () { + test('removes fields from matching documents', function () { + $criteria = ['sub' => ['foo' => 'green']]; + RemoveFields::byContains(ThrowawayDb::TABLE, $criteria, ['value']); + $docs = Find::byContains(ThrowawayDb::TABLE, $criteria, TestDocument::class); + expect($docs)->not->toBeNull()->hasItems()->toBeTrue(); + foreach ($docs->items() as $item) { + expect(['two', 'four'])->toContain($item->id) + ->and($item->value)->toBeEmpty(); + } + }); + test('does nothing when the field to remove does not exist', function () { + expect(Exists::byFields(ThrowawayDb::TABLE, [Field::exists('invalid_field')]))->toBeFalse(); + RemoveFields::byContains(ThrowawayDb::TABLE, ['sub' => ['foo' => 'green']], ['invalid_field']); // no exn = pass + }); + test('does nothing when no documents match', function () { + expect(Exists::byContains(ThrowawayDb::TABLE, ['value' => 'substantial']))->toBeFalse(); + RemoveFields::byContains(ThrowawayDb::TABLE, ['value' => 'substantial'], ['num_value']); // no exception = pass + }); +}); + +describe('::byJsonPath()', function () { + test('removes fields from matching documents', function () { + $path = '$.value ? (@ == "purple")'; + RemoveFields::byJsonPath(ThrowawayDb::TABLE, $path, ['sub']); + $docs = Find::byJsonPath(ThrowawayDb::TABLE, $path, TestDocument::class); + expect($docs)->not->toBeNull()->hasItems()->toBeTrue(); + foreach ($docs->items() as $item) { + expect(['four', 'five'])->toContain($item->id) + ->and($item->sub)->toBeNull(); + } + }); + test('does nothing when the field to remove does not exist', function () { + expect(Exists::byFields(ThrowawayDb::TABLE, [Field::exists('submarine')]))->toBeFalse(); + RemoveFields::byJsonPath(ThrowawayDb::TABLE, '$.value ? (@ == "purple")', ['submarine']); // no exception = pass + }); + test('does nothing when no documents match', function () { + expect(Exists::byJsonPath(ThrowawayDb::TABLE, '$.value ? (@ == "mauve")'))->toBeFalse(); + RemoveFields::byJsonPath(ThrowawayDb::TABLE, '$.value ? (@ == "mauve")', ['value']); // no exception = pass + }); +}); diff --git a/tests/integration/postgresql/ThrowawayDb.php b/tests/Integration/PostgreSQL/ThrowawayDb.php similarity index 79% rename from tests/integration/postgresql/ThrowawayDb.php rename to tests/Integration/PostgreSQL/ThrowawayDb.php index 84e2049..c434f93 100644 --- a/tests/integration/postgresql/ThrowawayDb.php +++ b/tests/Integration/PostgreSQL/ThrowawayDb.php @@ -37,6 +37,20 @@ class ThrowawayDb Configuration::resetPDO(); } + /** + * Load data into the test table + * + * @throws DocumentException If any is encountered + */ + public static function loadData(): void + { + Document::insert(self::TABLE, new TestDocument('one', 'FIRST!', 0)); + Document::insert(self::TABLE, new TestDocument('two', 'another', 10, new SubDocument('green', 'blue'))); + Document::insert(self::TABLE, new TestDocument('three', '', 4)); + Document::insert(self::TABLE, new TestDocument('four', 'purple', 17, new SubDocument('green', 'red'))); + Document::insert(self::TABLE, new TestDocument('five', 'purple', 18)); + } + /** * Create a throwaway PostgreSQL database * @@ -51,13 +65,10 @@ class ThrowawayDb Custom::nonQuery("CREATE DATABASE $dbName WITH OWNER " . Configuration::$username, []); self::configure($dbName); + Definition::ensureTable(self::TABLE); + if ($withData) { - Definition::ensureTable(self::TABLE); - Document::insert(self::TABLE, new TestDocument('one', 'FIRST!', 0)); - Document::insert(self::TABLE, new TestDocument('two', 'another', 10, new SubDocument('green', 'blue'))); - Document::insert(self::TABLE, new TestDocument('three', '', 4)); - Document::insert(self::TABLE, new TestDocument('four', 'purple', 17, new SubDocument('green', 'red'))); - Document::insert(self::TABLE, new TestDocument('five', 'purple', 18)); + self::loadData(); } return $dbName; diff --git a/tests/Integration/SQLite/CountTest.php b/tests/Integration/SQLite/CountTest.php new file mode 100644 index 0000000..4cb0646 --- /dev/null +++ b/tests/Integration/SQLite/CountTest.php @@ -0,0 +1,39 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Count, DocumentException, Field}; +use Test\Integration\SQLite\ThrowawayDb; + +pest()->group('integration', 'sqlite'); + +describe('::all()', function () { + test('counts all documents', function () { + expect(Count::all(ThrowawayDb::TABLE))->toBe(5); + }); +}); + +describe('::byFields()', function () { + test('counts by numeric range', function () { + expect(Count::byFields(ThrowawayDb::TABLE, [Field::between('num_value', 10, 20)]))->toBe(3); + }); + test('counts by non-numeric range', function () { + expect(Count::byFields(ThrowawayDb::TABLE, [Field::between('value', 'aardvark', 'apple')]))->toBe(1); + }); +}); + +describe('::byContains()', function () { + test('throws an exception', function () { + expect(fn () => Count::byContains('', []))->toThrow(DocumentException::class); + }); +}); + +describe('::byJsonPath()', function () { + test('throws an exception', function () { + expect(fn () => Count::byJsonPath('', ''))->toThrow(DocumentException::class); + }); +}); diff --git a/tests/Integration/SQLite/CustomTest.php b/tests/Integration/SQLite/CustomTest.php new file mode 100644 index 0000000..accadc6 --- /dev/null +++ b/tests/Integration/SQLite/CustomTest.php @@ -0,0 +1,94 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Count, Custom, DocumentException, Query}; +use BitBadger\PDODocument\Mapper\{CountMapper, DocumentMapper}; +use Test\Integration\SQLite\ThrowawayDb; +use Test\Integration\TestDocument; + +pest()->group('integration', 'sqlite'); + +describe('::runQuery()', function () { + test('runs a valid query successfully', function () { + $stmt = &Custom::runQuery('SELECT data FROM ' . ThrowawayDb::TABLE . ' LIMIT 1', []); + try { + expect($stmt)->not->toBeNull(); + } finally { + $stmt = null; + } + }); + test('fails with an invalid query', function () { + $stmt = null; + try { + expect(function () use (&$stmt) { $stmt = &Custom::runQuery('GRAB stuff FROM over_there UNTIL done', []); }) + ->toThrow(DocumentException::class); + } finally { + $stmt = null; + } + }); +}); + +describe('::list()', function () { + test('returns non-empty list when data is found', function () { + $list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE), [], new DocumentMapper(TestDocument::class)); + expect($list)->not->toBeNull(); + $count = 0; + foreach ($list->items() as $ignored) $count++; + expect($count)->toBe(5); + }); + test('returns empty list when not data is found', function () { + expect(Custom::list(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'num_value' > :value", + [':value' => 100], new DocumentMapper(TestDocument::class))) + ->not->toBeNull() + ->hasItems()->toBeFalse(); + + }); +}); + +describe('::array()', function () { + test('returns non-empty array when data is found', function () { + expect(Custom::array(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'sub' IS NOT NULL", [], + new DocumentMapper(TestDocument::class))) + ->not->toBeNull()->toHaveCount(2); + }); + test('returns empty array when data is not found', function () { + expect(Custom::array(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'value' = :value", + [':value' => 'not there'], new DocumentMapper(TestDocument::class))) + ->not->toBeNull()->toBeEmpty(); + }); +}); + +describe('::single()', function () { + test('returns a document when one is found', function () { + expect(Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id", [':id' => 'one'], + new DocumentMapper(TestDocument::class))) + ->isSome()->toBeTrue()->get()->id->toBe('one'); + }); + test('returns no document when none is found', function () { + expect(Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = :id", + [':id' => 'eighty'], new DocumentMapper(TestDocument::class))) + ->isNone()->toBeTrue(); + }); +}); + +describe('::nonQuery()', function () { + test('works when documents match the WHERE clause', function () { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + expect(Count::all(ThrowawayDb::TABLE))->toBe(0); + }); + test('works when no documents match the WHERE clause', function () { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE . " WHERE data->>'num_value' > :value", [':value' => 100]); + expect(Count::all(ThrowawayDb::TABLE))->toBe(5); + }); +}); + +describe('::scalar()', function () { + test('returns a scalar value', function () { + expect(Custom::scalar("SELECT 5 AS it", [], new CountMapper()))->toBe(5); + }); +}); diff --git a/tests/Integration/SQLite/DefinitionTest.php b/tests/Integration/SQLite/DefinitionTest.php new file mode 100644 index 0000000..d03da73 --- /dev/null +++ b/tests/Integration/SQLite/DefinitionTest.php @@ -0,0 +1,36 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Definition, DocumentException, DocumentIndex}; + +pest()->group('integration', 'sqlite'); + +describe('::ensureTable()', function () { + test('creates table and PK index', function () { + expect($this->dbObjectExists('ensured'))->toBeFalse() + ->and($this->dbObjectExists('idx_ensured_key'))->toBeFalse(); + Definition::ensureTable('ensured'); + expect($this->dbObjectExists('ensured'))->toBeTrue() + ->and($this->dbObjectExists('idx_ensured_key'))->toBeTrue(); + }); +}); + +describe('::ensureFieldIndex()', function () { + test('creates an index', function () { + expect($this->dbObjectExists('idx_ensured_test'))->toBeFalse(); + Definition::ensureTable('ensured'); + Definition::ensureFieldIndex('ensured', 'test', ['name', 'age']); + expect($this->dbObjectExists('idx_ensured_test'))->toBeTrue(); + }); +}); + +describe('::ensureDocumentIndex()', function () { + test('throws an exception', function () { + expect(fn () => Definition::ensureDocumentIndex('', DocumentIndex::Full))->toThrow(DocumentException::class); + }); +}); diff --git a/tests/Integration/SQLite/DeleteTest.php b/tests/Integration/SQLite/DeleteTest.php new file mode 100644 index 0000000..de10d81 --- /dev/null +++ b/tests/Integration/SQLite/DeleteTest.php @@ -0,0 +1,50 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Count, Delete, DocumentException, Field}; +use Test\Integration\SQLite\ThrowawayDb; + +pest()->group('integration', 'sqlite'); + +describe('::byId()', function () { + test('deletes a document when one exists', function () { + expect(Count::all(ThrowawayDb::TABLE))->toBe(5); + Delete::byId(ThrowawayDb::TABLE, 'four'); + expect(Count::all(ThrowawayDb::TABLE))->toBe(4); + }); + test('does nothing when the document does not exist', function () { + expect(Count::all(ThrowawayDb::TABLE))->toBe(5); + Delete::byId(ThrowawayDb::TABLE, 'negative four'); + expect(Count::all(ThrowawayDb::TABLE))->toBe(5); + }); +}); + +describe('::byFields()', function () { + test('deletes matching documents', function () { + expect(Count::all(ThrowawayDb::TABLE))->toBe(5); + Delete::byFields(ThrowawayDb::TABLE, [Field::notEqual('value', 'purple')]); + expect(Count::all(ThrowawayDb::TABLE))->toBe(2); + }); + test('does nothing when no documents match', function () { + expect(Count::all(ThrowawayDb::TABLE))->toBe(5); + Delete::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'crimson')]); + expect(Count::all(ThrowawayDb::TABLE))->toBe(5); + }); +}); + +describe('::byContains()', function () { + test('throws an exception', function () { + expect(fn () => Delete::byContains('', []))->toThrow(DocumentException::class); + }); +}); + +describe('::byJsonPath()', function () { + test('throws an exception', function () { + expect(fn () => Delete::byJsonPath('', ''))->toThrow(DocumentException::class); + }); +}); diff --git a/tests/Integration/SQLite/DocumentListTest.php b/tests/Integration/SQLite/DocumentListTest.php new file mode 100644 index 0000000..52f97be --- /dev/null +++ b/tests/Integration/SQLite/DocumentListTest.php @@ -0,0 +1,94 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{DocumentException, DocumentList, Query}; +use BitBadger\PDODocument\Mapper\DocumentMapper; +use Test\Integration\SQLite\ThrowawayDb; +use Test\Integration\TestDocument; + +pest()->group('integration', 'sqlite'); + +describe('::create()', function () { + test('creates a document list', function () { + $list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [], + new DocumentMapper(TestDocument::class)); + expect($list)->not->toBeNull(); + $list = null; // free database result + }); +}); + +describe('->items()', function () { + test('enumerates items in the list', function () { + $list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [], + new DocumentMapper(TestDocument::class)); + expect($list)->not->toBeNull(); + $count = 0; + foreach ($list->items() as $item) { + expect(['one', 'two', 'three', 'four', 'five'])->toContain($item->id); + $count++; + } + expect($count)->toBe(5); + }); + test('fails when the list is exhausted', function () { + $list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [], + new DocumentMapper(TestDocument::class)); + expect($list)->not->toBeNull()->hasItems()->toBeTrue(); + $ignored = iterator_to_array($list->items()); + expect($list)->hasItems()->toBeFalse() + ->and(fn () => iterator_to_array($list->items()))->toThrow(DocumentException::class); + }); +}); + +describe('->hasItems()', function () { + test('returns true when items exist', function () { + $list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [], + new DocumentMapper(TestDocument::class)); + expect($list)->not->toBeNull()->hasItems()->toBeTrue(); + foreach ($list->items() as $ignored) { + expect($list)->hasItems()->toBeTrue(); + } + expect($list)->hasItems()->toBeFalse(); + }); + test('returns false when no items exist', function () { + expect(DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'num_value' < 0", [], + new DocumentMapper(TestDocument::class))) + ->not->toBeNull()->hasItems()->toBeFalse(); + }); +}); + +describe('->map()', function () { + test('transforms the list', function () { + $list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [], + new DocumentMapper(TestDocument::class)); + expect($list)->not->toBeNull()->hasItems()->toBeTrue(); + foreach ($list->map(fn($doc) => strrev($doc->id)) as $mapped) { + expect(['eno', 'owt', 'eerht', 'ruof', 'evif'])->toContain($mapped); + } + }); +}); + +describe('->iter()', function () { + test('walks the list', function () { + $list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [], + new DocumentMapper(TestDocument::class)); + expect($list)->not->toBeNull()->hasItems()->toBeTrue(); + $splats = []; + $list->iter(function ($doc) use (&$splats) { $splats[] = str_repeat('*', strlen($doc->id)); }); + expect(implode(' ', $splats))->toBe('*** *** ***** **** ****'); + }); +}); + +describe('->mapToArray()', function () { + test('creates an associative array', function () { + $list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [], + new DocumentMapper(TestDocument::class)); + expect($list)->not->toBeNull()->hasItems()->toBeTrue() + ->and($list->mapToArray(fn($it) => $it->id, fn($it) => $it->value)) + ->toBe(['one' => 'FIRST!', 'two' => 'another', 'three' => '', 'four' => 'purple', 'five' => 'purple']); + }); +}); diff --git a/tests/Integration/SQLite/DocumentTest.php b/tests/Integration/SQLite/DocumentTest.php new file mode 100644 index 0000000..5395409 --- /dev/null +++ b/tests/Integration/SQLite/DocumentTest.php @@ -0,0 +1,231 @@ + + * @license MIT + */ + +declare(strict_types=1); + +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; + +pest()->group('integration', 'sqlite'); + +describe('::insert()', function () { + test('inserts an array with no automatic ID', function () { + Document::insert(ThrowawayDb::TABLE, ['id' => 'turkey', 'sub' => ['foo' => 'gobble', 'bar' => 'gobble']]); + $tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class); + expect($tryDoc)->isSome()->toBeTrue() + ->and($tryDoc->get()) + ->id->toBe('turkey') + ->num_value->toBe(0) + ->sub->not->toBeNull() + ->sub->foo->toBe('gobble') + ->sub->bar->toBe('gobble') + ->and($tryDoc->get()->value)->toBeEmpty(); + }); + test('inserts an array with auto-number ID, not provided', function () { + Configuration::$autoId = AutoId::Number; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + + Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'new', 'num_value' => 8]); + $doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper()); + expect($doc)->isSome()->toBeTrue() + ->and(json_decode($doc->get()['data']))->id->toBe(1); + + Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'again', 'num_value' => 7]); + $doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = 2", [], + new ArrayMapper()); + expect($doc)->isSome()->toBeTrue() + ->and(json_decode($doc->get()['data']))->id->toBe(2); + } finally { + Configuration::$autoId = AutoId::None; + } + }); + test('inserts an array with auto-number ID, provided', function () { + Configuration::$autoId = AutoId::Number; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + Document::insert(ThrowawayDb::TABLE, ['id' => 7, 'value' => 'new', 'num_value' => 8]); + $doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper()); + expect($doc)->isSome()->toBeTrue() + ->and(json_decode($doc->get()['data']))->id->toBe(7); + } finally { + Configuration::$autoId = AutoId::None; + } + }); + test('inserts an array with auto-UUID ID, not provided', function () { + Configuration::$autoId = AutoId::UUID; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + Document::insert(ThrowawayDb::TABLE, ['id' => '', 'num_value' => 5]); + expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 5)], TestDocument::class)) + ->isSome()->toBeTrue()->get()->id->not->toBeEmpty(); + } finally { + Configuration::$autoId = AutoId::None; + } + }); + test('inserts an array with auto-UUID ID, provided', function () { + Configuration::$autoId = AutoId::UUID; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + $uuid = AutoId::generateUUID(); + Document::insert(ThrowawayDb::TABLE, ['id' => $uuid, 'value' => 'uuid', 'num_value' => 12]); + expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 12)], TestDocument::class)) + ->isSome()->toBeTrue()->get()->id->toBe($uuid); + } finally { + Configuration::$autoId = AutoId::None; + } + }); + test('inserts an array with auto-string ID, not provided', function () { + Configuration::$autoId = AutoId::RandomString; + Configuration::$idStringLength = 6; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + Document::insert(ThrowawayDb::TABLE, ['id' => '', 'value' => 'new', 'num_value' => 8]); + expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 8)], TestDocument::class)) + ->isSome()->toBeTrue()->get()->id->toHaveLength(6); + } finally { + Configuration::$autoId = AutoId::None; + Configuration::$idStringLength = 16; + } + }); + test('inserts an array with auto-string ID, provided', function () { + Configuration::$autoId = AutoId::RandomString; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + Document::insert(ThrowawayDb::TABLE, ['id' => 'my-key', 'value' => 'old', 'num_value' => 3]); + expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class)) + ->isSome()->toBeTrue()->get()->id->toBe('my-key'); + } finally { + Configuration::$autoId = AutoId::None; + } + }); + test('inserts an object with no automatic ID', function () { + Document::insert(ThrowawayDb::TABLE, new TestDocument('turkey', sub: new SubDocument('gobble', 'gobble'))); + $tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class); + expect($tryDoc)->isSome()->toBeTrue() + ->and($tryDoc->get()) + ->id->toBe('turkey') + ->num_value->toBe(0) + ->sub->not->toBeNull() + ->sub->foo->toBe('gobble') + ->sub->bar->toBe('gobble') + ->and($tryDoc->get()->value)->toBeEmpty(); + }); + test('inserts an object with auto-number ID, not provided', function () { + Configuration::$autoId = AutoId::Number; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + + Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'taco')); + expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'taco')], NumDocument::class)) + ->isSome()->toBeTrue()->get()->id->toBe(1); + + Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'burrito')); + expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'burrito')], NumDocument::class)) + ->isSome()->toBeTrue()->get()->id->toBe(2); + } finally { + Configuration::$autoId = AutoId::None; + } + }); + test('inserts an object with auto-number ID, provided', function () { + Configuration::$autoId = AutoId::Number; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + Document::insert(ThrowawayDb::TABLE, new NumDocument(64, 'large')); + expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'large')], NumDocument::class)) + ->isSome()->toBeTrue()->get()->id->toBe(64); + } finally { + Configuration::$autoId = AutoId::None; + } + }); + test('inserts an object with auto-UUID ID, not provided', function () { + Configuration::$autoId = AutoId::UUID; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + Document::insert(ThrowawayDb::TABLE, new TestDocument(value: 'something', num_value: 9)); + expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::exists('value')], TestDocument::class)) + ->isSome()->toBeTrue()->get()->id->not->toBeEmpty(); + } finally { + Configuration::$autoId = AutoId::None; + } + }); + test('inserts an object with auto-UUID ID, provided', function () { + Configuration::$autoId = AutoId::UUID; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + $uuid = AutoId::generateUUID(); + Document::insert(ThrowawayDb::TABLE, new TestDocument($uuid, num_value: 14)); + expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 14)], TestDocument::class)) + ->isSome()->toBeTrue()->get()->id->toBe($uuid); + } finally { + Configuration::$autoId = AutoId::None; + } + }); + test('inserts an object with auto-string ID, not provided', function () { + Configuration::$autoId = AutoId::RandomString; + Configuration::$idStringLength = 40; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + Document::insert(ThrowawayDb::TABLE, new TestDocument(num_value: 55)); + expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 55)], TestDocument::class)) + ->isSome()->toBeTrue()->get()->id->toHaveLength(40); + } finally { + Configuration::$autoId = AutoId::None; + Configuration::$idStringLength = 16; + } + }); + test('inserts an object with auto-string ID, provided', function () { + Configuration::$autoId = AutoId::RandomString; + try { + Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); + Document::insert(ThrowawayDb::TABLE, new TestDocument('my-key', num_value: 3)); + expect(Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 3)], TestDocument::class)) + ->isSome()->toBeTrue()->get()->id->toBe('my-key'); + } finally { + Configuration::$autoId = AutoId::None; + } + }); + test('throws an exception for duplicate key', function () { + expect(fn () => Document::insert(ThrowawayDb::TABLE, new TestDocument('one'))) + ->toThrow(DocumentException::class); + }); +}); + +describe('::save()', function () { + test('inserts a new document', function () { + Document::save(ThrowawayDb::TABLE, new TestDocument('test', sub: new SubDocument('a', 'b'))); + expect(Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class))->isSome()->toBeTrue(); + }); + test('updates an existing document', function () { + Document::save(ThrowawayDb::TABLE, new TestDocument('two', num_value: 44)); + $tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class); + expect($tryDoc)->isSome()->toBeTrue() + ->and($tryDoc->get()) + ->num_value->toBe(44) + ->sub->toBeNull(); + }); +}); + +describe('::update()', function () { + test('replaces an existing document', function () { + Document::update(ThrowawayDb::TABLE, 'one', new TestDocument('one', 'howdy', 8, new SubDocument('y', 'z'))); + $tryDoc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class); + expect($tryDoc)->isSome()->toBeTrue() + ->and($tryDoc->get()) + ->num_value->toBe(8) + ->sub->not->toBeNull() + ->sub->foo->toBe('y') + ->sub->bar->toBe('z') + ->and($tryDoc->get()->value)->toBe('howdy'); + }); + test('does nothing for a non-existent document', function () { + expect(Exists::byId(ThrowawayDb::TABLE, 'two-hundred'))->toBeFalse(); + Document::update(ThrowawayDb::TABLE, 'two-hundred', new TestDocument('200')); + expect(Find::byId(ThrowawayDb::TABLE, 'two-hundred', TestDocument::class))->isNone()->toBeTrue(); + }); +}); 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 68% rename from tests/integration/sqlite/ThrowawayDb.php rename to tests/Integration/SQLite/ThrowawayDb.php index b199f31..1cb9a3d 100644 --- a/tests/integration/sqlite/ThrowawayDb.php +++ b/tests/Integration/SQLite/ThrowawayDb.php @@ -21,6 +21,20 @@ class ThrowawayDb /** @var string The table used for document manipulation */ public const TABLE = 'test_table'; + /** + * Load data into the test table + * + * @throws DocumentException If any is encountered + */ + public static function loadData(): void + { + Document::insert(self::TABLE, new TestDocument('one', 'FIRST!', 0)); + Document::insert(self::TABLE, new TestDocument('two', 'another', 10, new SubDocument('green', 'blue'))); + Document::insert(self::TABLE, new TestDocument('three', '', 4)); + Document::insert(self::TABLE, new TestDocument('four', 'purple', 17, new SubDocument('green', 'red'))); + Document::insert(self::TABLE, new TestDocument('five', 'purple', 18)); + } + /** * Create a throwaway SQLite database * @@ -34,13 +48,10 @@ class ThrowawayDb Configuration::useDSN("sqlite:./$fileName"); Configuration::resetPDO(); + Definition::ensureTable(self::TABLE); + if ($withData) { - Definition::ensureTable(self::TABLE); - Document::insert(self::TABLE, new TestDocument('one', 'FIRST!', 0)); - Document::insert(self::TABLE, new TestDocument('two', 'another', 10, new SubDocument('green', 'blue'))); - Document::insert(self::TABLE, new TestDocument('three', '', 4)); - Document::insert(self::TABLE, new TestDocument('four', 'purple', 17, new SubDocument('green', 'red'))); - Document::insert(self::TABLE, new TestDocument('five', 'purple', 18)); + self::loadData(); } return $fileName; diff --git a/tests/Integration/SQLiteIntegrationTest.php b/tests/Integration/SQLiteIntegrationTest.php new file mode 100644 index 0000000..7330f32 --- /dev/null +++ b/tests/Integration/SQLiteIntegrationTest.php @@ -0,0 +1,60 @@ + + * @license MIT + * @see https://github.com/Zaid-Ajaj/ThrowawayDb The origin concept + */ + +declare(strict_types=1); + +namespace Test\Integration; + +use BitBadger\PDODocument\{Configuration, Custom, Delete, DocumentException, Field}; +use BitBadger\PDODocument\Mapper\ExistsMapper; +use PHPUnit\Framework\TestCase; +use Test\Integration\SQLite\ThrowawayDb; + +/** + * Integration Test Class wrapper for SQLite integration tests + */ +class SQLiteIntegrationTest extends TestCase +{ + /** @var string Database name for throwaway database */ + static private string $dbName = ''; + + public static function setUpBeforeClass(): void + { + self::$dbName = ThrowawayDb::create(false); + } + + protected function setUp(): void + { + parent::setUp(); + ThrowawayDb::loadData(); + } + + protected function tearDown(): void + { + Delete::byFields(ThrowawayDb::TABLE, [ Field::exists(Configuration::$idField)]); + parent::tearDown(); + } + + public static function tearDownAfterClass(): void + { + ThrowawayDb::destroy(self::$dbName); + self::$dbName = ''; + } + + /** + * Does the given named object exist in the database? + * + * @param string $name The name of the object whose existence should be verified + * @return bool True if the object exists, false if not + * @throws DocumentException If any is encountered + */ + protected function dbObjectExists(string $name): bool + { + return Custom::scalar('SELECT EXISTS (SELECT 1 FROM sqlite_master WHERE name = :name)', + [':name' => $name], new ExistsMapper()); + } +} 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/Pest.php b/tests/Pest.php new file mode 100644 index 0000000..55d027f --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1,55 @@ +extend(Tests\TestCase::class)->in('Feature'); +pest()->extend(Test\Integration\PgIntegrationTest::class)->in('Integration/PostgreSQL'); +pest()->extend(Test\Integration\SQLiteIntegrationTest::class)->in('Integration/SQLite'); + +/* +|-------------------------------------------------------------------------- +| Expectations +|-------------------------------------------------------------------------- +| +| When you're writing tests, you often need to check that values meet certain conditions. The +| "expect()" function gives you access to a set of "expectations" methods that you can use +| to assert different things. Of course, you may extend the Expectation API at any time. +| +*/ + +expect()->extend('toBeOne', function () { + return $this->toBe(1); +}); + +/* +|-------------------------------------------------------------------------- +| Functions +|-------------------------------------------------------------------------- +| +| While Pest is very powerful out-of-the-box, you may have some testing code specific to your +| project that you don't want to repeat in every file. Here you can also expose helpers as +| global functions to help you to reduce the number of lines of code in your test files. +| +*/ + +/** + * Reset the database mode + */ +function reset_mode(): void +{ + \BitBadger\PDODocument\Configuration::overrideMode(null); +} + +function something() +{ + // .. +} diff --git a/tests/Unit/ConfigurationTest.php b/tests/Unit/ConfigurationTest.php new file mode 100644 index 0000000..93890f6 --- /dev/null +++ b/tests/Unit/ConfigurationTest.php @@ -0,0 +1,36 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{AutoId, Configuration, DocumentException}; + +pest()->group('unit'); + +describe('::$idField', function () { + test('has expected default value', function () { + expect(Configuration::$idField)->toBe('id'); + }); +}); + +describe('::$autoId', function () { + test('has expected default value', function () { + expect(Configuration::$autoId)->toBe(AutoId::None); + }); +}); + +describe('::$idStringLength', function () { + test('has expected default value', function () { + expect(Configuration::$idStringLength)->toBe(16); + }); +}); + +describe('::dbConn()', function () { + test('throws if DSN has not been set', function () { + Configuration::useDSN(''); + expect(fn() => Configuration::dbConn())->toThrow(DocumentException::class); + }); +}); diff --git a/tests/Unit/DocumentExceptionTest.php b/tests/Unit/DocumentExceptionTest.php new file mode 100644 index 0000000..21fb6e9 --- /dev/null +++ b/tests/Unit/DocumentExceptionTest.php @@ -0,0 +1,40 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\DocumentException; + +pest()->group('unit'); + +describe('Constructor', function () { + test('fills code and prior exception if provided', function () { + $priorEx = new Exception('Uh oh'); + expect(new DocumentException('Test Exception', 17, $priorEx)) + ->not->toBeNull() + ->getMessage()->toBe('Test Exception') + ->getCode()->toBe(17) + ->getPrevious()->toBe($priorEx); + }); + test('uses expected code and prior exception if not provided', function () { + expect(new DocumentException('Oops')) + ->not->toBeNull() + ->getMessage()->toBe('Oops') + ->getCode()->toBe(0) + ->getPrevious()->toBeNull(); + }); +}); + +describe('->__toString()', function () { + test('excludes code if 0', function () { + $ex = new DocumentException('Test failure'); + expect("$ex")->toBe("BitBadger\PDODocument\DocumentException: Test failure\n"); + }); + test('includes code if non-zero', function () { + $ex = new DocumentException('Oof', -6); + expect("$ex")->toBe("BitBadger\PDODocument\DocumentException: [-6] Oof\n"); + }); +}); diff --git a/tests/Unit/FieldMatchTest.php b/tests/Unit/FieldMatchTest.php new file mode 100644 index 0000000..fee3edb --- /dev/null +++ b/tests/Unit/FieldMatchTest.php @@ -0,0 +1,20 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\FieldMatch; + +pest()->group('unit'); + +describe('->toSQL()', function () { + test('returns AND for All', function () { + expect(FieldMatch::All)->toSQL()->toBe('AND'); + }); + test('returns OR for Any', function () { + expect(FieldMatch::Any)->toSQL()->toBe('OR'); + }); +}); diff --git a/tests/Unit/FieldTest.php b/tests/Unit/FieldTest.php new file mode 100644 index 0000000..cc1cc38 --- /dev/null +++ b/tests/Unit/FieldTest.php @@ -0,0 +1,418 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Configuration, Field, Mode, Op}; + +pest()->group('unit'); + +describe('->appendParameter()', function () { + afterEach(function () { Configuration::overrideMode(null); }); + test('appends no parameter for exists', function () { + expect(Field::exists('exists')->appendParameter([]))->toBeEmpty(); + }); + test('appends no parameter for notExists', function () { + expect(Field::notExists('absent')->appendParameter([]))->toBeEmpty(); + }); + test('appends two parameters for between', function () { + expect(Field::between('exists', 5, 9, '@num')->appendParameter([])) + ->toHaveLength(2) + ->toEqual(['@nummin' => 5, '@nummax' => 9]); + }); + test('appends a parameter for each value for in', function () { + expect(Field::in('it', ['test', 'unit', 'great'], ':val')->appendParameter([])) + ->toHaveLength(3) + ->toEqual([':val_0' => 'test', ':val_1' => 'unit', ':val_2' => 'great']); + }); + test('appends a parameter for each value for inArray [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Field::inArray('it', 'table', [2, 8, 64], ':bit')->appendParameter([])) + ->toHaveLength(3) + ->toEqual([':bit_0' => '2', ':bit_1' => '8', ':bit_2' => '64']); + })->group('postgresql'); + test('appends a parameter for each value for inArray [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(Field::inArray('it', 'table', [2, 8, 64], ':bit')->appendParameter([])) + ->toHaveLength(3) + ->toEqual([':bit_0' => 2, ':bit_1' => 8, ':bit_2' => 64]); + })->group('sqlite'); + test('appends a parameter for other operators', function () { + expect(Field::equal('the_field', 33, ':test')->appendParameter([])) + ->toHaveLength(1) + ->toEqual([':test' => 33]); + }); +}); + +describe('->path()', function () { + afterEach(function () { Configuration::overrideMode(null); }); + test('returns simple SQL path [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Field::equal('it', 'that'))->path()->toBe("data->>'it'"); + })->group('postgresql'); + test('returns simple SQL path [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(Field::equal('top', 'that'))->path()->toBe("data->>'top'"); + })->group('sqlite'); + test('returns nested SQL path [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Field::equal('parts.to.the.path', ''))->path()->toBe("data#>>'{parts,to,the,path}'"); + })->group('postgresql'); + test('returns nested SQL path [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(Field::equal('one.two.three', ''))->path()->toBe("data->'one'->'two'->>'three'"); + })->group('sqlite'); + test('returns simple JSON path [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Field::equal('it', 'that'))->path(true)->toBe("data->'it'"); + })->group('postgresql'); + test('returns simple JSON path [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(Field::equal('top', 'that'))->path(true)->toBe("data->'top'"); + })->group('sqlite'); + test('returns nested JSON path [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Field::equal('parts.to.the.path', ''))->path(true)->toBe("data#>'{parts,to,the,path}'"); + })->group('postgresql'); + test('returns nested JSON path [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(Field::equal('one.two.three', ''))->path(true)->toBe("data->'one'->'two'->'three'"); + })->group('sqlite'); +}); + +describe('->toWhere()', function () { + afterEach(function () { Configuration::overrideMode(null); }); + test('generates IS NOT NULL for exists w/o qualifier [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Field::exists('that_field'))->toWhere()->toBe("data->>'that_field' IS NOT NULL"); + })->group('postgresql'); + test('generates IS NOT NULL for exists w/o qualifier [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(Field::exists('that_field'))->toWhere()->toBe("data->>'that_field' IS NOT NULL"); + })->group('sqlite'); + test('generates IS NULL for notExists w/o qualifier [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Field::notExists('a_field'))->toWhere()->toBe("data->>'a_field' IS NULL"); + })->group('postgresql'); + test('generates IS NULL for notExists w/o qualifier [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(Field::notExists('a_field'))->toWhere()->toBe("data->>'a_field' IS NULL"); + })->group('sqlite'); + test('generates BETWEEN for between w/o qualifier [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(Field::between('age', 13, 17, '@age'))->toWhere()->toBe("data->>'age' BETWEEN @agemin AND @agemax"); + })->group('sqlite'); + test('generates BETWEEN for between w/o qualifier, numeric range [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Field::between('age', 13, 17, '@age'))->toWhere() + ->toBe("(data->>'age')::numeric BETWEEN @agemin AND @agemax"); + })->group('postgresql'); + test('generates BETWEEN for between w/o qualifier, non-numeric range [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Field::between('city', 'Atlanta', 'Chicago', ':city'))->toWhere() + ->toBe("data->>'city' BETWEEN :citymin AND :citymax"); + })->group('postgresql'); + test('generates BETWEEN for between w/ qualifier [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + $field = Field::between('age', 13, 17, '@age'); + $field->qualifier = 'me'; + expect($field)->toWhere()->toBe("me.data->>'age' BETWEEN @agemin AND @agemax"); + })->group('sqlite'); + test('generates BETWEEN for between w/ qualifier, numeric range [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + $field = Field::between('age', 13, 17, '@age'); + $field->qualifier = 'me'; + expect($field)->toWhere()->toBe("(me.data->>'age')::numeric BETWEEN @agemin AND @agemax"); + })->group('postgresql'); + test('generates BETWEEN for between w/ qualifier, non-numeric range [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + $field = Field::between('city', 'Atlanta', 'Chicago', ':city'); + $field->qualifier = 'me'; + expect($field)->toWhere()->toBe("me.data->>'city' BETWEEN :citymin AND :citymax"); + })->group('postgresql'); + test('generates IN for in, non-numeric values [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Field::in('test', ['Atlanta', 'Chicago'], ':city'))->toWhere() + ->toBe("data->>'test' IN (:city_0, :city_1)"); + })->group('postgresql'); + test('generates IN for in, numeric values [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Field::in('even', [2, 4, 6], ':nbr'))->toWhere() + ->toBe("(data->>'even')::numeric IN (:nbr_0, :nbr_1, :nbr_2)"); + })->group('postgresql'); + test('generates IN for in [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(Field::in('test', ['Atlanta', 'Chicago'], ':city'))->toWhere() + ->toBe("data->>'test' IN (:city_0, :city_1)"); + })->group('sqlite'); + test('generates clause for inArray [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Field::inArray('even', 'tbl', [2, 4, 6, 8], ':it'))->toWhere() + ->toBe("data->'even' ??| ARRAY[:it_0, :it_1, :it_2, :it_3]"); + })->group('postgresql'); + test('generates clause for inArray [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(Field::inArray('test', 'tbl', ['Atlanta', 'Chicago'], ':city'))->toWhere() + ->toBe("EXISTS (SELECT 1 FROM json_each(tbl.data, '\$.test') WHERE value IN (:city_0, :city_1))"); + })->group('sqlite'); + test('generates clause for other operators w/o qualifier [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Field::equal('some_field', '', ':value'))->toWhere()->toBe("data->>'some_field' = :value"); + })->group('postgresql'); + test('generates clause for other operators w/o qualifier [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(Field::equal('some_field', '', ':value'))->toWhere()->toBe("data->>'some_field' = :value"); + })->group('sqlite'); + test('generates no-parameter clause w/ qualifier [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + $field = Field::exists('no_field'); + $field->qualifier = 'test'; + expect($field)->toWhere()->toBe("test.data->>'no_field' IS NOT NULL"); + })->group('postgresql'); + test('generates no-parameter clause w/ qualifier [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + $field = Field::exists('no_field'); + $field->qualifier = 'test'; + expect($field)->toWhere()->toBe("test.data->>'no_field' IS NOT NULL"); + })->group('sqlite'); + test('generates parameter clause w/ qualifier [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + $field = Field::lessOrEqual('le_field', 18, ':it'); + $field->qualifier = 'q'; + expect($field)->toWhere()->toBe("(q.data->>'le_field')::numeric <= :it"); + })->group('postgresql'); + test('generates parameter clause w/ qualifier [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + $field = Field::lessOrEqual('le_field', 18, ':it'); + $field->qualifier = 'q'; + expect($field)->toWhere()->toBe("q.data->>'le_field' <= :it"); + })->group('sqlite'); +}); + +describe('::equal()', function () { + test('creates Field w/o parameter', function () { + $field = Field::equal('my_test', 9); + expect($field) + ->not->toBeNull() + ->fieldName->toBe('my_test') + ->op->toBe(Op::Equal) + ->paramName->toBeEmpty() + ->and($field->value)->toBe(9); + }); + test('creates Field w/ parameter', function () { + $field = Field::equal('another_test', 'turkey', ':test'); + expect($field) + ->not->toBeNull() + ->fieldName->toBe('another_test') + ->op->toBe(Op::Equal) + ->paramName->toBe(':test') + ->and($field->value)->toBe('turkey'); + }); +}); + +describe('::greater()', function () { + test('creates Field w/o parameter', function () { + $field = Field::greater('your_test', 4); + expect($field) + ->not->toBeNull() + ->fieldName->toBe('your_test') + ->op->toBe(Op::Greater) + ->paramName->toBeEmpty() + ->and($field->value)->toBe(4); + }); + test('creates Field w/ parameter', function () { + $field = Field::greater('more_test', 'chicken', ':value'); + expect($field) + ->not->toBeNull() + ->fieldName->toBe('more_test') + ->op->toBe(Op::Greater) + ->paramName->toBe(':value') + ->and($field->value)->toBe('chicken'); + }); +}); + +describe('::greaterOrEqual()', function () { + test('creates Field w/o parameter', function () { + $field = Field::greaterOrEqual('their_test', 6); + expect($field) + ->not->toBeNull() + ->fieldName->toBe('their_test') + ->op->toBe(Op::GreaterOrEqual) + ->paramName->toBeEmpty() + ->and($field->value)->toBe(6); + }); + test('creates Field w/ parameter', function () { + $field = Field::greaterOrEqual('greater_test', 'poultry', ':cluck'); + expect($field) + ->not->toBeNull() + ->fieldName->toBe('greater_test') + ->op->toBe(Op::GreaterOrEqual) + ->paramName->toBe(':cluck') + ->and($field->value)->toBe('poultry'); + }); +}); + +describe('::less()', function () { + test('creates Field w/o parameter', function () { + $field = Field::less('z', 32); + expect($field) + ->not->toBeNull() + ->fieldName->toBe('z') + ->op->toBe(Op::Less) + ->paramName->toBeEmpty() + ->and($field->value)->toBe(32); + }); + test('creates Field w/ parameter', function () { + $field = Field::less('additional_test', 'fowl', ':boo'); + expect($field) + ->not->toBeNull() + ->fieldName->toBe('additional_test') + ->op->toBe(Op::Less) + ->paramName->toBe(':boo') + ->and($field->value)->toBe('fowl'); + }); +}); + +describe('::lessOrEqual()', function () { + test('creates Field w/o parameter', function () { + $field = Field::lessOrEqual('g', 87); + expect($field) + ->not->toBeNull() + ->fieldName->toBe('g') + ->op->toBe(Op::LessOrEqual) + ->paramName->toBeEmpty() + ->and($field->value)->toBe(87); + }); + test('creates Field w/ parameter', function () { + $field = Field::lessOrEqual('lesser_test', 'hen', ':woo'); + expect($field) + ->not->toBeNull() + ->fieldName->toBe('lesser_test') + ->op->toBe(Op::LessOrEqual) + ->paramName->toBe(':woo') + ->and($field->value)->toBe('hen'); + }); +}); + +describe('::notEqual()', function () { + test('creates Field w/o parameter', function () { + $field = Field::notEqual('j', 65); + expect($field) + ->not->toBeNull() + ->fieldName->toBe('j') + ->op->toBe(Op::NotEqual) + ->paramName->toBeEmpty() + ->and($field->value)->toBe(65); + }); + test('creates Field w/ parameter', function () { + $field = Field::notEqual('unequal_test', 'egg', ':zoo'); + expect($field) + ->not->toBeNull() + ->fieldName->toBe('unequal_test') + ->op->toBe(Op::NotEqual) + ->paramName->toBe(':zoo') + ->and($field->value)->toBe('egg'); + }); +}); + +describe('::between()', function () { + test('creates Field w/o parameter', function () { + $field = Field::between('k', 'alpha', 'zed'); + expect($field) + ->not->toBeNull() + ->fieldName->toBe('k') + ->op->toBe(Op::Between) + ->paramName->toBeEmpty() + ->and($field->value)->toEqual(['alpha', 'zed']); + }); + test('creates Field w/ parameter', function () { + $field = Field::between('between_test', 18, 49, ':count'); + expect($field) + ->not->toBeNull() + ->fieldName->toBe('between_test') + ->op->toBe(Op::Between) + ->paramName->toBe(':count') + ->and($field->value)->toEqual([18, 49]); + }); +}); + +describe('::in()', function () { + test('creates Field w/o parameter', function () { + $field = Field::in('test', [1, 2, 3]); + expect($field) + ->not->toBeNull() + ->fieldName->toBe('test') + ->op->toBe(Op::In) + ->paramName->toBeEmpty() + ->and($field->value)->toEqual([1, 2, 3]); + }); + test('creates Field w/ parameter', function () { + $field = Field::in('unit', ['a', 'b'], ':inParam'); + expect($field) + ->not->toBeNull() + ->fieldName->toBe('unit') + ->op->toBe(Op::In) + ->paramName->toBe(':inParam') + ->and($field->value)->toEqual(['a', 'b']); + }); +}); + +describe('::inArray()', function () { + test('creates Field w/o parameter', function () { + $field = Field::inArray('test', 'tbl', [1, 2, 3]); + expect($field) + ->not->toBeNull() + ->fieldName->toBe('test') + ->op->toBe(Op::InArray) + ->paramName->toBeEmpty() + ->and($field->value)->toEqual(['table' => 'tbl', 'values' => [1, 2, 3]]); + }); + test('creates Field w/ parameter', function () { + $field = Field::inArray('unit', 'tab', ['a', 'b'], ':inAParam'); + expect($field) + ->not->toBeNull() + ->fieldName->toBe('unit') + ->op->toBe(Op::InArray) + ->paramName->toBe(':inAParam') + ->and($field->value)->toEqual(['table' => 'tab', 'values' => ['a', 'b']]); + }); +}); + +describe('::exists()', function () { + test('creates Field', function () { + $field = Field::exists('be_there'); + expect($field) + ->not->toBeNull() + ->fieldName->toBe('be_there') + ->op->toBe(Op::Exists) + ->paramName->toBeEmpty() + ->and($field->value)->toBeEmpty(); + }); +}); + +describe('::notExists()', function () { + test('creates Field', function () { + $field = Field::notExists('be_absent'); + expect($field) + ->not->toBeNull() + ->fieldName->toBe('be_absent') + ->op->toBe(Op::NotExists) + ->paramName->toBeEmpty() + ->and($field->value)->toBeEmpty(); + }); +}); + +describe('::named()', function () { + test('creates Field', function () { + $field = Field::named('the_field'); + expect($field) + ->not->toBeNull() + ->fieldName->toBe('the_field') + ->op->toBe(Op::Equal) + ->value->toBeEmpty() + ->and($field->value)->toBeEmpty(); + }); +}); diff --git a/tests/Unit/Mapper/ArrayMapperTest.php b/tests/Unit/Mapper/ArrayMapperTest.php new file mode 100644 index 0000000..094ff9f --- /dev/null +++ b/tests/Unit/Mapper/ArrayMapperTest.php @@ -0,0 +1,18 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\Mapper\ArrayMapper; + +pest()->group('unit'); + +describe('->map()', function () { + test('returns the given array', function () { + $result = ['one' => 2, 'three' => 4, 'eight' => 'five']; + expect((new ArrayMapper())->map($result))->toBe($result); + }); +}); diff --git a/tests/Unit/Mapper/CountMapperTest.php b/tests/Unit/Mapper/CountMapperTest.php new file mode 100644 index 0000000..e90e94f --- /dev/null +++ b/tests/Unit/Mapper/CountMapperTest.php @@ -0,0 +1,17 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\Mapper\CountMapper; + +pest()->group('unit'); + +describe('->map()', function () { + test('returns item 0 in the given array', function () { + expect((new CountMapper())->map([5, 8, 10]))->toBe(5); + }); +}); diff --git a/tests/Unit/Mapper/DocumentMapperTest.php b/tests/Unit/Mapper/DocumentMapperTest.php new file mode 100644 index 0000000..f77f71b --- /dev/null +++ b/tests/Unit/Mapper/DocumentMapperTest.php @@ -0,0 +1,65 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{DocumentException, Field}; +use BitBadger\PDODocument\Mapper\DocumentMapper; +use Test\{PjsonDocument, PjsonId}; + +// ** Test class hierarchy for serialization ** + +class DocMapSubDoc +{ + public function __construct(public int $id = 0, public string $name = '') { } +} + +class DocMapTestDoc +{ + public function __construct(public int $id = 0, public DocMapSubDoc $subDoc = new DocMapSubDoc()) { } +} + +pest()->group('unit'); + +describe('Constructor', function () { + test('uses "data" as the default field name', function () { + expect(new DocumentMapper(Field::class))->fieldName->toBe('data'); + }); + test('uses the provided field name', function () { + expect(new DocumentMapper(Field::class, 'json'))->fieldName->toBe('json'); + }); +}); + +describe('->map()', function () { + test('deserializes valid JSON', function () { + $doc = (new DocumentMapper(DocMapTestDoc::class)) + ->map(['data' => '{"id":7,"subDoc":{"id":22,"name":"tester"}}']); + expect($doc) + ->not->toBeNull() + ->id->toBe(7) + ->and($doc->subDoc) + ->not->toBeNull() + ->id->toBe(22) + ->name->toBe('tester'); + }); + test('deserializes valid JSON [Pjson]', function () { + $doc = (new DocumentMapper(PjsonDocument::class))->map(['data' => '{"id":"seven","name":"bob","num_value":8}']); + expect($doc) + ->not->toBeNull() + ->id->toEqual(new PjsonId('seven')) + ->name->toBe('bob') + ->numValue->toBe(8) + ->skipped->toBeEmpty(); + }); + test('throws for invalid JSON', function () { + expect(fn() => (new DocumentMapper(DocMapTestDoc::class))->map(['data' => 'this is not valid'])) + ->toThrow(DocumentException::class); + }); + test('throws for invalid JSON [Pjson]', function () { + expect(fn() => (new DocumentMapper(PjsonDocument::class))->map(['data' => 'not even close'])) + ->toThrow(DocumentException::class); + }); +}); diff --git a/tests/Unit/Mapper/ExistsMapperTest.php b/tests/Unit/Mapper/ExistsMapperTest.php new file mode 100644 index 0000000..ca7dc9a --- /dev/null +++ b/tests/Unit/Mapper/ExistsMapperTest.php @@ -0,0 +1,28 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Configuration, DocumentException, Mode}; +use BitBadger\PDODocument\Mapper\ExistsMapper; + +pest()->group('unit'); + +afterEach(function () { Configuration::overrideMode(null); }); + +describe('->map()', function () { + test('returns a boolean value from index 0 [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect((new ExistsMapper())->map([false, 'nope']))->toBeFalse(); + }); + test('returns a number value as boolean from index 0 [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect((new ExistsMapper())->map([1, 'yep']))->toBeTrue(); + }); + test('throws if mode is not set', function () { + expect(fn() => (new ExistsMapper())->map(['0']))->toThrow(DocumentException::class); + }); +}); diff --git a/tests/Unit/Mapper/StringMapperTest.php b/tests/Unit/Mapper/StringMapperTest.php new file mode 100644 index 0000000..11cdb32 --- /dev/null +++ b/tests/Unit/Mapper/StringMapperTest.php @@ -0,0 +1,23 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\Mapper\StringMapper; + +pest()->group('unit'); + +describe('->map()', function () { + test('returns existing string column value', function () { + expect(new StringMapper('test_field'))->map(['test_field' => 'test_value'])->toBe('test_value'); + }); + test('returns string value of non-string column', function () { + expect(new StringMapper('a_number'))->map(['a_number' => 6.7])->toBe('6.7'); + }); + test('returns null for a missing column', function () { + expect(new StringMapper('something_else'))->map([])->toBeNull(); + }); +}); diff --git a/tests/Unit/ModeTest.php b/tests/Unit/ModeTest.php new file mode 100644 index 0000000..e8f569a --- /dev/null +++ b/tests/Unit/ModeTest.php @@ -0,0 +1,23 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{DocumentException, Mode}; + +pest()->group('unit'); + +describe('::deriveFromDSN()', function () { + test('derives for PostgreSQL', function () { + expect(Mode::deriveFromDSN('pgsql:Host=localhost'))->toBe(Mode::PgSQL); + })->group('postgresql'); + test('derives for SQLite', function () { + expect(Mode::deriveFromDSN('sqlite:data.db'))->toBe(Mode::SQLite); + })->group('sqlite'); + test('throws for other drivers', function () { + expect(fn() => Mode::deriveFromDSN('mysql:Host=localhost'))->toThrow(DocumentException::class); + }); +}); diff --git a/tests/Unit/OpTest.php b/tests/Unit/OpTest.php new file mode 100644 index 0000000..8311ef2 --- /dev/null +++ b/tests/Unit/OpTest.php @@ -0,0 +1,47 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\Op; + +pest()->group('unit'); + +describe('->toSQL()', function () { + test('returns "=" for Equal', function () { + expect(Op::Equal)->toSQL()->toBe('='); + }); + test('returns ">" for Greater', function () { + expect(Op::Greater)->toSQL()->toBe('>'); + }); + test('returns ">=" for GreaterOrEqual', function () { + expect(Op::GreaterOrEqual)->toSQL()->toBe('>='); + }); + test('returns "<" for Less', function () { + expect(Op::Less)->toSQL()->toBe('<'); + }); + test('returns "<=" for LessOrEqual', function () { + expect(Op::LessOrEqual)->toSQL()->toBe('<='); + }); + test('returns "<>" for NotEqual', function () { + expect(Op::NotEqual)->toSQL()->toBe('<>'); + }); + test('returns "BETWEEN" for Between', function () { + expect(Op::Between)->toSQL()->toBe('BETWEEN'); + }); + test('returns "IN" for In', function () { + expect(Op::In)->toSQL()->toBe('IN'); + }); + test('returns "?|" (escaped) for InArray', function () { + expect(Op::InArray)->toSQL()->toBe('??|'); + }); + test('returns "IS NOT NULL" for Exists', function () { + expect(Op::Exists)->toSQL()->toBe('IS NOT NULL'); + }); + test('returns "IS NULL" for NotExists', function () { + expect(Op::NotExists)->toSQL()->toBe('IS NULL'); + }); +}); diff --git a/tests/Unit/ParametersTest.php b/tests/Unit/ParametersTest.php new file mode 100644 index 0000000..b40deea --- /dev/null +++ b/tests/Unit/ParametersTest.php @@ -0,0 +1,85 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode, Parameters}; +use Test\{PjsonDocument, PjsonId}; + +pest()->group('unit'); + +describe('::id()', function () { + test('creates string ID parameter', function () { + expect(Parameters::id('key'))->toEqual([':id' => 'key']); + }); + test('creates string from numeric ID parameter', function () { + expect(Parameters::id(7))->toEqual([':id' => '7']); + }); +}); + +describe('::json()', function () { + test('serializes an array', function () { + expect(Parameters::json(':it', ['id' => 18, 'url' => 'https://www.unittest.com'])) + ->toEqual([':it' => '{"id":18,"url":"https://www.unittest.com"}']); + }); + test('serializes an array w/ an empty array value', function () { + expect(Parameters::json(':it', ['id' => 18, 'urls' => []]))->toEqual([':it' => '{"id":18,"urls":[]}']); + }); + test('serializes a 1-D array w/ an empty array value', function () { + expect(Parameters::json(':it', ['urls' => []]))->toEqual([':it' => '{"urls":[]}']); + }); + test('serializes a stdClass instance', function () { + $obj = new stdClass(); + $obj->id = 19; + $obj->url = 'https://testhere.info'; + expect(Parameters::json(':it', $obj))->toEqual([':it' => '{"id":19,"url":"https://testhere.info"}']); + }); + test('serializes a Pjson class instance', function () { + expect(Parameters::json(':it', new PjsonDocument(new PjsonId('999'), 'a test', 98, 'nothing'))) + ->toEqual([':it' => '{"id":"999","name":"a test","num_value":98}']); + }); + test('serializes an array of Pjson class instances', function () { + expect(Parameters::json(':it', + ['pjson' => [new PjsonDocument(new PjsonId('997'), 'another test', 94, 'nothing')]])) + ->toEqual([':it' => '{"pjson":[{"id":"997","name":"another test","num_value":94}]}']); + }); +}); + +describe('::nameFields()', function () { + test('provides missing parameter names', function () { + $named = [Field::equal('it', 17), Field::equal('also', 22, ':also'), Field::equal('other', 24)]; + Parameters::nameFields($named); + expect($named) + ->toHaveLength(3) + ->sequence( + fn($it) => $it->paramName->toBe(':field0'), + fn($it) => $it->paramName->toBe(':also'), + fn($it) => $it->paramName->toBe(':field2')); + }); +}); + +describe('::addFields()', function () { + test('appends to an existing parameter array', function () { + expect(Parameters::addFields([Field::equal('b', 'two', ':b'), Field::equal('z', 18, ':z')], [':a' => 1])) + ->toEqual([':a' => 1, ':b' => 'two', ':z' => 18]); + }); +}); + +describe('::fieldNames()', function () { + afterEach(function () { Configuration::overrideMode(null); }); + test('generates names [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Parameters::fieldNames(':names', ['one', 'two', 'seven']))->toEqual([':names' => "{one,two,seven}"]); + })->group('postgresql'); + test('generates names [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(Parameters::fieldNames(':it', ['test', 'unit', 'wow'])) + ->toEqual([':it0' => '$.test', ':it1' => '$.unit', ':it2' => '$.wow']); + })->group('sqlite'); + test('throws when mode is not set', function () { + expect(fn() => Parameters::fieldNames('', []))->toThrow(DocumentException::class); + }); +}); diff --git a/tests/Unit/Query/CountTest.php b/tests/Unit/Query/CountTest.php new file mode 100644 index 0000000..25f8cd9 --- /dev/null +++ b/tests/Unit/Query/CountTest.php @@ -0,0 +1,51 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode}; +use BitBadger\PDODocument\Query\Count; + +pest()->group('unit'); + +afterEach(function () { Configuration::overrideMode(null); }); + +describe('::all()', function () { + test('generates the correct SQL', function () { + expect(Count::all('a_table'))->toBe('SELECT COUNT(*) FROM a_table'); + }); +}); + +describe('::byFields()', function () { + test('generates the correct SQL', function () { + Configuration::overrideMode(Mode::SQLite); + expect(Count::byFields('somewhere', [Field::greater('errors', 10, ':errors')])) + ->toBe("SELECT COUNT(*) FROM somewhere WHERE data->>'errors' > :errors"); + }); +}); + +describe('::byContains()', function () { + test('generates the correct SQL [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Count::byContains('the_table'))->toBe('SELECT COUNT(*) FROM the_table WHERE data @> :criteria'); + })->group('postgresql'); + test('throws an exception [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(fn () => Count::byContains(''))->toThrow(DocumentException::class); + })->group('sqlite'); +}); + +describe('::byJsonPath()', function () { + test('generates the correct SQL [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Count::byJsonPath('a_table')) + ->toBe('SELECT COUNT(*) FROM a_table WHERE jsonb_path_exists(data, :path::jsonpath)'); + })->group('postgresql'); + test('throws an exception [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(fn () => Count::byJsonPath(''))->toThrow(DocumentException::class); + })->group('sqlite'); +}); diff --git a/tests/Unit/Query/DefinitionTest.php b/tests/Unit/Query/DefinitionTest.php new file mode 100644 index 0000000..d182422 --- /dev/null +++ b/tests/Unit/Query/DefinitionTest.php @@ -0,0 +1,65 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Configuration, DocumentException, DocumentIndex, Mode}; +use BitBadger\PDODocument\Query\Definition; + +pest()->group('unit'); + +describe('::ensureTable()', function () { + afterEach(function () { Configuration::overrideMode(null); }); + test('generates correct SQL [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Definition::ensureTable('documents')) + ->toBe('CREATE TABLE IF NOT EXISTS documents (data JSONB NOT NULL)'); + })->group('postgresql'); + test('generates correct SQL [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(Definition::ensureTable('dox'))->toBe('CREATE TABLE IF NOT EXISTS dox (data TEXT NOT NULL)'); + })->group('sqlite'); + test('throws an exception if mode is not set', function () { + expect(fn () => Definition::ensureTable(''))->toThrow(DocumentException::class); + }); +}); + +describe('::ensureIndexOn()', function () { + test('generates correct SQL for unqualified table, single ascending field', function () { + expect(Definition::ensureIndexOn('test', 'fields', ['details'])) + ->toBe("CREATE INDEX IF NOT EXISTS idx_test_fields ON test ((data->>'details'))"); + }); + test('generates correct SQL for qualified table, multiple fields', function () { + expect(Definition::ensureIndexOn('sch.testing', 'json', ['group', 'sub_group DESC'])) + ->toBe('CREATE INDEX IF NOT EXISTS idx_testing_json ON sch.testing ' + . "((data->>'group'), (data->>'sub_group') DESC)"); + }); +}); + +describe('::ensureKey()', function () { + test('generates correct SQL', function () { + expect(Definition::ensureKey('tbl')) + ->toBe("CREATE UNIQUE INDEX IF NOT EXISTS idx_tbl_key ON tbl ((data->>'id'))"); + }); +}); + +describe('::ensureDocumentIndexOn()', function () { + afterEach(function () { Configuration::overrideMode(null); }); + test('generates correct SQL for qualified table, full index [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Definition::ensureDocumentIndexOn('my.tbl', DocumentIndex::Full)) + ->toBe("CREATE INDEX IF NOT EXISTS idx_tbl_document ON my.tbl USING GIN (data)"); + })->group('postgresql'); + test('generates correct SQL for unqualified table, optimized index [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Definition::ensureDocumentIndexOn('it', DocumentIndex::Optimized)) + ->toBe("CREATE INDEX IF NOT EXISTS idx_it_document ON it USING GIN (data jsonb_path_ops)"); + })->group('postgresql'); + test('throws an exception [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(fn () => Definition::ensureDocumentIndexOn('', DocumentIndex::Full))->toThrow(DocumentException::class); + })->group('sqlite'); +}); diff --git a/tests/Unit/Query/DeleteTest.php b/tests/Unit/Query/DeleteTest.php new file mode 100644 index 0000000..7a95360 --- /dev/null +++ b/tests/Unit/Query/DeleteTest.php @@ -0,0 +1,52 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode}; +use BitBadger\PDODocument\Query\Delete; + +pest()->group('unit'); + +afterEach(function () { Configuration::overrideMode(null); }); + +describe('::byId()', function () { + test('generates correct SQL', function () { + Configuration::overrideMode(Mode::SQLite); + expect(Delete::byId('over_there'))->toBe("DELETE FROM over_there WHERE data->>'id' = :id"); + }); +}); + +describe('::byFields()', function () { + test('generates correct SQL', function () { + Configuration::overrideMode(Mode::SQLite); + expect(Delete::byFields('my_table', + [Field::less('value', 99, ':max'), Field::greaterOrEqual('value', 18, ':min')])) + ->toBe("DELETE FROM my_table WHERE data->>'value' < :max AND data->>'value' >= :min"); + }); +}); + +describe('::byContains()', function () { + test('generates correct SQL [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Delete::byContains('somewhere'))->toBe('DELETE FROM somewhere WHERE data @> :criteria'); + })->group('postgresql'); + test('throws an exception [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(fn () => Delete::byContains(''))->toThrow(DocumentException::class); + })->group('sqlite'); +}); + +describe('::byJsonPath()', function () { + test('generates correct SQL [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Delete::byJsonPath('here'))->toBe('DELETE FROM here WHERE jsonb_path_exists(data, :path::jsonpath)'); + })->group('postgresql'); + test('throws an exception [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(fn () => Delete::byJsonPath(''))->toThrow(DocumentException::class); + }); +}); diff --git a/tests/Unit/Query/ExistsTest.php b/tests/Unit/Query/ExistsTest.php new file mode 100644 index 0000000..216c312 --- /dev/null +++ b/tests/Unit/Query/ExistsTest.php @@ -0,0 +1,59 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode}; +use BitBadger\PDODocument\Query\Exists; + +pest()->group('unit'); + +afterEach(function () { Configuration::overrideMode(null); }); + +describe('::query()', function () { + test('generates correct SQL', function () { + Configuration::overrideMode(Mode::SQLite); + expect(Exists::query('abc', 'def'))->toBe('SELECT EXISTS (SELECT 1 FROM abc WHERE def)'); + }); +}); + +describe('::byId()', function () { + test('generates correct SQL', function () { + Configuration::overrideMode(Mode::SQLite); + expect(Exists::byId('dox'))->toBe("SELECT EXISTS (SELECT 1 FROM dox WHERE data->>'id' = :id)"); + }); +}); + +describe('::byFields()', function () { + test('generates correct SQL', function () { + Configuration::overrideMode(Mode::SQLite); + expect(Exists::byFields('box', [Field::notEqual('status', 'occupied', ':status')])) + ->toBe("SELECT EXISTS (SELECT 1 FROM box WHERE data->>'status' <> :status)"); + }); +}); + +describe('::byContains()', function () { + test('generates correct SQL [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Exists::byContains('pocket'))->toBe('SELECT EXISTS (SELECT 1 FROM pocket WHERE data @> :criteria)'); + })->group('postgresql'); + test('throws an exception [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(fn () => Exists::byContains(''))->toThrow(DocumentException::class); + })->group('sqlite'); +}); + +describe('::byJsonPath()', function () { + test('generates correct SQL [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Exists::byJsonPath('lint')) + ->toBe('SELECT EXISTS (SELECT 1 FROM lint WHERE jsonb_path_exists(data, :path::jsonpath))'); + })->group('postgresql'); + test('throws an exception [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(fn () => Exists::byJsonPath(''))->toThrow(DocumentException::class); + })->group('sqlite'); +}); diff --git a/tests/Unit/Query/FindTest.php b/tests/Unit/Query/FindTest.php new file mode 100644 index 0000000..a5e1dcc --- /dev/null +++ b/tests/Unit/Query/FindTest.php @@ -0,0 +1,53 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Configuration, DocumentException, Field, FieldMatch, Mode}; +use BitBadger\PDODocument\Query\Find; + +pest()->group('unit'); + +afterEach(function () { Configuration::overrideMode(null); }); + +describe('::byId()', function () { + test('generates correct SQL', function () { + Configuration::overrideMode(Mode::SQLite); + expect(Find::byId('here'))->toBe("SELECT data FROM here WHERE data->>'id' = :id"); + }); +}); + +describe('::byFields()', function () { + test('generates correct SQL', function () { + Configuration::overrideMode(Mode::SQLite); + expect(Find::byFields('there', [Field::equal('active', true, ':act'), Field::equal('locked', true, ':lock')], + FieldMatch::Any)) + ->toBe("SELECT data FROM there WHERE data->>'active' = :act OR data->>'locked' = :lock"); + }); +}); + +describe('::byContains()', function () { + test('generates correct SQL [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Find::byContains('disc'))->toBe('SELECT data FROM disc WHERE data @> :criteria'); + })->group('postgresql'); + test('throws an exception [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(fn () => Find::byContains(''))->toThrow(DocumentException::class); + })->group('sqlite'); +}); + +describe('::byJsonPath()', function () { + test('generates correct SQL [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Find::byJsonPath('light')) + ->toBe('SELECT data FROM light WHERE jsonb_path_exists(data, :path::jsonpath)'); + })->group('postgresql'); + test('throws an exception [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(fn () => Find::byJsonPath(''))->toThrow(DocumentException::class); + })->group('sqlite'); +}); diff --git a/tests/Unit/Query/PatchTest.php b/tests/Unit/Query/PatchTest.php new file mode 100644 index 0000000..c1a4e6e --- /dev/null +++ b/tests/Unit/Query/PatchTest.php @@ -0,0 +1,68 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode}; +use BitBadger\PDODocument\Query\Patch; + +pest()->group('unit'); + +afterEach(function () { Configuration::overrideMode(null); }); + +describe('::byId()', function () { + test('generates correct SQL [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Patch::byId('doc_table'))->toBe("UPDATE doc_table SET data = data || :data WHERE data->>'id' = :id"); + })->group('postgresql'); + test('generates correct SQL [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(Patch::byId('my_table')) + ->toBe("UPDATE my_table SET data = json_patch(data, json(:data)) WHERE data->>'id' = :id"); + })->group('sqlite'); + test('throws an exception [mode not set]', function () { + expect(fn () => Patch::byId(''))->toThrow(DocumentException::class); + }); +}); + +describe('::byFields()', function () { + test('generates correct SQL [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Patch::byFields('that', [Field::less('something', 17, ':some')])) + ->toBe("UPDATE that SET data = data || :data WHERE (data->>'something')::numeric < :some"); + })->group('postgresql'); + test('generates correct SQL [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(Patch::byFields('a_table', [Field::greater('something', 17, ':it')])) + ->toBe("UPDATE a_table SET data = json_patch(data, json(:data)) WHERE data->>'something' > :it"); + })->group('sqlite'); + test('throws an exception [mode not set]', function () { + expect(fn () => Patch::byFields('', []))->toThrow(DocumentException::class); + }); +}); + +describe('::byContains()', function () { + test('generates correct SQL [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Patch::byContains('this'))->toBe('UPDATE this SET data = data || :data WHERE data @> :criteria'); + })->group('postgresql'); + test('throws an exception [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(fn () => Patch::byContains(''))->toThrow(DocumentException::class); + })->group('sqlite'); +}); + +describe('::byJsonPath()', function () { + test('generates correct SQL [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Patch::byJsonPath('that')) + ->toBe('UPDATE that SET data = data || :data WHERE jsonb_path_exists(data, :path::jsonpath)'); + })->group('postgresql'); + test('throws an exception [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(fn () => Patch::byJsonPath(''))->toThrow(DocumentException::class); + })->group('sqlite'); +}); diff --git a/tests/Unit/Query/RemoveFieldsTest.php b/tests/Unit/Query/RemoveFieldsTest.php new file mode 100644 index 0000000..5b96fb5 --- /dev/null +++ b/tests/Unit/Query/RemoveFieldsTest.php @@ -0,0 +1,86 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode, Parameters}; +use BitBadger\PDODocument\Query\RemoveFields; + +pest()->group('unit'); + +afterEach(function () { Configuration::overrideMode(null); }); + +describe('::update()', function () { + test('generates correct SQL [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(RemoveFields::update('taco', [':names' => "{one,two}"], 'it = true')) + ->toBe('UPDATE taco SET data = data - :names::text[] WHERE it = true'); + })->group('postgresql'); + test('generates correct SQL [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(RemoveFields::update('burrito', Parameters::fieldNames(':name', ['one', 'two', 'ten']), 'a = b')) + ->toBe('UPDATE burrito SET data = json_remove(data, :name0, :name1, :name2) WHERE a = b'); + })->group('sqlite'); + test('throws an exception [mode not set]', function () { + expect(fn () => RemoveFields::update('', [], ''))->toThrow(DocumentException::class); + }); +}); + +describe('::byId()', function () { + test('generates correct SQL [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(RemoveFields::byId('churro', Parameters::fieldNames(':bite', ['byte']))) + ->toBe("UPDATE churro SET data = data - :bite::text[] WHERE data->>'id' = :id"); + })->group('postgresql'); + test('generates correct SQL [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(RemoveFields::byId('quesadilla', Parameters::fieldNames(':bite', ['byte']))) + ->toBe("UPDATE quesadilla SET data = json_remove(data, :bite0) WHERE data->>'id' = :id"); + })->group('sqlite'); + test('throws an exception [mode not set]', function () { + expect(fn () => RemoveFields::byId('', []))->toThrow(DocumentException::class); + }); +}); + +describe('::byFields()', function () { + test('generates correct SQL [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(RemoveFields::byFields('enchilada', [Field::equal('cheese', 'jack', ':queso')], + Parameters::fieldNames(':sauce', ['white']))) + ->toBe("UPDATE enchilada SET data = data - :sauce::text[] WHERE data->>'cheese' = :queso"); + })->group('postgresql'); + test('generates correct SQL [SQLite]', function () { + Configuration::overrideMode(Mode::SQLite); + expect(RemoveFields::byFields('chimichanga', [Field::equal('side', 'beans', ':rice')], + Parameters::fieldNames(':filling', ['beef']))) + ->toBe("UPDATE chimichanga SET data = json_remove(data, :filling0) WHERE data->>'side' = :rice"); + })->group('sqlite'); + test('throws an exception [mode not set]', function () { + expect(fn () => RemoveFields::byFields('', [], []))->toThrow(DocumentException::class); + }); +}); + +describe('::byContains()', function () { + test('generates correct SQL [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(RemoveFields::byContains('food', Parameters::fieldNames(':drink', ['a', 'b']))) + ->toBe('UPDATE food SET data = data - :drink::text[] WHERE data @> :criteria'); + })->group('postgresql'); + test('throws an exception [SQLite]', function () { + expect(fn () => RemoveFields::byContains('', []))->toThrow(DocumentException::class); + })->group('sqlite'); +}); + +describe('::byJsonPath()', function () { + test('generates correct SQL [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(RemoveFields::byJsonPath('dessert', Parameters::fieldNames(':cake', ['b', 'c']))) + ->toBe('UPDATE dessert SET data = data - :cake::text[] WHERE jsonb_path_exists(data, :path::jsonpath)'); + }); + test('throws an exception [SQLite]', function () { + expect(fn () => RemoveFields::byJsonPath('', []))->toThrow(DocumentException::class); + })->group('sqlite'); +}); diff --git a/tests/Unit/QueryTest.php b/tests/Unit/QueryTest.php new file mode 100644 index 0000000..5c78624 --- /dev/null +++ b/tests/Unit/QueryTest.php @@ -0,0 +1,201 @@ + + * @license MIT + */ + +declare(strict_types=1); + +use BitBadger\PDODocument\{AutoId, Configuration, DocumentException, Field, FieldMatch, Mode, Query}; + +pest()->group('unit'); + +beforeEach(function () { Configuration::overrideMode(Mode::SQLite); }); +afterEach(function () { Configuration::overrideMode(null); }); + +describe('::selectFromTable()', function () { + test('correctly forms a query', function () { + expect(Query::selectFromTable('testing'))->toBe('SELECT data FROM testing'); + }); +}); + +describe('::whereByFields()', function () { + test('generates a single field correctly', function () { + expect(Query::whereByFields([Field::lessOrEqual('test_field', '', ':it')]))->toBe("data->>'test_field' <= :it"); + }); + test('generates all fields correctly', function () { + expect(Query::whereByFields( + + [Field::lessOrEqual('test_field', '', ':it'), Field::equal('other_field', '', ':other')])) + ->toBe("data->>'test_field' <= :it AND data->>'other_field' = :other",); + }); + test('generates any field correctly', function () { + expect(Query::whereByFields( + [Field::lessOrEqual('test_field', '', ':it'), Field::equal('other_field', '', ':other')], + FieldMatch::Any)) + ->toBe("data->>'test_field' <= :it OR data->>'other_field' = :other"); + }); +}); + +describe('::whereById()', function () { + test('uses default parameter name', function () { + expect(Query::whereById())->toBe("data->>'id' = :id"); + }); + test('uses provided parameter name', function () { + expect(Query::whereById(':di'))->toBe("data->>'id' = :di"); + }); +}); + +describe('::whereDataContains()', function () { + test('uses default parameter [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Query::whereDataContains())->toBe('data @> :criteria'); + }); + test('uses provided parameter [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Query::whereDataContains(':it'))->toBe('data @> :it'); + }); + test('throws [SQLite]', function () { + expect(fn () => Query::whereDataContains())->toThrow(DocumentException::class); + }); +}); + +describe('::whereJsonPathMatches()', function () { + test('uses default parameter [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Query::whereJsonPathMatches())->toBe('jsonb_path_exists(data, :path::jsonpath)'); + }); + test('uses provided parameter [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Query::whereJsonPathMatches(':road'))->toBe('jsonb_path_exists(data, :road::jsonpath)'); + }); + test('throws [SQLite]', function () { + expect(fn () => Query::whereJsonPathMatches())->toThrow(DocumentException::class); + }); +}); + +describe('::insert()', function () { + test('generates with no auto-ID [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Query::insert('test_tbl'))->toBe('INSERT INTO test_tbl VALUES (:data)'); + }); + test('generates with no auto-ID [SQLite]', function () { + expect(Query::insert('test_tbl'))->toBe('INSERT INTO test_tbl VALUES (:data)'); + }); + test('generates with auto numeric ID [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Query::insert('test_tbl', AutoId::Number)) + ->toBe("INSERT INTO test_tbl VALUES (:data::jsonb || ('{\"id\":' " + . "|| (SELECT COALESCE(MAX((data->>'id')::numeric), 0) + 1 FROM test_tbl) || '}')::jsonb)"); + }); + test('generates with auto numeric ID [SQLite]', function () { + expect(Query::insert('test_tbl', AutoId::Number)) + ->toBe("INSERT INTO test_tbl VALUES (json_set(:data, '$.id', " + . "(SELECT coalesce(max(data->>'id'), 0) + 1 FROM test_tbl)))"); + }); + test('generates with auto UUID [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Query::insert('test_tbl', AutoId::UUID)) + ->toStartWith("INSERT INTO test_tbl VALUES (:data::jsonb || '{\"id\":\"") + ->toEndWith("\"}')"); + }); + test('generates with auto UUID [SQLite]', function () { + expect(Query::insert('test_tbl', AutoId::UUID)) + ->toStartWith("INSERT INTO test_tbl VALUES (json_set(:data, '$.id', '") + ->toEndWith("'))"); + }); + test('generates with auto random string [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + Configuration::$idStringLength = 8; + try { + $query = Query::insert('test_tbl', AutoId::RandomString); + expect($query) + ->toStartWith("INSERT INTO test_tbl VALUES (:data::jsonb || '{\"id\":\"") + ->toEndWith("\"}')") + ->and(str_replace(["INSERT INTO test_tbl VALUES (:data::jsonb || '{\"id\":\"", "\"}')"], '', $query)) + ->toHaveLength(8); + } finally { + Configuration::$idStringLength = 16; + } + }); + test('generates with auto random string [SQLite]', function () { + $query = Query::insert('test_tbl', AutoId::RandomString); + expect($query) + ->toStartWith("INSERT INTO test_tbl VALUES (json_set(:data, '$.id', '") + ->toEndWith("'))") + ->and(str_replace(["INSERT INTO test_tbl VALUES (json_set(:data, '$.id', '", "'))"], '', $query)) + ->toHaveLength(16); + }); + test('throws when mode not set', function () { + Configuration::overrideMode(null); + expect(fn () => Query::insert('kaboom'))->toThrow(DocumentException::class); + }); +}); + +describe('::save()', function () { + test('generates the correct query', function () { + expect(Query::save('test_tbl')) + ->toBe("INSERT INTO test_tbl VALUES (:data) ON CONFLICT ((data->>'id')) DO UPDATE SET data = EXCLUDED.data"); + }); +}); + +describe('::update()', function () { + test('generates the correct query', function () { + expect(Query::update('testing'))->toBe("UPDATE testing SET data = :data WHERE data->>'id' = :id"); + }); +}); + +describe('::orderBy()', function () { + test('returns blank for no criteria [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Query::orderBy([]))->toBeEmpty(); + }); + test('returns blank for no criteria [SQLite]', function () { + expect(Query::orderBy([]))->toBeEmpty(); + }); + test('generates one field with no direction [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Query::orderBy([Field::named('TestField')]))->toBe(" ORDER BY data->>'TestField'"); + }); + test('generates one field with no direction [SQLite]', function () { + expect(Query::orderBy([Field::named('TestField')]))->toBe(" ORDER BY data->>'TestField'"); + }); + test('generates with one qualified field [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + $field = Field::named('TestField'); + $field->qualifier = 'qual'; + expect(Query::orderBy([$field]))->toBe(" ORDER BY qual.data->>'TestField'"); + }); + test('generates with one qualified field [SQLite]', function () { + $field = Field::named('TestField'); + $field->qualifier = 'qual'; + expect(Query::orderBy([$field]))->toBe(" ORDER BY qual.data->>'TestField'"); + }); + test('generates with multiple fields and direction [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Query::orderBy( + [Field::named('Nested.Test.Field DESC'), Field::named('AnotherField'), Field::named('It DESC')])) + ->toBe(" ORDER BY data#>>'{Nested,Test,Field}' DESC, data->>'AnotherField', data->>'It' DESC"); + }); + test('generates with multiple fields and direction [SQLite]', function () { + expect(Query::orderBy( + [Field::named('Nested.Test.Field DESC'), Field::named('AnotherField'), Field::named('It DESC')])) + ->toBe(" ORDER BY data->'Nested'->'Test'->>'Field' DESC, data->>'AnotherField', data->>'It' DESC"); + }); + test('generates with numeric field [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Query::orderBy([Field::named('n:Test')]))->toBe(" ORDER BY (data->>'Test')::numeric"); + }); + test('generates with numeric field [SQLite]', function () { + expect(Query::orderBy([Field::named('n:Test')]))->toBe(" ORDER BY data->>'Test'"); + }); + test('generates case-insensitive ordering [PostgreSQL]', function () { + Configuration::overrideMode(Mode::PgSQL); + expect(Query::orderBy([Field::named('i:Test.Field DESC NULLS FIRST')])) + ->toBe(" ORDER BY LOWER(data#>>'{Test,Field}') DESC NULLS FIRST"); + }); + test('generates case-insensitive ordering [SQLite]', function () { + expect(Query::orderBy([Field::named('i:Test.Field ASC NULLS LAST')])) + ->toBe(" ORDER BY data->'Test'->>'Field' COLLATE NOCASE ASC NULLS LAST"); + }); +}); diff --git a/tests/integration/postgresql/CountTest.php b/tests/integration/postgresql/CountTest.php deleted file mode 100644 index ccda872..0000000 --- a/tests/integration/postgresql/CountTest.php +++ /dev/null @@ -1,84 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Integration\PostgreSQL; - -use BitBadger\PDODocument\{Count, Field}; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * PostgreSQL integration tests for the Count class - */ -#[TestDox('Count (PostgreSQL integration)')] -class CountTest 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')] - 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::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::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('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('byJsonPath() succeeds when no documents match')] - public function testByJsonPathSucceedsWhenNoDocumentsMatch(): void - { - $this->assertEquals(0, Count::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 100)'), - 'There should have been no matching documents'); - } -} diff --git a/tests/integration/postgresql/CustomTest.php b/tests/integration/postgresql/CustomTest.php deleted file mode 100644 index a12b9d6..0000000 --- a/tests/integration/postgresql/CustomTest.php +++ /dev/null @@ -1,138 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Integration\PostgreSQL; - -use BitBadger\PDODocument\{Count, Custom, DocumentException, Query}; -use BitBadger\PDODocument\Mapper\{CountMapper, DocumentMapper}; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; -use Test\Integration\TestDocument; - -/** - * PostgreSQL integration tests for the Custom class - */ -#[TestDox('Custom (PostgreSQL integration)')] -class CustomTest extends TestCase -{ - /** @var string Database name for throwaway database */ - private string $dbName; - - public function setUp(): void - { - parent::setUp(); - $this->dbName = ThrowawayDb::create(); - } - - public function tearDown(): void - { - ThrowawayDb::destroy($this->dbName); - } - - #[TestDox('runQuery() succeeds with a valid query')] - public function testRunQuerySucceedsWithAValidQuery(): void - { - $stmt = &Custom::runQuery('SELECT data FROM ' . ThrowawayDb::TABLE . ' LIMIT 1', []); - try { - $this->assertNotNull($stmt, 'The statement should not have been null'); - } finally { - $stmt = null; - } - } - - #[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', []); - try { - $this->assertTrue(false, 'This code should not be reached'); - } finally { - $stmt = null; - } - } - - #[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'); - $count = 0; - foreach ($list->items() as $ignored) $count++; - $this->assertEquals(5, $count, 'There should have been 5 documents in the list'); - } - - #[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", - [':value' => 100], new DocumentMapper(TestDocument::class)); - $this->assertNotNull($list, 'The document list should not be null'); - $this->assertFalse($list->hasItems(), 'There should have been no documents in the list'); - } - - #[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)); - $this->assertNotNull($array, 'The document array should not be null'); - $this->assertCount(2, $array, 'There should have been 2 documents in the array'); - } - - #[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)); - $this->assertNotNull($array, 'The document array should not be null'); - $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'], - new DocumentMapper(TestDocument::class)); - $this->assertTrue($doc->isSome(), 'There should have been a document returned'); - $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", - [':id' => 'eighty'], new DocumentMapper(TestDocument::class)); - $this->assertTrue($doc->isNone(), 'There should not have been a document returned'); - } - - #[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'); - } - - #[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]); - $remaining = Count::all(ThrowawayDb::TABLE); - $this->assertEquals(5, $remaining, 'There should be 5 documents remaining in the table'); - } - - #[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'); - } -} diff --git a/tests/integration/postgresql/DefinitionTest.php b/tests/integration/postgresql/DefinitionTest.php deleted file mode 100644 index 0aa7433..0000000 --- a/tests/integration/postgresql/DefinitionTest.php +++ /dev/null @@ -1,88 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Integration\PostgreSQL; - -use BitBadger\PDODocument\{Custom, Definition, DocumentException, DocumentIndex}; -use BitBadger\PDODocument\Mapper\ExistsMapper; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * PostgreSQL integration tests for the Definition class - */ -#[TestDox('Definition (PostgreSQL integration)')] -class DefinitionTest extends TestCase -{ - /** @var string Database name for throwaway database */ - private string $dbName; - - protected function setUp(): void - { - parent::setUp(); - $this->dbName = ThrowawayDb::create(withData: false); - } - - protected function tearDown(): void - { - ThrowawayDb::destroy($this->dbName); - parent::tearDown(); - } - - /** - * Does the given named object exist in the database? - * - * @param string $name The name of the object whose existence should be verified - * @return bool True if the object exists, false if not - * @throws DocumentException If any is encountered - */ - private function itExists(string $name): bool - { - return Custom::scalar('SELECT EXISTS (SELECT 1 FROM pg_class WHERE relname = :name)', - [':name' => $name], new ExistsMapper()); - } - - #[TestDox('ensureTable() succeeds')] - public function testEnsureTableSucceeds(): void - { - $this->assertFalse($this->itExists('ensured'), 'The table should not exist already'); - $this->assertFalse($this->itExists('idx_ensured_key'), 'The key index should not exist already'); - Definition::ensureTable('ensured'); - $this->assertTrue($this->itExists('ensured'), 'The table should now exist'); - $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'); - Definition::ensureTable('ensured'); - Definition::ensureFieldIndex('ensured', 'test', ['name', 'age']); - $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'; - Definition::ensureTable(ThrowawayDb::TABLE); - $this->assertFalse($this->itExists($docIdx), 'The document index should not exist'); - Definition::ensureDocumentIndex(ThrowawayDb::TABLE, DocumentIndex::Full); - $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'; - Definition::ensureTable(ThrowawayDb::TABLE); - $this->assertFalse($this->itExists($docIdx), 'The document index should not exist'); - Definition::ensureDocumentIndex(ThrowawayDb::TABLE, DocumentIndex::Optimized); - $this->assertTrue($this->itExists($docIdx), 'The document index should now exist'); - } -} diff --git a/tests/integration/postgresql/DeleteTest.php b/tests/integration/postgresql/DeleteTest.php deleted file mode 100644 index d354fe0..0000000 --- a/tests/integration/postgresql/DeleteTest.php +++ /dev/null @@ -1,99 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Integration\PostgreSQL; - -use BitBadger\PDODocument\{Count, Delete, Field}; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * PostgreSQL integration tests for the Delete class - */ -#[TestDox('Delete (PostgreSQL integration)')] -class DeleteTest 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 deleted')] - public function testByIdSucceedsWhenADocumentIsDeleted(): void - { - $this->assertEquals(5, Count::all(ThrowawayDb::TABLE), 'There should have been 5 documents to start'); - Delete::byId(ThrowawayDb::TABLE, 'four'); - $this->assertEquals(4, Count::all(ThrowawayDb::TABLE), 'There should have been 4 documents remaining'); - } - - #[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'); - Delete::byId(ThrowawayDb::TABLE, 'negative four'); - $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::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::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'); - Delete::byContains(ThrowawayDb::TABLE, ['value' => 'purple']); - $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'); - Delete::byContains(ThrowawayDb::TABLE, ['target' => 'acquired']); - $this->assertEquals(5, Count::all(ThrowawayDb::TABLE), 'There should have been 5 documents remaining'); - } - - #[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'); - Delete::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ <> 0)'); - $this->assertEquals(1, Count::all(ThrowawayDb::TABLE), 'There should have been 1 document remaining'); - } - - #[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'); - Delete::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ < 0)'); - $this->assertEquals(5, Count::all(ThrowawayDb::TABLE), 'There should have been 5 documents remaining'); - } -} diff --git a/tests/integration/postgresql/DocumentListTest.php b/tests/integration/postgresql/DocumentListTest.php deleted file mode 100644 index 827247b..0000000 --- a/tests/integration/postgresql/DocumentListTest.php +++ /dev/null @@ -1,135 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Integration\PostgreSQL; - -use BitBadger\PDODocument\{DocumentException, DocumentList, Query}; -use BitBadger\PDODocument\Mapper\DocumentMapper; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; -use Test\Integration\TestDocument; - -/** - * PostgreSQL integration tests for the DocumentList class - */ -#[TestDox('DocumentList (PostgreSQL integration)')] -class DocumentListTest 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('create() succeeds')] - public function testCreateSucceeds(): void - { - $list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [], - new DocumentMapper(TestDocument::class)); - $this->assertNotNull($list, 'There should have been a document list created'); - $list = null; - } - - #[TestDox('items() succeeds')] - public function testItemsSucceeds(): void - { - $list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [], - new DocumentMapper(TestDocument::class)); - $this->assertNotNull($list, 'There should have been a document list created'); - $count = 0; - foreach ($list->items() as $item) { - $this->assertContains($item->id, ['one', 'two', 'three', 'four', 'five'], - 'An unexpected document ID was returned'); - $count++; - } - $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), [], - new DocumentMapper(TestDocument::class)); - $this->assertNotNull($list, 'There should have been a document list created'); - $this->assertTrue($list->hasItems(), 'There should be items in the list'); - $ignored = iterator_to_array($list->items()); - $this->assertFalse($list->hasItems(), 'The list should no longer have items'); - $this->expectException(DocumentException::class); - 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')::numeric < 0", [], - new DocumentMapper(TestDocument::class)); - $this->assertNotNull($list, 'There should have been a document list created'); - $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), [], - new DocumentMapper(TestDocument::class)); - $this->assertNotNull($list, 'There should have been a document list created'); - $this->assertTrue($list->hasItems(), 'There should be items in the list'); - foreach ($list->items() as $ignored) { - $this->assertTrue($list->hasItems(), 'There should be items remaining in the list'); - } - $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), [], - new DocumentMapper(TestDocument::class)); - $this->assertNotNull($list, 'There should have been a document list created'); - $this->assertTrue($list->hasItems(), 'There should be items in the list'); - foreach ($list->map(fn($doc) => strrev($doc->id)) as $mapped) { - $this->assertContains($mapped, ['eno', 'owt', 'eerht', 'ruof', 'evif'], - 'An unexpected mapped value was returned'); - } - } - - #[TestDox('iter() succeeds')] - public function testIterSucceeds(): void - { - $list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [], - new DocumentMapper(TestDocument::class)); - $this->assertNotNull($list, 'There should have been a document list created'); - $this->assertTrue($list->hasItems(), 'There should be items in the list'); - $splats = []; - $list->iter(function ($doc) use (&$splats) { $splats[] = str_repeat('*', strlen($doc->id)); }); - $this->assertEquals('*** *** ***** **** ****', implode(' ', $splats), - 'Iteration did not have the expected result'); - } - - #[TestDox('mapToArray() succeeds')] - public function testMapToArraySucceeds(): void - { - $list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [], - new DocumentMapper(TestDocument::class)); - $this->assertNotNull($list, 'There should have been a document list created'); - $this->assertTrue($list->hasItems(), 'There should be items in the list'); - $lookup = $list->mapToArray(fn($it) => $it->id, fn($it) => $it->value); - $expected = ['one' => 'FIRST!', 'two' => 'another', 'three' => '', 'four' => 'purple', 'five' => 'purple']; - $this->assertEquals($expected, $lookup, 'The array was not mapped correctly'); - } -} diff --git a/tests/integration/postgresql/DocumentTest.php b/tests/integration/postgresql/DocumentTest.php deleted file mode 100644 index 0997b5a..0000000 --- a/tests/integration/postgresql/DocumentTest.php +++ /dev/null @@ -1,319 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Integration\PostgreSQL; - -use BitBadger\PDODocument\{AutoId, Configuration, Custom, Document, DocumentException, Field, Find, Query}; -use BitBadger\PDODocument\Mapper\ArrayMapper; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; -use Test\Integration\{NumDocument, SubDocument, TestDocument}; - -/** - * PostgreSQL integration tests for the Document class - */ -#[TestDox('Document (PostgreSQL integration)')] -class DocumentTest 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('insert() succeeds for array no auto ID')] - public function testInsertSucceedsForArrayNoAutoId(): void - { - Document::insert(ThrowawayDb::TABLE, ['id' => 'turkey', 'sub' => ['foo' => 'gobble', 'bar' => 'gobble']]); - $tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class); - $this->assertTrue($tryDoc->isSome(), 'There should have been a document inserted'); - $doc = $tryDoc->get(); - $this->assertEquals('turkey', $doc->id, 'The ID was incorrect'); - $this->assertEquals('', $doc->value, 'The value was incorrect'); - $this->assertEquals(0, $doc->num_value, 'The numeric value was incorrect'); - $this->assertNotNull($doc->sub, 'The sub-document should not have been null'); - $this->assertEquals('gobble', $doc->sub->foo, 'The sub-document foo property was incorrect'); - $this->assertEquals('gobble', $doc->sub->bar, 'The sub-document bar property was incorrect'); - } - - #[TestDox('insert() succeeds for array with auto number ID not provided')] - public function testInsertSucceedsForArrayWithAutoNumberIdNotProvided(): void - { - Configuration::$autoId = AutoId::Number; - try { - Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); - - Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'new', 'num_value' => 8]); - $doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper()); - $this->assertTrue($doc->isSome(), 'There should have been a document returned'); - $obj = json_decode($doc->get()['data']); - $this->assertEquals(1, $obj->id, 'The ID 1 should have been auto-generated'); - - Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'again', 'num_value' => 7]); - $doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE " . Query::whereById(docId: 2), - [':id' => 2], new ArrayMapper()); - $this->assertTrue($doc->isSome(), 'There should have been a document returned'); - $obj = json_decode($doc->get()['data']); - $this->assertEquals(2, $obj->id, 'The ID 2 should have been auto-generated'); - } finally { - Configuration::$autoId = AutoId::None; - } - } - - #[TestDox('insert() succeeds for array with auto number ID with ID provided')] - public function testInsertSucceedsForArrayWithAutoNumberIdWithIdProvided(): void - { - Configuration::$autoId = AutoId::Number; - try { - Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); - Document::insert(ThrowawayDb::TABLE, ['id' => 7, 'value' => 'new', 'num_value' => 8]); - $doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper()); - $this->assertTrue($doc->isSome(), 'There should have been a document returned'); - $obj = json_decode($doc->get()['data']); - $this->assertEquals(7, $obj->id, 'The ID 7 should have been stored'); - } finally { - Configuration::$autoId = AutoId::None; - } - } - - #[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::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 { - Configuration::$autoId = AutoId::None; - } - } - - #[TestDox('insert() succeeds for array with auto UUID ID with ID provided')] - public function testInsertSucceedsForArrayWithAutoUuidIdWithIdProvided(): void - { - Configuration::$autoId = AutoId::UUID; - try { - Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); - $uuid = AutoId::generateUUID(); - Document::insert(ThrowawayDb::TABLE, ['id' => $uuid, 'value' => 'uuid', 'num_value' => 12]); - $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 12)], TestDocument::class); - $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 { - Configuration::$autoId = AutoId::None; - } - } - - #[TestDox('insert() succeeds for array with auto string ID not provided')] - public function testInsertSucceedsForArrayWithAutoStringIdNotProvided(): void - { - Configuration::$autoId = AutoId::RandomString; - Configuration::$idStringLength = 6; - try { - Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); - Document::insert(ThrowawayDb::TABLE, ['id' => '', 'value' => 'new', 'num_value' => 8]); - $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 8)], TestDocument::class); - $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'); - } finally { - Configuration::$autoId = AutoId::None; - Configuration::$idStringLength = 16; - } - } - - #[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::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 { - Configuration::$autoId = AutoId::None; - } - } - - #[TestDox('insert() succeeds for object no auto ID')] - public function testInsertSucceedsForObjectNoAutoId(): void - { - Document::insert(ThrowawayDb::TABLE, new TestDocument('turkey', sub: new SubDocument('gobble', 'gobble'))); - $tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class); - $this->assertTrue($tryDoc->isSome(), 'There should have been a document inserted'); - $doc = $tryDoc->get(); - $this->assertEquals('turkey', $doc->id, 'The ID was incorrect'); - $this->assertEquals('', $doc->value, 'The value was incorrect'); - $this->assertEquals(0, $doc->num_value, 'The numeric value was incorrect'); - $this->assertNotNull($doc->sub, 'The sub-document should not have been null'); - $this->assertEquals('gobble', $doc->sub->foo, 'The sub-document foo property was incorrect'); - $this->assertEquals('gobble', $doc->sub->bar, 'The sub-document bar property was incorrect'); - } - - #[TestDox('insert() succeeds for object with auto number ID not provided')] - public function testInsertSucceedsForObjectWithAutoNumberIdNotProvided(): void - { - Configuration::$autoId = AutoId::Number; - try { - Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); - - Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'taco')); - $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'taco')], NumDocument::class); - $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::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 { - Configuration::$autoId = AutoId::None; - } - } - - #[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::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 { - Configuration::$autoId = AutoId::None; - } - } - - #[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::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 { - Configuration::$autoId = AutoId::None; - } - } - - #[TestDox('insert() succeeds for object with auto UUID ID with ID provided')] - public function testInsertSucceedsForObjectWithAutoUuidIdWithIdProvided(): void - { - Configuration::$autoId = AutoId::UUID; - try { - Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); - $uuid = AutoId::generateUUID(); - Document::insert(ThrowawayDb::TABLE, new TestDocument($uuid, num_value: 14)); - $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 14)], TestDocument::class); - $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 { - Configuration::$autoId = AutoId::None; - } - } - - #[TestDox('insert() succeeds for object with auto string ID not provided')] - public function testInsertSucceedsForObjectWithAutoStringIdNotProvided(): void - { - Configuration::$autoId = AutoId::RandomString; - Configuration::$idStringLength = 40; - try { - Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); - Document::insert(ThrowawayDb::TABLE, new TestDocument(num_value: 55)); - $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 55)], TestDocument::class); - $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'); - } finally { - Configuration::$autoId = AutoId::None; - Configuration::$idStringLength = 16; - } - } - - #[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::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 { - Configuration::$autoId = AutoId::None; - } - } - - #[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'))); - $doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class); - $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)); - $tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class); - $this->assertTrue($tryDoc->isSome(), 'There should have been a document returned'); - $doc = $tryDoc->get(); - $this->assertEquals(44, $doc->num_value, 'The numeric value was not updated'); - $this->assertNull($doc->sub, 'The sub-document should have been null'); - } - - #[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'))); - $tryDoc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class); - $this->assertNotFalse($tryDoc->isSome(), 'There should have been a document returned'); - $doc = $tryDoc->get(); - $this->assertEquals('howdy', $doc->value, 'The value was incorrect'); - $this->assertEquals(8, $doc->num_value, 'The numeric value was incorrect'); - $this->assertNotNull($doc->sub, 'The sub-document should not have been null'); - $this->assertEquals('y', $doc->sub->foo, 'The sub-document foo property was incorrect'); - $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')); - $doc = Find::byId(ThrowawayDb::TABLE, 'two-hundred', TestDocument::class); - $this->assertTrue($doc->isNone(), 'There should not have been a document returned'); - } -} diff --git a/tests/integration/postgresql/ExistsTest.php b/tests/integration/postgresql/ExistsTest.php deleted file mode 100644 index 097d08c..0000000 --- a/tests/integration/postgresql/ExistsTest.php +++ /dev/null @@ -1,90 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Integration\PostgreSQL; - -use BitBadger\PDODocument\{Exists, Field}; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * PostgreSQL integration tests for the Exists class - */ -#[TestDox('Exists (PostgreSQL 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() 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('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('byJsonPath() succeeds when no matching documents exist')] - public function testByJsonPathSucceedsWhenNoMatchingDocumentsExist(): void - { - $this->assertFalse(Exists::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ == 10.1)'), - 'There should have been existing documents'); - } -} diff --git a/tests/integration/postgresql/FindTest.php b/tests/integration/postgresql/FindTest.php deleted file mode 100644 index 3fd2683..0000000 --- a/tests/integration/postgresql/FindTest.php +++ /dev/null @@ -1,323 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Integration\PostgreSQL; - -use BitBadger\PDODocument\{Custom, Delete, Document, Field, FieldMatch, Find}; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; -use Test\Integration\{ArrayDocument, NumDocument, TestDocument}; - -/** - * PostgreSQL integration tests for the Find class - */ -#[TestDox('Find (PostgreSQL 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 - { - 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('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() succeeds when documents are found')] - public function testByContainsSucceedsWhenDocumentsAreFound(): void - { - $docs = Find::byContains(ThrowawayDb::TABLE, ['value' => 'purple'], TestDocument::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('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); - $this->assertNotNull($docs, 'There should have been a document list returned'); - $this->assertFalse($docs->hasItems(), 'The document list should be empty'); - } - - #[TestDox('byJsonPath() succeeds when documents are found')] - public function testByJsonPathSucceedsWhenDocumentsAreFound(): void - { - $docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::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('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); - $this->assertNotNull($docs, 'There should have been a document list returned'); - $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::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() succeeds when a document is found')] - public function testFirstByContainsSucceedsWhenADocumentIsFound(): void - { - $doc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'FIRST!'], TestDocument::class); - $this->assertTrue($doc->isSome(), 'There should have been a document returned'); - $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); - $this->assertTrue($doc->isSome(), 'There should have been a document returned'); - $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('firstByJsonPath() succeeds when a document is found')] - public function testFirstByJsonPathSucceedsWhenADocumentIsFound(): void - { - $doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ == 10)', 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('firstByJsonPath() succeeds when multiple documents are found')] - public function testFirstByJsonPathSucceedsWhenMultipleDocumentsAreFound(): void - { - $doc = Find::firstByJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class); - $this->assertTrue($doc->isSome(), 'There should have been a document returned'); - $this->assertContains($doc->get()->id, ['four', 'five'], 'An incorrect document was returned'); - } - - #[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); - $this->assertTrue($doc->isNone(), 'There should not have been a document returned'); - } -} diff --git a/tests/integration/postgresql/PatchTest.php b/tests/integration/postgresql/PatchTest.php deleted file mode 100644 index 9955441..0000000 --- a/tests/integration/postgresql/PatchTest.php +++ /dev/null @@ -1,115 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Integration\PostgreSQL; - -use BitBadger\PDODocument\{Count, Exists, Field, Find, Patch}; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; -use Test\Integration\TestDocument; - -/** - * PostgreSQL integration tests for the Patch class - */ -#[TestDox('Patch (PostgreSQL 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 - { - $id = 'forty-seven'; - $this->assertFalse(Exists::byId(ThrowawayDb::TABLE, $id), 'The document should not exist'); - Patch::byId(ThrowawayDb::TABLE, $id, ['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 - { - $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]); - $tryDoc = Find::firstByContains(ThrowawayDb::TABLE, ['value' => 'another'], TestDocument::class); - $this->assertTrue($tryDoc->isSome(), 'There should have been a document returned'); - $doc = $tryDoc->get(); - $this->assertEquals('two', $doc->id, 'An incorrect document was returned'); - $this->assertEquals(12, $doc->num_value, 'The document was not patched'); - } - - #[TestDox('byContains() succeeds when no documents are updated')] - public function testByContainsSucceedsWhenNoDocumentsAreUpdated(): void - { - $criteria = ['value' => 'updated']; - $this->assertEquals(0, Count::byContains(ThrowawayDb::TABLE, $criteria), - 'There should be no matching documents'); - Patch::byContains(ThrowawayDb::TABLE, $criteria, ['sub.foo' => 'green']); - $this->assertTrue(true, 'The above not throwing an exception is the test'); - } - - #[TestDox('byJsonPath() succeeds when documents are updated')] - public function testByJsonPathSucceedsWhenDocumentsAreUpdated(): void - { - Patch::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', ['value' => 'blue']); - $docs = Find::byJsonPath(ThrowawayDb::TABLE, '$.num_value ? (@ > 10)', TestDocument::class); - $this->assertNotNull($docs, 'There should have been a document list returned'); - $this->assertTrue($docs->hasItems(), 'The document list should not be empty'); - foreach ($docs->items() as $item) { - $this->assertContains($item->id, ['four', 'five'], 'An incorrect document was returned'); - $this->assertEquals('blue', $item->value, 'The document was not patched'); - } - } - - #[TestDox('byJsonPath() succeeds when documents are not updated')] - public function testByJsonPathSucceedsWhenDocumentsAreNotUpdated(): void - { - $path = '$.num_value ? (@ > 100)'; - $this->assertEquals(0, Count::byJsonPath(ThrowawayDb::TABLE, $path), - 'There should be no documents matching this path'); - Patch::byJsonPath(ThrowawayDb::TABLE, $path, ['value' => 'blue']); - $this->assertTrue(true, 'The above not throwing an exception is the test'); - } -} diff --git a/tests/integration/postgresql/RemoveFieldsTest.php b/tests/integration/postgresql/RemoveFieldsTest.php deleted file mode 100644 index f83a200..0000000 --- a/tests/integration/postgresql/RemoveFieldsTest.php +++ /dev/null @@ -1,140 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Integration\PostgreSQL; - -use BitBadger\PDODocument\{Field, Find, RemoveFields}; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; -use Test\Integration\TestDocument; - -/** - * PostgreSQL integration tests for the RemoveFields class - */ -#[TestDox('Remove Fields (PostgreSQL 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() succeeds when a field is removed')] - public function testByContainsSucceedsWhenAFieldIsRemoved(): void - { - $criteria = ['sub' => ['foo' => 'green']]; - RemoveFields::byContains(ThrowawayDb::TABLE, $criteria, ['value']); - $docs = Find::byContains(ThrowawayDb::TABLE, $criteria, TestDocument::class); - $this->assertNotNull($docs, 'There should have been a document list returned'); - $this->assertTrue($docs->hasItems(), 'The document list should not have been empty'); - foreach ($docs->items() as $item) { - $this->assertContains($item->id, ['two', 'four'], 'An incorrect document was returned'); - $this->assertEquals('', $item->value, 'The value field was not removed'); - } - } - - #[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('byJsonPath() succeeds when a field is removed')] - public function testByJsonPathSucceedsWhenAFieldIsRemoved(): void - { - $path = '$.value ? (@ == "purple")'; - RemoveFields::byJsonPath(ThrowawayDb::TABLE, $path, ['sub']); - $docs = Find::byJsonPath(ThrowawayDb::TABLE, $path, TestDocument::class); - $this->assertNotNull($docs, 'There should have been a document list returned'); - $this->assertTrue($docs->hasItems(), 'The document list should not have been empty'); - foreach ($docs->items() as $item) { - $this->assertContains($item->id, ['four', 'five'], 'An incorrect document was returned'); - $this->assertNull($item->sub, 'The sub field was 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('byJsonPath() succeeds when no document is matched')] - public function testByJsonPathSucceedsWhenNoDocumentIsMatched(): void - { - RemoveFields::byJsonPath(ThrowawayDb::TABLE, '$.value ? (@ == "mauve")', ['value']); - $this->assertTrue(true, 'The above not throwing an exception is the test'); - } -} diff --git a/tests/integration/sqlite/CountTest.php b/tests/integration/sqlite/CountTest.php deleted file mode 100644 index b94e3d1..0000000 --- a/tests/integration/sqlite/CountTest.php +++ /dev/null @@ -1,70 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Integration\SQLite; - -use BitBadger\PDODocument\{Count, DocumentException, Field}; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * SQLite integration tests for the Count class - */ -#[TestDox('Count (SQLite integration)')] -class CountTest 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')] - 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::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::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('byJsonPath() fails')] - public function testByJsonPathFails(): void - { - $this->expectException(DocumentException::class); - Count::byJsonPath('', ''); - } -} diff --git a/tests/integration/sqlite/CustomTest.php b/tests/integration/sqlite/CustomTest.php deleted file mode 100644 index a167975..0000000 --- a/tests/integration/sqlite/CustomTest.php +++ /dev/null @@ -1,136 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Integration\SQLite; - -use BitBadger\PDODocument\{Count, Custom, DocumentException, Query}; -use BitBadger\PDODocument\Mapper\{CountMapper, DocumentMapper}; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; -use Test\Integration\TestDocument; - -/** - * SQLite Integration tests for the Custom class - */ -#[TestDox('Custom (SQLite integration)')] -class CustomTest extends TestCase -{ - /** @var string Database name for throwaway database */ - private string $dbName; - - public function setUp(): void - { - parent::setUp(); - $this->dbName = ThrowawayDb::create(); - } - - public function tearDown(): void - { - ThrowawayDb::destroy($this->dbName); - } - - #[TestDox('runQuery() succeeds with a valid query')] - public function testRunQuerySucceedsWithAValidQuery(): void - { - $stmt = &Custom::runQuery('SELECT data FROM ' . ThrowawayDb::TABLE . ' LIMIT 1', []); - try { - $this->assertNotNull($stmt, 'The statement should not have been null'); - } finally { - $stmt = null; - } - } - - #[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', []); - try { - $this->assertTrue(false, 'This code should not be reached'); - } finally { - $stmt = null; - } - } - - #[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'); - $count = 0; - foreach ($list->items() as $ignored) $count++; - $this->assertEquals(5, $count, 'There should have been 5 documents in the list'); - } - - #[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)); - $this->assertNotNull($list, 'The document list should not be null'); - $this->assertFalse($list->hasItems(), 'There should have been no documents in the list'); - } - - #[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)); - $this->assertNotNull($array, 'The document array should not be null'); - $this->assertCount(2, $array, 'There should have been 2 documents in the array'); - } - - #[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)); - $this->assertNotNull($array, 'The document array should not be null'); - $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'], - new DocumentMapper(TestDocument::class)); - $this->assertTrue($doc->isSome(), 'There should have been a document returned'); - $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", - [':id' => 'eighty'], new DocumentMapper(TestDocument::class)); - $this->assertTrue($doc->isNone(), 'There should not have been a document returned'); - } - - #[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'); - } - - #[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'); - } - - #[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'); - } -} diff --git a/tests/integration/sqlite/DefinitionTest.php b/tests/integration/sqlite/DefinitionTest.php deleted file mode 100644 index d861126..0000000 --- a/tests/integration/sqlite/DefinitionTest.php +++ /dev/null @@ -1,75 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Integration\SQLite; - -use BitBadger\PDODocument\{Custom, Definition, DocumentException, DocumentIndex}; -use BitBadger\PDODocument\Mapper\ExistsMapper; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * SQLite integration tests for the Definition class - */ -#[TestDox('Definition (SQLite integration)')] -class DefinitionTest extends TestCase -{ - /** @var string Database name for throwaway database */ - private string $dbName; - - protected function setUp(): void - { - parent::setUp(); - $this->dbName = ThrowawayDb::create(withData: false); - } - - protected function tearDown(): void - { - ThrowawayDb::destroy($this->dbName); - parent::tearDown(); - } - - /** - * Does the given named object exist in the database? - * - * @param string $name The name of the object whose existence should be verified - * @return bool True if the object exists, false if not - * @throws DocumentException If any is encountered - */ - private function itExists(string $name): bool - { - return Custom::scalar('SELECT EXISTS (SELECT 1 FROM sqlite_master WHERE name = :name)', - [':name' => $name], new ExistsMapper()); - } - - #[TestDox('ensureTable() succeeds')] - public function testEnsureTableSucceeds(): void - { - $this->assertFalse($this->itExists('ensured'), 'The table should not exist already'); - $this->assertFalse($this->itExists('idx_ensured_key'), 'The key index should not exist already'); - Definition::ensureTable('ensured'); - $this->assertTrue($this->itExists('ensured'), 'The table should now exist'); - $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'); - Definition::ensureTable('ensured'); - Definition::ensureFieldIndex('ensured', 'test', ['name', 'age']); - $this->assertTrue($this->itExists('idx_ensured_test'), 'The index should now exist'); - } - - #[TestDox('ensureDocumentIndex() fails')] - public function testEnsureDocumentIndexFails(): void - { - $this->expectException(DocumentException::class); - Definition::ensureDocumentIndex('nope', DocumentIndex::Full); - } -} diff --git a/tests/integration/sqlite/DeleteTest.php b/tests/integration/sqlite/DeleteTest.php deleted file mode 100644 index 11d5dd9..0000000 --- a/tests/integration/sqlite/DeleteTest.php +++ /dev/null @@ -1,81 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Integration\SQLite; - -use BitBadger\PDODocument\{Count, Delete, DocumentException, Field}; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * SQLite integration tests for the Delete class - */ -#[TestDox('Delete (SQLite integration)')] -class DeleteTest 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 deleted')] - public function testByIdSucceedsWhenADocumentIsDeleted(): void - { - $this->assertEquals(5, Count::all(ThrowawayDb::TABLE), 'There should have been 5 documents to start'); - Delete::byId(ThrowawayDb::TABLE, 'four'); - $this->assertEquals(4, Count::all(ThrowawayDb::TABLE), 'There should have been 4 documents remaining'); - } - - #[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'); - Delete::byId(ThrowawayDb::TABLE, 'negative four'); - $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::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::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('byJsonPath() fails')] - public function testByJsonPathFails(): void - { - $this->expectException(DocumentException::class); - Delete::byJsonPath('', ''); - } -} diff --git a/tests/integration/sqlite/DocumentListTest.php b/tests/integration/sqlite/DocumentListTest.php deleted file mode 100644 index 8ea2647..0000000 --- a/tests/integration/sqlite/DocumentListTest.php +++ /dev/null @@ -1,134 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Integration\SQLite; - -use BitBadger\PDODocument\{DocumentException, DocumentList, Query}; -use BitBadger\PDODocument\Mapper\DocumentMapper; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; -use Test\Integration\TestDocument; - -/** - * SQLite integration tests for the DocumentList class - */ -#[TestDox('DocumentList (SQLite integration)')] -class DocumentListTest 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('create() succeeds')] - public function testCreateSucceeds(): void - { - $list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [], - new DocumentMapper(TestDocument::class)); - $this->assertNotNull($list, 'There should have been a document list created'); - $list = null; - } - - #[TestDox('items() succeeds')] - public function testItemsSucceeds(): void - { - $list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [], - new DocumentMapper(TestDocument::class)); - $this->assertNotNull($list, 'There should have been a document list created'); - $count = 0; - foreach ($list->items() as $item) { - $this->assertContains($item->id, ['one', 'two', 'three', 'four', 'five'], - 'An unexpected document ID was returned'); - $count++; - } - $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), [], - new DocumentMapper(TestDocument::class)); - $this->assertNotNull($list, 'There should have been a document list created'); - $this->assertTrue($list->hasItems(), 'There should be items in the list'); - $ignored = iterator_to_array($list->items()); - $this->assertFalse($list->hasItems(), 'The list should no longer have items'); - $this->expectException(DocumentException::class); - 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", [], - new DocumentMapper(TestDocument::class)); - $this->assertNotNull($list, 'There should have been a document list created'); - $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), [], - new DocumentMapper(TestDocument::class)); - $this->assertNotNull($list, 'There should have been a document list created'); - $this->assertTrue($list->hasItems(), 'There should be items in the list'); - foreach ($list->items() as $ignored) { - $this->assertTrue($list->hasItems(), 'There should be items remaining in the list'); - } - $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), [], - new DocumentMapper(TestDocument::class)); - $this->assertNotNull($list, 'There should have been a document list created'); - $this->assertTrue($list->hasItems(), 'There should be items in the list'); - foreach ($list->map(fn($doc) => strrev($doc->id)) as $mapped) { - $this->assertContains($mapped, ['eno', 'owt', 'eerht', 'ruof', 'evif'], - 'An unexpected mapped value was returned'); - } - } - - #[TestDox('iter() succeeds')] - public function testIterSucceeds(): void - { - $list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [], - new DocumentMapper(TestDocument::class)); - $this->assertNotNull($list, 'There should have been a document list created'); - $this->assertTrue($list->hasItems(), 'There should be items in the list'); - $splats = []; - $list->iter(function ($doc) use (&$splats) { $splats[] = str_repeat('*', strlen($doc->id)); }); - $this->assertEquals('*** *** ***** **** ****', implode(' ', $splats), - 'Iteration did not have the expected result'); - } - - #[TestDox('mapToArray() succeeds')] - public function testMapToArraySucceeds(): void - { - $list = DocumentList::create(Query::selectFromTable(ThrowawayDb::TABLE), [], - new DocumentMapper(TestDocument::class)); - $this->assertNotNull($list, 'There should have been a document list created'); - $this->assertTrue($list->hasItems(), 'There should be items in the list'); - $lookup = $list->mapToArray(fn($it) => $it->id, fn($it) => $it->value); - $expected = ['one' => 'FIRST!', 'two' => 'another', 'three' => '', 'four' => 'purple', 'five' => 'purple']; - $this->assertEquals($expected, $lookup, 'The array was not mapped correctly'); - } -} diff --git a/tests/integration/sqlite/DocumentTest.php b/tests/integration/sqlite/DocumentTest.php deleted file mode 100644 index 6abbbde..0000000 --- a/tests/integration/sqlite/DocumentTest.php +++ /dev/null @@ -1,319 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Integration\SQLite; - -use BitBadger\PDODocument\{AutoId, Configuration, Custom, Document, DocumentException, Field, Find}; -use BitBadger\PDODocument\Mapper\ArrayMapper; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; -use Test\Integration\{NumDocument, SubDocument, TestDocument}; - -/** - * SQLite integration tests for the Document class - */ -#[TestDox('Document (SQLite integration)')] -class DocumentTest 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('insert() succeeds for array no auto ID')] - public function testInsertSucceedsForArrayNoAutoId(): void - { - Document::insert(ThrowawayDb::TABLE, ['id' => 'turkey', 'sub' => ['foo' => 'gobble', 'bar' => 'gobble']]); - $tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class); - $this->assertTrue($tryDoc->isSome(), 'There should have been a document inserted'); - $doc = $tryDoc->get(); - $this->assertEquals('turkey', $doc->id, 'The ID was incorrect'); - $this->assertEquals('', $doc->value, 'The value was incorrect'); - $this->assertEquals(0, $doc->num_value, 'The numeric value was incorrect'); - $this->assertNotNull($doc->sub, 'The sub-document should not have been null'); - $this->assertEquals('gobble', $doc->sub->foo, 'The sub-document foo property was incorrect'); - $this->assertEquals('gobble', $doc->sub->bar, 'The sub-document bar property was incorrect'); - } - - #[TestDox('insert() succeeds for array with auto number ID not provided')] - public function testInsertSucceedsForArrayWithAutoNumberIdNotProvided(): void - { - Configuration::$autoId = AutoId::Number; - try { - Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); - - Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'new', 'num_value' => 8]); - $doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper()); - $this->assertTrue($doc->isSome(), 'There should have been a document returned'); - $obj = json_decode($doc->get()['data']); - $this->assertEquals(1, $obj->id, 'The ID 1 should have been auto-generated'); - - Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'again', 'num_value' => 7]); - $doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = 2", [], - new ArrayMapper()); - $this->assertTrue($doc->isSome(), 'There should have been a document returned'); - $obj = json_decode($doc->get()['data']); - $this->assertEquals(2, $obj->id, 'The ID 2 should have been auto-generated'); - } finally { - Configuration::$autoId = AutoId::None; - } - } - - #[TestDox('insert() succeeds for array with auto number ID with ID provided')] - public function testInsertSucceedsForArrayWithAutoNumberIdWithIdProvided(): void - { - Configuration::$autoId = AutoId::Number; - try { - Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); - Document::insert(ThrowawayDb::TABLE, ['id' => 7, 'value' => 'new', 'num_value' => 8]); - $doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper()); - $this->assertTrue($doc->isSome(), 'There should have been a document returned'); - $obj = json_decode($doc->get()['data']); - $this->assertEquals(7, $obj->id, 'The ID 7 should have been stored'); - } finally { - Configuration::$autoId = AutoId::None; - } - } - - #[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::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 { - Configuration::$autoId = AutoId::None; - } - } - - #[TestDox('insert() succeeds for array with auto UUID ID with ID provided')] - public function testInsertSucceedsForArrayWithAutoUuidIdWithIdProvided(): void - { - Configuration::$autoId = AutoId::UUID; - try { - Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); - $uuid = AutoId::generateUUID(); - Document::insert(ThrowawayDb::TABLE, ['id' => $uuid, 'value' => 'uuid', 'num_value' => 12]); - $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 12)], TestDocument::class); - $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 { - Configuration::$autoId = AutoId::None; - } - } - - #[TestDox('insert() succeeds for array with auto string ID not provided')] - public function testInsertSucceedsForArrayWithAutoStringIdNotProvided(): void - { - Configuration::$autoId = AutoId::RandomString; - Configuration::$idStringLength = 6; - try { - Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); - Document::insert(ThrowawayDb::TABLE, ['id' => '', 'value' => 'new', 'num_value' => 8]); - $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 8)], TestDocument::class); - $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'); - } finally { - Configuration::$autoId = AutoId::None; - Configuration::$idStringLength = 16; - } - } - - #[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::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 { - Configuration::$autoId = AutoId::None; - } - } - - #[TestDox('insert() succeeds for object no auto ID')] - public function testInsertSucceedsForObjectNoAutoId(): void - { - Document::insert(ThrowawayDb::TABLE, new TestDocument('turkey', sub: new SubDocument('gobble', 'gobble'))); - $tryDoc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class); - $this->assertNotFalse($tryDoc->isSome(), 'There should have been a document inserted'); - $doc = $tryDoc->get(); - $this->assertEquals('turkey', $doc->id, 'The ID was incorrect'); - $this->assertEquals('', $doc->value, 'The value was incorrect'); - $this->assertEquals(0, $doc->num_value, 'The numeric value was incorrect'); - $this->assertNotNull($doc->sub, 'The sub-document should not have been null'); - $this->assertEquals('gobble', $doc->sub->foo, 'The sub-document foo property was incorrect'); - $this->assertEquals('gobble', $doc->sub->bar, 'The sub-document bar property was incorrect'); - } - - #[TestDox('insert() succeeds for object with auto number ID not provided')] - public function testInsertSucceedsForObjectWithAutoNumberIdNotProvided(): void - { - Configuration::$autoId = AutoId::Number; - try { - Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); - - Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'taco')); - $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('value', 'taco')], NumDocument::class); - $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::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 { - Configuration::$autoId = AutoId::None; - } - } - - #[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::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 { - Configuration::$autoId = AutoId::None; - } - } - - #[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::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 { - Configuration::$autoId = AutoId::None; - } - } - - #[TestDox('insert() succeeds for object with auto UUID ID with ID provided')] - public function testInsertSucceedsForObjectWithAutoUuidIdWithIdProvided(): void - { - Configuration::$autoId = AutoId::UUID; - try { - Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); - $uuid = AutoId::generateUUID(); - Document::insert(ThrowawayDb::TABLE, new TestDocument($uuid, num_value: 14)); - $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 14)], TestDocument::class); - $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 { - Configuration::$autoId = AutoId::None; - } - } - - #[TestDox('insert() succeeds for object with auto string ID not provided')] - public function testInsertSucceedsForObjectWithAutoStringIdNotProvided(): void - { - Configuration::$autoId = AutoId::RandomString; - Configuration::$idStringLength = 40; - try { - Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []); - Document::insert(ThrowawayDb::TABLE, new TestDocument(num_value: 55)); - $doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::equal('num_value', 55)], TestDocument::class); - $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'); - } finally { - Configuration::$autoId = AutoId::None; - Configuration::$idStringLength = 16; - } - } - - #[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::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 { - Configuration::$autoId = AutoId::None; - } - } - - #[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'))); - $doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class); - $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)); - $tryDoc = Find::byId(ThrowawayDb::TABLE, 'two', TestDocument::class); - $this->assertTrue($tryDoc->isSome(), 'There should have been a document returned'); - $doc = $tryDoc->get(); - $this->assertEquals(44, $doc->num_value, 'The numeric value was not updated'); - $this->assertNull($doc->sub, 'The sub-document should have been null'); - } - - #[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'))); - $tryDoc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class); - $this->assertTrue($tryDoc->isSome(), 'There should have been a document returned'); - $doc = $tryDoc->get(); - $this->assertEquals('howdy', $doc->value, 'The value was incorrect'); - $this->assertEquals(8, $doc->num_value, 'The numeric value was incorrect'); - $this->assertNotNull($doc->sub, 'The sub-document should not have been null'); - $this->assertEquals('y', $doc->sub->foo, 'The sub-document foo property was incorrect'); - $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')); - $doc = Find::byId(ThrowawayDb::TABLE, 'two-hundred', TestDocument::class); - $this->assertTrue($doc->isNone(), 'There should not have been a document returned'); - } -} 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('', '', []); - } -} diff --git a/tests/unit/ConfigurationTest.php b/tests/unit/ConfigurationTest.php deleted file mode 100644 index acbffe1..0000000 --- a/tests/unit/ConfigurationTest.php +++ /dev/null @@ -1,58 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Unit; - -use BitBadger\PDODocument\{AutoId, Configuration, DocumentException}; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * Unit tests for the Configuration class - */ -#[TestDox('Configuration (Unit tests)')] -class ConfigurationTest extends TestCase -{ - #[TestDox('id default succeeds')] - public function testIdFieldDefaultSucceeds(): void - { - $this->assertEquals('id', Configuration::$idField, 'Default ID field should be "id"'); - } - - #[TestDox('id change succeeds')] - public function testIdFieldChangeSucceeds(): void - { - try { - Configuration::$idField = 'EyeDee'; - $this->assertEquals('EyeDee', Configuration::$idField, 'ID field should have been updated'); - } finally { - Configuration::$idField = 'id'; - $this->assertEquals('id', Configuration::$idField, 'Default ID value should have been restored'); - } - } - - #[TestDox('autoId default succeeds')] - public function testAutoIdDefaultSucceeds(): void - { - $this->assertEquals(AutoId::None, Configuration::$autoId, 'Auto ID should default to None'); - } - - #[TestDox('idStringLength default succeeds')] - public function testIdStringLengthDefaultSucceeds(): void - { - $this->assertEquals(16, Configuration::$idStringLength, 'ID string length should default to 16'); - } - - #[TestDox("dbConn() fails when no DSN specified")] - public function testDbConnFailsWhenNoDSNSpecified(): void - { - $this->expectException(DocumentException::class); - Configuration::useDSN(''); - Configuration::dbConn(); - } -} diff --git a/tests/unit/DocumentExceptionTest.php b/tests/unit/DocumentExceptionTest.php deleted file mode 100644 index c656055..0000000 --- a/tests/unit/DocumentExceptionTest.php +++ /dev/null @@ -1,56 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Unit; - -use BitBadger\PDODocument\DocumentException; -use Exception; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * Unit tests for the DocumentException class - */ -#[TestDox('Document Exception (Unit tests)')] -class DocumentExceptionTest extends TestCase -{ - public function testConstructorSucceedsWithCodeAndPriorException(): void - { - $priorEx = new Exception('Uh oh'); - $ex = new DocumentException('Test Exception', 17, $priorEx); - $this->assertNotNull($ex, 'The exception should not have been null'); - $this->assertEquals('Test Exception', $ex->getMessage(), 'Message not filled properly'); - $this->assertEquals(17, $ex->getCode(), 'Code not filled properly'); - $this->assertSame($priorEx, $ex->getPrevious(), 'Prior exception not filled properly'); - } - - public function testConstructorSucceedsWithoutCodeAndPriorException(): void - { - $ex = new DocumentException('Oops'); - $this->assertNotNull($ex, 'The exception should not have been null'); - $this->assertEquals('Oops', $ex->getMessage(), 'Message not filled properly'); - $this->assertEquals(0, $ex->getCode(), 'Code not filled properly'); - $this->assertNull($ex->getPrevious(), 'Prior exception should have been null'); - } - - #[TestDox('toString() succeeds without code')] - public function testToStringSucceedsWithoutCode(): void - { - $ex = new DocumentException('Test failure'); - $this->assertEquals("BitBadger\PDODocument\DocumentException: Test failure\n", "$ex", - 'toString not generated correctly'); - } - - #[TestDox('toString() succeeds with code')] - public function testToStringSucceedsWithCode(): void - { - $ex = new DocumentException('Oof', -6); - $this->assertEquals("BitBadger\PDODocument\DocumentException: [-6] Oof\n", "$ex", - 'toString not generated correctly'); - } -} diff --git a/tests/unit/FieldMatchTest.php b/tests/unit/FieldMatchTest.php deleted file mode 100644 index f1d0193..0000000 --- a/tests/unit/FieldMatchTest.php +++ /dev/null @@ -1,32 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Unit; - -use BitBadger\PDODocument\FieldMatch; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * Unit tests for the FieldMatch enum - */ -#[TestDox('Field Match (Unit tests)')] -class FieldMatchTest extends TestCase -{ - #[TestDox('toSQL() succeeds for All')] - public function testToSQLSucceedsForAll(): void - { - $this->assertEquals('AND', FieldMatch::All->toSQL(), 'All should have returned AND'); - } - - #[TestDox('toSQL() succeeds for Any')] - public function testToSQLSucceedsForAny(): void - { - $this->assertEquals('OR', FieldMatch::Any->toSQL(), 'Any should have returned OR'); - } -} diff --git a/tests/unit/FieldTest.php b/tests/unit/FieldTest.php deleted file mode 100644 index 4bf06ec..0000000 --- a/tests/unit/FieldTest.php +++ /dev/null @@ -1,683 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Unit; - -use BitBadger\PDODocument\{Configuration, Field, Mode, Op}; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * Unit tests for the Field class - */ -#[TestDox('Field (Unit tests)')] -class FieldTest extends TestCase -{ - #[TestDox('appendParameter() succeeds for exists')] - public function testAppendParameterSucceedsForExists(): void - { - $this->assertEquals([], Field::exists('exists')->appendParameter([]), - 'exists should not have appended a parameter'); - } - - #[TestDox('appendParameter() succeeds for notExists')] - public function testAppendParameterSucceedsForNotExists(): void - { - $this->assertEquals([], Field::notExists('absent')->appendParameter([]), - 'notExists should not have appended a parameter'); - } - - #[TestDox('appendParameter() succeeds for between')] - public function testAppendParameterSucceedsForBetween(): void - { - $this->assertEquals(['@nummin' => 5, '@nummax' => 9], - Field::between('exists', 5, 9, '@num')->appendParameter([]), - 'Between should have appended min and max parameters'); - } - - #[TestDox('appendParameter() succeeds for in')] - public function testAppendParameterSucceedsForIn(): void - { - $this->assertEquals([':val_0' => 'test', ':val_1' => 'unit', ':val_2' => 'great'], - Field::in('it', ['test', 'unit', 'great'], ':val')->appendParameter([]), - 'In should have appended 3 parameters for the input values'); - } - - #[TestDox('appendParameter() succeeds for inArray for PostgreSQL')] - public function testAppendParameterSucceedsForInArrayForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - try { - $this->assertEquals([':bit_0' => '2', ':bit_1' => '8', ':bit_2' => '64'], - Field::inArray('it', 'table', [2, 8, 64], ':bit')->appendParameter([]), - 'InArray should have appended 3 string parameters for the input values'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('appendParameter() succeeds for inArray for SQLite')] - public function testAppendParameterSucceedsForInArrayForSQLite(): void - { - Configuration::overrideMode(Mode::SQLite); - try { - $this->assertEquals([':bit_0' => 2, ':bit_1' => 8, ':bit_2' => 64], - Field::inArray('it', 'table', [2, 8, 64], ':bit')->appendParameter([]), - 'InArray should have appended 3 parameters for the input values'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('appendParameter() succeeds for others')] - public function testAppendParameterSucceedsForOthers(): void - { - $this->assertEquals(['@test' => 33], Field::equal('the_field', 33, '@test')->appendParameter([]), - 'Field parameter not returned correctly'); - } - - #[TestDox('path() succeeds for simple SQL path for PostgreSQL')] - public function testPathSucceedsForSimpleSqlPathForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - try { - $this->assertEquals("data->>'it'", Field::equal('it', 'that')->path(), - 'SQL value path not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('path() succeeds for simple SQL path for SQLite')] - public function testPathSucceedsForSimpleSqlPathForSQLite(): void - { - Configuration::overrideMode(Mode::SQLite); - try { - $this->assertEquals("data->>'top'", Field::equal('top', 'that')->path(), - 'SQL value path not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('path() succeeds for nested SQL path for PostgreSQL')] - public function testPathSucceedsForNestedSqlPathForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - try { - $this->assertEquals("data#>>'{parts,to,the,path}'", Field::equal('parts.to.the.path', '')->path(), - 'SQL value path not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('path() succeeds for nested SQL path for SQLite')] - public function testPathSucceedsForNestedSqlPathForSQLite(): void - { - Configuration::overrideMode(Mode::SQLite); - try { - $this->assertEquals("data->'one'->'two'->>'three'", Field::equal('one.two.three', '')->path(), - 'SQL value path not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('path() succeeds for simple JSON path for PostgreSQL')] - public function testPathSucceedsForSimpleJsonPathForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - try { - $this->assertEquals("data->'it'", Field::equal('it', 'that')->path(true), - 'JSON value path not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('path() succeeds for simple JSON path for SQLite')] - public function testPathSucceedsForSimpleJsonPathForSQLite(): void - { - Configuration::overrideMode(Mode::SQLite); - try { - $this->assertEquals("data->'top'", Field::equal('top', 'that')->path(true), - 'JSON value path not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('path() succeeds for nested JSON path for PostgreSQL')] - public function testPathSucceedsForNestedJsonPathForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - try { - $this->assertEquals("data#>'{parts,to,the,path}'", Field::equal('parts.to.the.path', '')->path(true), - 'JSON value path not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('path() succeeds for nested JSON path for SQLite')] - public function testPathSucceedsForNestedJsonPathForSQLite(): void - { - Configuration::overrideMode(Mode::SQLite); - try { - $this->assertEquals("data->'one'->'two'->'three'", Field::equal('one.two.three', '')->path(true), - 'SQL value path not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('toWhere() succeeds for exists without qualifier for PostgreSQL')] - public function testToWhereSucceedsForExistsWithoutQualifierForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - try { - $this->assertEquals("data->>'that_field' IS NOT NULL", Field::exists('that_field')->toWhere(), - 'WHERE fragment not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('toWhere() succeeds for exists without qualifier for SQLite')] - public function testToWhereSucceedsForExistsWithoutQualifierForSQLite(): void - { - Configuration::overrideMode(Mode::SQLite); - try { - $this->assertEquals("data->>'that_field' IS NOT NULL", Field::exists('that_field')->toWhere(), - 'WHERE fragment not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('toWhere() succeeds for notExists without qualifier for PostgreSQL')] - public function testToWhereSucceedsForNotExistsWithoutQualifierForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - try { - $this->assertEquals("data->>'a_field' IS NULL", Field::notExists('a_field')->toWhere(), - 'WHERE fragment not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('toWhere() succeeds for notExists without qualifier for SQLite')] - public function testToWhereSucceedsForNotExistsWithoutQualifierForSQLite(): void - { - Configuration::overrideMode(Mode::SQLite); - try { - $this->assertEquals("data->>'a_field' IS NULL", Field::notExists('a_field')->toWhere(), - 'WHERE fragment not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('toWhere() succeeds for between without qualifier for SQLite')] - public function testToWhereSucceedsForBetweenWithoutQualifierForSQLite(): void - { - Configuration::overrideMode(Mode::SQLite); - try { - $this->assertEquals("data->>'age' BETWEEN @agemin AND @agemax", - Field::between('age', 13, 17, '@age')->toWhere(), 'WHERE fragment not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('toWhere() succeeds for between without qualifier for PostgreSQL with numeric range')] - public function testToWhereSucceedsForBetweenWithoutQualifierForPostgreSQLWithNumericRange(): void - { - Configuration::overrideMode(Mode::PgSQL); - try { - $this->assertEquals("(data->>'age')::numeric BETWEEN @agemin AND @agemax", - Field::between('age', 13, 17, '@age')->toWhere(), 'WHERE fragment not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('toWhere() succeeds for between without qualifier for PostgreSQL with non-numeric range')] - public function testToWhereSucceedsForBetweenWithoutQualifierForPostgreSQLWithNonNumericRange(): void - { - Configuration::overrideMode(Mode::PgSQL); - try { - $this->assertEquals("data->>'city' BETWEEN :citymin AND :citymax", - Field::between('city', 'Atlanta', 'Chicago', ':city')->toWhere(), - 'WHERE fragment not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('toWhere() succeeds for between with qualifier for SQLite')] - public function testToWhereSucceedsForBetweenWithQualifierForSQLite(): void - { - Configuration::overrideMode(Mode::SQLite); - try { - $field = Field::between('age', 13, 17, '@age'); - $field->qualifier = 'me'; - $this->assertEquals("me.data->>'age' BETWEEN @agemin AND @agemax", $field->toWhere(), - 'WHERE fragment not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('toWhere() succeeds for between with qualifier for PostgreSQL with numeric range')] - public function testToWhereSucceedsForBetweenWithQualifierForPostgreSQLWithNumericRange(): void - { - Configuration::overrideMode(Mode::PgSQL); - try { - $field = Field::between('age', 13, 17, '@age'); - $field->qualifier = 'me'; - $this->assertEquals("(me.data->>'age')::numeric BETWEEN @agemin AND @agemax", $field->toWhere(), - 'WHERE fragment not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('toWhere() succeeds for between with qualifier for PostgreSQL with non-numeric range')] - public function testToWhereSucceedsForBetweenWithQualifierForPostgreSQLWithNonNumericRange(): void - { - Configuration::overrideMode(Mode::PgSQL); - try { - $field = Field::between('city', 'Atlanta', 'Chicago', ':city'); - $field->qualifier = 'me'; - $this->assertEquals("me.data->>'city' BETWEEN :citymin AND :citymax", $field->toWhere(), - 'WHERE fragment not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('toWhere() succeeds for in for PostgreSQL with non-numeric values')] - public function testToWhereSucceedsForInForPostgreSQLWithNonNumericValues(): void - { - Configuration::overrideMode(Mode::PgSQL); - try { - $field = Field::in('test', ['Atlanta', 'Chicago'], ':city'); - $this->assertEquals("data->>'test' IN (:city_0, :city_1)", $field->toWhere(), - 'WHERE fragment not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('toWhere() succeeds for in for PostgreSQL with numeric values')] - public function testToWhereSucceedsForInForPostgreSQLWithNumericValues(): void - { - Configuration::overrideMode(Mode::PgSQL); - try { - $field = Field::in('even', [2, 4, 6], ':nbr'); - $this->assertEquals("(data->>'even')::numeric IN (:nbr_0, :nbr_1, :nbr_2)", $field->toWhere(), - 'WHERE fragment not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('toWhere() succeeds for in for SQLite')] - public function testToWhereSucceedsForInForSQLite(): void - { - Configuration::overrideMode(Mode::SQLite); - try { - $field = Field::in('test', ['Atlanta', 'Chicago'], ':city'); - $this->assertEquals("data->>'test' IN (:city_0, :city_1)", $field->toWhere(), - 'WHERE fragment not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('toWhere() succeeds for inArray for PostgreSQL')] - public function testToWhereSucceedsForInArrayForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - try { - $field = Field::inArray('even', 'tbl', [2, 4, 6, 8], ':it'); - $this->assertEquals("data->'even' ??| ARRAY[:it_0, :it_1, :it_2, :it_3]", $field->toWhere(), - 'WHERE fragment not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('toWhere() succeeds for inArray for SQLite')] - public function testToWhereSucceedsForInArrayForSQLite(): void - { - Configuration::overrideMode(Mode::SQLite); - try { - $field = Field::inArray('test', 'tbl', ['Atlanta', 'Chicago'], ':city'); - $this->assertEquals( - "EXISTS (SELECT 1 FROM json_each(tbl.data, '\$.test') WHERE value IN (:city_0, :city_1))", - $field->toWhere(), 'WHERE fragment not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('toWhere() succeeds for others without qualifier for PostgreSQL')] - public function testToWhereSucceedsForOthersWithoutQualifierForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - try { - $this->assertEquals("data->>'some_field' = @value", Field::equal('some_field', '', '@value')->toWhere(), - 'WHERE fragment not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('toWhere() succeeds for others without qualifier for SQLite')] - public function testToWhereSucceedsForOthersWithoutQualifierForSQLite(): void - { - Configuration::overrideMode(Mode::SQLite); - try { - $this->assertEquals("data->>'some_field' = @value", Field::equal('some_field', '', '@value')->toWhere(), - 'WHERE fragment not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('toWhere() succeeds with qualifier no parameter for PostgreSQL')] - public function testToWhereSucceedsWithQualifierNoParameterForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - try { - $field = Field::exists('no_field'); - $field->qualifier = 'test'; - $this->assertEquals("test.data->>'no_field' IS NOT NULL", $field->toWhere(), - 'WHERE fragment not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('toWhere() succeeds with qualifier no parameter for SQLite')] - public function testToWhereSucceedsWithQualifierNoParameterForSQLite(): void - { - Configuration::overrideMode(Mode::SQLite); - try { - $field = Field::exists('no_field'); - $field->qualifier = 'test'; - $this->assertEquals("test.data->>'no_field' IS NOT NULL", $field->toWhere(), - 'WHERE fragment not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('toWhere() succeeds with qualifier and parameter for PostgreSQL')] - public function testToWhereSucceedsWithQualifierAndParameterForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - try { - $field = Field::lessOrEqual('le_field', 18, '@it'); - $field->qualifier = 'q'; - $this->assertEquals("(q.data->>'le_field')::numeric <= @it", $field->toWhere(), - 'WHERE fragment not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('toWhere() succeeds with qualifier and parameter for SQLite')] - public function testToWhereSucceedsWithQualifierAndParameterForSQLite(): void - { - Configuration::overrideMode(Mode::SQLite); - try { - $field = Field::lessOrEqual('le_field', 18, '@it'); - $field->qualifier = 'q'; - $this->assertEquals("q.data->>'le_field' <= @it", $field->toWhere(), - 'WHERE fragment not generated correctly'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('equal() succeeds without parameter')] - public function testEqualSucceedsWithoutParameter(): void - { - $field = Field::equal('my_test', 9); - $this->assertNotNull($field, 'The field should not have been null'); - $this->assertEquals('my_test', $field->fieldName, 'Field name not filled correctly'); - $this->assertEquals(Op::Equal, $field->op, 'Operation not filled correctly'); - $this->assertEquals(9, $field->value, 'Value not filled correctly'); - $this->assertEquals('', $field->paramName, 'Parameter name should have been blank'); - } - - #[TestDox('equal() succeeds with parameter')] - public function testEqualSucceedsWithParameter(): void - { - $field = Field::equal('another_test', 'turkey', '@test'); - $this->assertNotNull($field, 'The field should not have been null'); - $this->assertEquals('another_test', $field->fieldName, 'Field name not filled correctly'); - $this->assertEquals(Op::Equal, $field->op, 'Operation not filled correctly'); - $this->assertEquals('turkey', $field->value, 'Value not filled correctly'); - $this->assertEquals('@test', $field->paramName, 'Parameter name not filled correctly'); - } - - #[TestDox('greater() succeeds without parameter')] - public function testGreaterSucceedsWithoutParameter(): void - { - $field = Field::greater('your_test', 4); - $this->assertNotNull($field, 'The field should not have been null'); - $this->assertEquals('your_test', $field->fieldName, 'Field name not filled correctly'); - $this->assertEquals(Op::Greater, $field->op, 'Operation not filled correctly'); - $this->assertEquals(4, $field->value, 'Value not filled correctly'); - $this->assertEquals('', $field->paramName, 'Parameter name should have been blank'); - } - - #[TestDox('greater() succeeds with parameter')] - public function testGreaterSucceedsWithParameter(): void - { - $field = Field::greater('more_test', 'chicken', '@value'); - $this->assertNotNull($field, 'The field should not have been null'); - $this->assertEquals('more_test', $field->fieldName, 'Field name not filled correctly'); - $this->assertEquals(Op::Greater, $field->op, 'Operation not filled correctly'); - $this->assertEquals('chicken', $field->value, 'Value not filled correctly'); - $this->assertEquals('@value', $field->paramName, 'Parameter name not filled correctly'); - } - - #[TestDox('greaterOrEqual() succeeds without parameter')] - public function testGreaterOrEqualSucceedsWithoutParameter(): void - { - $field = Field::greaterOrEqual('their_test', 6); - $this->assertNotNull($field, 'The field should not have been null'); - $this->assertEquals('their_test', $field->fieldName, 'Field name not filled correctly'); - $this->assertEquals(Op::GreaterOrEqual, $field->op, 'Operation not filled correctly'); - $this->assertEquals(6, $field->value, 'Value not filled correctly'); - $this->assertEquals('', $field->paramName, 'Parameter name should have been blank'); - } - - #[TestDox('greaterOrEqual() succeeds with parameter')] - public function testGreaterOrEqualSucceedsWithParameter(): void - { - $field = Field::greaterOrEqual('greater_test', 'poultry', '@cluck'); - $this->assertNotNull($field, 'The field should not have been null'); - $this->assertEquals('greater_test', $field->fieldName, 'Field name not filled correctly'); - $this->assertEquals(Op::GreaterOrEqual, $field->op, 'Operation not filled correctly'); - $this->assertEquals('poultry', $field->value, 'Value not filled correctly'); - $this->assertEquals('@cluck', $field->paramName, 'Parameter name not filled correctly'); - } - - #[TestDox('less() succeeds without parameter')] - public function testLessSucceedsWithoutParameter(): void - { - $field = Field::less('z', 32); - $this->assertNotNull($field, 'The field should not have been null'); - $this->assertEquals('z', $field->fieldName, 'Field name not filled correctly'); - $this->assertEquals(Op::Less, $field->op, 'Operation not filled correctly'); - $this->assertEquals(32, $field->value, 'Value not filled correctly'); - $this->assertEquals('', $field->paramName, 'Parameter name should have been blank'); - } - - #[TestDox('less() succeeds with parameter')] - public function testLessSucceedsWithParameter(): void - { - $field = Field::less('additional_test', 'fowl', '@boo'); - $this->assertNotNull($field, 'The field should not have been null'); - $this->assertEquals('additional_test', $field->fieldName, 'Field name not filled correctly'); - $this->assertEquals(Op::Less, $field->op, 'Operation not filled correctly'); - $this->assertEquals('fowl', $field->value, 'Value not filled correctly'); - $this->assertEquals('@boo', $field->paramName, 'Parameter name not filled correctly'); - } - - #[TestDox('lessOrEqual() succeeds without parameter')] - public function testLessOrEqualSucceedsWithoutParameter(): void - { - $field = Field::lessOrEqual('g', 87); - $this->assertNotNull($field, 'The field should not have been null'); - $this->assertEquals('g', $field->fieldName, 'Field name not filled correctly'); - $this->assertEquals(Op::LessOrEqual, $field->op, 'Operation not filled correctly'); - $this->assertEquals(87, $field->value, 'Value not filled correctly'); - $this->assertEquals('', $field->paramName, 'Parameter name should have been blank'); - } - - #[TestDox('lessOrEqual() succeeds with parameter')] - public function testLessOrEqualSucceedsWithParameter(): void - { - $field = Field::lessOrEqual('lesser_test', 'hen', '@woo'); - $this->assertNotNull($field, 'The field should not have been null'); - $this->assertEquals('lesser_test', $field->fieldName, 'Field name not filled correctly'); - $this->assertEquals(Op::LessOrEqual, $field->op, 'Operation not filled correctly'); - $this->assertEquals('hen', $field->value, 'Value not filled correctly'); - $this->assertEquals('@woo', $field->paramName, 'Parameter name not filled correctly'); - } - - #[TestDox('notEqual() succeeds without parameter')] - public function testNotEqualSucceedsWithoutParameter(): void - { - $field = Field::notEqual('j', 65); - $this->assertNotNull($field, 'The field should not have been null'); - $this->assertEquals('j', $field->fieldName, 'Field name not filled correctly'); - $this->assertEquals(Op::NotEqual, $field->op, 'Operation not filled correctly'); - $this->assertEquals(65, $field->value, 'Value not filled correctly'); - $this->assertEquals('', $field->paramName, 'Parameter name should have been blank'); - } - - #[TestDox('notEqual() succeeds with parameter')] - public function testNotEqualSucceedsWithParameter(): void - { - $field = Field::notEqual('unequal_test', 'egg', '@zoo'); - $this->assertNotNull($field, 'The field should not have been null'); - $this->assertEquals('unequal_test', $field->fieldName, 'Field name not filled correctly'); - $this->assertEquals(Op::NotEqual, $field->op, 'Operation not filled correctly'); - $this->assertEquals('egg', $field->value, 'Value not filled correctly'); - $this->assertEquals('@zoo', $field->paramName, 'Parameter name not filled correctly'); - } - - #[TestDox('between() succeeds without parameter')] - public function testBetweenSucceedsWithoutParameter(): void - { - $field = Field::between('k', 'alpha', 'zed'); - $this->assertNotNull($field, 'The field should not have been null'); - $this->assertEquals('k', $field->fieldName, 'Field name not filled correctly'); - $this->assertEquals(Op::Between, $field->op, 'Operation not filled correctly'); - $this->assertEquals(['alpha', 'zed'], $field->value, 'Value not filled correctly'); - $this->assertEquals('', $field->paramName, 'Parameter name should have been blank'); - } - - #[TestDox('between() succeeds with parameter')] - public function testBetweenSucceedsWithParameter(): void - { - $field = Field::between('between_test', 18, 49, '@count'); - $this->assertNotNull($field, 'The field should not have been null'); - $this->assertEquals('between_test', $field->fieldName, 'Field name not filled correctly'); - $this->assertEquals(Op::Between, $field->op, 'Operation not filled correctly'); - $this->assertEquals([18, 49], $field->value, 'Value not filled correctly'); - $this->assertEquals('@count', $field->paramName, 'Parameter name not filled correctly'); - } - - #[TestDox('in() succeeds without parameter')] - public function testInSucceedsWithoutParameter(): void - { - $field = Field::in('test', [1, 2, 3]); - $this->assertNotNull($field, 'The field should not have been null'); - $this->assertEquals('test', $field->fieldName, 'Field name not filled correctly'); - $this->assertEquals(Op::In, $field->op, 'Operation not filled correctly'); - $this->assertEquals([1, 2, 3], $field->value, 'Value not filled correctly'); - $this->assertEquals('', $field->paramName, 'Parameter name should have been blank'); - } - - #[TestDox('in() succeeds with parameter')] - public function testInSucceedsWithParameter(): void - { - $field = Field::in('unit', ['a', 'b'], '@inParam'); - $this->assertNotNull($field, 'The field should not have been null'); - $this->assertEquals('unit', $field->fieldName, 'Field name not filled correctly'); - $this->assertEquals(Op::In, $field->op, 'Operation not filled correctly'); - $this->assertEquals(['a', 'b'], $field->value, 'Value not filled correctly'); - $this->assertEquals('@inParam', $field->paramName, 'Parameter name not filled correctly'); - } - - #[TestDox('inArray() succeeds without parameter')] - public function testInArraySucceedsWithoutParameter(): void - { - $field = Field::inArray('test', 'tbl', [1, 2, 3]); - $this->assertNotNull($field, 'The field should not have been null'); - $this->assertEquals('test', $field->fieldName, 'Field name not filled correctly'); - $this->assertEquals(Op::InArray, $field->op, 'Operation not filled correctly'); - $this->assertEquals(['table' => 'tbl', 'values' => [1, 2, 3]], $field->value, 'Value not filled correctly'); - $this->assertEquals('', $field->paramName, 'Parameter name should have been blank'); - } - - #[TestDox('inArray() succeeds with parameter')] - public function testInArraySucceedsWithParameter(): void - { - $field = Field::inArray('unit', 'tab', ['a', 'b'], '@inAParam'); - $this->assertNotNull($field, 'The field should not have been null'); - $this->assertEquals('unit', $field->fieldName, 'Field name not filled correctly'); - $this->assertEquals(Op::InArray, $field->op, 'Operation not filled correctly'); - $this->assertEquals(['table' => 'tab', 'values' => ['a', 'b']], $field->value, 'Value not filled correctly'); - $this->assertEquals('@inAParam', $field->paramName, 'Parameter name not filled correctly'); - } - - #[TestDox('exists() succeeds')] - public function testExistsSucceeds(): void - { - $field = Field::exists('be_there'); - $this->assertNotNull($field, 'The field should not have been null'); - $this->assertEquals('be_there', $field->fieldName, 'Field name not filled correctly'); - $this->assertEquals(Op::Exists, $field->op, 'Operation not filled correctly'); - $this->assertEquals('', $field->value, 'Value should have been blank'); - $this->assertEquals('', $field->paramName, 'Parameter name should have been blank'); - } - - #[TestDox('notExists() succeeds')] - public function testNotExistsSucceeds(): void - { - $field = Field::notExists('be_absent'); - $this->assertNotNull($field, 'The field should not have been null'); - $this->assertEquals('be_absent', $field->fieldName, 'Field name not filled correctly'); - $this->assertEquals(Op::NotExists, $field->op, 'Operation not filled correctly'); - $this->assertEquals('', $field->value, 'Value should have been blank'); - $this->assertEquals('', $field->paramName, 'Parameter name should have been blank'); - } - - #[TestDox('named() succeeds')] - public function testNamedSucceeds(): void - { - $field = Field::named('the_field'); - $this->assertNotNull($field, 'The field should not have been null'); - $this->assertEquals('the_field', $field->fieldName, 'Field name not filled correctly'); - $this->assertEquals(Op::Equal, $field->op, 'Operation not filled correctly'); - $this->assertEquals('', $field->value, 'Value should have been blank'); - $this->assertEquals('', $field->paramName, 'Parameter name should have been blank'); - } -} diff --git a/tests/unit/Mapper/ArrayMapperTest.php b/tests/unit/Mapper/ArrayMapperTest.php deleted file mode 100644 index d7e84f9..0000000 --- a/tests/unit/Mapper/ArrayMapperTest.php +++ /dev/null @@ -1,28 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Unit\Mapper; - -use BitBadger\PDODocument\Mapper\ArrayMapper; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * Unit tests for the ArrayMapper class - */ -#[TestDox('Array Mapper (Unit tests)')] -class ArrayMapperTest extends TestCase -{ - #[TestDox('map() succeeds')] - public function testMapSucceeds(): void - { - $result = ['one' => 2, 'three' => 4, 'eight' => 'five']; - $mapped = (new ArrayMapper())->map($result); - $this->assertSame($result, $mapped, 'The array mapper should return the parameter given to it'); - } -} diff --git a/tests/unit/Mapper/CountMapperTest.php b/tests/unit/Mapper/CountMapperTest.php deleted file mode 100644 index 67155c9..0000000 --- a/tests/unit/Mapper/CountMapperTest.php +++ /dev/null @@ -1,26 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Unit\Mapper; - -use BitBadger\PDODocument\Mapper\CountMapper; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * Unit tests for the CountMapper class - */ -#[TestDox('Count Mapper (Unit tests)')] -class CountMapperTest extends TestCase -{ - #[TestDox('map() succeeds')] - public function testMapSucceeds(): void - { - $this->assertEquals(5, (new CountMapper())->map([5, 8, 10]), 'Count not correct'); - } -} diff --git a/tests/unit/Mapper/DocumentMapperTest.php b/tests/unit/Mapper/DocumentMapperTest.php deleted file mode 100644 index 7a03f4a..0000000 --- a/tests/unit/Mapper/DocumentMapperTest.php +++ /dev/null @@ -1,82 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Unit\Mapper; - -use BitBadger\PDODocument\{DocumentException, Field}; -use BitBadger\PDODocument\Mapper\DocumentMapper; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; -use Test\{PjsonDocument, PjsonId}; - -// ** Test class hierarchy for serialization ** - -class SubDocument -{ - public function __construct(public int $id = 0, public string $name = '') { } -} - -class TestDocument -{ - public function __construct(public int $id = 0, public SubDocument $subDoc = new SubDocument()) { } -} - -/** - * Unit tests for the DocumentMapper class - */ -#[TestDox('Document Mapper (Unit tests)')] -class DocumentMapperTest extends TestCase -{ - public function testConstructorSucceedsWithDefaultField(): void - { - $mapper = new DocumentMapper(Field::class); - $this->assertEquals('data', $mapper->fieldName, 'Default field name should have been "data"'); - } - - public function testConstructorSucceedsWithSpecifiedField(): void - { - $mapper = new DocumentMapper(Field::class, 'json'); - $this->assertEquals('json', $mapper->fieldName, 'Field name not recorded correctly'); - } - - #[TestDox('map() succeeds with valid JSON')] - public function testMapSucceedsWithValidJSON(): void - { - $doc = (new DocumentMapper(TestDocument::class))->map(['data' => '{"id":7,"subDoc":{"id":22,"name":"tester"}}']); - $this->assertNotNull($doc, 'The document should not have been null'); - $this->assertEquals(7, $doc->id, 'ID not filled correctly'); - $this->assertNotNull($doc->subDoc, 'The sub-document should not have been null'); - $this->assertEquals(22, $doc->subDoc->id, 'Sub-document ID not filled correctly'); - $this->assertEquals('tester', $doc->subDoc->name, 'Sub-document name not filled correctly'); - } - - #[TestDox('map() succeeds with valid JSON for Pjson class')] - public function testMapSucceedsWithValidJSONForPjsonClass(): void - { - $doc = (new DocumentMapper(PjsonDocument::class))->map(['data' => '{"id":"seven","name":"bob","num_value":8}']); - $this->assertNotNull($doc, 'The document should not have been null'); - $this->assertEquals(new PjsonId('seven'), $doc->id, 'ID not filled correctly'); - $this->assertEquals('bob', $doc->name, 'Name not filled correctly'); - $this->assertEquals(8, $doc->numValue, 'Numeric value not filled correctly'); - $this->assertFalse(isset($doc->skipped), 'Non-JSON field has not been set'); - } - - #[TestDox('map() fails with invalid JSON')] - public function testMapFailsWithInvalidJSON(): void - { - $this->expectException(DocumentException::class); - (new DocumentMapper(TestDocument::class))->map(['data' => 'this is not valid']); - } - - #[TestDox('map() fails with invalid JSON for Pjson class')] - public function testMapFailsWithInvalidJSONForPjsonClass(): void - { - $this->expectException(DocumentException::class); - (new DocumentMapper(PjsonDocument::class))->map(['data' => 'not even close']); - } -} diff --git a/tests/unit/Mapper/ExistsMapperTest.php b/tests/unit/Mapper/ExistsMapperTest.php deleted file mode 100644 index 22e3f45..0000000 --- a/tests/unit/Mapper/ExistsMapperTest.php +++ /dev/null @@ -1,51 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Unit\Mapper; - -use BitBadger\PDODocument\{Configuration, DocumentException, Mode}; -use BitBadger\PDODocument\Mapper\ExistsMapper; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * Unit tests for the ExistsMapper class - */ -#[TestDox('Exists Mapper (Unit tests)')] -class ExistsMapperTest extends TestCase -{ - #[TestDox('map() succeeds for PostgreSQL')] - public function testMapSucceedsForPostgreSQL(): void - { - try { - Configuration::overrideMode(Mode::PgSQL); - $this->assertFalse((new ExistsMapper())->map([false, 'nope']), 'Result should have been false'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('map() succeeds for SQLite')] - public function testMapSucceedsForSQLite(): void - { - try { - Configuration::overrideMode(Mode::SQLite); - $this->assertTrue((new ExistsMapper())->map([1, 'yep']), 'Result should have been true'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('map() fails when mode not set')] - public function testMapFailsWhenModeNotSet(): void - { - $this->expectException(DocumentException::class); - Configuration::overrideMode(null); - (new ExistsMapper())->map(['0']); - } -} diff --git a/tests/unit/Mapper/StringMapperTest.php b/tests/unit/Mapper/StringMapperTest.php deleted file mode 100644 index 3a4d0bc..0000000 --- a/tests/unit/Mapper/StringMapperTest.php +++ /dev/null @@ -1,43 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Unit\Mapper; - -use BitBadger\PDODocument\Mapper\StringMapper; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * Unit tests for the StringMapper class - */ -#[TestDox('String Mapper (Unit tests)')] -class StringMapperTest extends TestCase -{ - #[TestDox('map() succeeds when field is present and string')] - public function testMapSucceedsWhenFieldIsPresentAndString(): void - { - $result = ['test_field' => 'test_value']; - $mapper = new StringMapper('test_field'); - $this->assertEquals('test_value', $mapper->map($result), 'String value not returned correctly'); - } - - #[TestDox('map() succeeds when field is present and not string')] - public function testMapSucceedsWhenFieldIsPresentAndNotString(): void - { - $result = ['a_number' => 6.7]; - $mapper = new StringMapper('a_number'); - $this->assertEquals('6.7', $mapper->map($result), 'Number value not returned correctly'); - } - - #[TestDox('map() succeeds when field is not present')] - public function testMapSucceedsWhenFieldIsNotPresent(): void - { - $mapper = new StringMapper('something_else'); - $this->assertNull($mapper->map([]), 'Missing value not returned correctly'); - } -} diff --git a/tests/unit/ModeTest.php b/tests/unit/ModeTest.php deleted file mode 100644 index 0987bcd..0000000 --- a/tests/unit/ModeTest.php +++ /dev/null @@ -1,39 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Unit; - -use BitBadger\PDODocument\{DocumentException, Mode}; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * Unit tests for the Mode enumeration - */ -#[TestDox('Mode (Unit tests)')] -class ModeTest extends TestCase -{ - #[TestDox('deriveFromDSN() succeeds for PostgreSQL')] - public function testDeriveFromDSNSucceedsForPostgreSQL(): void - { - $this->assertEquals(Mode::PgSQL, Mode::deriveFromDSN('pgsql:Host=localhost'), 'PostgreSQL mode incorrect'); - } - - #[TestDox('deriveFromDSN() succeeds for SQLite')] - public function testDeriveFromDSNSucceedsForSQLite(): void - { - $this->assertEquals(Mode::SQLite, Mode::deriveFromDSN('sqlite:data.db'), 'SQLite mode incorrect'); - } - - #[TestDox('deriveFromDSN() fails for MySQL')] - public function testDeriveFromDSNFailsForMySQL(): void - { - $this->expectException(DocumentException::class); - Mode::deriveFromDSN('mysql:Host=localhost'); - } -} diff --git a/tests/unit/OpTest.php b/tests/unit/OpTest.php deleted file mode 100644 index 380d24f..0000000 --- a/tests/unit/OpTest.php +++ /dev/null @@ -1,86 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Unit; - -use BitBadger\PDODocument\Op; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * Unit tests for the Op enumeration - */ -#[TestDox('Op (Unit tests)')] -class OpTest extends TestCase -{ - #[TestDox('toSQL() succeeds for Equal')] - public function testToSQLSucceedsForEqual(): void - { - $this->assertEquals('=', Op::Equal->toSQL(), 'Equal SQL operator incorrect'); - } - - #[TestDox('toSQL() succeeds for Greater')] - public function testToSQLSucceedsForGreater(): void - { - $this->assertEquals('>', Op::Greater->toSQL(), 'Greater SQL operator incorrect'); - } - - #[TestDox('toSQL() succeeds for GreaterOrEqual')] - public function testToSQLSucceedsForGreaterOrEqual(): void - { - $this->assertEquals('>=', Op::GreaterOrEqual->toSQL(), 'GreaterOrEqual SQL operator incorrect'); - } - - #[TestDox('toSQL() succeeds for Less')] - public function testToSQLSucceedsForLess(): void - { - $this->assertEquals('<', Op::Less->toSQL(), 'Less SQL operator incorrect'); - } - - #[TestDox('toSQL() succeeds for LessOrEqual')] - public function testToSQLSucceedsForLessOrEqual(): void - { - $this->assertEquals('<=', Op::LessOrEqual->toSQL(), 'LessOrEqual SQL operator incorrect'); - } - - #[TestDox('toSQL() succeeds for NotEqual')] - public function testToSQLSucceedsForNotEqual(): void - { - $this->assertEquals('<>', Op::NotEqual->toSQL(), 'NotEqual SQL operator incorrect'); - } - - #[TestDox('toSQL() succeeds for Between')] - public function testToSQLSucceedsForBetween(): void - { - $this->assertEquals('BETWEEN', Op::Between->toSQL(), 'Between SQL operator incorrect'); - } - - #[TestDox('toSQL() succeeds for In')] - public function testToSQLSucceedsForIn(): void - { - $this->assertEquals('IN', Op::In->toSQL(), 'In SQL operator incorrect'); - } - - #[TestDox('toSQL() succeeds for InArray')] - public function testToSQLSucceedsForInArray(): void - { - $this->assertEquals('??|', Op::InArray->toSQL(), 'InArray SQL operator incorrect'); - } - - #[TestDox('toSQL() succeeds for Exists')] - public function testToSQLSucceedsForExists(): void - { - $this->assertEquals('IS NOT NULL', Op::Exists->toSQL(), 'Exists SQL operator incorrect'); - } - - #[TestDox('toSQL() succeeds for NotExists')] - public function testToSQLSucceedsForNEX(): void - { - $this->assertEquals('IS NULL', Op::NotExists->toSQL(), 'NotExists SQL operator incorrect'); - } -} diff --git a/tests/unit/ParametersTest.php b/tests/unit/ParametersTest.php deleted file mode 100644 index 68da6d9..0000000 --- a/tests/unit/ParametersTest.php +++ /dev/null @@ -1,134 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Unit; - -use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode, Parameters}; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; -use stdClass; -use Test\{PjsonDocument, PjsonId}; - -/** - * Unit tests for the Parameters class - */ -#[TestDox('Parameters (Unit tests)')] -class ParametersTest extends TestCase -{ - #[TestDox('id() succeeds with string')] - public function testIdSucceedsWithString(): void - { - $this->assertEquals([':id' => 'key'], Parameters::id('key'), 'ID parameter not constructed correctly'); - } - - #[TestDox('id() succeeds with non string')] - public function testIdSucceedsWithNonString(): void - { - $this->assertEquals([':id' => '7'], Parameters::id(7), 'ID parameter not constructed correctly'); - } - - #[TestDox('json() succeeds for array')] - public function testJsonSucceedsForArray(): void - { - $this->assertEquals([':it' => '{"id":18,"url":"https://www.unittest.com"}'], - Parameters::json(':it', ['id' => 18, 'url' => 'https://www.unittest.com']), - 'JSON parameter not constructed correctly'); - } - - #[TestDox('json() succeeds for array with empty array parameter')] - public function testJsonSucceedsForArrayWithEmptyArrayParameter(): void - { - $this->assertEquals([':it' => '{"id":18,"urls":[]}'], Parameters::json(':it', ['id' => 18, 'urls' => []]), - 'JSON parameter not constructed correctly'); - } - - #[TestDox('json() succeeds for 1D array with empty array parameter')] - public function testJsonSucceedsFor1DArrayWithEmptyArrayParameter(): void - { - $this->assertEquals([':it' => '{"urls":[]}'], Parameters::json(':it', ['urls' => []]), - 'JSON parameter not constructed correctly'); - } - - #[TestDox('json() succeeds for stdClass')] - public function testJsonSucceedsForStdClass(): void - { - $obj = new stdClass(); - $obj->id = 19; - $obj->url = 'https://testhere.info'; - $this->assertEquals([':it' => '{"id":19,"url":"https://testhere.info"}'], Parameters::json(':it', $obj), - 'JSON parameter not constructed correctly'); - } - - #[TestDox('json() succeeds for Pjson class')] - public function testJsonSucceedsForPjsonClass(): void - { - $this->assertEquals([':it' => '{"id":"999","name":"a test","num_value":98}'], - Parameters::json(':it', new PjsonDocument(new PjsonId('999'), 'a test', 98, 'nothing')), - 'JSON parameter not constructed correctly'); - } - - #[TestDox('json() succeeds for array of Pjson class')] - public function testJsonSucceedsForArrayOfPjsonClass(): void - { - $this->assertEquals([':it' => '{"pjson":[{"id":"997","name":"another test","num_value":94}]}'], - Parameters::json(':it', - ['pjson' => [new PjsonDocument(new PjsonId('997'), 'another test', 94, 'nothing')]]), - 'JSON parameter not constructed correctly'); - } - - #[TestDox('nameFields() succeeds')] - public function testNameFieldsSucceeds(): void - { - $named = [Field::equal('it', 17), Field::equal('also', 22, ':also'), Field::equal('other', 24)]; - Parameters::nameFields($named); - $this->assertCount(3, $named, 'There should be 3 parameters in the array'); - $this->assertEquals(':field0', $named[0]->paramName, 'Parameter 1 not named correctly'); - $this->assertEquals(':also', $named[1]->paramName, 'Parameter 2 not named correctly'); - $this->assertEquals(':field2', $named[2]->paramName, 'Parameter 3 not named correctly'); - } - - #[TestDox('addFields() succeeds')] - public function testAddFieldsSucceeds(): void - { - $this->assertEquals([':a' => 1, ':b' => 'two', ':z' => 18], - Parameters::addFields([Field::equal('b', 'two', ':b'), Field::equal('z', 18, ':z')], [':a' => 1]), - 'Field parameters not added correctly'); - } - - #[TestDox('fieldNames() succeeds for PostgreSQL')] - public function testFieldNamesSucceedsForPostgreSQL(): void - { - try { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals([':names' => "{one,two,seven}"], - Parameters::fieldNames(':names', ['one', 'two', 'seven']), 'Field name parameters not correct'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('fieldNames() succeeds for SQLite')] - public function testFieldNamesSucceedsForSQLite(): void - { - try { - Configuration::overrideMode(Mode::SQLite); - $this->assertEquals([':it0' => '$.test', ':it1' => '$.unit', ':it2' => '$.wow'], - Parameters::fieldNames(':it', ['test', 'unit', 'wow']), 'Field name parameters not correct'); - } finally { - Configuration::overrideMode(null); - } - } - - #[TestDox('fieldNames() fails when mode not set')] - public function testFieldNamesFailsWhenModeNotSet(): void - { - $this->expectException(DocumentException::class); - Configuration::overrideMode(null); - Parameters::fieldNames('', []); - } -} diff --git a/tests/unit/Query/CountTest.php b/tests/unit/Query/CountTest.php deleted file mode 100644 index cdad3ac..0000000 --- a/tests/unit/Query/CountTest.php +++ /dev/null @@ -1,73 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Unit\Query; - -use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode}; -use BitBadger\PDODocument\Query\Count; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * Unit tests for the Count class - */ -#[TestDox('Count Queries (Unit tests)')] -class CountTest extends TestCase -{ - public function tearDown(): void - { - Configuration::overrideMode(null); - parent::tearDown(); - } - - #[TestDox('all() succeeds')] - public function testAllSucceeds(): void - { - $this->assertEquals('SELECT COUNT(*) FROM a_table', Count::all('a_table'), - 'SELECT statement not generated correctly'); - } - - #[TestDox('byFields() succeeds')] - public function testByFieldsSucceeds(): void - { - Configuration::overrideMode(Mode::SQLite); - $this->assertEquals("SELECT COUNT(*) FROM somewhere WHERE data->>'errors' > :errors", - Count::byFields('somewhere', [Field::greater('errors', 10, ':errors')]), - 'SELECT statement not generated correctly'); - } - - #[TestDox('byContains() succeeds for PostgreSQL')] - public function testByContainsSucceedsForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals('SELECT COUNT(*) FROM the_table WHERE data @> :criteria', Count::byContains('the_table'), - 'SELECT statement not generated correctly'); - } - - #[TestDox('byContains() fails for non PostgreSQL')] - public function testByContainsFailsForNonPostgreSQL(): void - { - $this->expectException(DocumentException::class); - Count::byContains(''); - } - - #[TestDox('byJsonPath() succeeds for PostgreSQL')] - public function testByJsonPathSucceedsForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals('SELECT COUNT(*) FROM a_table WHERE jsonb_path_exists(data, :path::jsonpath)', - Count::byJsonPath('a_table'), 'SELECT statement not generated correctly'); - } - - #[TestDox('byJsonPath() fails for non PostgreSQL')] - public function testByJsonPathFailsForNonPostgreSQL(): void - { - $this->expectException(DocumentException::class); - Count::byJsonPath(''); - } -} diff --git a/tests/unit/Query/DefinitionTest.php b/tests/unit/Query/DefinitionTest.php deleted file mode 100644 index 3af4c42..0000000 --- a/tests/unit/Query/DefinitionTest.php +++ /dev/null @@ -1,96 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Unit\Query; - -use BitBadger\PDODocument\{Configuration, DocumentException, DocumentIndex, Mode}; -use BitBadger\PDODocument\Query\Definition; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * Unit tests for the Definition class - */ -#[TestDox('Definition Queries (Unit tests)')] -class DefinitionTest extends TestCase -{ - protected function tearDown(): void - { - Configuration::overrideMode(null); - parent::tearDown(); - } - - #[TestDox('ensureTable() succeeds for PosgtreSQL')] - public function testEnsureTableSucceedsForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals('CREATE TABLE IF NOT EXISTS documents (data JSONB NOT NULL)', - Definition::ensureTable('documents'), 'CREATE TABLE statement not generated correctly'); - } - - #[TestDox('ensureTable() succeeds for SQLite')] - public function testEnsureTableSucceedsForSQLite(): void - { - Configuration::overrideMode(Mode::SQLite); - $this->assertEquals('CREATE TABLE IF NOT EXISTS dox (data TEXT NOT NULL)', Definition::ensureTable('dox'), - 'CREATE TABLE statement not generated correctly'); - } - - #[TestDox('ensureTable() fails when mode not set')] - public function testEnsureTableFailsWhenModeNotSet(): void - { - $this->expectException(DocumentException::class); - Definition::ensureTable('boom'); - } - - #[TestDox('ensureIndexOn() succeeds without schema single ascending field')] - public function testEnsureIndexOnSucceedsWithoutSchemaSingleAscendingField(): void - { - $this->assertEquals("CREATE INDEX IF NOT EXISTS idx_test_fields ON test ((data->>'details'))", - Definition::ensureIndexOn('test', 'fields', ['details']), 'CREATE INDEX statement not generated correctly'); - } - - #[TestDox('ensureIndexOn() succeeds with schema multiple fields')] - public function testEnsureIndexOnSucceedsWithSchemaMultipleFields(): void - { - $this->assertEquals( - "CREATE INDEX IF NOT EXISTS idx_testing_json ON sch.testing ((data->>'group'), (data->>'sub_group') DESC)", - Definition::ensureIndexOn('sch.testing', 'json', ['group', 'sub_group DESC']), - 'CREATE INDEX statement not generated correctly'); - } - - #[TestDox('ensureKey() succeeds')] - public function testEnsureKeySucceeds(): void - { - $this->assertEquals("CREATE UNIQUE INDEX IF NOT EXISTS idx_tbl_key ON tbl ((data->>'id'))", - Definition::ensureKey('tbl'), 'CREATE INDEX statement for document key not generated correctly'); - } - - #[TestDox('ensureDocumentIndexOn() succeeds for schema and Full')] - public function testEnsureDocumentIndexOnSucceedsForSchemaAndFull(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals("CREATE INDEX IF NOT EXISTS idx_tbl_document ON my.tbl USING GIN (data)", - Definition::ensureDocumentIndexOn('my.tbl', DocumentIndex::Full)); - } - - #[TestDox('ensureDocumentIndexOn() succeeds for no schema and Optimized')] - public function testEnsureDocumentIndexOnSucceedsForNoSchemaAndOptimized(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals("CREATE INDEX IF NOT EXISTS idx_it_document ON it USING GIN (data jsonb_path_ops)", - Definition::ensureDocumentIndexOn('it', DocumentIndex::Optimized)); - } - - #[TestDox('ensureDocumentIndexOn() fails for non PostgreSQL')] - public function testEnsureDocumentIndexOnFailsForNonPostgreSQL(): void - { - $this->expectException(DocumentException::class); - Definition::ensureDocumentIndexOn('', DocumentIndex::Full); - } -} diff --git a/tests/unit/Query/DeleteTest.php b/tests/unit/Query/DeleteTest.php deleted file mode 100644 index 0c3adce..0000000 --- a/tests/unit/Query/DeleteTest.php +++ /dev/null @@ -1,74 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Unit\Query; - -use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode}; -use BitBadger\PDODocument\Query\Delete; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * Unit tests for the Delete class - */ -#[TestDox('Delete Queries (Unit tests)')] -class DeleteTest extends TestCase -{ - protected function tearDown(): void - { - Configuration::overrideMode(null); - } - - #[TestDox('byId() succeeds')] - public function testByIdSucceeds(): void - { - Configuration::overrideMode(Mode::SQLite); - $this->assertEquals("DELETE FROM over_there WHERE data->>'id' = :id", Delete::byId('over_there'), - 'DELETE statement not constructed correctly'); - } - - #[TestDox('byFields() succeeds')] - public function testByFieldsSucceeds(): void - { - Configuration::overrideMode(Mode::SQLite); - $this->assertEquals("DELETE FROM my_table WHERE data->>'value' < :max AND data->>'value' >= :min", - Delete::byFields('my_table', - [Field::less('value', 99, ':max'), Field::greaterOrEqual('value', 18, ':min')]), - 'DELETE statement not constructed correctly'); - } - - #[TestDox('byContains() succeeds for PostgreSQL')] - public function testByContainsSucceedsForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals('DELETE FROM somewhere WHERE data @> :criteria', Delete::byContains('somewhere'), - 'DELETE statement not constructed correctly'); - } - - #[TestDox('byContains() fails for non PostgreSQL')] - public function testByContainsFailsForNonPostgreSQL(): void - { - $this->expectException(DocumentException::class); - Delete::byContains(''); - } - - #[TestDox('byJsonPath() succeeds for PostgreSQL')] - public function testByJsonPathSucceedsForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals('DELETE FROM here WHERE jsonb_path_exists(data, :path::jsonpath)', - Delete::byJsonPath('here'), 'DELETE statement not constructed correctly'); - } - - #[TestDox('byJsonPath() fails for non PostgreSQL')] - public function testByJsonPathFailsForNonPostgreSQL(): void - { - $this->expectException(DocumentException::class); - Delete::byJsonPath(''); - } -} diff --git a/tests/unit/Query/ExistsTest.php b/tests/unit/Query/ExistsTest.php deleted file mode 100644 index 1eb0fa4..0000000 --- a/tests/unit/Query/ExistsTest.php +++ /dev/null @@ -1,81 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Unit\Query; - -use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode}; -use BitBadger\PDODocument\Query\Exists; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * Unit tests for the Exists class - */ -#[TestDox('Exists Queries (Unit tests)')] -class ExistsTest extends TestCase -{ - protected function tearDown(): void - { - Configuration::overrideMode(null); - } - - #[TestDox('query() succeeds')] - public function testQuerySucceeds(): void - { - Configuration::overrideMode(Mode::SQLite); - $this->assertEquals('SELECT EXISTS (SELECT 1 FROM abc WHERE def)', Exists::query('abc', 'def'), - 'Existence query not generated correctly'); - } - - #[TestDox('byId() succeeds')] - public function testByIdSucceeds(): void - { - Configuration::overrideMode(Mode::SQLite); - $this->assertEquals("SELECT EXISTS (SELECT 1 FROM dox WHERE data->>'id' = :id)", Exists::byId('dox'), - 'Existence query not generated correctly'); - } - - #[TestDox('byFields() succeeds')] - public function testByFieldsSucceeds(): void - { - Configuration::overrideMode(Mode::SQLite); - $this->assertEquals("SELECT EXISTS (SELECT 1 FROM box WHERE data->>'status' <> :status)", - Exists::byFields('box', [Field::notEqual('status', 'occupied', ':status')]), - 'Existence query not generated correctly'); - } - - #[TestDox('byContains() succeeds for PostgreSQL')] - public function testByContainsSucceedsForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals('SELECT EXISTS (SELECT 1 FROM pocket WHERE data @> :criteria)', - Exists::byContains('pocket'), 'Existence query not generated correctly'); - } - - #[TestDox('byContains() fails for non PostgreSQL')] - public function testByContainsFailsForNonPostgreSQL(): void - { - $this->expectException(DocumentException::class); - Exists::byContains(''); - } - - #[TestDox('byJsonPath() succeeds for PostgreSQL')] - public function testByJsonPathSucceedsForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals('SELECT EXISTS (SELECT 1 FROM lint WHERE jsonb_path_exists(data, :path::jsonpath))', - Exists::byJsonPath('lint'), 'Existence query not generated correctly'); - } - - #[TestDox('byJsonPath() fails for non PostgreSQL')] - public function testByJsonPathFailsForNonPostgreSQL(): void - { - $this->expectException(DocumentException::class); - Exists::byJsonPath(''); - } -} diff --git a/tests/unit/Query/FindTest.php b/tests/unit/Query/FindTest.php deleted file mode 100644 index c1f471b..0000000 --- a/tests/unit/Query/FindTest.php +++ /dev/null @@ -1,74 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Unit\Query; - -use BitBadger\PDODocument\{Configuration, DocumentException, Field, FieldMatch, Mode}; -use BitBadger\PDODocument\Query\Find; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * Unit tests for the Find class - */ -#[TestDox('Find Queries (Unit tests)')] -class FindTest extends TestCase -{ - protected function tearDown(): void - { - Configuration::overrideMode(null); - } - - #[TestDox('byId() succeeds')] - public function testByIdSucceeds(): void - { - Configuration::overrideMode(Mode::SQLite); - $this->assertEquals("SELECT data FROM here WHERE data->>'id' = :id", Find::byId('here'), - 'SELECT query not generated correctly'); - } - - #[TestDox('byFields() succeeds')] - public function testByFieldsSucceeds(): void - { - Configuration::overrideMode(Mode::SQLite); - $this->assertEquals("SELECT data FROM there WHERE data->>'active' = :act OR data->>'locked' = :lock", - Find::byFields('there', [Field::equal('active', true, ':act'), Field::equal('locked', true, ':lock')], - FieldMatch::Any), - 'SELECT query not generated correctly'); - } - - #[TestDox('byContains() succeeds for PostgreSQL')] - public function testByContainsSucceedsForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals('SELECT data FROM disc WHERE data @> :criteria', Find::byContains('disc'), - 'SELECT query not generated correctly'); - } - - #[TestDox('byContains() fails for non PostgreSQL')] - public function testByContainsFailsForNonPostgreSQL(): void - { - $this->expectException(DocumentException::class); - Find::byContains(''); - } - - #[TestDox('byJsonPath() succeeds for PostgreSQL')] - public function testByJsonPathSucceedsForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals('SELECT data FROM light WHERE jsonb_path_exists(data, :path::jsonpath)', - Find::byJsonPath('light'), 'SELECT query not generated correctly'); - } - - #[TestDox('byJsonPath() fails for non PostgreSQL')] - public function testByJsonPathFailsForNonPostgreSQL(): void - { - $this->expectException(DocumentException::class); - Find::byJsonPath(''); - } -} diff --git a/tests/unit/Query/PatchTest.php b/tests/unit/Query/PatchTest.php deleted file mode 100644 index 415ff0c..0000000 --- a/tests/unit/Query/PatchTest.php +++ /dev/null @@ -1,104 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Unit\Query; - -use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode}; -use BitBadger\PDODocument\Query\Patch; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * Unit tests for the Patch class - */ -#[TestDox('Patch Queries (Unit tests)')] -class PatchTest extends TestCase -{ - protected function tearDown(): void - { - Configuration::overrideMode(null); - parent::tearDown(); - } - #[TestDox('byId() succeeds for PostgreSQL')] - public function testByIdSucceedsForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals("UPDATE doc_table SET data = data || :data WHERE data->>'id' = :id", - Patch::byId('doc_table'), 'Patch UPDATE statement is not correct'); - } - - #[TestDox('byId() succeeds for SQLite')] - public function testByIdSucceedsForSQLite(): void - { - Configuration::overrideMode(Mode::SQLite); - $this->assertEquals("UPDATE my_table SET data = json_patch(data, json(:data)) WHERE data->>'id' = :id", - Patch::byId('my_table'), 'Patch UPDATE statement is not correct'); - } - - #[TestDox('byId() fails when mode not set')] - public function testByIdFailsWhenModeNotSet(): void - { - $this->expectException(DocumentException::class); - Patch::byId('oof'); - } - - #[TestDox('byFields() succeeds for PostgreSQL')] - public function testByFieldsSucceedsForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals("UPDATE that SET data = data || :data WHERE (data->>'something')::numeric < :some", - Patch::byFields('that', [Field::less('something', 17, ':some')]), 'Patch UPDATE statement is not correct'); - } - - #[TestDox('byFields() succeeds for SQLite')] - public function testByFieldsSucceedsForSQLite(): void - { - Configuration::overrideMode(Mode::SQLite); - $this->assertEquals( - "UPDATE a_table SET data = json_patch(data, json(:data)) WHERE data->>'something' > :it", - Patch::byFields('a_table', [Field::greater('something', 17, ':it')]), - 'Patch UPDATE statement is not correct'); - } - - #[TestDox('byFields() fails when mode not set')] - public function testByFieldsFailsWhenModeNotSet(): void - { - $this->expectException(DocumentException::class); - Patch::byFields('oops', []); - } - - #[TestDox('byContains() succeeds for PostgreSQL')] - public function testByContainsSucceedsForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals('UPDATE this SET data = data || :data WHERE data @> :criteria', Patch::byContains('this'), - 'Patch UPDATE statement is not correct'); - } - - #[TestDox('byContains() fails for non PostgreSQL')] - public function testByContainsFailsForNonPostgreSQL(): void - { - $this->expectException(DocumentException::class); - Patch::byContains(''); - } - - #[TestDox('byJsonPath() succeeds for PostgreSQL')] - public function testByJsonPathSucceedsForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals('UPDATE that SET data = data || :data WHERE jsonb_path_exists(data, :path::jsonpath)', - Patch::byJsonPath('that'), 'Patch UPDATE statement is not correct'); - } - - #[TestDox('byJsonPath() fails for non PostgreSQL')] - public function testByJsonPathFailsForNonPostgreSQL(): void - { - $this->expectException(DocumentException::class); - Patch::byJsonPath(''); - } -} diff --git a/tests/unit/Query/RemoveFieldsTest.php b/tests/unit/Query/RemoveFieldsTest.php deleted file mode 100644 index 7f8d0a9..0000000 --- a/tests/unit/Query/RemoveFieldsTest.php +++ /dev/null @@ -1,135 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Unit\Query; - -use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode, Parameters}; -use BitBadger\PDODocument\Query\RemoveFields; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * Unit tests for the RemoveFields class - */ -#[TestDox('Remove Fields Queries (Unit tests)')] -class RemoveFieldsTest extends TestCase -{ - protected function tearDown(): void - { - Configuration::overrideMode(null); - } - - #[TestDox('update() succeeds for PostgreSQL')] - public function testUpdateSucceedsForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals('UPDATE taco SET data = data - :names::text[] WHERE it = true', - RemoveFields::update('taco', [':names' => "{one,two}"], 'it = true'), 'UPDATE statement not correct'); - } - - #[TestDox('update() succeeds for SQLite')] - public function testUpdateSucceedsForSQLite(): void - { - Configuration::overrideMode(Mode::SQLite); - $this->assertEquals('UPDATE burrito SET data = json_remove(data, :name0, :name1, :name2) WHERE a = b', - RemoveFields::update('burrito', Parameters::fieldNames(':name', ['one', 'two', 'ten']), 'a = b'), - 'UPDATE statement not correct'); - } - - #[TestDox('update() fails when mode not set')] - public function testUpdateFailsWhenModeNotSet(): void - { - $this->expectException(DocumentException::class); - RemoveFields::update('wow', [], ''); - } - - #[TestDox('byId() succeeds for PostgreSQL')] - public function testByIdSucceedsForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals("UPDATE churro SET data = data - :bite::text[] WHERE data->>'id' = :id", - RemoveFields::byId('churro', Parameters::fieldNames(':bite', ['byte'])), 'UPDATE statement not correct'); - } - - #[TestDox('byId() succeeds for SQLite')] - public function testByIdSucceedsForSQLite(): void - { - Configuration::overrideMode(Mode::SQLite); - $this->assertEquals("UPDATE quesadilla SET data = json_remove(data, :bite0) WHERE data->>'id' = :id", - RemoveFields::byId('quesadilla', Parameters::fieldNames(':bite', ['byte'])), - 'UPDATE statement not correct'); - } - - #[TestDox('byId() fails when mode not set')] - public function testByIdFailsWhenModeNotSet(): void - { - $this->expectException(DocumentException::class); - RemoveFields::byId('oof', []); - } - - #[TestDox('byFields() succeeds for PostgreSQL')] - public function testByFieldsSucceedsForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals("UPDATE enchilada SET data = data - :sauce::text[] WHERE data->>'cheese' = :queso", - RemoveFields::byFields('enchilada', [Field::equal('cheese', 'jack', ':queso')], - Parameters::fieldNames(':sauce', ['white'])), - 'UPDATE statement not correct'); - } - - #[TestDox('byFields() succeeds for SQLite')] - public function testByFieldsSucceedsForSQLite(): void - { - Configuration::overrideMode(Mode::SQLite); - $this->assertEquals( - "UPDATE chimichanga SET data = json_remove(data, :filling0) WHERE data->>'side' = :rice", - RemoveFields::byFields('chimichanga', [Field::equal('side', 'beans', ':rice')], - Parameters::fieldNames(':filling', ['beef'])), - 'UPDATE statement not correct'); - } - - #[TestDox('byFields() fails when mode not set')] - public function testByFieldsFailsWhenModeNotSet(): void - { - $this->expectException(DocumentException::class); - RemoveFields::byFields('boing', [], []); - } - - #[TestDox('byContains() succeeds for PostgreSQL')] - public function testByContainsSucceedsForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals('UPDATE food SET data = data - :drink::text[] WHERE data @> :criteria', - RemoveFields::byContains('food', Parameters::fieldNames(':drink', ['a', 'b'])), - 'UPDATE statement not correct'); - } - - #[TestDox('byContains() fails for non PostgreSQL')] - public function testByContainsFailsForNonPostgreSQL(): void - { - $this->expectException(DocumentException::class); - RemoveFields::byContains('', []); - } - - #[TestDox('byJsonPath() succeeds for PostgreSQL')] - public function testByJsonPathSucceedsForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals( - 'UPDATE dessert SET data = data - :cake::text[] WHERE jsonb_path_exists(data, :path::jsonpath)', - RemoveFields::byJsonPath('dessert', Parameters::fieldNames(':cake', ['b', 'c'])), - 'UPDATE statement not correct'); - } - - #[TestDox('byJsonPath() fails for non PostgreSQL')] - public function testByJsonPathFailsForNonPostgreSQL(): void - { - $this->expectException(DocumentException::class); - RemoveFields::byJsonPath('', []); - } -} diff --git a/tests/unit/QueryTest.php b/tests/unit/QueryTest.php deleted file mode 100644 index 75606a4..0000000 --- a/tests/unit/QueryTest.php +++ /dev/null @@ -1,323 +0,0 @@ - - * @license MIT - */ - -declare(strict_types=1); - -namespace Test\Unit; - -use BitBadger\PDODocument\{AutoId, Configuration, DocumentException, Field, FieldMatch, Mode, Query}; -use PHPUnit\Framework\Attributes\TestDox; -use PHPUnit\Framework\TestCase; - -/** - * Unit tests for the Query class - */ -#[TestDox('Query (Unit tests)')] -class QueryTest extends TestCase -{ - protected function setUp(): void - { - Configuration::overrideMode(Mode::SQLite); - } - - protected function tearDown(): void - { - Configuration::overrideMode(null); - } - - #[TestDox('selectFromTable() succeeds')] - public function testSelectFromTableSucceeds(): void - { - $this->assertEquals('SELECT data FROM testing', Query::selectFromTable('testing'), - 'Query not constructed correctly'); - } - - #[TestDox('whereByFields() succeeds for single field')] - public function testWhereByFieldsSucceedsForSingleField(): void - { - $this->assertEquals("data->>'test_field' <= :it", - Query::whereByFields([Field::lessOrEqual('test_field', '', ':it')]), - 'WHERE fragment not constructed correctly'); - } - - #[TestDox('whereByFields() succeeds for multiple fields All')] - public function testWhereByFieldsSucceedsForMultipleFieldsAll(): void - { - $this->assertEquals("data->>'test_field' <= :it AND data->>'other_field' = :other", - Query::whereByFields( - [Field::lessOrEqual('test_field', '', ':it'), Field::equal('other_field', '', ':other')]), - 'WHERE fragment not constructed correctly'); - } - - #[TestDox('whereByFields() succeeds for multiple fields Any')] - public function testWhereByFieldsSucceedsForMultipleFieldsAny(): void - { - $this->assertEquals("data->>'test_field' <= :it OR data->>'other_field' = :other", - Query::whereByFields( - [Field::lessOrEqual('test_field', '', ':it'), Field::equal('other_field', '', ':other')], - FieldMatch::Any), - 'WHERE fragment not constructed correctly'); - } - - #[TestDox('whereById() succeeds with default parameter')] - public function testWhereByIdSucceedsWithDefaultParameter(): void - { - $this->assertEquals("data->>'id' = :id", Query::whereById(), 'WHERE fragment not constructed correctly'); - } - - #[TestDox('whereById() succeeds with specific parameter')] - public function testWhereByIdSucceedsWithSpecificParameter(): void - { - $this->assertEquals("data->>'id' = :di", Query::whereById(':di'), 'WHERE fragment not constructed correctly'); - } - - #[TestDox('whereDataContains() succeeds with default parameter')] - public function testWhereDataContainsSucceedsWithDefaultParameter(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals('data @> :criteria', Query::whereDataContains(), - 'WHERE fragment not constructed correctly'); - } - - #[TestDox('whereDataContains() succeeds with specific parameter')] - public function testWhereDataContainsSucceedsWithSpecifiedParameter(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals('data @> :it', Query::whereDataContains(':it'), 'WHERE fragment not constructed correctly'); - } - - #[TestDox('whereDataContains() fails if not PostgreSQL')] - public function testWhereDataContainsFailsIfNotPostgreSQL(): void - { - Configuration::overrideMode(null); - $this->expectException(DocumentException::class); - Query::whereDataContains(); - } - - #[TestDox('whereJsonPathMatches() succeeds with default parameter')] - public function testWhereJsonPathMatchesSucceedsWithDefaultParameter(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals('jsonb_path_exists(data, :path::jsonpath)', Query::whereJsonPathMatches(), - 'WHERE fragment not constructed correctly'); - } - - #[TestDox('whereJsonPathMatches() succeeds with specified parameter')] - public function testWhereJsonPathMatchesSucceedsWithSpecifiedParameter(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals('jsonb_path_exists(data, :road::jsonpath)', Query::whereJsonPathMatches(':road'), - 'WHERE fragment not constructed correctly'); - } - - #[TestDox('whereJsonPathMatches() fails if not PostgreSQL')] - public function testWhereJsonPathMatchesFailsIfNotPostgreSQL(): void - { - Configuration::overrideMode(null); - $this->expectException(DocumentException::class); - Query::whereJsonPathMatches(); - } - - #[TestDox('insert() succeeds with no auto-ID for PostgreSQL')] - public function testInsertSucceedsWithNoAutoIdForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals('INSERT INTO test_tbl VALUES (:data)', Query::insert('test_tbl'), - 'INSERT statement not constructed correctly'); - } - - #[TestDox('insert() succeeds with no auto-ID for SQLite')] - public function testInsertSucceedsWithNoAutoIdForSQLite(): void - { - $this->assertEquals('INSERT INTO test_tbl VALUES (:data)', Query::insert('test_tbl'), - 'INSERT statement not constructed correctly'); - } - - #[TestDox('insert() succeeds with auto numeric ID for PostgreSQL')] - public function testInsertSucceedsWithAutoNumericIdForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals( - "INSERT INTO test_tbl VALUES (:data::jsonb || ('{\"id\":' " - . "|| (SELECT COALESCE(MAX((data->>'id')::numeric), 0) + 1 FROM test_tbl) || '}')::jsonb)", - Query::insert('test_tbl', AutoId::Number), 'INSERT statement not constructed correctly'); - } - - #[TestDox('insert() succeeds with auto numeric ID for SQLite')] - public function testInsertSucceedsWithAutoNumericIdForSQLite(): void - { - $this->assertEquals( - "INSERT INTO test_tbl VALUES (json_set(:data, '$.id', " - . "(SELECT coalesce(max(data->>'id'), 0) + 1 FROM test_tbl)))", - Query::insert('test_tbl', AutoId::Number), 'INSERT statement not constructed correctly'); - } - - #[TestDox('insert() succeeds with auto UUID for PostgreSQL')] - public function testInsertSucceedsWithAutoUuidForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $query = Query::insert('test_tbl', AutoId::UUID); - $this->assertStringStartsWith("INSERT INTO test_tbl VALUES (:data::jsonb || '{\"id\":\"", $query, - 'INSERT statement not constructed correctly'); - $this->assertStringEndsWith("\"}')", $query, 'INSERT statement not constructed correctly'); - } - - #[TestDox('insert() succeeds with auto UUID for SQLite')] - public function testInsertSucceedsWithAutoUuidForSQLite(): void - { - $query = Query::insert('test_tbl', AutoId::UUID); - $this->assertStringStartsWith("INSERT INTO test_tbl VALUES (json_set(:data, '$.id', '", $query, - 'INSERT statement not constructed correctly'); - $this->assertStringEndsWith("'))", $query, 'INSERT statement not constructed correctly'); - } - - #[TestDox('insert() succeeds with auto random string for PostgreSQL')] - public function testInsertSucceedsWithAutoRandomStringForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - Configuration::$idStringLength = 8; - try { - $query = Query::insert('test_tbl', AutoId::RandomString); - $this->assertStringStartsWith("INSERT INTO test_tbl VALUES (:data::jsonb || '{\"id\":\"", $query, - 'INSERT statement not constructed correctly'); - $this->assertStringEndsWith("\"}')", $query, 'INSERT statement not constructed correctly'); - $id = str_replace(["INSERT INTO test_tbl VALUES (:data::jsonb || '{\"id\":\"", "\"}')"], '', $query); - $this->assertEquals(8, strlen($id), "Generated ID [$id] should have been 8 characters long"); - } finally { - Configuration::$idStringLength = 16; - } - } - - #[TestDox('insert() succeeds with auto random string for SQLite')] - public function testInsertSucceedsWithAutoRandomStringForSQLite(): void - { - $query = Query::insert('test_tbl', AutoId::RandomString); - $this->assertStringStartsWith("INSERT INTO test_tbl VALUES (json_set(:data, '$.id', '", $query, - 'INSERT statement not constructed correctly'); - $this->assertStringEndsWith("'))", $query, 'INSERT statement not constructed correctly'); - $id = str_replace(["INSERT INTO test_tbl VALUES (json_set(:data, '$.id', '", "'))"], '', $query); - $this->assertEquals(16, strlen($id), "Generated ID [$id] should have been 16 characters long"); - } - - #[TestDox('insert() fails when mode not set')] - public function testInsertFailsWhenModeNotSet(): void - { - $this->expectException(DocumentException::class); - Configuration::overrideMode(null); - Query::insert('kaboom'); - } - - #[TestDox('save() succeeds')] - public function testSaveSucceeds(): void - { - $this->assertEquals( - "INSERT INTO test_tbl VALUES (:data) ON CONFLICT ((data->>'id')) DO UPDATE SET data = EXCLUDED.data", - Query::save('test_tbl'), 'INSERT ON CONFLICT statement not constructed correctly'); - } - - #[TestDox('update() succeeds')] - public function testUpdateSucceeds(): void - { - $this->assertEquals("UPDATE testing SET data = :data WHERE data->>'id' = :id", Query::update('testing'), - 'UPDATE statement not constructed correctly'); - } - - #[TestDox('orderBy() succeeds with no fields for PostgreSQL')] - public function testOrderBySucceedsWithNoFieldsForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals('', Query::orderBy([]), 'ORDER BY should have been blank'); - } - - #[TestDox('orderBy() succeeds with no fields for SQLite')] - public function testOrderBySucceedsWithNoFieldsForSQLite(): void - { - $this->assertEquals('', Query::orderBy([]), 'ORDER BY should have been blank'); - } - - #[TestDox('orderBy() succeeds with one field and no direction for PostgreSQL')] - public function testOrderBySucceedsWithOneFieldAndNoDirectionForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals(" ORDER BY data->>'TestField'", Query::orderBy([Field::named('TestField')]), - 'ORDER BY not constructed correctly'); - } - - #[TestDox('orderBy() succeeds with one field and no direction for SQLite')] - public function testOrderBySucceedsWithOneFieldAndNoDirectionForSQLite(): void - { - $this->assertEquals(" ORDER BY data->>'TestField'", Query::orderBy([Field::named('TestField')]), - 'ORDER BY not constructed correctly'); - } - - #[TestDox('orderBy() succeeds with one qualified field for PostgreSQL')] - public function testOrderBySucceedsWithOneQualifiedFieldForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $field = Field::named('TestField'); - $field->qualifier = 'qual'; - $this->assertEquals(" ORDER BY qual.data->>'TestField'", Query::orderBy([$field]), - 'ORDER BY not constructed correctly'); - } - - #[TestDox('orderBy() succeeds with one qualified field for SQLite')] - public function testOrderBySucceedsWithOneQualifiedFieldForSQLite(): void - { - $field = Field::named('TestField'); - $field->qualifier = 'qual'; - $this->assertEquals(" ORDER BY qual.data->>'TestField'", Query::orderBy([$field]), - 'ORDER BY not constructed correctly'); - } - - #[TestDox('orderBy() succeeds with multiple fields and direction for PostgreSQL')] - public function testOrderBySucceedsWithMultipleFieldsAndDirectionForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals(" ORDER BY data#>>'{Nested,Test,Field}' DESC, data->>'AnotherField', data->>'It' DESC", - Query::orderBy( - [Field::named('Nested.Test.Field DESC'), Field::named('AnotherField'), Field::named('It DESC')]), - 'ORDER BY not constructed correctly'); - } - - #[TestDox('orderBy() succeeds with multiple fields and direction for SQLite')] - public function testOrderBySucceedsWithMultipleFieldsAndDirectionForSQLite(): void - { - $this->assertEquals(" ORDER BY data->'Nested'->'Test'->>'Field' DESC, data->>'AnotherField', data->>'It' DESC", - Query::orderBy( - [Field::named('Nested.Test.Field DESC'), Field::named('AnotherField'), Field::named('It DESC')]), - 'ORDER BY not constructed correctly'); - } - - #[TestDox('orderBy() succeeds with numeric field for PostgreSQL')] - public function testOrderBySucceedsWithNumericFieldForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals(" ORDER BY (data->>'Test')::numeric", Query::orderBy([Field::named('n:Test')]), - 'ORDER BY not constructed correctly'); - } - - #[TestDox('orderBy() succeeds with numeric field for SQLite')] - public function testOrderBySucceedsWithNumericFieldForSQLite(): void - { - $this->assertEquals(" ORDER BY data->>'Test'", Query::orderBy([Field::named('n:Test')]), - 'ORDER BY not constructed correctly'); - } - - #[TestDox('orderBy() succeeds with case-insensitive ordering for PostgreSQL')] - public function testOrderBySucceedsWithCaseInsensitiveOrderingForPostgreSQL(): void - { - Configuration::overrideMode(Mode::PgSQL); - $this->assertEquals(" ORDER BY LOWER(data#>>'{Test,Field}') DESC NULLS FIRST", - Query::orderBy([Field::named('i:Test.Field DESC NULLS FIRST')]), 'ORDER BY not constructed correctly'); - } - - #[TestDox('orderBy() succeeds with case-insensitive ordering for SQLite')] - public function testOrderBySucceedsWithCaseInsensitiveOrderingForSQLite(): void - { - $this->assertEquals(" ORDER BY data->'Test'->>'Field' COLLATE NOCASE ASC NULLS LAST", - Query::orderBy([Field::named('i:Test.Field ASC NULLS LAST')]), 'ORDER BY not constructed correctly'); - } -}