Restructure tests

- Avoid database leftovers
This commit is contained in:
Philipp 2023-02-13 20:52:24 +01:00
parent 704508fc65
commit e1813e3d73
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
11 changed files with 161 additions and 104 deletions

View file

@ -71,7 +71,11 @@ trait DatabaseTestTrait
}
foreach ($rows as $row) {
if (is_array($row)) {
$dba->insert($tableName, $row, true);
} else {
throw new \Exception('row isn\'t an array');
}
}
}
}

View file

@ -32,6 +32,8 @@ use GuzzleHttp\HandlerStack;
*/
trait DiceHttpMockHandlerTrait
{
use FixtureTestTrait;
/**
* Handler for mocking requests anywhere for testing purpose
*
@ -41,9 +43,7 @@ trait DiceHttpMockHandlerTrait
protected function setupHttpMockHandler(): void
{
if (!empty($this->httpRequestHandler) && $this->httpRequestHandler instanceof HandlerStack) {
return;
}
$this->setUpFixtures();
$this->httpRequestHandler = HandlerStack::create();
@ -59,10 +59,8 @@ trait DiceHttpMockHandlerTrait
DI::init($newDice);
}
protected function tearDown(): void
protected function tearDownHandler(): void
{
\Mockery::close();
parent::tearDown();
$this->tearDownFixtures();
}
}

View file

@ -39,66 +39,21 @@ use Friendica\Test\Util\VFSTrait;
/**
* Parent class for test cases requiring fixtures
*/
abstract class FixtureTest extends DatabaseTest
abstract class FixtureTest extends MockedTest
{
use VFSTrait;
use FixtureTestTrait;
/** @var Dice */
protected $dice;
/**
* Create variables used by tests.
*/
protected function setUp(): void
{
$this->setUpVfsDir();
parent::setUp();
$server = $_SERVER;
$server['REQUEST_METHOD'] = Router::GET;
$this->dice = (new Dice())
->addRules(include __DIR__ . '/../static/dependencies.config.php')
->addRule(ConfigFileManager::class, [
'instanceOf' => Config::class,
'call' => [['createConfigFileManager', [$this->root->url(), $server,],
Dice::CHAIN_CALL]]])
->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true])
->addRule(IHandleSessions::class, ['instanceOf' => Memory::class, 'shared' => true, 'call' => null])
->addRule(Arguments::class, [
'instanceOf' => Arguments::class,
'call' => [
['determine', [$server, $_GET], Dice::CHAIN_CALL],
],
]);
DI::init($this->dice);
$config = $this->dice->create(IManageConfigValues::class);
$config->set('database', 'disable_pdo', true);
/** @var Database $dba */
$dba = $this->dice->create(Database::class);
$dba->setTestmode(true);
// Load the API dataset for the whole API
$this->loadFixture(__DIR__ . '/datasets/api.fixture.php', $dba);
$this->setUpFixtures();
}
protected function useHttpMethod(string $method = Router::GET)
protected function tearDown(): void
{
$server = $_SERVER;
$server['REQUEST_METHOD'] = $method;
$this->tearDownFixtures();
$this->dice = $this->dice
->addRule(Arguments::class, [
'instanceOf' => Arguments::class,
'call' => [
['determine', [$server, $_GET], Dice::CHAIN_CALL],
],
]);
DI::init($this->dice);
parent::tearDown();
}
}

View file

@ -0,0 +1,87 @@
<?php
namespace Friendica\Test;
use Dice\Dice;
use Friendica\App\Arguments;
use Friendica\App\Router;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Config\Factory\Config;
use Friendica\Core\Config\Util\ConfigFileManager;
use Friendica\Core\Session\Capability\IHandleSessions;
use Friendica\Core\Session\Type\Memory;
use Friendica\Database\Database;
use Friendica\Database\DBStructure;
use Friendica\DI;
use Friendica\Test\Util\Database\StaticDatabase;
use Friendica\Test\Util\VFSTrait;
trait FixtureTestTrait
{
use VFSTrait;
use DatabaseTestTrait;
/** @var Dice */
protected $dice;
/**
* Create variables used by tests.
*/
protected function setUpFixtures(): void
{
$this->setUpVfsDir();
$this->setUpDb();
$server = $_SERVER;
$server['REQUEST_METHOD'] = Router::GET;
$this->dice = (new Dice())
->addRules(include __DIR__ . '/../static/dependencies.config.php')
->addRule(ConfigFileManager::class, [
'instanceOf' => Config::class,
'call' => [['createConfigFileManager', [$this->root->url(), $server,],
Dice::CHAIN_CALL]]])
->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true])
->addRule(IHandleSessions::class, ['instanceOf' => Memory::class, 'shared' => true, 'call' => null])
->addRule(Arguments::class, [
'instanceOf' => Arguments::class,
'call' => [
['determine', [$server, $_GET], Dice::CHAIN_CALL],
],
]);
DI::init($this->dice, true);
$config = $this->dice->create(IManageConfigValues::class);
$config->set('database', 'disable_pdo', true);
/** @var Database $dba */
$dba = $this->dice->create(Database::class);
$dba->setTestmode(true);
DBStructure::checkInitialValues();
// Load the API dataset for the whole API
$this->loadFixture(__DIR__ . '/datasets/api.fixture.php', $dba);
}
protected function tearDownFixtures(): void
{
$this->tearDownDb();
}
protected function useHttpMethod(string $method = Router::GET)
{
$server = $_SERVER;
$server['REQUEST_METHOD'] = $method;
$this->dice = $this->dice
->addRule(Arguments::class, [
'instanceOf' => Arguments::class,
'call' => [
['determine', [$server, $_GET], Dice::CHAIN_CALL],
],
]);
DI::init($this->dice);
}
}

