1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-10 15:14:14 +02:00

Upgrade phpstan, phpunit (#1923)

* Upgrade phpstan, phpunit

* Fix phpunit deprecation

* Fix hg tests

* Fix php-console tests

* Fix phpunit on 8.1

* Bump phpconsole
This commit is contained in:
Jordi Boggiano
2024-11-11 15:21:55 +01:00
committed by GitHub
parent 7af6f41cff
commit f43e3d5637
43 changed files with 210 additions and 153 deletions

View File

@@ -68,7 +68,6 @@ class ErrorHandlerTest extends \PHPUnit\Framework\TestCase
{
$ref = new \ReflectionClass(\get_class($instance));
$prop = $ref->getProperty($property);
$prop->setAccessible(true);
return $prop->getValue($instance);
}
@@ -91,6 +90,7 @@ class ErrorHandlerTest extends \PHPUnit\Framework\TestCase
$this->assertEquals($expectedFatalLevel, $this->getPrivatePropertyValue($errHandler, 'fatalLevel'));
}
#[WithoutErrorHandler]
public function testHandleException()
{
$logger = new Logger('test', [$handler = new TestHandler]);
@@ -106,12 +106,14 @@ class ErrorHandlerTest extends \PHPUnit\Framework\TestCase
$errHandler->registerExceptionHandler([], true);
$prop = $this->getPrivatePropertyValue($errHandler, 'previousExceptionHandler');
$this->assertTrue(\is_callable($prop));
restore_exception_handler();
restore_exception_handler();
}
public function testCodeToString()
{
$method = new \ReflectionMethod(ErrorHandler::class, 'codeToString');
$method->setAccessible(true);
$this->assertEquals('E_ERROR', $method->invokeArgs(null, [E_ERROR]));
$this->assertEquals('E_WARNING', $method->invokeArgs(null, [E_WARNING]));

View File

@@ -44,11 +44,9 @@ class MongoDBFormatterTest extends TestCase
$formatter = new MongoDBFormatter($traceDepth, $traceAsString);
$reflTrace = new \ReflectionProperty($formatter, 'exceptionTraceAsString');
$reflTrace->setAccessible(true);
$this->assertEquals($expectedTraceAsString, $reflTrace->getValue($formatter));
$reflDepth = new \ReflectionProperty($formatter, 'maxNestingLevel');
$reflDepth->setAccessible(true);
$this->assertEquals($expectedTraceDepth, $reflDepth->getValue($formatter));
}

View File

@@ -220,7 +220,6 @@ class NormalizerFormatterTest extends TestCase
$formatter = new NormalizerFormatter();
$reflMethod = new \ReflectionMethod($formatter, 'toJson');
$reflMethod->setAccessible(true);
$res = $reflMethod->invoke($formatter, [$foo, $bar], true);
restore_error_handler();
@@ -255,7 +254,6 @@ class NormalizerFormatterTest extends TestCase
$formatter = new NormalizerFormatter();
$reflMethod = new \ReflectionMethod($formatter, 'toJson');
$reflMethod->setAccessible(true);
$res = $reflMethod->invoke($formatter, [$resource], true);
restore_error_handler();
@@ -299,7 +297,6 @@ class NormalizerFormatterTest extends TestCase
{
$formatter = new NormalizerFormatter();
$reflMethod = new \ReflectionMethod($formatter, 'toJson');
$reflMethod->setAccessible(true);
// send an invalid unicode sequence as a object that can't be cleaned
$record = new \stdClass;
@@ -312,7 +309,6 @@ class NormalizerFormatterTest extends TestCase
{
$formatter = new NormalizerFormatter();
$reflMethod = new \ReflectionMethod($formatter, 'toJson');
$reflMethod->setAccessible(true);
$res = $reflMethod->invoke($formatter, ['message' => "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE"]);

View File

@@ -27,7 +27,6 @@ class BrowserConsoleHandlerTest extends TestCase
protected function generateScript()
{
$reflMethod = new \ReflectionMethod('Monolog\Handler\BrowserConsoleHandler', 'generateScript');
$reflMethod->setAccessible(true);
return $reflMethod->invoke(null);
}

View File

@@ -39,6 +39,13 @@ class FleepHookHandlerTest extends TestCase
$this->handler = new FleepHookHandler(self::TOKEN);
}
public function tearDown(): void
{
parent::tearDown();
unset($this->handler);
}
/**
* @covers ::__construct
*/

View File

@@ -40,6 +40,7 @@ class FlowdockHandlerTest extends TestCase
parent::tearDown();
unset($this->res);
unset($this->handler);
}
public function testWriteHeader()
@@ -73,7 +74,6 @@ class FlowdockHandlerTest extends TestCase
->getMock();
$reflectionProperty = new \ReflectionProperty('Monolog\Handler\SocketHandler', 'connectionString');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->handler, 'localhost:1234');
$this->handler->expects($this->any())

