1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-23 17:12:41 +00:00
Arsse/tests/cases/Database/AbstractTest.php

110 lines
3.6 KiB
PHP
Raw Normal View History

<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse\TestCase\Database;
use JKingWeb\Arsse\Test\Database;
use JKingWeb\Arsse\Arsse;
use JKingWeb\Arsse\User;
2019-09-11 19:25:26 +00:00
abstract class AbstractTest extends \JKingWeb\Arsse\Test\AbstractTest {
2018-11-25 05:03:56 +00:00
use SeriesMiscellany;
use SeriesMeta;
use SeriesUser;
use SeriesSession;
use SeriesToken;
2018-11-25 05:03:56 +00:00
use SeriesFolder;
use SeriesFeed;
2020-11-05 21:51:46 +00:00
use SeriesIcon;
2018-11-25 05:03:56 +00:00
use SeriesSubscription;
use SeriesLabel;
use SeriesTag;
use SeriesArticle;
2018-11-25 05:03:56 +00:00
use SeriesCleanup;
/** @var \JKingWeb\Arsse\Db\Driver */
protected static $drv;
protected static $failureReason = "";
protected $primed = false;
2018-12-05 22:28:11 +00:00
abstract protected function nextID(string $table): int;
protected function findTraitOfTest(string $test): string {
$class = new \ReflectionClass(self::class);
foreach ($class->getTraits() as $trait) {
if ($trait->hasMethod($test)) {
return $trait->getShortName();
}
}
return $class->getShortName();
}
2019-10-16 18:42:43 +00:00
public static function setUpBeforeClass(): void {
// establish a clean baseline
static::clearData();
// perform an initial connection to the database to reset its version to zero
2018-12-05 22:28:11 +00:00
// in the case of SQLite this will always be the case (we use a memory database),
// but other engines should clean up from potentially interrupted prior tests
static::setConf();
try {
static::$drv = new static::$dbDriverClass;
} catch (\JKingWeb\Arsse\Db\Exception $e) {
static::$failureReason = $e->getMessage();
return;
}
// wipe the database absolutely clean
static::dbRaze(static::$drv);
// create the database interface with the suitable driver and apply the latest schema
Arsse::$db = new Database(static::$drv);
Arsse::$db->driverSchemaUpdate();
}
2019-10-16 18:42:43 +00:00
public function setUp(): void {
// get the name of the test's test series
$this->series = $this->findTraitofTest($this->getName(false));
static::clearData();
2018-11-25 05:03:56 +00:00
static::setConf();
if (strlen(static::$failureReason)) {
$this->markTestSkipped(static::$failureReason);
}
Arsse::$db = new Database(static::$drv);
Arsse::$db->driverSchemaUpdate();
// create a mock user manager
2019-09-05 14:03:32 +00:00
Arsse::$user = \Phake::mock(User::class);
// call the series-specific setup method
$setUp = "setUp".$this->series;
$this->$setUp();
// prime the database with series data if it hasn't already been done
if (!$this->primed && isset($this->data)) {
2019-06-21 22:52:27 +00:00
$this->primeDatabase(static::$drv, $this->data);
}
}
2019-10-16 18:42:43 +00:00
public function tearDown(): void {
// call the series-specific teardown method
$this->series = $this->findTraitofTest($this->getName(false));
$tearDown = "tearDown".$this->series;
$this->$tearDown();
// clean up
$this->primed = false;
// call the database-specific table cleanup function
static::dbTruncate(static::$drv);
// clear state
static::clearData();
}
2019-10-16 18:42:43 +00:00
public static function tearDownAfterClass(): void {
if (static::$drv) {
// wipe the database absolutely clean
static::dbRaze(static::$drv);
// clean up
static::$drv = null;
}
static::$failureReason = "";
static::clearData();
}
}