Skip to content

Commit

Permalink
Merge testcase fixes from main
Browse files Browse the repository at this point in the history
  • Loading branch information
fisharebest committed Oct 3, 2023
1 parent 5a8d692 commit 2f41c55
Show file tree
Hide file tree
Showing 1,274 changed files with 1,253 additions and 4,189 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"autoload-dev": {
"classmap": [
"tests/TestCase.php",
"tests/MockGlobalFunctions.php"
"tests/MockGlobalFunctions.php",
"tests/app/Elements/AbstractElementTestCase.php"
]
},
"config": {
Expand Down
7 changes: 0 additions & 7 deletions tests/MockGlobalFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,5 @@
*/
abstract class MockGlobalFunctions
{
/**
* Mock version of ini_get()
*
* @param string $varname
*
* @return string
*/
abstract public function iniGet(string $varname): string;
}
161 changes: 62 additions & 99 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Fisharebest\Webtrees\Services\TreeService;
use Illuminate\Database\Capsule\Manager as DB;
use Nyholm\Psr7\Factory\Psr17Factory;
use PHPUnit\Framework\Constraint\Callback;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
Expand All @@ -43,7 +44,7 @@
use Psr\Http\Message\UploadedFileInterface;
use Psr\Http\Message\UriFactoryInterface;

use function app;
use function array_shift;
use function basename;
use function filesize;
use function http_build_query;
Expand All @@ -66,63 +67,10 @@ class TestCase extends \PHPUnit\Framework\TestCase

protected static bool $uses_database = false;

/**
* Things to run once, before all the tests.
*/
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();

$webtrees = new Webtrees();
$webtrees->bootstrap();

// PSR7 messages and PSR17 message-factories
Webtrees::set(ResponseFactoryInterface::class, Psr17Factory::class);
Webtrees::set(ServerRequestFactoryInterface::class, Psr17Factory::class);
Webtrees::set(StreamFactoryInterface::class, Psr17Factory::class);
Webtrees::set(UploadedFileFactoryInterface::class, Psr17Factory::class);
Webtrees::set(UriFactoryInterface::class, Psr17Factory::class);

// This is normally set in middleware.
Webtrees::set(ModuleThemeInterface::class, WebtreesTheme::class);

// Need the routing table, to generate URLs.
$router_container = new RouterContainer('/');
(new WebRoutes())->load($router_container->getMap());
Webtrees::set(RouterContainer::class, $router_container);

if (static::$uses_database) {
static::createTestDatabase();

I18N::init('en-US');

// This is normally set in middleware.
(new Gedcom())->registerTags(Registry::elementFactory(), true);

// Boot modules
(new ModuleService())->bootModules(new WebtreesTheme());
} else {
I18N::init('en-US', true);
}
}

/**
* Things to run once, AFTER all the tests.
*/
public static function tearDownAfterClass(): void
{
if (static::$uses_database) {
$pdo = DB::connection()->getPdo();
unset($pdo);
}

parent::tearDownAfterClass();
}

