Add classes and tests from common project
This commit is contained in:
26
tests/unit/Query/CountTest.php
Normal file
26
tests/unit/Query/CountTest.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Test\Unit\Query;
|
||||
|
||||
use BitBadger\PDODocument\Field;
|
||||
use BitBadger\PDODocument\Query\Count;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Unit tests for the Count class
|
||||
*/
|
||||
class CountTest extends TestCase
|
||||
{
|
||||
|
||||
public function testAllSucceeds()
|
||||
{
|
||||
$this->assertEquals('SELECT COUNT(*) FROM a_table', Count::all('a_table'),
|
||||
'SELECT statement not generated correctly');
|
||||
}
|
||||
|
||||
public function testByFieldsSucceeds()
|
||||
{
|
||||
$this->assertEquals("SELECT COUNT(*) FROM somewhere WHERE data->>'errors' > @errors",
|
||||
Count::byFields('somewhere', [Field::GT('errors', 10, '@errors')]));
|
||||
}
|
||||
}
|
||||
38
tests/unit/Query/DefinitionTest.php
Normal file
38
tests/unit/Query/DefinitionTest.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Test\Unit\Query;
|
||||
|
||||
use BitBadger\PDODocument\Query\Definition;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Unit tests for the Definition class
|
||||
*/
|
||||
class DefinitionTest extends TestCase
|
||||
{
|
||||
public function testEnsureTableForSucceeds(): void
|
||||
{
|
||||
$this->assertEquals('CREATE TABLE IF NOT EXISTS documents (data JSON NOT NULL)',
|
||||
Definition::ensureTableFor('documents', 'JSON'), 'CREATE TABLE statement not generated correctly');
|
||||
}
|
||||
|
||||
public function testEnsureIndexOnSucceedsWithoutSchemaSingleAscendingField(): void
|
||||
{
|
||||
$this->assertEquals("CREATE INDEX IF NOT EXISTS idx_test_fields ON test ((data->>'details'))",
|
||||
Definition::ensureIndexOn('test', 'fields', ['details']), 'CREATE INDEX statement not generated correctly');
|
||||
}
|
||||
|
||||
public function testEnsureIndexOnSucceedsWithSchemaMultipleFields(): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
"CREATE INDEX IF NOT EXISTS idx_testing_json ON sch.testing ((data->>'group'), (data->>'sub_group') DESC)",
|
||||
Definition::ensureIndexOn('sch.testing', 'json', ['group', 'sub_group DESC']),
|
||||
'CREATE INDEX statement not generated correctly');
|
||||
}
|
||||
|
||||
public function testEnsureKey(): void
|
||||
{
|
||||
$this->assertEquals("CREATE UNIQUE INDEX IF NOT EXISTS idx_tbl_key ON tbl ((data->>'id'))",
|
||||
Definition::ensureKey('tbl'), 'CREATE INDEX statement for document key not generated correctly');
|
||||
}
|
||||
}
|
||||
26
tests/unit/Query/DeleteTest.php
Normal file
26
tests/unit/Query/DeleteTest.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Test\Unit\Query;
|
||||
|
||||
use BitBadger\PDODocument\Field;
|
||||
use BitBadger\PDODocument\Query\Delete;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Unit tests for the Delete class
|
||||
*/
|
||||
class DeleteTest extends TestCase
|
||||
{
|
||||
public function testByIdSucceeds(): void
|
||||
{
|
||||
$this->assertEquals("DELETE FROM over_there WHERE data->>'id' = @id", Delete::byId('over_there'),
|
||||
'DELETE statement not constructed correctly');
|
||||
}
|
||||
|
||||
public function testByFieldsSucceeds(): void
|
||||
{
|
||||
$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');
|
||||
}
|
||||
}
|
||||
32
tests/unit/Query/ExistsTest.php
Normal file
32
tests/unit/Query/ExistsTest.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Test\Unit\Query;
|
||||
|
||||
use BitBadger\PDODocument\Field;
|
||||
use BitBadger\PDODocument\Query\Exists;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Unit tests for the Exists class
|
||||
*/
|
||||
class ExistsTest extends TestCase
|
||||
{
|
||||
public function testQuerySucceeds(): void
|
||||
{
|
||||
$this->assertEquals('SELECT EXISTS (SELECT 1 FROM abc WHERE def)', Exists::query('abc', 'def'),
|
||||
'Existence query not generated correctly');
|
||||
}
|
||||
|
||||
public function testByIdSucceeds(): void
|
||||
{
|
||||
$this->assertEquals("SELECT EXISTS (SELECT 1 FROM dox WHERE data->>'id' = @id)", Exists::byId('dox'),
|
||||
'Existence query not generated correctly');
|
||||
}
|
||||
|
||||
public function testByFieldsSucceeds(): void
|
||||
{
|
||||
$this->assertEquals("SELECT EXISTS (SELECT 1 FROM box WHERE data->>'status' <> @status)",
|
||||
Exists::byFields('box', [Field::NE('status', 'occupied', '@status')]),
|
||||
'Existence query not generated correctly');
|
||||
}
|
||||
}
|
||||
26
tests/unit/Query/FindTest.php
Normal file
26
tests/unit/Query/FindTest.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Test\Unit\Query;
|
||||
|
||||
use BitBadger\PDODocument\Field;
|
||||
use BitBadger\PDODocument\Query\Find;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Unit tests for the Find class
|
||||
*/
|
||||
class FindTest extends TestCase
|
||||
{
|
||||
public function testByIdSucceeds(): void
|
||||
{
|
||||
$this->assertEquals("SELECT data FROM here WHERE data->>'id' = @id", Find::byId('here'),
|
||||
'SELECT query not generated correctly');
|
||||
}
|
||||
|
||||
public function testByFieldsSucceeds(): void
|
||||
{
|
||||
$this->assertEquals("SELECT data FROM there WHERE data->>'active' = @act OR data->>'locked' = @lock",
|
||||
Find::byFields('there', [Field::EQ('active', true, '@act'), Field::EQ('locked', true, '@lock')], 'OR'),
|
||||
'SELECT query not generated correctly');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user