pdo-document/tests/unit/Query/DeleteTest.php
Daniel J. Summers d067f8983f Changes for beta10 (#5)
- Add In/InArray support
- Add ORDER BY support for `Find` functions
- Update dependencies
- Implement fixes identified via static analysis

Reviewed-on: #5
2024-09-27 02:15:00 +00:00

75 lines
2.4 KiB
PHP

<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @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('');
}
}