Merge pull request #12789 from nupplaphil/bug/flasky_test

[tests] Fix flaky tests
This commit is contained in:
Hypolite Petovan 2023-02-13 17:06:26 -05:00 committed by GitHub
commit 262f960120
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 163 additions and 109 deletions

View file

@ -71,7 +71,11 @@ trait DatabaseTestTrait
}
foreach ($rows as $row) {
$dba->insert($tableName, $row, true);
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,71 +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);
if (DI::lock()->acquire('Test-checkInitialValues', 0)) {
DBStructure::checkInitialValues();
DI::lock()->release('Test-checkInitialValues');
}
// 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();
}
}

105
tests/FixtureTestTrait.php Normal file
View file

@ -0,0 +1,105 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
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

@ -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 [