/**
* Create an SQLite in-memory database for testing
*/
protected static function createTestDatabase(): void
private static function createTestDatabase(): void
{
$capsule = new DB();
$capsule->addConnection([
Expand All @@ -131,9 +79,6 @@ protected static function createTestDatabase(): void
]);
$capsule->setAsGlobal();

// Migrations create logs, which requires an IP address, which requires a request
self::createRequest();

// Create tables
$migration_service = new MigrationService();
$migration_service->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION);
Expand All @@ -149,7 +94,7 @@ protected static function createTestDatabase(): void
* @param array<string> $query
* @param array<string> $params
* @param array<UploadedFileInterface> $files
* @param array<string> $attributes
* @param array<string|Tree> $attributes
*
* @return ServerRequestInterface
*/
Expand All @@ -160,8 +105,7 @@ protected static function createRequest(
array $files = [],
array $attributes = []
): ServerRequestInterface {
/** @var ServerRequestFactoryInterface */
$server_request_factory = app(ServerRequestFactoryInterface::class);
$server_request_factory = Webtrees::make(ServerRequestFactoryInterface::class);

$uri = 'https://webtrees.test/index.php?' . http_build_query($query);

Expand All @@ -179,11 +123,11 @@ protected static function createRequest(
$request = $request->withAttribute($key, $value);

if ($key === 'tree') {
app()->instance(Tree::class, $value);
Webtrees::set(Tree::class, $value);
}
}

app()->instance(ServerRequestInterface::class, $request);
Webtrees::set(ServerRequestInterface::class, $request);

return $request;
}
Expand All @@ -195,9 +139,41 @@ protected function setUp(): void
{
parent::setUp();

$webtrees = new Webtrees();
$webtrees->bootstrap();

// PSR7 messages and PSR17 message-factories
Webtrees::set(ResponseFactoryInterface::class, Psr17Factory::class);
Webtrees::set(ServerRequestFactoryInterface::class, Psr17Factory::class);
Webtrees::set(StreamFactoryInterface::class, Psr17Factory::class);
Webtrees::set(UploadedFileFactoryInterface::class, Psr17Factory::class);
Webtrees::set(UriFactoryInterface::class, Psr17Factory::class);

// This is normally set in middleware.
Webtrees::set(ModuleThemeInterface::class, new WebtreesTheme());

// Need the routing table, to generate URLs.
$router_container = new RouterContainer('/');
(new WebRoutes())->load($router_container->getMap());
Webtrees::set(RouterContainer::class, $router_container);
$module_service = new ModuleService();
Webtrees::set(ModuleService::class, $module_service);

if (static::$uses_database) {
DB::connection()->beginTransaction();
static::createTestDatabase();

// This is normally set in middleware.
(new Gedcom())->registerTags(Registry::elementFactory(), true);

// Boot modules
$module_service->bootModules(new WebtreesTheme());

I18N::init('en-US');
} else {
I18N::init('en-US', true);
}

self::createRequest();
}

/**
Expand All @@ -206,27 +182,19 @@ protected function setUp(): void
protected function tearDown(): void
{
if (static::$uses_database) {
DB::connection()->rollBack();
DB::connection()->disconnect();
}

Site::$preferences = [];

Auth::logout();
Session::clear(); // Session data is stored in the super-global
Site::$preferences = []; // These are cached from the database
}

/**
* Import a GEDCOM file into the test database.
*
* @param string $gedcom_file
*
* @return Tree
*/
protected function importTree(string $gedcom_file): Tree
{
$gedcom_import_service = new GedcomImportService();
$tree_service = new TreeService($gedcom_import_service);
$tree = $tree_service->create(basename($gedcom_file), basename($gedcom_file));
$stream = app(StreamFactoryInterface::class)->createStreamFromFile(__DIR__ . '/data/' . $gedcom_file);
$stream = Webtrees::make(StreamFactoryInterface::class)->createStreamFromFile(__DIR__ . '/data/' . $gedcom_file);

$tree_service->importGedcomFile($tree, $stream, $gedcom_file, '');

Expand All @@ -243,21 +211,10 @@ protected function importTree(string $gedcom_file): Tree
return $tree;
}

/**
* Create an uploaded file for a request.
*
* @param string $filename
* @param string $mime_type
*
* @return UploadedFileInterface
*/
protected function createUploadedFile(string $filename, string $mime_type): UploadedFileInterface
{
/** @var StreamFactoryInterface */
$stream_factory = app(StreamFactoryInterface::class);

/** @var UploadedFileFactoryInterface */
$uploaded_file_factory = app(UploadedFileFactoryInterface::class);
$stream_factory = Webtrees::make(StreamFactoryInterface::class);
$uploaded_file_factory = Webtrees::make(UploadedFileFactoryInterface::class);

$stream = $stream_factory->createStreamFromFile($filename);
$size = filesize($filename);
Expand All @@ -267,11 +224,6 @@ protected function createUploadedFile(string $filename, string $mime_type): Uplo
return $uploaded_file_factory->createUploadedFile($stream, $size, $status, $client_name, $mime_type);
}

/**
* Assert that a response contains valid HTML - either a full page or a fragment.
*
* @param ResponseInterface $response
*/
protected function validateHtmlResponse(ResponseInterface $response): void
{
self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
Expand All @@ -285,11 +237,6 @@ protected function validateHtmlResponse(ResponseInterface $response): void
$this->validateHtml(substr($html, strlen('<DOCTYPE html>')));
}

/**
* Assert that a response contains valid HTML - either a full page or a fragment.
*
* @param string $html
*/
protected function validateHtml(string $html): void
{
$stack = [];
Expand Down Expand Up @@ -343,4 +290,20 @@ protected function validateHtml(string $html): void

static::assertSame([], $stack);
}

