2024-06-08 23:58:45 +00:00
|
|
|
<?php
|
2024-07-21 01:47:21 +00:00
|
|
|
/**
|
|
|
|
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
|
|
|
* @license MIT
|
|
|
|
* @see https://github.com/Zaid-Ajaj/ThrowawayDb The origin concept
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2024-06-08 23:58:45 +00:00
|
|
|
|
|
|
|
namespace Test\Integration\SQLite;
|
|
|
|
|
2024-07-21 01:47:21 +00:00
|
|
|
use BitBadger\PDODocument\{AutoId, Configuration, Definition, Document, DocumentException};
|
2024-06-21 13:46:41 +00:00
|
|
|
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-07-21 01:47:21 +00:00
|
|
|
public const TABLE = 'test_table';
|
2024-06-08 23:58:45 +00:00
|
|
|
|
2024-11-16 00:23:28 +00:00
|
|
|
/**
|
|
|
|
* Load data into the test table
|
|
|
|
*
|
|
|
|
* @throws DocumentException If any is encountered
|
|
|
|
*/
|
|
|
|
public static function loadData(): void
|
|
|
|
{
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
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-21 13:46:41 +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-21 13:46:41 +00:00
|
|
|
$fileName = sprintf('throwaway-%s.db', AutoId::generateRandom(10));
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::useDSN("sqlite:./$fileName");
|
2024-06-08 23:58:45 +00:00
|
|
|
Configuration::resetPDO();
|
|
|
|
|
2024-11-16 00:23:28 +00:00
|
|
|
Definition::ensureTable(self::TABLE);
|
|
|
|
|
2024-06-08 23:58:45 +00:00
|
|
|
if ($withData) {
|
2024-11-16 00:23:28 +00:00
|
|
|
self::loadData();
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2024-06-21 13:46:41 +00:00
|
|
|
if (file_exists("./$fileName")) unlink("./$fileName");
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
}
|