Upgrade test framework to PHPUnit 10.5

This commit is contained in:
Harald Eilertsen
2024-05-27 06:17:05 +00:00
committed by Mario
parent a10402a788
commit cad82d12d2
25 changed files with 469 additions and 485 deletions

View File

@@ -58,7 +58,7 @@ before_script:
- echo "USE $MYSQL_DATABASE; SHOW TABLES;" | mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host="$DB_HOST" "$MYSQL_DATABASE"
# Run the actual tests
- touch dbfail.out
- vendor/bin/phpunit --configuration tests/phpunit.xml --verbose --stop-on-error --coverage-text --colors=never --log-junit tests/results/junit.xml || exit_code=$?
- vendor/bin/phpunit --configuration tests/phpunit.xml --no-progress --stop-on-error --coverage-text --colors=never --log-junit tests/results/junit.xml || exit_code=$?
- if [ $exit_code -ne 0 ]; then echo "Test barfed!"; cat dbfail.out; exit $exit_code; fi
coverage: '/^\s*Lines:\s*\d+.\d+\%/'
@@ -83,7 +83,7 @@ before_script:
- psql -h "postgres" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "\dt;"
# Run the actual tests
- touch dbfail.out
- vendor/bin/phpunit --configuration tests/phpunit.xml --verbose --stop-on-error --coverage-text --colors=never --log-junit tests/results/junit.xml || exit_code=$?
- vendor/bin/phpunit --configuration tests/phpunit.xml --no-progress --stop-on-error --coverage-text --colors=never --log-junit tests/results/junit.xml || exit_code=$?
- if [ $exit_code -ne 0 ]; then echo "Test barfed!"; cat dbfail.out; exit $exit_code; fi
coverage: '/^\s*Lines:\s*\d+.\d+\%/'

View File

@@ -393,7 +393,7 @@ class Setup extends \Zotlabs\Web\Controller {
$this->check_add($checks, t('PHP version'), false, true, $help);
}
if(strlen($phpath)) {
if(!empty($phpath)) {
$passed = file_exists($phpath);
}
elseif(function_exists('shell_exec')) {

View File

@@ -59,12 +59,12 @@
},
"require-dev": {
"ext-yaml": "*",
"phpunit/phpunit": "^9.4",
"php-mock/php-mock-phpunit": "^2.6",
"phpunit/phpunit": "^10.5",
"php-mock/php-mock-phpunit": "^2.10",
"phpmd/phpmd": "^2.6",
"squizlabs/php_codesniffer": "*",
"php-mock/php-mock": "^2.2",
"dms/phpunit-arraysubset-asserts": "^0.2.1"
"dms/phpunit-arraysubset-asserts": "^0.5.0"
},
"autoload": {
"psr-4": {

767
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,11 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="../boot.php"
colors="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="../boot.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache">
<php>
<includePath>..</includePath>
<!-- env name="HZ_TEST_DB_HOST" value=""/-->
@@ -24,10 +18,7 @@
<directory suffix="Test.php" prefix="API">./unit/</directory>
</testsuite>
</testsuites>
<coverage
processUncoveredFiles="false"
cacheDirectory=".cache/phpunit"
>
<source>
<include>
<directory suffix=".php">../Zotlabs/</directory>
<directory suffix=".php">../include/</directory>
@@ -35,5 +26,5 @@
<exclude>
<directory suffix=".php">../Zotlabs/Update/</directory>
</exclude>
</coverage>
</source>
</phpunit>

View File

@@ -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' => '<acid>',

View File

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

View File

@@ -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],

View File

@@ -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'><script>alert('boom')</script>",

View File

@@ -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'],

View File

@@ -1,5 +1,9 @@
<?php
class LibzotTest extends \Zotlabs\Tests\Unit\UnitTestCase {
namespace Zotlabs\Tests\Unit\Lib;
use Zotlabs\Tests\Unit\UnitTestCase;
class ZotlibTest extends UnitTestCase {
/**
* Test the `get_rpost_path` function.
*
@@ -11,7 +15,7 @@ class LibzotTest extends \Zotlabs\Tests\Unit\UnitTestCase {
$this->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=',

View File

@@ -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");

View File

@@ -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();

View File

@@ -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();

View File

@@ -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;
}
/**

View File

@@ -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' => [
'',

View File

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

View File

@@ -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' => '']],

View File

@@ -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' => [
"<p>A paragraph over\nmultiple lines\nshould be unwrapped</p>",

View File

@@ -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 = '<tag>
$expected = '<tag>
<id>uri</id>
<name>nick</name>
<uri>uri</uri>
@@ -57,7 +57,10 @@ class FeedutilsTest extends UnitTestCase {
<poco:displayName>name</poco:displayName>
</tag>';
$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 {
<poco:displayName>Chan</poco:displayName>
</tag>';
$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 .= '</feed>';
$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 .= '</feed>';
$this->assertXmlStringEqualsXmlString($expected_xml, $actual_xml);
}
}

View File

@@ -46,7 +46,7 @@ class LanguageTest extends UnitTestCase {
}
}
public function getLanguageNameProvider() {
public static function getLanguageNameProvider() {
return [
'empty language code' => [
'',

View File

@@ -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' => [
'',

View File

@@ -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],

View File

@@ -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>', '[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', '<value>'],
'text with angle bracket' => ['<value>', '<[value]>'],

View File

@@ -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);
}