pdo-document/tests/integration/sqlite/DefinitionTest.php

59 lines
2.0 KiB
PHP
Raw Normal View History

<?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');
}
}