/**
* Workaround for removal of withConsecutive in phpunit 10.
*
* @param array<int,mixed> $parameters
*/
protected static function withConsecutive(array $parameters): Callback
{
return self::callback(static function (mixed $parameter) use ($parameters): bool {
static $array = null;

$array ??= $parameters;

return $parameter === array_shift($array);
});
}
}
22 changes: 0 additions & 22 deletions tests/app/AgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ class AgeTest extends TestCase
* @covers \Fisharebest\Webtrees\Age::ageYears
* @covers \Fisharebest\Webtrees\Age::ageYearsString
* @covers \Fisharebest\Webtrees\Age::__toString
*
* @return void
*/
public function testSameDayMonthYear(): void
{
Expand All @@ -53,8 +51,6 @@ public function testSameDayMonthYear(): void
* @covers \Fisharebest\Webtrees\Age::ageYears
* @covers \Fisharebest\Webtrees\Age::ageYearsString
* @covers \Fisharebest\Webtrees\Age::__toString
*
* @return void
*/
public function testSameMonthYear(): void
{
Expand All @@ -74,8 +70,6 @@ public function testSameMonthYear(): void
* @covers \Fisharebest\Webtrees\Age::ageYears
* @covers \Fisharebest\Webtrees\Age::ageYearsString
* @covers \Fisharebest\Webtrees\Age::__toString
*
* @return void
*/
public function testSameYear(): void
{
Expand All @@ -95,8 +89,6 @@ public function testSameYear(): void
* @covers \Fisharebest\Webtrees\Age::ageYears
* @covers \Fisharebest\Webtrees\Age::ageYearsString
* @covers \Fisharebest\Webtrees\Age::__toString
*
* @return void
*/
public function testReversed(): void
{
Expand All @@ -116,8 +108,6 @@ public function testReversed(): void
* @covers \Fisharebest\Webtrees\Age::ageYears
* @covers \Fisharebest\Webtrees\Age::ageYearsString
* @covers \Fisharebest\Webtrees\Age::__toString
*
* @return void
*/
public function testStartDateInvalid(): void
{
Expand All @@ -137,8 +127,6 @@ public function testStartDateInvalid(): void
* @covers \Fisharebest\Webtrees\Age::ageYears
* @covers \Fisharebest\Webtrees\Age::ageYearsString
* @covers \Fisharebest\Webtrees\Age::__toString
*
* @return void
*/
public function testEndDateInvalid(): void
{
Expand All @@ -158,8 +146,6 @@ public function testEndDateInvalid(): void
* @covers \Fisharebest\Webtrees\Age::ageYears
* @covers \Fisharebest\Webtrees\Age::ageYearsString
* @covers \Fisharebest\Webtrees\Age::__toString
*
* @return void
*/
public function testOverlappingDates1(): void
{
Expand All @@ -179,8 +165,6 @@ public function testOverlappingDates1(): void
* @covers \Fisharebest\Webtrees\Age::ageYears
* @covers \Fisharebest\Webtrees\Age::ageYearsString
* @covers \Fisharebest\Webtrees\Age::__toString
*
* @return void
*/
public function testOverlappingDates2(): void
{
Expand All @@ -200,8 +184,6 @@ public function testOverlappingDates2(): void
* @covers \Fisharebest\Webtrees\Age::ageYears
* @covers \Fisharebest\Webtrees\Age::ageYearsString
* @covers \Fisharebest\Webtrees\Age::__toString
*
* @return void
*/
public function testDifferentDay(): void
{
Expand All @@ -221,8 +203,6 @@ public function testDifferentDay(): void
* @covers \Fisharebest\Webtrees\Age::ageYears
* @covers \Fisharebest\Webtrees\Age::ageYearsString
* @covers \Fisharebest\Webtrees\Age::__toString
*
* @return void
*/
public function testDifferentMonth(): void
{
Expand All @@ -242,8 +222,6 @@ public function testDifferentMonth(): void
* @covers \Fisharebest\Webtrees\Age::ageYears
* @covers \Fisharebest\Webtrees\Age::ageYearsString
* @covers \Fisharebest\Webtrees\Age::__toString
*
* @return void
*/
public function testDifferentYear(): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/app/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/**
* Test harness for the class Auth
*
* @covers \Fisharebest\Webtrees\Auth
* @covers Fisharebest\Webtrees\Auth
*/
class AuthTest extends TestCase
{
Expand Down
Loading

0 comments on commit 2f41c55

Please sign in to comment.