View file

@ -35,6 +35,22 @@ return [
'workerqueue',
'mail',
'post-delivery-data',
'gserver' => [
[
'id' => 42,
'url' => 'https://friendica.test',
'info' => 'test_node',
'active-week-users' => 1,
'active-month-users' => 1,
'active-halfyear-users' => 1,
'local-posts' => 42,
'local-comments' => 42,
'protocol' => 'apub',
'detection-method' => 'none',
'blocked' => 0,
'failed' => 0,
],
],
// Base test config to avoid notice messages
'user' => [
[

View file

@ -21,7 +21,6 @@
namespace Friendica\Test\functional;
use Dice\Dice;
use Friendica\App;
use Friendica\Core\Cache\Capability\ICanCache;
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
@ -29,37 +28,17 @@ use Friendica\Core\Config\ValueObject\Cache;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Lock\Capability\ICanLock;
use Friendica\Database\Database;
use Friendica\Test\Util\VFSTrait;
use Friendica\Test\FixtureTest;
use Friendica\Util\BasePath;
use Friendica\Core\Config\Util\ConfigFileManager;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
class DependencyCheckTest extends TestCase
class DependencyCheckTest extends FixtureTest
{
use VFSTrait;
/**
* @var Dice
*/
private $dice;
protected function setUp() : void
{
parent::setUp();
$this->setUpVfsDir();
$this->dice = (new Dice())
->addRules(include __DIR__ . '/../../static/dependencies.config.php')
->addRule(BasePath::class, [
'constructParams' => [
$this->root->url(),
[],
],
])
->addRule(LoggerInterface::class, ['constructParams' => ['test']]);
/** @var IManageConfigValues $config */
$config = $this->dice->create(IManageConfigValues::class);
$config->set('system', 'logfile', $this->root->url() . '/logs/friendica.log');
@ -75,6 +54,9 @@ class DependencyCheckTest extends TestCase
self::assertInstanceOf(BasePath::class, $basePath);
self::assertEquals($this->root->url(), $basePath->getPath());
/** @var Database $dba */
$dba = $this->dice->create(Database::class);
}
/**

View file

@ -21,15 +21,15 @@
namespace Friendica\Test\src\Console;
use Dice\Dice;
use Friendica\Console\ServerBlock;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\DI;
use Friendica\Moderation\DomainPatternBlocklist;
use Friendica\Test\FixtureTestTrait;
use Mockery;
class ServerBlockConsoleTest extends ConsoleTest
{
use FixtureTestTrait;
protected $defaultBlockList = [
[
'domain' => 'social.nobodyhasthe.biz',
@ -49,9 +49,18 @@ class ServerBlockConsoleTest extends ConsoleTest
{
parent::setUp();
$this->setUpFixtures();
$this->blocklistMock = Mockery::mock(DomainPatternBlocklist::class);
}
protected function tearDown(): void
{
$this->tearDownFixtures();
parent::tearDown();
}
/**
* Test to list the default blocked servers
*/
@ -80,11 +89,6 @@ CONS;
*/
public function testAddBlockedServer()
{
$dice = new Dice();
$dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
DI::init($dice, true);
$this->blocklistMock
->shouldReceive('addPattern')
->with('testme.now', 'I like it!')
@ -105,11 +109,6 @@ CONS;
*/
public function testUpdateBlockedServer()
{
$dice = new Dice();
$dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
DI::init($dice, true);
$this->blocklistMock
->shouldReceive('addPattern')
->with('pod.ordoevangelistarum.com', 'Other reason')
@ -130,11 +129,6 @@ CONS;
*/
public function testRemoveBlockedServer()
{
$dice = new Dice();
$dice = $dice->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
DI::init($dice, true);
$this->blocklistMock
->shouldReceive('removePattern')
->with('pod.ordoevangelistarum.com')

View file

@ -21,9 +21,9 @@
namespace Friendica\Test\src\Content;
use Friendica\Test\MockedTest;
use Friendica\Test\DatabaseTest;
class PageInfoTest extends MockedTest
class PageInfoTest extends DatabaseTest
{
public function dataGetRelevantUrlFromBody()
{

View file

@ -38,6 +38,13 @@ class HTTPClientTest extends MockedTest
$this->setupHttpMockHandler();
}
protected function tearDown(): void
{
$this->tearDownHandler();
parent::tearDown();
}
/**
* Test for issue https://github.com/friendica/friendica/issues/10473#issuecomment-907749093
*/

View file

@ -23,10 +23,10 @@ namespace Friendica\Test\src\Network;
use Friendica\Network\Probe;
use Friendica\Test\DiceHttpMockHandlerTrait;
use Friendica\Test\FixtureTest;
use Friendica\Test\MockedTest;
use GuzzleHttp\Middleware;
class ProbeTest extends FixtureTest
class ProbeTest extends MockedTest
{
use DiceHttpMockHandlerTrait;
@ -37,6 +37,13 @@ class ProbeTest extends FixtureTest
$this->setupHttpMockHandler();
}
protected function tearDown(): void
{
$this->tearDownHandler();
parent::tearDown();
}
const TEMPLATENOBASE = '
<!DOCTYPE html>
<html lang="en-us">

View file

@ -40,6 +40,13 @@ class ImagesTest extends MockedTest
$this->setupHttpMockHandler();
}
protected function tearDown(): void
{
$this->tearDownFixtures();
parent::tearDown();
}
public function dataImages()
{
return [