2024-06-03 23:46:39 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Test\Unit\Query;
|
|
|
|
|
2024-06-08 14:49:52 +00:00
|
|
|
use BitBadger\PDODocument\{Configuration, Field, Mode};
|
2024-06-03 23:46:39 +00:00
|
|
|
use BitBadger\PDODocument\Query\Exists;
|
2024-06-04 01:09:03 +00:00
|
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
2024-06-03 23:46:39 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for the Exists class
|
|
|
|
*/
|
|
|
|
class ExistsTest extends TestCase
|
|
|
|
{
|
2024-06-08 14:49:52 +00:00
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
Configuration::$mode = Mode::SQLite;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown(): void
|
|
|
|
{
|
|
|
|
Configuration::$mode = null;
|
|
|
|
}
|
|
|
|
|
2024-06-03 23:46:39 +00:00
|
|
|
public function testQuerySucceeds(): void
|
|
|
|
{
|
|
|
|
$this->assertEquals('SELECT EXISTS (SELECT 1 FROM abc WHERE def)', Exists::query('abc', 'def'),
|
|
|
|
'Existence query not generated correctly');
|
|
|
|
}
|
|
|
|
|
2024-06-04 01:09:03 +00:00
|
|
|
#[TestDox('By ID succeeds')]
|
2024-06-03 23:46:39 +00:00
|
|
|
public function testByIdSucceeds(): void
|
|
|
|
{
|
2024-06-05 00:59:24 +00:00
|
|
|
$this->assertEquals("SELECT EXISTS (SELECT 1 FROM dox WHERE data->>'id' = :id)", Exists::byId('dox'),
|
2024-06-03 23:46:39 +00:00
|
|
|
'Existence query not generated correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testByFieldsSucceeds(): void
|
|
|
|
{
|
2024-06-05 00:59:24 +00:00
|
|
|
$this->assertEquals("SELECT EXISTS (SELECT 1 FROM box WHERE data->>'status' <> :status)",
|
|
|
|
Exists::byFields('box', [Field::NE('status', 'occupied', ':status')]),
|
2024-06-03 23:46:39 +00:00
|
|
|
'Existence query not generated correctly');
|
|
|
|
}
|
|
|
|
}
|