2024-06-08 23:58:45 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Test\Unit\Query;
|
|
|
|
|
2024-06-12 21:43:17 +00:00
|
|
|
use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode};
|
2024-06-08 23:58:45 +00:00
|
|
|
use BitBadger\PDODocument\Query\Delete;
|
|
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for the Delete class
|
|
|
|
*/
|
2024-06-12 21:43:17 +00:00
|
|
|
#[TestDox('Delete Queries (Unit tests)')]
|
2024-06-08 23:58:45 +00:00
|
|
|
class DeleteTest extends TestCase
|
|
|
|
{
|
|
|
|
protected function tearDown(): void
|
|
|
|
{
|
|
|
|
Configuration::$mode = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('By ID succeeds')]
|
|
|
|
public function testByIdSucceeds(): void
|
|
|
|
{
|
2024-06-12 21:43:17 +00:00
|
|
|
Configuration::$mode = Mode::SQLite;
|
2024-06-08 23:58:45 +00:00
|
|
|
$this->assertEquals("DELETE FROM over_there WHERE data->>'id' = :id", Delete::byId('over_there'),
|
|
|
|
'DELETE statement not constructed correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testByFieldsSucceeds(): void
|
|
|
|
{
|
2024-06-12 21:43:17 +00:00
|
|
|
Configuration::$mode = Mode::SQLite;
|
2024-06-08 23:58:45 +00:00
|
|
|
$this->assertEquals("DELETE FROM my_table WHERE data->>'value' < :max AND data->>'value' >= :min",
|
|
|
|
Delete::byFields('my_table', [Field::LT('value', 99, ':max'), Field::GE('value', 18, ':min')]),
|
|
|
|
'DELETE statement not constructed correctly');
|
|
|
|
}
|
2024-06-12 21:43:17 +00:00
|
|
|
|
|
|
|
#[TestDox('By contains succeeds for PostgreSQL')]
|
|
|
|
public function testByContainsSucceedsForPostgreSQL(): void
|
|
|
|
{
|
|
|
|
Configuration::$mode = Mode::PgSQL;
|
|
|
|
$this->assertEquals('DELETE FROM somewhere WHERE data @> :criteria', Delete::byContains('somewhere'),
|
|
|
|
'DELETE statement not constructed correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('By contains fails for non PostgreSQL')]
|
|
|
|
public function testByContainsFailsForNonPostgreSQL(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Delete::byContains('');
|
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('By JSON Path succeeds for PostgreSQL')]
|
|
|
|
public function testByJsonPathSucceedsForPostgreSQL(): void
|
|
|
|
{
|
|
|
|
Configuration::$mode = Mode::PgSQL;
|
2024-06-14 01:39:16 +00:00
|
|
|
$this->assertEquals('DELETE FROM here WHERE jsonb_path_exists(data, :path::jsonpath)',
|
|
|
|
Delete::byJsonPath('here'), 'DELETE statement not constructed correctly');
|
2024-06-12 21:43:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[TestDox('By JSON Path fails for non PostgreSQL')]
|
|
|
|
public function testByJsonPathFailsForNonPostgreSQL(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Delete::byJsonPath('');
|
|
|
|
}
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|