From 704508fc6515359e84c3874c1dc6612066103ff2 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 13 Feb 2023 19:24:01 +0100 Subject: [PATCH 1/4] Remove flasky testpart --- tests/FixtureTest.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/FixtureTest.php b/tests/FixtureTest.php index dac71f0cf6..8483aee089 100644 --- a/tests/FixtureTest.php +++ b/tests/FixtureTest.php @@ -82,11 +82,6 @@ abstract class FixtureTest extends DatabaseTest $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); } From e1813e3d7379d4395ddb786a62eee95a42724717 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 13 Feb 2023 20:52:24 +0100 Subject: [PATCH 2/4] Restructure tests - Avoid database leftovers --- tests/DatabaseTestTrait.php | 6 +- tests/DiceHttpMockHandlerTrait.php | 12 ++- tests/FixtureTest.php | 57 ++---------- tests/FixtureTestTrait.php | 87 +++++++++++++++++++ tests/datasets/api.fixture.php | 16 ++++ tests/functional/DependencyCheckTest.php | 28 ++---- tests/src/Console/ServerBlockConsoleTest.php | 30 +++---- tests/src/Content/PageInfoTest.php | 4 +- .../HTTPClient/Client/HTTPClientTest.php | 7 ++ tests/src/Network/ProbeTest.php | 11 ++- tests/src/Util/ImagesTest.php | 7 ++ 11 files changed, 161 insertions(+), 104 deletions(-) create mode 100644 tests/FixtureTestTrait.php diff --git a/tests/DatabaseTestTrait.php b/tests/DatabaseTestTrait.php index 4badf85e90..6d3a75ab1b 100644 --- a/tests/DatabaseTestTrait.php +++ b/tests/DatabaseTestTrait.php @@ -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'); + } } } } diff --git a/tests/DiceHttpMockHandlerTrait.php b/tests/DiceHttpMockHandlerTrait.php index 581687a024..56250817d6 100644 --- a/tests/DiceHttpMockHandlerTrait.php +++ b/tests/DiceHttpMockHandlerTrait.php @@ -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(); } } diff --git a/tests/FixtureTest.php b/tests/FixtureTest.php index 8483aee089..e2e8ad725a 100644 --- a/tests/FixtureTest.php +++ b/tests/FixtureTest.php @@ -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(); } } diff --git a/tests/FixtureTestTrait.php b/tests/FixtureTestTrait.php new file mode 100644 index 0000000000..d8524bae9b --- /dev/null +++ b/tests/FixtureTestTrait.php @@ -0,0 +1,87 @@ +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); + } +} diff --git a/tests/datasets/api.fixture.php b/tests/datasets/api.fixture.php index b50f706251..f1e5d01233 100644 --- a/tests/datasets/api.fixture.php +++ b/tests/datasets/api.fixture.php @@ -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' => [ [ diff --git a/tests/functional/DependencyCheckTest.php b/tests/functional/DependencyCheckTest.php index 27e693295e..89b8f79a54 100644 --- a/tests/functional/DependencyCheckTest.php +++ b/tests/functional/DependencyCheckTest.php @@ -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); } /** diff --git a/tests/src/Console/ServerBlockConsoleTest.php b/tests/src/Console/ServerBlockConsoleTest.php index e8d75863c3..6179cf733e 100644 --- a/tests/src/Console/ServerBlockConsoleTest.php +++ b/tests/src/Console/ServerBlockConsoleTest.php @@ -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') diff --git a/tests/src/Content/PageInfoTest.php b/tests/src/Content/PageInfoTest.php index 98ceeecbed..701771049e 100644 --- a/tests/src/Content/PageInfoTest.php +++ b/tests/src/Content/PageInfoTest.php @@ -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() { diff --git a/tests/src/Network/HTTPClient/Client/HTTPClientTest.php b/tests/src/Network/HTTPClient/Client/HTTPClientTest.php index cd3d3328d5..2e6afc841e 100644 --- a/tests/src/Network/HTTPClient/Client/HTTPClientTest.php +++ b/tests/src/Network/HTTPClient/Client/HTTPClientTest.php @@ -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 */ diff --git a/tests/src/Network/ProbeTest.php b/tests/src/Network/ProbeTest.php index 013126b4d3..d9c0f074ec 100644 --- a/tests/src/Network/ProbeTest.php +++ b/tests/src/Network/ProbeTest.php @@ -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 = ' diff --git a/tests/src/Util/ImagesTest.php b/tests/src/Util/ImagesTest.php index 36a1afde01..384e909364 100644 --- a/tests/src/Util/ImagesTest.php +++ b/tests/src/Util/ImagesTest.php @@ -40,6 +40,13 @@ class ImagesTest extends MockedTest $this->setupHttpMockHandler(); } + protected function tearDown(): void + { + $this->tearDownFixtures(); + + parent::tearDown(); + } + public function dataImages() { return [ From 51ab7f4fcb806d624f3973f6e85b595d3209ee2f Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 13 Feb 2023 20:56:19 +0100 Subject: [PATCH 3/4] code review --- tests/FixtureTestTrait.php | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/tests/FixtureTestTrait.php b/tests/FixtureTestTrait.php index d8524bae9b..4bb750bf9d 100644 --- a/tests/FixtureTestTrait.php +++ b/tests/FixtureTestTrait.php @@ -1,4 +1,23 @@ . + * + */ namespace Friendica\Test; @@ -39,8 +58,7 @@ trait FixtureTestTrait ->addRules(include __DIR__ . '/../static/dependencies.config.php') ->addRule(ConfigFileManager::class, [ 'instanceOf' => Config::class, - 'call' => [['createConfigFileManager', [$this->root->url(), $server,], - Dice::CHAIN_CALL]]]) + '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, [ From 1e4ed3d5a1328195623c047fc0ad95b4842cfba6 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 13 Feb 2023 21:02:48 +0100 Subject: [PATCH 4/4] revert api.fixture.php change --- tests/datasets/api.fixture.php | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/tests/datasets/api.fixture.php b/tests/datasets/api.fixture.php index f1e5d01233..b50f706251 100644 --- a/tests/datasets/api.fixture.php +++ b/tests/datasets/api.fixture.php @@ -35,22 +35,6 @@ 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' => [ [