pdo-document/tests/unit/QueryTest.php

81 lines
2.8 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
namespace Test\Unit;
use BitBadger\PDODocument\{Configuration, Field, Mode, Query};
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
/**
* Unit tests for the Query class
*/
class QueryTest extends TestCase
{
protected function setUp(): void
{
Configuration::$mode = Mode::SQLite;
}
protected function tearDown(): void
{
Configuration::$mode = null;
}
public function testSelectFromTableSucceeds(): void
{
$this->assertEquals('SELECT data FROM testing', Query::selectFromTable('testing'),
'Query not constructed correctly');
}
public function testWhereByFieldsSucceedsForSingleField(): void
{
2024-06-05 00:59:24 +00:00
$this->assertEquals("data->>'test_field' <= :it",
Query::whereByFields([Field::LE('test_field', '', ':it')]), 'WHERE fragment not constructed correctly');
}
public function testWhereByFieldsSucceedsForMultipleFields(): void
{
2024-06-05 00:59:24 +00:00
$this->assertEquals("data->>'test_field' <= :it AND data->>'other_field' = :other",
Query::whereByFields([Field::LE('test_field', '', ':it'), Field::EQ('other_field', '', ':other')]),
'WHERE fragment not constructed correctly');
}
public function testWhereByFieldsSucceedsForMultipleFieldsWithOr(): void
{
2024-06-05 00:59:24 +00:00
$this->assertEquals("data->>'test_field' <= :it OR data->>'other_field' = :other",
Query::whereByFields([Field::LE('test_field', '', ':it'), Field::EQ('other_field', '', ':other')], 'OR'),
'WHERE fragment not constructed correctly');
}
#[TestDox('Where by ID succeeds with default parameter')]
public function testWhereByIdSucceedsWithDefaultParameter(): void
{
2024-06-05 00:59:24 +00:00
$this->assertEquals("data->>'id' = :id", Query::whereById(), 'WHERE fragment not constructed correctly');
}
#[TestDox('Where by ID succeeds with specific parameter')]
public function testWhereByIdSucceedsWithSpecificParameter(): void
{
2024-06-05 00:59:24 +00:00
$this->assertEquals("data->>'id' = :di", Query::whereById(':di'), 'WHERE fragment not constructed correctly');
}
public function testInsertSucceeds(): void
{
2024-06-05 00:59:24 +00:00
$this->assertEquals('INSERT INTO my_table VALUES (:data)', Query::insert('my_table'),
'INSERT statement not constructed correctly');
}
public function testSaveSucceeds(): void
{
$this->assertEquals(
2024-06-05 00:59:24 +00:00
"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');
}
public function testUpdateSucceeds()
{
2024-06-05 00:59:24 +00:00
$this->assertEquals("UPDATE testing SET data = :data WHERE data->>'id' = :id", Query::update('testing'),
'UPDATE statement not constructed correctly');
}
}