View File

@@ -36,6 +36,7 @@ class HandlerWrapperTest extends TestCase
parent::tearDown();
unset($this->wrapper);
unset($this->handler);
}
public static function trueFalseDataProvider(): array

View File

@@ -33,6 +33,7 @@ class InsightOpsHandlerTest extends TestCase
parent::tearDown();
unset($this->resource);
unset($this->handler);
}
public function testWriteContent()
@@ -68,7 +69,6 @@ class InsightOpsHandlerTest extends TestCase
->getMock();
$reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->handler, 'localhost:1234');
$this->handler->expects($this->any())

View File

@@ -32,6 +32,7 @@ class LogEntriesHandlerTest extends TestCase
parent::tearDown();
unset($this->res);
unset($this->handler);
}
public function testWriteContent()
@@ -72,7 +73,6 @@ class LogEntriesHandlerTest extends TestCase
->getMock();
$reflectionProperty = new \ReflectionProperty('Monolog\Handler\SocketHandler', 'connectionString');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->handler, 'localhost:1234');
$this->handler->expects($this->any())

View File

@@ -32,6 +32,7 @@ class LogmaticHandlerTest extends TestCase
parent::tearDown();
unset($this->res);
unset($this->handler);
}
public function testWriteContent()
@@ -72,7 +73,6 @@ class LogmaticHandlerTest extends TestCase
->getMock();
$reflectionProperty = new \ReflectionProperty('Monolog\Handler\SocketHandler', 'connectionString');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->handler, 'localhost:1234');
$this->handler->expects($this->any())

View File

