From cad82d12d2aad1a54fa821061dd3dc1ffc732c5a Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 27 May 2024 06:17:05 +0000 Subject: Upgrade test framework to PHPUnit 10.5 --- tests/phpunit.xml | 15 ++----- tests/unit/Access/AccessListTest.php | 2 +- tests/unit/Access/PermissionRolesTest.php | 2 + tests/unit/Access/PermissionsTest.php | 6 +-- tests/unit/AntiXSSTest.php | 2 +- tests/unit/Lib/ActivityTest.php | 2 +- tests/unit/Lib/ZotlibTest.php | 8 +++- tests/unit/Module/HelpTest.php | 2 +- tests/unit/Module/SetupTest.php | 4 +- tests/unit/Module/TestCase.php | 1 + tests/unit/UnitTestCase.php | 31 +++++++++----- tests/unit/Web/HttpSigTest.php | 2 +- tests/unit/Widget/HelpindexTest.php | 4 +- tests/unit/includes/AccountTest.php | 2 +- tests/unit/includes/BBCodeTest.php | 6 +-- tests/unit/includes/FeedutilsTest.php | 66 +++++++++++++++++++++++++++-- tests/unit/includes/LanguageTest.php | 2 +- tests/unit/includes/MarkdownTest.php | 4 +- tests/unit/includes/NetworkTest.php | 4 +- tests/unit/includes/TextTest.php | 4 +- tests/unit/includes/dba/TransactionTest.php | 6 +-- 21 files changed, 123 insertions(+), 52 deletions(-) (limited to 'tests') diff --git a/tests/phpunit.xml b/tests/phpunit.xml index a92dd530f..3531cd05c 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -1,11 +1,5 @@ - - + .. @@ -24,10 +18,7 @@ ./unit/ - + ../Zotlabs/ ../include/ @@ -35,5 +26,5 @@ ../Zotlabs/Update/ - + diff --git a/tests/unit/Access/AccessListTest.php b/tests/unit/Access/AccessListTest.php index 635f09930..feb9efebd 100644 --- a/tests/unit/Access/AccessListTest.php +++ b/tests/unit/Access/AccessListTest.php @@ -185,7 +185,7 @@ class AccessListTest extends UnitTestCase { $this->assertTrue($accessListPrivate->is_private()); } - public function isprivateProvider() { + public static function isprivateProvider() { return [ 'all set' => [[ 'channel_allow_cid' => '', diff --git a/tests/unit/Access/PermissionRolesTest.php b/tests/unit/Access/PermissionRolesTest.php index 3d3cbbae5..ccd6df205 100644 --- a/tests/unit/Access/PermissionRolesTest.php +++ b/tests/unit/Access/PermissionRolesTest.php @@ -23,6 +23,7 @@ namespace Zotlabs\Tests\Unit\Access; +use PHPUnit\Framwork\Attributes\IgnoreDeprecations; use Zotlabs\Tests\Unit\UnitTestCase; use Zotlabs\Access\PermissionRoles; use phpmock\phpunit\PHPMock; @@ -35,6 +36,7 @@ use DMS\PHPUnitExtensions\ArraySubset\Assert; * * @covers Zotlabs\Access\PermissionRoles */ +#[IgnoreDeprecations] class PermissionRolesTest extends UnitTestCase { use PHPMock; diff --git a/tests/unit/Access/PermissionsTest.php b/tests/unit/Access/PermissionsTest.php index 3ab567ad0..dd001e68a 100644 --- a/tests/unit/Access/PermissionsTest.php +++ b/tests/unit/Access/PermissionsTest.php @@ -135,7 +135,7 @@ class PermissionsTest extends UnitTestCase { * * \e array Indexed array which is passed as parameter to FilledPerms() * * \e array Expected associative result array with filled perms */ - public function FilledPermsProvider() { + public static function FilledPermsProvider() { return [ 'Empty param array' => [ [], @@ -253,7 +253,7 @@ class PermissionsTest extends UnitTestCase { * * \e array Array with perms to test * * \e array Expected result array */ - public function OPermsProvider() { + public static function OPermsProvider() { return [ 'empty' => [ [], @@ -286,7 +286,7 @@ class PermissionsTest extends UnitTestCase { * * \e array 2nd array with perms * * \e boolean expected result for the perms comparison */ - public function permsCompareProvider() { + public static function permsCompareProvider() { return [ 'equal' => [ ['perm1' => 1, 'perm2' => 0], diff --git a/tests/unit/AntiXSSTest.php b/tests/unit/AntiXSSTest.php index 1de9d54cd..642224f60 100644 --- a/tests/unit/AntiXSSTest.php +++ b/tests/unit/AntiXSSTest.php @@ -31,7 +31,7 @@ class AntiXSSTest extends TestCase { $this->assertEquals($expected, escape_url($url)); } - public function urlTestProvider() : array { + public static function urlTestProvider() : array { return [ [ "https://example.com/settings/calendar/?f=&rpath=https://example.com/cdav/calendar'>", diff --git a/tests/unit/Lib/ActivityTest.php b/tests/unit/Lib/ActivityTest.php index c9ce79d8c..0e2703f2b 100644 --- a/tests/unit/Lib/ActivityTest.php +++ b/tests/unit/Lib/ActivityTest.php @@ -19,7 +19,7 @@ class ActivityTest extends UnitTestCase { /** * Dataprovider for test_get_textfield. */ - private function get_textfield_provider(): array { + public static function get_textfield_provider(): array { return [ 'get content field' => [ ['content' => 'Some content'], diff --git a/tests/unit/Lib/ZotlibTest.php b/tests/unit/Lib/ZotlibTest.php index 05522678f..0ab89dc2f 100644 --- a/tests/unit/Lib/ZotlibTest.php +++ b/tests/unit/Lib/ZotlibTest.php @@ -1,5 +1,9 @@ assertEquals($expected, \Zotlabs\Lib\Libzot::get_rpost_path($observer)); } - private function get_rpost_path_provider() : array { + public static function get_rpost_path_provider() : array { return [ 'xchan_url without port' => [ 'https://example.com/rpost?f=', diff --git a/tests/unit/Module/HelpTest.php b/tests/unit/Module/HelpTest.php index 3fb7687d3..c345d5e52 100644 --- a/tests/unit/Module/HelpTest.php +++ b/tests/unit/Module/HelpTest.php @@ -52,7 +52,7 @@ class HelpTest extends \Zotlabs\Tests\Unit\Module\TestCase { $fgc_stub = $this->getFunctionMock('Zotlabs\Module', 'file_get_contents'); $fgc_stub ->expects($this->once()) - ->willReturn($this->returnValueMap($file_content_map)); + ->willReturnMap($file_content_map); $this->get("help/about/help_topic"); diff --git a/tests/unit/Module/SetupTest.php b/tests/unit/Module/SetupTest.php index 96a5ef932..3575dd477 100644 --- a/tests/unit/Module/SetupTest.php +++ b/tests/unit/Module/SetupTest.php @@ -6,6 +6,8 @@ * SPDX-License-Identifier: MIT */ +namespace Zotlabs\Tests\Unit\Module; + /** * SetupModuleTest * @@ -16,7 +18,7 @@ * This is a complex module, so expect the tests to grow as more of it will be * covered. */ -class SetupModuleTest extends \Zotlabs\Tests\Unit\Module\TestCase { +class SetupTest extends TestCase { public function test_that_setup_is_available_if_no_accounts_in_db(): void { $this->with_no_accounts_in_db(); diff --git a/tests/unit/Module/TestCase.php b/tests/unit/Module/TestCase.php index 5ad213e81..e7051e001 100644 --- a/tests/unit/Module/TestCase.php +++ b/tests/unit/Module/TestCase.php @@ -24,6 +24,7 @@ class TestCase extends \Zotlabs\Tests\Unit\UnitTestCase { } $_SERVER['REQUEST_METHOD'] = 'GET'; + $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; $_REQUEST = $_GET; \App::init(); diff --git a/tests/unit/UnitTestCase.php b/tests/unit/UnitTestCase.php index 9ab6a534a..844746a51 100644 --- a/tests/unit/UnitTestCase.php +++ b/tests/unit/UnitTestCase.php @@ -22,8 +22,8 @@ namespace Zotlabs\Tests\Unit; +use PHPUnit\Framework\Attributes\{Before, After}; use PHPUnit\Framework\TestCase; -use PHPUnit\Framework\TestResult; /* * Make sure global constants and the global App object is available to the @@ -46,33 +46,44 @@ require_once 'include/dba/dba_driver.php' ; */ class UnitTestCase extends TestCase { protected array $fixtures = array(); + protected ?\DbaTransaction $db_transacton = null; /** - * Override the run method, so we can wrap it in a database transaction. + * Connect to the test db, load fixtures and global config. * - * The transaction is automatically rolled back when the test completes, to - * leave the test database in a known pristine state. + * This function is executed before every test. * - * @SuppressWarnings(PHPMD.UnusedLocalVariable) + * The transaction is rolled back in the `cleanup_test_db()` function + * that's executed after every test. */ - public function run(TestResult $result = null): TestResult { + #[Before] + protected function setup_test_db(): void { if (! \DBA::$dba) { $this->connect_to_test_db(); } // The $transactuion variable is needed to hold the transaction until the // function returns. - $transaction = new \DbaTransaction(\DBA::$dba); + $this->db_transaction = new \DbaTransaction(\DBA::$dba); $this->loadFixtures(); // Make sure app config is reset and loaded from fixtures \App::$config = array(); \Zotlabs\Lib\Config::Load('system'); + } - $result = parent::run($result); - - return $result; + /** + * Roll back test database to it's original state, cleaning up + * any changes from the test. + * + * This function is executes after evert tests. + */ + #[After] + protected function cleanup_test_db(): void { + // Setting the transaction to `null`, runs the destructor + // which rolls backk the transacton. + $this->db_transaction = null; } /** diff --git a/tests/unit/Web/HttpSigTest.php b/tests/unit/Web/HttpSigTest.php index c01e4f6a3..d705be4ff 100644 --- a/tests/unit/Web/HttpSigTest.php +++ b/tests/unit/Web/HttpSigTest.php @@ -46,7 +46,7 @@ class HttpSigTest extends UnitTestCase { HTTPSig::generate_digest_header($text) ); } - public function generate_digestProvider() { + public static function generate_digestProvider() { return [ 'empty body text' => [ '', diff --git a/tests/unit/Widget/HelpindexTest.php b/tests/unit/Widget/HelpindexTest.php index 2d984985d..26aa34104 100644 --- a/tests/unit/Widget/HelpindexTest.php +++ b/tests/unit/Widget/HelpindexTest.php @@ -23,8 +23,8 @@ class HelpindexTest extends \Zotlabs\Tests\Unit\Module\TestCase { * @beforeClass */ public static function define_stubs(): void { - \phpmock\phpunit\PHPMock::defineFunctionMock('Zotlabs\Lib\Traits', 'file_exists'); - \phpmock\phpunit\PHPMock::defineFunctionMock('Zotlabs\Widget', 'file_get_contents'); + self::defineFunctionMock('Zotlabs\Lib\Traits', 'file_exists'); + self::defineFunctionMock('Zotlabs\Widget', 'file_get_contents'); } public function test_loading_toc(): void { diff --git a/tests/unit/includes/AccountTest.php b/tests/unit/includes/AccountTest.php index af5bcd3c1..3978f9d04 100644 --- a/tests/unit/includes/AccountTest.php +++ b/tests/unit/includes/AccountTest.php @@ -19,7 +19,7 @@ class AccountTest extends Zotlabs\Tests\Unit\UnitTestCase { $this->assertEquals($expected, check_account_email($email)); } - function check_account_email_provider() : array { + public static function check_account_email_provider() : array { return [ // Empty and valid emails return the same result ['', ['error' => false, 'message' => '']], diff --git a/tests/unit/includes/BBCodeTest.php b/tests/unit/includes/BBCodeTest.php index 94cad1367..daa66bf72 100644 --- a/tests/unit/includes/BBCodeTest.php +++ b/tests/unit/includes/BBCodeTest.php @@ -101,7 +101,7 @@ class BBCodeTest extends UnitTestCase { * * @SuppressWarnings(PHPMD.UnusedPrivateMethod) */ - private function bbcode_to_html_provider(): array { + public static function bbcode_to_html_provider(): array { return [ 'code block' => [ "[code]\ntestvar = \"this is a test\"\necho \"the message is \$testvar\"\n[/code]", @@ -153,7 +153,7 @@ class BBCodeTest extends UnitTestCase { * * @SuppressWarnings(PHPMD.UnusedPrivateMethod) */ - private function bbcode_observer_provider(): array { + public static function bbcode_observer_provider(): array { return [ 'authenticated observer' => [ '[observer=1]This should be visible[/observer][observer=0]but not this[/observer]', @@ -205,7 +205,7 @@ class BBCodeTest extends UnitTestCase { * * @SuppressWarnings(PHPMD.UnusedPrivateMethod) */ - private function html2bbcode_provider(): array { + public static function html2bbcode_provider(): array { return [ 'paragraph over multiple lines' => [ "

A paragraph over\nmultiple lines\nshould be unwrapped

", diff --git a/tests/unit/includes/FeedutilsTest.php b/tests/unit/includes/FeedutilsTest.php index bda0bf425..05bff06a3 100644 --- a/tests/unit/includes/FeedutilsTest.php +++ b/tests/unit/includes/FeedutilsTest.php @@ -47,7 +47,7 @@ class FeedutilsTest extends UnitTestCase { public function test_atom_author() { $this->assertEquals('', atom_author('', 'nick', 'name', 'uri', 72, 72, 'png', 'photourl')); - $a = ' + $expected = ' uri nick uri @@ -57,7 +57,10 @@ class FeedutilsTest extends UnitTestCase { name '; - $this->assertXmlStringEqualsXmlString($a, atom_author('tag', 'nick', 'name', 'uri', 72, 72, 'png', 'http://photourl')); + $this->assertAtomAuthorMatches( + $expected, + atom_author('tag', 'nick', 'name', 'uri', 72, 72, 'png', 'http://photourl') + ); } /** @@ -86,6 +89,63 @@ class FeedutilsTest extends UnitTestCase { Chan '; - $this->assertXmlStringEqualsXmlString($a, atom_render_author('tag', $xchan)); + $this->assertAtomAuthorMatches($a, atom_render_author('tag', $xchan)); + } + + /** + * Helper method to assert that the generated author tag matches + * what we expect. + * + * Calling `assertXmlStringEqualsXmlString` directly on the fragments + * does not work anymore in PHPUnit >= 10.x, as t will throw an XMLException + * because of undefined namespaces. + * + * To overcome that we wrap the generated tags in the proper template, + * and compare the fully generated XML from the template instead. + * + * @param string $expected The expected author XML fragment. + * @param string $actual The actually generated authr XML fragment. + */ + private function assertAtomAuthorMatches(string $expected, string $actual): void { + + // Make sure the template engine is initialized before we try to render + // the template. + // + // This may be problematic, as it will compile the template into the same + // directory as the site. Assuming that nobody is crazy enough to run the + // test suite in a production server, it should probably be fine for now. + $smarty = new \Zotlabs\Render\SmartyTemplate(); + \App::register_template_engine(get_class($smarty)); + + $feed_template = get_markup_template('atom_feed.tpl'); + $expected_xml = replace_macros($feed_template, [ + '$version' => 42, + '$generator' => 'Hubzilla test', + '$generator_uri' => 'https://hubzilla.test', + '$feed_id' => 'test_channel', + '$feed_title' => 'Test channel', + '$feed_updated' => 'Sometime', + '$author' => $expected, + '$owner' => $expected, + '$profile_page' => xmlify('https://hubzilla.test/channel/test'), + ]); + + $expected_xml .= ''; + + $actual_xml = replace_macros($feed_template, [ + '$version' => 42, + '$generator' => 'Hubzilla test', + '$generator_uri' => 'https://hubzilla.test', + '$feed_id' => 'test_channel', + '$feed_title' => 'Test channel', + '$feed_updated' => 'Sometime', + '$author' => $actual, + '$owner' => $actual, + '$profile_page' => xmlify('https://hubzilla.test/channel/test'), + ]); + + $actual_xml .= ''; + + $this->assertXmlStringEqualsXmlString($expected_xml, $actual_xml); } } diff --git a/tests/unit/includes/LanguageTest.php b/tests/unit/includes/LanguageTest.php index 3367232f3..8f62e71e2 100644 --- a/tests/unit/includes/LanguageTest.php +++ b/tests/unit/includes/LanguageTest.php @@ -46,7 +46,7 @@ class LanguageTest extends UnitTestCase { } } - public function getLanguageNameProvider() { + public static function getLanguageNameProvider() { return [ 'empty language code' => [ '', diff --git a/tests/unit/includes/MarkdownTest.php b/tests/unit/includes/MarkdownTest.php index 960c15139..310130bf1 100644 --- a/tests/unit/includes/MarkdownTest.php +++ b/tests/unit/includes/MarkdownTest.php @@ -39,7 +39,7 @@ class MarkdownTest extends UnitTestCase { $this->assertEquals($expected, markdown_to_bb($src)); } - private function markdown_to_bbcode_provider(): array { + public static function markdown_to_bbcode_provider(): array { return [ 'empty text' => [ '', @@ -104,7 +104,7 @@ class MarkdownTest extends UnitTestCase { $this->assertEquals($markdown, html2markdown($html)); } - public function html2markdownProvider(): array { + public static function html2markdownProvider(): array { return [ 'empty text' => [ '', diff --git a/tests/unit/includes/NetworkTest.php b/tests/unit/includes/NetworkTest.php index 9fb00e9d3..5bf175953 100644 --- a/tests/unit/includes/NetworkTest.php +++ b/tests/unit/includes/NetworkTest.php @@ -20,7 +20,7 @@ class NetworkTest extends Zotlabs\Tests\Unit\UnitTestCase { $this->assertEquals($expected, is_local_url($url)); } - public function localUrlTestProvider() : array { + public static function localUrlTestProvider() : array { return [ [ '/some/path', true ], [ 'https://mytest.org/some/path', true ], @@ -47,7 +47,7 @@ class NetworkTest extends Zotlabs\Tests\Unit\UnitTestCase { $this->assertTrue(validate_email($email)); } - function validate_email_provider() : array { + public static function validate_email_provider() : array { return [ // First some invalid email addresses ['', false], diff --git a/tests/unit/includes/TextTest.php b/tests/unit/includes/TextTest.php index b76b15dcf..1e80d71d8 100644 --- a/tests/unit/includes/TextTest.php +++ b/tests/unit/includes/TextTest.php @@ -85,7 +85,7 @@ empty line above'; public function testNotags($string, $expected) { $this->assertEquals($expected, notags($string)); } - public function notagsProvider() { + public static function notagsProvider() { return [ 'empty string' => ['', ''], 'simple tag' => ['', '[value]'], @@ -102,7 +102,7 @@ empty line above'; sanitise_acl($string); $this->assertEquals($expected, $string); } - public function sanitise_aclProvider() { + public static function sanitise_aclProvider() { return [ 'text' => ['value', ''], 'text with angle bracket' => ['', '<[value]>'], diff --git a/tests/unit/includes/dba/TransactionTest.php b/tests/unit/includes/dba/TransactionTest.php index 99e3f459d..0b986c6aa 100644 --- a/tests/unit/includes/dba/TransactionTest.php +++ b/tests/unit/includes/dba/TransactionTest.php @@ -24,8 +24,8 @@ require_once 'tests/fakes/fake_dba.php'; require_once 'include/dba/dba_transaction.php'; -use \PHPUnit\Framework\TestCase; -use \Zotlabs\Tests\Fakes\FakeDba; +use PHPUnit\Framework\TestCase; +use Zotlabs\Tests\Fakes\FakeDba; /** * Test database transactions. @@ -39,7 +39,7 @@ class DbaTransactionTest extends TestCase { private $pdo_stub; public function setUp(): void { - $this->pdo_stub = $this->createStub(PDO::class); + $this->pdo_stub = $this->createMock(PDO::class); } -- cgit v1.2.3