2024-07-21 01:47:21 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2024-06-08 23:58:45 +00:00
|
|
|
|
|
|
|
namespace Test\Unit\Query;
|
|
|
|
|
2024-06-21 13:46:41 +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-21 13:46:41 +00:00
|
|
|
#[TestDox('Delete Queries (Unit tests)')]
|
2024-06-08 23:58:45 +00:00
|
|
|
class DeleteTest extends TestCase
|
|
|
|
{
|
|
|
|
protected function tearDown(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(null);
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 00:18:17 +00:00
|
|
|
#[TestDox('byId() succeeds')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testByIdSucceeds(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(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');
|
|
|
|
}
|
|
|
|
|
2024-09-23 00:18:17 +00:00
|
|
|
#[TestDox('byFields() succeeds')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testByFieldsSucceeds(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::SQLite);
|
2024-06-08 23:58:45 +00:00
|
|
|
$this->assertEquals("DELETE FROM my_table WHERE data->>'value' < :max AND data->>'value' >= :min",
|
2024-09-21 00:29:47 +00:00
|
|
|
Delete::byFields('my_table',
|
|
|
|
[Field::less('value', 99, ':max'), Field::greaterOrEqual('value', 18, ':min')]),
|
2024-06-08 23:58:45 +00:00
|
|
|
'DELETE statement not constructed correctly');
|
|
|
|
}
|
2024-06-21 13:46:41 +00:00
|
|
|
|
2024-09-23 00:18:17 +00:00
|
|
|
#[TestDox('byContains() succeeds for PostgreSQL')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByContainsSucceedsForPostgreSQL(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertEquals('DELETE FROM somewhere WHERE data @> :criteria', Delete::byContains('somewhere'),
|
|
|
|
'DELETE statement not constructed correctly');
|
|
|
|
}
|
|
|
|
|
2024-09-23 00:18:17 +00:00
|
|
|
#[TestDox('byContains() fails for non PostgreSQL')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByContainsFailsForNonPostgreSQL(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Delete::byContains('');
|
|
|
|
}
|
|
|
|
|
2024-09-23 00:18:17 +00:00
|
|
|
#[TestDox('byJsonPath() succeeds for PostgreSQL')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByJsonPathSucceedsForPostgreSQL(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertEquals('DELETE FROM here WHERE jsonb_path_exists(data, :path::jsonpath)',
|
|
|
|
Delete::byJsonPath('here'), 'DELETE statement not constructed correctly');
|
|
|
|
}
|
|
|
|
|
2024-09-23 00:18:17 +00:00
|
|
|
#[TestDox('byJsonPath() fails for non PostgreSQL')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByJsonPathFailsForNonPostgreSQL(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Delete::byJsonPath('');
|
|
|
|
}
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|