From 4eb7e29bab109463956365fc5f05dc0caa627227 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 2 Dec 2025 19:58:41 +0100 Subject: [PATCH] Improve test isolation To ensure tests don't step on each others toes, make sure we back up the static properties of the global App class before running tests that modify any of these properties. --- tests/unit/Lib/ConfigTest.php | 4 ++++ tests/unit/Lib/PermissionDescriptionTest.php | 3 +++ tests/unit/Module/AdminAccountsTest.php | 2 ++ tests/unit/Module/HelpTest.php | 3 +++ tests/unit/Module/RpostTest.php | 3 +++ tests/unit/includes/BBCodeTest.php | 7 +++++-- 6 files changed, 20 insertions(+), 2 deletions(-) diff --git a/tests/unit/Lib/ConfigTest.php b/tests/unit/Lib/ConfigTest.php index c853cdd87..7979df5d4 100644 --- a/tests/unit/Lib/ConfigTest.php +++ b/tests/unit/Lib/ConfigTest.php @@ -1,6 +1,9 @@ assertEquals('2 accounts blocked/unblocked', $this->notice[0]); } + #[BackupStaticProperties(App::class)] public function test_deleting_accounts_remove_them_from_db(): void { $params = [ 'user' => [ 42, 44 ], diff --git a/tests/unit/Module/HelpTest.php b/tests/unit/Module/HelpTest.php index e1aea1a06..c878bcddd 100644 --- a/tests/unit/Module/HelpTest.php +++ b/tests/unit/Module/HelpTest.php @@ -6,6 +6,8 @@ * SPDX-License-Identifier: MIT */ +use PHPUnit\Framework\Attributes\BackupStaticProperties; + class HelpTest extends \Zotlabs\Tests\Unit\Module\TestCase { use \phpmock\phpunit\PHPMock; @@ -103,6 +105,7 @@ class HelpTest extends \Zotlabs\Tests\Unit\Module\TestCase { $this->get('help/first'); } + #[BackupStaticProperties(App::class)] public function test_fall_back_to_english_if_localized_topic_dont_exist(): void { \App::$language = 'nb'; diff --git a/tests/unit/Module/RpostTest.php b/tests/unit/Module/RpostTest.php index ad94f2f06..59cddd964 100644 --- a/tests/unit/Module/RpostTest.php +++ b/tests/unit/Module/RpostTest.php @@ -8,6 +8,8 @@ * SPDX-License-Identifier: MIT */ +use PHPUnit\Framework\Attributes\BackupStaticProperties; + class RpostTest extends \Zotlabs\Tests\Unit\Module\TestCase { /** @@ -59,6 +61,7 @@ class RpostTest extends \Zotlabs\Tests\Unit\Module\TestCase { * * @param array $query An associative array of query parameters. */ + #[BackupStaticProperties(App::class)] private function get_authenticated(array $query = []): void { // Mock `local_chanel()` to emulate a valid logged in channel $lc_mock = $this->getFunctionMock('Zotlabs\Module', 'local_channel') diff --git a/tests/unit/includes/BBCodeTest.php b/tests/unit/includes/BBCodeTest.php index 982ef4eb9..82c0536c2 100644 --- a/tests/unit/includes/BBCodeTest.php +++ b/tests/unit/includes/BBCodeTest.php @@ -24,6 +24,8 @@ namespace Zotlabs\Tests\Unit\includes; use App; +use PHPUnit\Framework\Attributes\BackupStaticProperties; +use PHPUnit\Framework\Attributes\DataProvider; use Zotlabs\Tests\Unit\UnitTestCase; class BBCodeTest extends UnitTestCase { @@ -38,9 +40,9 @@ class BBCodeTest extends UnitTestCase { /** * Test the `[observer]` BBCode tags. - * - * @dataProvider bbcode_observer_provider */ + #[BackupStaticProperties(App::class)] + #[DataProvider('bbcode_observer_provider')] public function test_bbcode_observer(string $src, bool $logged_in, string $lang, string $expected): void { if ($logged_in) { App::set_observer([ @@ -64,6 +66,7 @@ class BBCodeTest extends UnitTestCase { /** * Test parsing the `[channel]` tag. */ + #[BackupStaticProperties(App::class)] public function test_bbcode_channel(): void { $src = '[channel=1]This is only for channels[/channel][channel=0]This is for everyone else[/channel]';