Initial SQLite development (#1)
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
71
tests/integration/sqlite/ThrowawayDb.php
Normal file
71
tests/integration/sqlite/ThrowawayDb.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace Test\Integration\SQLite;
|
||||
|
||||
use BitBadger\PDODocument\Configuration;
|
||||
use BitBadger\PDODocument\Definition;
|
||||
use BitBadger\PDODocument\Document;
|
||||
use BitBadger\PDODocument\DocumentException;
|
||||
use BitBadger\PDODocument\Mode;
|
||||
use Test\Integration\SubDocument;
|
||||
use Test\Integration\TestDocument;
|
||||
|
||||
/**
|
||||
* Utilities to create and destroy a throwaway SQLite database to use for testing
|
||||
*/
|
||||
class ThrowawayDb
|
||||
{
|
||||
/** @var string The table used for document manipulation */
|
||||
public const string TABLE = "test_table";
|
||||
|
||||
/**
|
||||
* Create a throwaway SQLite database
|
||||
*
|
||||
* @param bool $withData Whether to initialize this database with data (optional; defaults to `true`)
|
||||
* @return string The name of the database (use to pass to `destroy` function at end of test)
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function create(bool $withData = true): string
|
||||
{
|
||||
$fileName = sprintf('throwaway-%s-%d.db', date('His'), rand(10, 99));
|
||||
Configuration::$pdoDSN = "sqlite:./$fileName";
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
Configuration::resetPDO();
|
||||
|
||||
if ($withData) {
|
||||
Definition::ensureTable(self::TABLE);
|
||||
Document::insert(self::TABLE, new TestDocument('one', 'FIRST!', 0));
|
||||
Document::insert(self::TABLE, new TestDocument('two', 'another', 10, new SubDocument('green', 'blue')));
|
||||
Document::insert(self::TABLE, new TestDocument('three', '', 4));
|
||||
Document::insert(self::TABLE, new TestDocument('four', 'purple', 17, new SubDocument('green', 'red')));
|
||||
Document::insert(self::TABLE, new TestDocument('five', 'purple', 18));
|
||||
}
|
||||
|
||||
return $fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy a throwaway SQLite database
|
||||
*
|
||||
* @param string $fileName The name of the SQLite database to be deleted
|
||||
*/
|
||||
public static function destroy(string $fileName): void
|
||||
{
|
||||
Configuration::resetPDO();
|
||||
unlink("./$fileName");
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy the given throwaway database and create another
|
||||
*
|
||||
* @param string $fileName The name of the database to be destroyed
|
||||
* @param bool $withData Whether to initialize the database with data (optional; defaults to `true`)
|
||||
* @return string The name of the new database
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function exchange(string $fileName, bool $withData = true): string
|
||||
{
|
||||
self::destroy($fileName);
|
||||
return self::create($withData);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user