Merge branch 'develop' into phpstan-level-3

This commit is contained in:
Art4 2025-04-28 11:44:30 +00:00
commit d6b072477d
29 changed files with 763 additions and 165 deletions

View file

@ -0,0 +1,75 @@
<?php
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace Friendica\Test\Unit\Core\Logger\Factory;
use Exception;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Logger\Capability\LogChannel;
use Friendica\Core\Logger\Factory\DelegatingLoggerFactory;
use Friendica\Core\Logger\Factory\LoggerFactory;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Psr\Log\NullLogger;
class DelegatingLoggerFactoryTest extends TestCase
{
public function testCreateLoggerReturnsPsrLogger(): void
{
$config = $this->createStub(IManageConfigValues::class);
$config->method('get')->willReturnMap([
['system', 'logger_config', null, 'test'],
]);
$factory = new DelegatingLoggerFactory($config);
$factory->registerFactory('test', $this->createStub(LoggerFactory::class));
$this->assertInstanceOf(
LoggerInterface::class,
$factory->createLogger(LogLevel::DEBUG, LogChannel::DEFAULT)
);
}
public function testCreateLoggerWithoutRegisteredFactoryReturnsNullLogger(): void
{
$config = $this->createStub(IManageConfigValues::class);
$config->method('get')->willReturnMap([
['system', 'logger_config', null, 'not-existing-factory'],
]);
$factory = new DelegatingLoggerFactory($config);
$this->assertInstanceOf(
NullLogger::class,
$factory->createLogger(LogLevel::DEBUG, LogChannel::DEFAULT)
);
}
public function testCreateLoggerWithExceptionThrowingFactoryReturnsNullLogger(): void
{
$config = $this->createStub(IManageConfigValues::class);
$config->method('get')->willReturnMap([
['system', 'logger_config', null, 'test'],
]);
$factory = new DelegatingLoggerFactory($config);
$brokenFactory = $this->createStub(LoggerFactory::class);
$brokenFactory->method('createLogger')->willThrowException(new Exception());
$factory->registerFactory('test', $brokenFactory);
$this->assertInstanceOf(
NullLogger::class,
$factory->createLogger(LogLevel::DEBUG, LogChannel::DEFAULT)
);
}
}

View file

@ -1,36 +0,0 @@
<?php
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace Friendica\Test\Unit\Core\Logger\Factory;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Hooks\Capability\ICanCreateInstances;
use Friendica\Core\Logger\Capability\LogChannel;
use Friendica\Core\Logger\Factory\LegacyLoggerFactory;
use Friendica\Util\Profiler;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
class LegacyLoggerFactoryTest extends TestCase
{
public function testCreateLoggerReturnsPsrLogger(): void
{
$factory = new LegacyLoggerFactory(
$this->createStub(ICanCreateInstances::class),
$this->createStub(IManageConfigValues::class),
$this->createStub(Profiler::class),
);
$this->assertInstanceOf(
LoggerInterface::class,
$factory->createLogger(LogLevel::DEBUG, LogChannel::DEFAULT)
);
}
}

View file

@ -0,0 +1,81 @@
<?php
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace Friendica\Test\Unit\Core\Logger\Factory;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Logger\Capability\IHaveCallIntrospections;
use Friendica\Core\Logger\Capability\LogChannel;
use Friendica\Core\Logger\Exception\LoggerArgumentException;
use Friendica\Core\Logger\Exception\LogLevelException;
use Friendica\Core\Logger\Factory\StreamLoggerFactory;
use Friendica\Core\Logger\Util\FileSystemUtil;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
class StreamLoggerFactoryTest extends TestCase
{
public function testCreateLoggerReturnsPsrLogger(): void
{
$config = $this->createStub(IManageConfigValues::class);
$config->method('get')->willReturnMap([
['system', 'logfile', null, dirname(__DIR__, 4) . '/datasets/log/empty.friendica.log.txt'],
]);
$factory = new StreamLoggerFactory(
$config,
$this->createStub(IHaveCallIntrospections::class),
$this->createStub(FileSystemUtil::class),
);
$this->assertInstanceOf(
LoggerInterface::class,
$factory->createLogger(LogLevel::DEBUG, LogChannel::DEFAULT)
);
}
public function testCreateLoggerWithInvalidLogfileThrowsException(): void
{
$config = $this->createStub(IManageConfigValues::class);
$config->method('get')->willReturnMap([
['system', 'logfile', null, dirname(__DIR__, 1) . '/not-existing-logfile.txt'],
]);
$factory = new StreamLoggerFactory(
$config,
$this->createStub(IHaveCallIntrospections::class),
$this->createStub(FileSystemUtil::class),
);
$this->expectException(LoggerArgumentException::class);
$this->expectExceptionMessage('tests/Unit/Core/Logger/not-existing-logfile.txt" is not a valid logfile.');
$factory->createLogger(LogLevel::DEBUG, LogChannel::DEFAULT);
}
public function testCreateLoggerWithInvalidLoglevelThrowsException(): void
{
$config = $this->createStub(IManageConfigValues::class);
$config->method('get')->willReturnMap([
['system', 'logfile', null, dirname(__DIR__, 4) . '/datasets/log/empty.friendica.log.txt'],
]);
$factory = new StreamLoggerFactory(
$config,
$this->createStub(IHaveCallIntrospections::class),
$this->createStub(FileSystemUtil::class),
);
$this->expectException(LogLevelException::class);
$this->expectExceptionMessage('The log level "unsupported-loglevel" is not supported by "Friendica\Core\Logger\Type\StreamLogger".');
$factory->createLogger('unsupported-loglevel', LogChannel::DEFAULT);
}
}

