2024-06-08 23:58:45 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Test\Integration\SQLite;
|
|
|
|
|
2024-06-12 19:21:53 +00:00
|
|
|
use BitBadger\PDODocument\{AutoId, Configuration, Definition, Document, DocumentException, Mode};
|
|
|
|
use Random\RandomException;
|
|
|
|
use Test\Integration\{SubDocument, TestDocument};
|
2024-06-08 23:58:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Utilities to create and destroy a throwaway SQLite database to use for testing
|
|
|
|
*/
|
|
|
|
class ThrowawayDb
|
|
|
|
{
|
|
|
|
/** @var string The table used for document manipulation */
|
2024-06-12 19:21:53 +00:00
|
|
|
public const TABLE = "test_table";
|
2024-06-08 23:58:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
2024-06-12 19:21:53 +00:00
|
|
|
* @throws DocumentException|RandomException If any is encountered
|
2024-06-08 23:58:45 +00:00
|
|
|
*/
|
|
|
|
public static function create(bool $withData = true): string
|
|
|
|
{
|
2024-06-12 19:21:53 +00:00
|
|
|
$fileName = sprintf('throwaway-%s.db', AutoId::generateRandom(10));
|
2024-06-08 23:58:45 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|