Add SQLite Patch integration tests

- Use multiple-class use statements
This commit is contained in:
2024-06-07 23:06:31 -04:00
parent 1ab961e35a
commit 9fba3781d6
21 changed files with 143 additions and 67 deletions

View File

@@ -2,13 +2,8 @@
namespace Test\Integration\SQLite;
use BitBadger\PDODocument\Count;
use BitBadger\PDODocument\Custom;
use BitBadger\PDODocument\DocumentException;
use BitBadger\PDODocument\Mapper\CountMapper;
use BitBadger\PDODocument\Mapper\DocumentMapper;
use BitBadger\PDODocument\Mapper\StringMapper;
use BitBadger\PDODocument\Query;
use BitBadger\PDODocument\{Count, Custom, DocumentException, Query};
use BitBadger\PDODocument\Mapper\{CountMapper, DocumentMapper};
use PHPUnit\Framework\TestCase;
use Test\Integration\TestDocument;
@@ -105,12 +100,8 @@ class CustomTest extends TestCase
public function testNonQuerySucceedsWhenOperatingOnData()
{
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
try {
$remaining = Count::all(ThrowawayDb::TABLE);
$this->assertEquals(0, $remaining, 'There should be no documents remaining in the table');
} finally {
$this->dbName = ThrowawayDb::exchange($this->dbName);
}
$remaining = Count::all(ThrowawayDb::TABLE);
$this->assertEquals(0, $remaining, 'There should be no documents remaining in the table');
}
public function testNonQuerySucceedsWhenNoDataMatchesWhereClause()

View File

@@ -0,0 +1,58 @@
<?php
namespace Test\Integration\SQLite;
use BitBadger\PDODocument\{Custom, Definition, DocumentException};
use BitBadger\PDODocument\Mapper\ExistsMapper;
use PHPUnit\Framework\TestCase;
/**
* SQLite integration tests for the Definition class
*/
class DefinitionTest extends TestCase
{
/** @var string Database name for throwaway database */
private string $dbName;
protected function setUp(): void
{
parent::setUp();
$this->dbName = ThrowawayDb::create(withData: false);
}
protected function tearDown(): void
{
ThrowawayDb::destroy($this->dbName);
parent::tearDown();
}
/**
* Does the given named object exist in the database?
*
* @param string $name The name of the object whose existence should be verified
* @return bool True if the object exists, false if not
* @throws DocumentException If any is encountered
*/
private function itExists(string $name): bool
{
return Custom::scalar('SELECT EXISTS (SELECT 1 FROM sqlite_master WHERE name = :name)',
[':name' => $name], new ExistsMapper());
}
public function testEnsureTableSucceeds()
{
$this->assertFalse($this->itExists('ensured'), 'The table should not exist already');
$this->assertFalse($this->itExists('idx_ensured_key'), 'The key index should not exist already');
Definition::ensureTable('ensured');
$this->assertTrue($this->itExists('ensured'), 'The table should now exist');
$this->assertTrue($this->itExists('idx_ensured_key'), 'The key index should now exist');
}
public function testEnsureFieldIndexSucceeds()
{
$this->assertFalse($this->itExists('idx_ensured_test'), 'The index should not exist already');
Definition::ensureTable('ensured');
Definition::ensureFieldIndex('ensured', 'test', ['name', 'age']);
$this->assertTrue($this->itExists('idx_ensured_test'), 'The index should now exist');
}
}

View File

@@ -0,0 +1,59 @@
<?php declare(strict_types=1);
namespace Test\Integration\SQLite;
use BitBadger\PDODocument\{Count, Field, Find, Patch};
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
use Test\Integration\TestDocument;
/**
* SQLite integration tests for the Patch class
*/
class PatchTest extends TestCase
{
/** @var string Database name for throwaway database */
private string $dbName;
protected function setUp(): void
{
parent::setUp();
$this->dbName = ThrowawayDb::create();
}
protected function tearDown(): void
{
ThrowawayDb::destroy($this->dbName);
parent::tearDown();
}
#[TestDox('By ID succeeds when a document is updated')]
public function testByIdSucceedsWhenADocumentIsUpdated(): void
{
Patch::byId(ThrowawayDb::TABLE, 'one', ['num_value' => 44]);
$doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
$this->assertNotFalse($doc, 'There should have been a document returned');
$this->assertEquals(44, $doc->num_value, 'The updated document is not correct');
}
#[TestDox('By ID succeeds when no document is updated')]
public function testByIdSucceedsWhenNoDocumentIsUpdated(): void
{
Patch::byId(ThrowawayDb::TABLE, 'forty-seven', ['foo' => 'green']);
$this->assertTrue(true, 'The above not throwing an exception is the test');
}
public function testByFieldsSucceedsWhenADocumentIsUpdated(): void
{
Patch::byFields(ThrowawayDb::TABLE, [Field::EQ('value', 'purple')], ['num_value' => 77]);
$after = Count::byFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 77)]);
$this->assertEquals(2, $after, 'There should have been 2 documents updated');
}
public function testByFieldsSucceedsWhenNoDocumentIsUpdated(): void
{
Patch::byFields(ThrowawayDb::TABLE, [Field::EQ('value', 'burgundy')], ['foo' => 'green']);
$this->assertTrue(true, 'The above not throwing an exception is the test');
}
}