View file

@ -0,0 +1,61 @@
<?php
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace Friendica\Test\Unit\Core\Logger\Factory;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Logger\Capability\IHaveCallIntrospections;
use Friendica\Core\Logger\Capability\LogChannel;
use Friendica\Core\Logger\Exception\LogLevelException;
use Friendica\Core\Logger\Factory\SyslogLoggerFactory;
use Friendica\Core\Logger\Type\SyslogLogger;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
class SyslogLoggerFactoryTest extends TestCase
{
public function testCreateLoggerReturnsPsrLogger(): void
{
$config = $this->createStub(IManageConfigValues::class);
$config->method('get')->willReturnMap([
['system', 'syslog_flags', null, SyslogLogger::DEFAULT_FLAGS],
['system', 'syslog_facility', null, SyslogLogger::DEFAULT_FACILITY],
]);
$factory = new SyslogLoggerFactory(
$config,
$this->createStub(IHaveCallIntrospections::class),
);
$this->assertInstanceOf(
LoggerInterface::class,
$factory->createLogger(LogLevel::DEBUG, LogChannel::DEFAULT)
);
}
public function testCreateLoggerWithInvalidLoglevelThrowsException(): void
{
$config = $this->createStub(IManageConfigValues::class);
$config->method('get')->willReturnMap([
['system', 'syslog_flags', null, SyslogLogger::DEFAULT_FLAGS],
['system', 'syslog_facility', null, SyslogLogger::DEFAULT_FACILITY],
]);
$factory = new SyslogLoggerFactory(
$config,
$this->createStub(IHaveCallIntrospections::class),
);
$this->expectException(LogLevelException::class);
$this->expectExceptionMessage('The log level "unsupported-loglevel" is not supported by "Friendica\Core\Logger\Type\SyslogLogger".');
$factory->createLogger('unsupported-loglevel', LogChannel::DEFAULT);
}
}

View file