@@ -21,6 +21,7 @@ use PhpConsole\Dispatcher\Debug as DebugDispatcher;
use PhpConsole\Dispatcher\Errors as ErrorDispatcher;
use PhpConsole\Handler as VendorPhpConsoleHandler;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
use PHPUnit\Framework\MockObject\MockObject;
/**
@@ -169,6 +170,7 @@ class PHPConsoleHandlerTest extends TestCase
}
}
#[WithoutErrorHandler]
public function testError($classesPartialsTraceIgnore = null)
{
$code = E_USER_NOTICE;
@@ -182,10 +184,12 @@ class PHPConsoleHandlerTest extends TestCase
$this->equalTo($line),
$classesPartialsTraceIgnore ?: $this->equalTo($this->getHandlerDefaultOption('classesPartialsTraceIgnore'))
);
$errorHandler = ErrorHandler::register($this->initLogger($classesPartialsTraceIgnore ? ['classesPartialsTraceIgnore' => $classesPartialsTraceIgnore] : []), false);
$errorHandler = ErrorHandler::register($this->initLogger($classesPartialsTraceIgnore ? ['classesPartialsTraceIgnore' => $classesPartialsTraceIgnore] : []), false, false);
$errorHandler->registerErrorHandler([], false, E_USER_WARNING);
$reflMethod = new \ReflectionMethod($errorHandler, 'handleError');
$reflMethod->invoke($errorHandler, $code, $message, $file, $line);
restore_error_handler();
}
public function testException()
@@ -215,11 +219,6 @@ class PHPConsoleHandlerTest extends TestCase
$this->initLogger(['enabled' => false])->debug('test');
}
public function testOptionClassesPartialsTraceIgnore()
{
$this->testError(['Class', 'Namespace\\']);
}
public function testOptionDebugTagsKeysInContext()
{
$this->testDebugTags(['key1', 'key2']);
@@ -232,6 +231,11 @@ class PHPConsoleHandlerTest extends TestCase
}));
$this->assertEquals([VendorPhpConsoleHandler::getInstance(), 'handleException'], set_exception_handler(function () {
}));
restore_exception_handler();
restore_error_handler();
restore_exception_handler();
restore_error_handler();
}
public static function provideConnectorMethodsOptionsSets()

View File

@@ -22,7 +22,7 @@ class ProcessHandlerTest extends TestCase
*
* @var string
*/
const DUMMY_COMMAND = 'echo';
const DUMMY_COMMAND = 'php -r "echo fgets(STDIN);"';
/**
* @covers Monolog\Handler\ProcessHandler::__construct
@@ -42,7 +42,6 @@ class ProcessHandlerTest extends TestCase
$mockBuilder = $this->getMockBuilder('Monolog\Handler\ProcessHandler');
$mockBuilder->onlyMethods(['writeProcessInput']);
// using echo as command, as it is most probably available
$mockBuilder->setConstructorArgs([self::DUMMY_COMMAND]);
$handler = $mockBuilder->getMock();
@@ -163,7 +162,6 @@ class ProcessHandlerTest extends TestCase
{
$mockBuilder = $this->getMockBuilder('Monolog\Handler\ProcessHandler');
$mockBuilder->onlyMethods(['readProcessErrors']);
// using echo as command, as it is most probably available
$mockBuilder->setConstructorArgs([self::DUMMY_COMMAND]);
$handler = $mockBuilder->getMock();
@@ -184,7 +182,6 @@ class ProcessHandlerTest extends TestCase
{
$class = new \ReflectionClass('Monolog\Handler\ProcessHandler');
$property = $class->getProperty('process');
$property->setAccessible(true);
$handler = new ProcessHandler(self::DUMMY_COMMAND);
$handler->handle($this->getRecord(Level::Warning, '21 is only the half truth'));

View File

@@ -32,6 +32,7 @@ class PushoverHandlerTest extends TestCase
parent::tearDown();
unset($this->res);
unset($this->handler);
}
public function testWriteHeader()
@@ -131,7 +132,6 @@ class PushoverHandlerTest extends TestCase
->getMock();
$reflectionProperty = new \ReflectionProperty('Monolog\Handler\SocketHandler', 'connectionString');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->handler, 'localhost:1234');
$this->handler->expects($this->any())

View File

@@ -42,6 +42,7 @@ class SlackHandlerTest extends TestCase
parent::tearDown();
unset($this->res);
unset($this->handler);
}
public function testWriteHeader()
@@ -139,7 +140,6 @@ class SlackHandlerTest extends TestCase
->getMock();
$reflectionProperty = new \ReflectionProperty('Monolog\Handler\SocketHandler', 'connectionString');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->handler, 'localhost:1234');
$this->handler->expects($this->any())

View File

@@ -32,6 +32,7 @@ class SocketHandlerTest extends TestCase
parent::tearDown();
unset($this->res);
unset($this->handler);
}
public function testInvalidHostname()

View File

@@ -263,7 +263,7 @@ STRING;
#[DataProvider('provideMemoryValues')]
public function testPreventOOMError($phpMemory, $expectedChunkSize): void
{
$previousValue = ini_set('memory_limit', $phpMemory);
$previousValue = @ini_set('memory_limit', $phpMemory);
if ($previousValue === false) {
$this->markTestSkipped('We could not set a memory limit that would trigger the error.');

View File

@@ -23,6 +23,13 @@ class TelegramBotHandlerTest extends TestCase
{
private TelegramBotHandler&MockObject $handler;
public function tearDown(): void
{
parent::tearDown();
unset($this->handler);
}
public function testSendTelegramRequest(): void
{
$this->createHandler();

View File

@@ -743,7 +743,6 @@ class LoggerTest extends TestCase
$getProperty = function ($object, $property) {
$reflectionProperty = new \ReflectionProperty(\get_class($object), $property);
$reflectionProperty->setAccessible(true);
return $reflectionProperty->getValue($object);
};

View File

@@ -15,6 +15,41 @@ use Monolog\Test\TestCase;
class MercurialProcessorTest extends TestCase
{
private string $oldCwd;
private string $testDir;
protected function setUp(): void
{
parent::setUp();
$this->oldCwd = getcwd();
$this->testDir = sys_get_temp_dir().'/monolog-processor-mercurial-test';
mkdir($this->testDir, recursive: true);
chdir($this->testDir);
}
protected function tearDown(): void
{
parent::tearDown();
chdir($this->oldCwd);
if (!file_exists($this->testDir)) {
return;
}
$items = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($this->testDir, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($items as $item) {
$item->isDir() ? rmdir((string) $item) : unlink((string) $item);
}
rmdir($this->testDir);
}
/**
* @covers Monolog\Processor\MercurialProcessor::__invoke
*/
@@ -31,12 +66,17 @@ class MercurialProcessorTest extends TestCase
return;
}
`hg init`;
exec('hg init');
exec('hg branch default');
touch('test.txt');
exec('hg add test.txt');
exec('hg commit -u foo -m "initial commit"');
$processor = new MercurialProcessor();
$record = $processor($this->getRecord());
$this->assertArrayHasKey('hg', $record->extra);
$this->assertTrue(!\is_array($record->extra['hg']['branch']));
$this->assertTrue(!\is_array($record->extra['hg']['revision']));
$this->assertSame('default', $record->extra['hg']['branch']);
$this->assertSame('0', $record->extra['hg']['revision']);
}
}