@ -16,39 +16,39 @@ class DateTimeFormatTest extends MockedTestCase
{
return [
'validNormal' => [
'input' => '1990-10',
'input' => '1990-10',
'assert' => true,
],
'validOneCharMonth' => [
'input' => '1990-1',
'input' => '1990-1',
'assert' => true,
],
'validTwoCharMonth' => [
'input' => '1990-01',
'input' => '1990-01',
'assert' => true,
],
'invalidFormat' => [
'input' => '199-11',
'input' => '199-11',
'assert' => false,
],
'invalidFormat2' => [
'input' => '1990-15',
'input' => '1990-15',
'assert' => false,
],
'invalidFormat3' => [
'input' => '99-101',
'input' => '99-101',
'assert' => false,
],
'invalidFormat4' => [
'input' => '11-1990',
'input' => '11-1990',
'assert' => false,
],
'invalidFuture' => [
'input' => '3030-12',
'input' => '3030-12',
'assert' => false,
],
'invalidYear' => [
'input' => '-100-10',
'input' => '-100-10',
'assert' => false,
],
];
@ -79,51 +79,55 @@ class DateTimeFormatTest extends MockedTestCase
return [
'Mo, 19 Sep 2022 14:51:00 +0200' => [
'expectedDate' => '2022-09-19T14:51:00+02:00',
'dateString' => 'Mo, 19 Sep 2022 14:51:00 +0200',
'dateString' => 'Mo, 19 Sep 2022 14:51:00 +0200',
],
'2020-11-21T12:00:13.745339ZZ' => [
'expectedDate' => '2020-11-21T12:00:13+00:00',
'dateString' => '2020-11-21T12:00:13.745339ZZ',
'dateString' => '2020-11-21T12:00:13.745339ZZ',
],
'2016-09-09T13:32:00ZZ' => [
'expectedDate' => '2016-09-09T13:32:00+00:00',
'dateString' => '2016-09-09T13:32:00ZZ',
'dateString' => '2016-09-09T13:32:00ZZ',
],
'Sun, 10/03/2021 - 12:41' => [
'expectedDate' => '2021-10-03T12:41:00+00:00',
'dateString' => 'Sun, 10/03/2021 - 12:41',
'dateString' => 'Sun, 10/03/2021 - 12:41',
],
'4:30 PM, Sep 13, 2022' => [
'expectedDate' => '2022-09-13T16:30:00+00:00',
'dateString' => '4:30 PM, Sep 13, 2022',
'dateString' => '4:30 PM, Sep 13, 2022',
],
'August 27, 2022 - 21:00' => [
'expectedDate' => '2022-08-27T21:00:00+00:00',
'dateString' => 'August 27, 2022 - 21:00',
'dateString' => 'August 27, 2022 - 21:00',
],
'2021-09-19T14:06:03&#x2B;00:00' => [
'expectedDate' => '2021-09-19T14:06:03+00:00',
'dateString' => '2021-09-19T14:06:03&#x2B;00:00',
'dateString' => '2021-09-19T14:06:03&#x2B;00:00',
],
'Eastern Time timezone' => [
'expectedDate' => '2022-09-30T00:00:00-05:00',
'dateString' => 'September 30, 2022, 12:00 a.m. ET',
'dateString' => 'September 30, 2022, 12:00 a.m. ET',
],
'German date time string' => [
'expectedDate' => '2022-10-05T16:34:00+02:00',
'dateString' => '05 Okt 2022 16:34:00 +0200',
'dateString' => '05 Okt 2022 16:34:00 +0200',
],
'(Coordinated Universal Time)' => [
'expectedDate' => '2022-12-30T14:29:10+00:00',
'dateString' => 'Fri Dec 30 2022 14:29:10 GMT+0000 (Coordinated Universal Time)',
'dateString' => 'Fri Dec 30 2022 14:29:10 GMT+0000 (Coordinated Universal Time)',
],
'Double HTML encode' => [
'expectedDate' => '2015-05-22T08:48:00+12:00',
'dateString' => '2015-05-22T08:48:00&amp;#43;12:00'
'dateString' => '2015-05-22T08:48:00&amp;#43;12:00'
],
'2023-04-02\T17:22:42+05:30' => [
'expectedDate' => '2023-04-02T17:22:42+05:30',
'dateString' => '2023-04-02\T17:22:42+05:30'
'dateString' => '2023-04-02\T17:22:42+05:30'
],
'2025-03-07T08:54:14.341+01:00[Europe/Berlin]' => [
'expectedDate' => '2025-03-07T08:54:14+01:00',
'dateString' => '2025-03-07T08:54:14.341+01:00[Europe/Berlin]'
],
];
}
@ -151,9 +155,87 @@ class DateTimeFormatTest extends MockedTestCase
*/
public function testConvertRelative()
{
$now = DateTimeFormat::utcNow('U');
$now = DateTimeFormat::utcNow('U');
$date = DateTimeFormat::utc('now - 3 days', 'U');
$this->assertEquals(259200, $now - $date);
}
public function dataConvert()
{
return [
'unix timestamp' => [
'expected' => '2025-03-12 16:18:27',
's' => '1741796307',
],
'ATOM' => [
'expected' => '2022-06-02 15:58:35',
's' => '2022-06-02T16:58:35+01:00',
],
'COOKIE' => [
'expected' => '2022-06-02 14:58:35',
's' => 'Thursday, 02-Jun-2022 16:58:35 Africa/Cairo',
],
'ISO 8601/RFC 3339' => [
'expected' => '2022-06-02 13:58:35',
's' => '2022-06-02T16:58:35+0300',
],
'RFC 822/RFC 1036' => [
'expected' => '2022-06-02 12:58:35',
's' => 'Thu, 02 Jun 22 16:58:35 +0400',
],
'RFC 850' => [
'expected' => '2022-06-02 11:58:35',
's' => 'Thursday, 02-Jun-22 16:58:35 Indian/Kerguelen',
],
'RFC 1123/RFC 2822/RSS' => [
'expected' => '2022-06-02 10:58:35',
's' => 'Thu, 02 Jun 2022 16:58:35 +0600',
],
'RFC 3339/W3C' => [
'expected' => '2025-03-07 01:54:14',
's' => '2025-03-07T08:54:14+07:00',
],
'RFC 3339 extended' => [
'expected' => '2025-03-07 00:54:14',
's' => '2025-03-07T08:54:14.341+08:00',
],
'RFC 7231' => [
'expected' => '2022-06-02 07:58:35',
's' => 'Thu, 02 Jun 2022 16:58:35 Asia/Tokyo',
],
];
}
/**
* @dataProvider dataConvert
*/
public function testConvert($expected, string $s = 'now', string $tz_to = 'UTC', string $tz_from = 'UTC', string $format = DateTimeFormat::MYSQL)
{
$this->assertSame($expected, DateTimeFormat::convert($s, $tz_to, $tz_from, $format));
}
public function dataConvertNow()
{
return [
'now missing' => [
],
'now empty' => [
's' => '',
],
'now now' => [
's' => 'now',
],
];
}
/**
* @dataProvider dataConvertNow
*/
public function testConvertNow(string $s = 'now', string $tz_to = 'UTC', string $tz_from = 'UTC', string $format = DateTimeFormat::MYSQL)
{
$this->assertSame(date(DateTimeFormat::MYSQL), DateTimeFormat::convert($s, $tz_to, $tz_from, $format));
}
}