View File

@@ -77,7 +77,6 @@ class UtilsTest extends \PHPUnit_Framework_TestCase
public function testDetectAndCleanUtf8($in, $expect)
{
$reflMethod = new \ReflectionMethod(Utils::class, 'detectAndCleanUtf8');
$reflMethod->setAccessible(true);
$args = [&$in];
$reflMethod->invokeArgs(null, $args);
$this->assertSame($expect, $in);
@@ -102,35 +101,6 @@ class UtilsTest extends \PHPUnit_Framework_TestCase
];
}
#[DataProvider('providesPcreLastErrorMessage')]
public function testPcreLastErrorMessage(int $code, string $msg)
{
if (PHP_VERSION_ID >= 80000) {
$this->assertSame('No error', Utils::pcreLastErrorMessage($code));
return;
}
$this->assertEquals($msg, Utils::pcreLastErrorMessage($code));
}
/**
* @return array[]
*/
public static function providesPcreLastErrorMessage(): array
{
return [
[0, 'PREG_NO_ERROR'],
[1, 'PREG_INTERNAL_ERROR'],
[2, 'PREG_BACKTRACK_LIMIT_ERROR'],
[3, 'PREG_RECURSION_LIMIT_ERROR'],
[4, 'PREG_BAD_UTF8_ERROR'],
[5, 'PREG_BAD_UTF8_OFFSET_ERROR'],
[6, 'PREG_JIT_STACKLIMIT_ERROR'],
[-1, 'UNDEFINED_ERROR'],
];
}
public static function provideIniValuesToConvertToBytes()
{
return [