From b127292ee05ed58ee442aff172fb8d840ca8d60f Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 12 Apr 2024 17:26:33 +0200 Subject: [PATCH] Fix phpunit deprecations --- composer.json | 2 +- tests/Monolog/ErrorHandlerTest.php | 10 +++- tests/Monolog/Formatter/LineFormatterTest.php | 23 ++++---- .../Formatter/MongoDBFormatterTest.php | 10 +--- .../Monolog/Formatter/SyslogFormatterTest.php | 11 +--- tests/Monolog/Handler/AbstractHandlerTest.php | 8 +-- .../Handler/AbstractProcessingHandlerTest.php | 22 +++---- tests/Monolog/Handler/AmqpHandlerTest.php | 8 +-- .../Monolog/Handler/ChromePHPHandlerTest.php | 5 +- tests/Monolog/Handler/DynamoDbHandlerTest.php | 16 +---- tests/Monolog/Handler/ElasticaHandlerTest.php | 3 +- .../Handler/ElasticsearchHandlerTest.php | 18 ++---- tests/Monolog/Handler/FlowdockHandlerTest.php | 7 +-- tests/Monolog/Handler/HandlerWrapperTest.php | 22 +++---- .../Monolog/Handler/InsightOpsHandlerTest.php | 7 +-- .../Monolog/Handler/LogEntriesHandlerTest.php | 7 +-- tests/Monolog/Handler/LogmaticHandlerTest.php | 7 +-- tests/Monolog/Handler/MailHandlerTest.php | 15 +++-- tests/Monolog/Handler/MongoDBHandlerTest.php | 2 +- tests/Monolog/Handler/NoopHandlerTest.php | 9 +-- .../Monolog/Handler/PHPConsoleHandlerTest.php | 11 ++-- tests/Monolog/Handler/ProcessHandlerTest.php | 12 ++-- tests/Monolog/Handler/PsrHandlerTest.php | 5 +- tests/Monolog/Handler/PushoverHandlerTest.php | 7 +-- tests/Monolog/Handler/RedisHandlerTest.php | 50 ++++++++-------- .../Handler/RotatingFileHandlerTest.php | 21 ++----- .../Monolog/Handler/Slack/SlackRecordTest.php | 23 +++----- tests/Monolog/Handler/SlackHandlerTest.php | 12 ++-- tests/Monolog/Handler/SocketHandlerTest.php | 32 +++++----- tests/Monolog/Handler/StreamHandlerTest.php | 9 ++- tests/Monolog/Handler/TestHandlerTest.php | 5 +- .../Handler/ZendMonitorHandlerTest.php | 4 +- tests/Monolog/LoggerTest.php | 59 ++++++++++--------- .../Processor/ClosureContextProcessorTest.php | 5 +- .../Processor/PsrLogMessageProcessorTest.php | 5 +- tests/Monolog/PsrLogCompatTest.php | 7 +-- tests/Monolog/RegistryTest.php | 6 +- tests/Monolog/SignalHandlerTest.php | 9 +-- tests/Monolog/UtilsTest.php | 28 +++------ 39 files changed, 220 insertions(+), 302 deletions(-) diff --git a/composer.json b/composer.json index 4ccd07211..30a99d7af 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ "phpstan/phpstan": "^1.9", "phpstan/phpstan-deprecation-rules": "^1.0", "phpstan/phpstan-strict-rules": "^1.4", - "phpunit/phpunit": "^10.1", + "phpunit/phpunit": "^10.5.17", "predis/predis": "^1.1 || ^2", "ruflin/elastica": "^7", "symfony/mailer": "^5.4 || ^6", diff --git a/tests/Monolog/ErrorHandlerTest.php b/tests/Monolog/ErrorHandlerTest.php index 6ff6431f4..98c0e9960 100644 --- a/tests/Monolog/ErrorHandlerTest.php +++ b/tests/Monolog/ErrorHandlerTest.php @@ -12,6 +12,8 @@ namespace Monolog; use Monolog\Handler\TestHandler; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\WithoutErrorHandler; use Psr\Log\LogLevel; class ErrorHandlerTest extends \PHPUnit\Framework\TestCase @@ -23,6 +25,7 @@ public function testRegister() $this->assertInstanceOf(ErrorHandler::class, ErrorHandler::register($logger, false, false, false)); } + #[WithoutErrorHandler] public function testHandleError() { $logger = new Logger('test', [$handler = new TestHandler]); @@ -44,7 +47,9 @@ public function testHandleError() $this->assertTrue($handler->hasErrorRecords()); trigger_error('Foo', E_USER_NOTICE); $this->assertCount(2, $handler->getRecords()); + // check that the remapping of notice to emergency above worked $this->assertTrue($handler->hasEmergencyRecords()); + $this->assertFalse($handler->hasNoticeRecords()); } finally { // restore previous handler set_error_handler($phpunitHandler); @@ -68,9 +73,8 @@ protected function getPrivatePropertyValue($instance, $property) return $prop->getValue($instance); } - /** - * @dataProvider fatalHandlerProvider - */ + #[DataProvider('fatalHandlerProvider')] + #[WithoutErrorHandler] public function testFatalHandler( $level, $reservedMemorySize, diff --git a/tests/Monolog/Formatter/LineFormatterTest.php b/tests/Monolog/Formatter/LineFormatterTest.php index e82c55dcc..09b4f7673 100644 --- a/tests/Monolog/Formatter/LineFormatterTest.php +++ b/tests/Monolog/Formatter/LineFormatterTest.php @@ -13,6 +13,7 @@ use Monolog\Test\TestCase; use Monolog\Level; +use PHPUnit\Framework\Attributes\DataProvider; use RuntimeException; /** @@ -291,9 +292,7 @@ public function testIndentStackTraces(): void $this->assertStringContainsString(' #1', $message); } - /** - * @dataProvider providerMaxLevelNameLength - */ + #[DataProvider('providerMaxLevelNameLength')] public function testMaxLevelNameLength(?int $maxLength, Level $logLevel, string $expectedLevelName): void { $formatter = new LineFormatter(); @@ -307,21 +306,21 @@ public static function providerMaxLevelNameLength(): array { return [ 'info_no_max_length' => [ - 'max_length' => null, - 'level' => Level::Info, - 'expected_level_name' => 'INFO', + 'maxLength' => null, + 'logLevel' => Level::Info, + 'expectedLevelName' => 'INFO', ], 'error_max_length_3' => [ - 'max_length' => 3, - 'level' => Level::Error, - 'expected_level_name' => 'ERR', + 'maxLength' => 3, + 'logLevel' => Level::Error, + 'expectedLevelName' => 'ERR', ], 'debug_max_length_2' => [ - 'max_length' => 2, - 'level' => Level::Debug, - 'expected_level_name' => 'DE', + 'maxLength' => 2, + 'logLevel' => Level::Debug, + 'expectedLevelName' => 'DE', ], ]; } diff --git a/tests/Monolog/Formatter/MongoDBFormatterTest.php b/tests/Monolog/Formatter/MongoDBFormatterTest.php index a1b1fce0f..c01e0260d 100644 --- a/tests/Monolog/Formatter/MongoDBFormatterTest.php +++ b/tests/Monolog/Formatter/MongoDBFormatterTest.php @@ -16,6 +16,7 @@ use MongoDB\BSON\UTCDateTime; use Monolog\Level; use Monolog\Test\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; /** * @author Florian Plattner @@ -37,14 +38,7 @@ public static function constructArgumentProvider() ]; } - /** - * @param $traceDepth - * @param $traceAsString - * @param $expectedTraceDepth - * @param $expectedTraceAsString - * - * @dataProvider constructArgumentProvider - */ + #[DataProvider('constructArgumentProvider')] public function testConstruct($traceDepth, $traceAsString, $expectedTraceDepth, $expectedTraceAsString) { $formatter = new MongoDBFormatter($traceDepth, $traceAsString); diff --git a/tests/Monolog/Formatter/SyslogFormatterTest.php b/tests/Monolog/Formatter/SyslogFormatterTest.php index 7d0fdc615..b9f277541 100644 --- a/tests/Monolog/Formatter/SyslogFormatterTest.php +++ b/tests/Monolog/Formatter/SyslogFormatterTest.php @@ -14,23 +14,16 @@ use DateTimeImmutable; use Monolog\Level; use Monolog\LogRecord; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class SyslogFormatterTest extends TestCase { /** - * @dataProvider formatDataProvider - * - * @param string $expected - * @param DateTimeImmutable $dateTime - * @param string $channel - * @param Level $level - * @param string $message - * @param string|null $appName * @param mixed[] $context * @param mixed[] $extra - * @return void */ + #[DataProvider('formatDataProvider')] public function testFormat( string $expected, DateTimeImmutable $dateTime, diff --git a/tests/Monolog/Handler/AbstractHandlerTest.php b/tests/Monolog/Handler/AbstractHandlerTest.php index 58de1dd9f..4520e9893 100644 --- a/tests/Monolog/Handler/AbstractHandlerTest.php +++ b/tests/Monolog/Handler/AbstractHandlerTest.php @@ -25,7 +25,7 @@ class AbstractHandlerTest extends TestCase */ public function testConstructAndGetSet() { - $handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractHandler', [Level::Warning, false]); + $handler = $this->getMockBuilder('Monolog\Handler\AbstractHandler')->setConstructorArgs([Level::Warning, false])->onlyMethods(['handle'])->getMock(); $this->assertEquals(Level::Warning, $handler->getLevel()); $this->assertEquals(false, $handler->getBubble()); @@ -40,7 +40,7 @@ public function testConstructAndGetSet() */ public function testHandleBatch() { - $handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractHandler'); + $handler = $this->createPartialMock('Monolog\Handler\AbstractHandler', ['handle']); $handler->expects($this->exactly(2)) ->method('handle'); $handler->handleBatch([$this->getRecord(), $this->getRecord()]); @@ -51,7 +51,7 @@ public function testHandleBatch() */ public function testIsHandling() { - $handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractHandler', [Level::Warning, false]); + $handler = $this->getMockBuilder('Monolog\Handler\AbstractHandler')->setConstructorArgs([Level::Warning, false])->onlyMethods(['handle'])->getMock(); $this->assertTrue($handler->isHandling($this->getRecord())); $this->assertFalse($handler->isHandling($this->getRecord(Level::Debug))); } @@ -61,7 +61,7 @@ public function testIsHandling() */ public function testHandlesPsrStyleLevels() { - $handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractHandler', ['warning', false]); + $handler = $this->getMockBuilder('Monolog\Handler\AbstractHandler')->setConstructorArgs(['warning', false])->onlyMethods(['handle'])->getMock(); $this->assertFalse($handler->isHandling($this->getRecord(Level::Debug))); $handler->setLevel('debug'); $this->assertTrue($handler->isHandling($this->getRecord(Level::Debug))); diff --git a/tests/Monolog/Handler/AbstractProcessingHandlerTest.php b/tests/Monolog/Handler/AbstractProcessingHandlerTest.php index 6fc3c38b8..d9479c018 100644 --- a/tests/Monolog/Handler/AbstractProcessingHandlerTest.php +++ b/tests/Monolog/Handler/AbstractProcessingHandlerTest.php @@ -24,7 +24,7 @@ class AbstractProcessingHandlerTest extends TestCase */ public function testConstructAndGetSet() { - $handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractProcessingHandler', [Level::Warning, false]); + $handler = $this->getMockBuilder('Monolog\Handler\AbstractProcessingHandler')->setConstructorArgs([Level::Warning, false])->onlyMethods(['write'])->getMock(); $handler->setFormatter($formatter = new LineFormatter); $this->assertSame($formatter, $handler->getFormatter()); } @@ -34,7 +34,7 @@ public function testConstructAndGetSet() */ public function testHandleLowerLevelMessage() { - $handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractProcessingHandler', [Level::Warning, true]); + $handler = $this->getMockBuilder('Monolog\Handler\AbstractProcessingHandler')->setConstructorArgs([Level::Warning, true])->onlyMethods(['write'])->getMock(); $this->assertFalse($handler->handle($this->getRecord(Level::Debug))); } @@ -43,7 +43,7 @@ public function testHandleLowerLevelMessage() */ public function testHandleBubbling() { - $handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractProcessingHandler', [Level::Debug, true]); + $handler = $this->getMockBuilder('Monolog\Handler\AbstractProcessingHandler')->setConstructorArgs([Level::Debug, true])->onlyMethods(['write'])->getMock(); $this->assertFalse($handler->handle($this->getRecord())); } @@ -52,7 +52,7 @@ public function testHandleBubbling() */ public function testHandleNotBubbling() { - $handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractProcessingHandler', [Level::Debug, false]); + $handler = $this->getMockBuilder('Monolog\Handler\AbstractProcessingHandler')->setConstructorArgs([Level::Debug, false])->onlyMethods(['write'])->getMock(); $this->assertTrue($handler->handle($this->getRecord())); } @@ -61,7 +61,7 @@ public function testHandleNotBubbling() */ public function testHandleIsFalseWhenNotHandled() { - $handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractProcessingHandler', [Level::Warning, false]); + $handler = $this->getMockBuilder('Monolog\Handler\AbstractProcessingHandler')->setConstructorArgs([Level::Warning, false])->onlyMethods(['write'])->getMock(); $this->assertTrue($handler->handle($this->getRecord())); $this->assertFalse($handler->handle($this->getRecord(Level::Debug))); } @@ -71,7 +71,7 @@ public function testHandleIsFalseWhenNotHandled() */ public function testProcessRecord() { - $handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractProcessingHandler'); + $handler = $this->createPartialMock('Monolog\Handler\AbstractProcessingHandler', ['write']); $handler->pushProcessor(new WebProcessor([ 'REQUEST_URI' => '', 'REQUEST_METHOD' => '', @@ -82,9 +82,9 @@ public function testProcessRecord() $handledRecord = null; $handler->expects($this->once()) ->method('write') - ->will($this->returnCallback(function ($record) use (&$handledRecord) { + ->willReturnCallback(function ($record) use (&$handledRecord) { $handledRecord = $record; - })) + }) ; $handler->handle($this->getRecord()); $this->assertEquals(6, count($handledRecord['extra'])); @@ -96,7 +96,7 @@ public function testProcessRecord() */ public function testPushPopProcessor() { - $logger = $this->getMockForAbstractClass('Monolog\Handler\AbstractProcessingHandler'); + $logger = $this->createPartialMock('Monolog\Handler\AbstractProcessingHandler', ['write']); $processor1 = new WebProcessor; $processor2 = new WebProcessor; @@ -116,7 +116,7 @@ public function testPushPopProcessor() */ public function testPushProcessorWithNonCallable() { - $handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractProcessingHandler'); + $handler = $this->createPartialMock('Monolog\Handler\AbstractProcessingHandler', ['write']); $this->expectException(\TypeError::class); @@ -129,7 +129,7 @@ public function testPushProcessorWithNonCallable() */ public function testGetFormatterInitializesDefault() { - $handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractProcessingHandler'); + $handler = $this->createPartialMock('Monolog\Handler\AbstractProcessingHandler', ['write']); $this->assertInstanceOf(LineFormatter::class, $handler->getFormatter()); } } diff --git a/tests/Monolog/Handler/AmqpHandlerTest.php b/tests/Monolog/Handler/AmqpHandlerTest.php index 54cb90ef3..f60e787ee 100644 --- a/tests/Monolog/Handler/AmqpHandlerTest.php +++ b/tests/Monolog/Handler/AmqpHandlerTest.php @@ -39,9 +39,9 @@ public function testHandleAmqpExt() $exchange->expects($this->any()) ->method('publish') - ->will($this->returnCallback(function ($message, $routing_key, $flags = 0, $attributes = []) use (&$messages) { + ->willReturnCallback(function ($message, $routing_key, $flags = 0, $attributes = []) use (&$messages) { $messages[] = [$message, $routing_key, $flags, $attributes]; - })) + }) ; $handler = new AmqpHandler($exchange); @@ -96,9 +96,9 @@ public function testHandlePhpAmqpLib() $exchange->expects($this->any()) ->method('basic_publish') - ->will($this->returnCallback(function (AMQPMessage $msg, $exchange = "", $routing_key = "", $mandatory = false, $immediate = false, $ticket = null) use (&$messages) { + ->willReturnCallback(function (AMQPMessage $msg, $exchange = "", $routing_key = "", $mandatory = false, $immediate = false, $ticket = null) use (&$messages) { $messages[] = [$msg, $exchange, $routing_key, $mandatory, $immediate, $ticket]; - })) + }) ; $handler = new AmqpHandler($exchange, 'log'); diff --git a/tests/Monolog/Handler/ChromePHPHandlerTest.php b/tests/Monolog/Handler/ChromePHPHandlerTest.php index 4b2f29537..b4812d0f7 100644 --- a/tests/Monolog/Handler/ChromePHPHandlerTest.php +++ b/tests/Monolog/Handler/ChromePHPHandlerTest.php @@ -13,6 +13,7 @@ use Monolog\Test\TestCase; use Monolog\Level; +use PHPUnit\Framework\Attributes\DataProvider; /** * @covers Monolog\Handler\ChromePHPHandler @@ -25,9 +26,7 @@ protected function setUp(): void $_SERVER['HTTP_USER_AGENT'] = 'Monolog Test; Chrome/1.0'; } - /** - * @dataProvider agentsProvider - */ + #[DataProvider('agentsProvider')] public function testHeaders($agent) { $_SERVER['HTTP_USER_AGENT'] = $agent; diff --git a/tests/Monolog/Handler/DynamoDbHandlerTest.php b/tests/Monolog/Handler/DynamoDbHandlerTest.php index 33cf62c39..53c3a9cd5 100644 --- a/tests/Monolog/Handler/DynamoDbHandlerTest.php +++ b/tests/Monolog/Handler/DynamoDbHandlerTest.php @@ -30,19 +30,10 @@ public function setUp(): void $this->isV3 = defined('Aws\Sdk::VERSION') && version_compare(\Aws\Sdk::VERSION, '3.0', '>='); $implementedMethods = ['__call']; - $absentMethods = []; - if (method_exists(DynamoDbClient::class, 'formatAttributes')) { - $implementedMethods[] = 'formatAttributes'; - } else { - $absentMethods[] = 'formatAttributes'; - } $clientMockBuilder = $this->getMockBuilder(DynamoDbClient::class) ->onlyMethods($implementedMethods) ->disableOriginalConstructor(); - if ($absentMethods) { - $clientMockBuilder->addMethods($absentMethods); - } $this->client = $clientMockBuilder->getMock(); } @@ -78,12 +69,7 @@ public function testHandle() ->expects($this->once()) ->method('format') ->with($record) - ->will($this->returnValue($formatted)); - $this->client - ->expects($this->isV3 ? $this->never() : $this->once()) - ->method('formatAttributes') - ->with($this->isType('array')) - ->will($this->returnValue($formatted)); + ->willReturn($formatted); $this->client ->expects($this->once()) ->method('__call') diff --git a/tests/Monolog/Handler/ElasticaHandlerTest.php b/tests/Monolog/Handler/ElasticaHandlerTest.php index a97a8cf49..e8d0f5df7 100644 --- a/tests/Monolog/Handler/ElasticaHandlerTest.php +++ b/tests/Monolog/Handler/ElasticaHandlerTest.php @@ -18,6 +18,7 @@ use Elastica\Client; use Elastica\Request; use Elastica\Response; +use PHPUnit\Framework\Attributes\DataProvider; /** * @group Elastica @@ -128,8 +129,8 @@ public function testOptions() /** * @covers Monolog\Handler\ElasticaHandler::bulkSend - * @dataProvider providerTestConnectionErrors */ + #[DataProvider('providerTestConnectionErrors')] public function testConnectionErrors($ignore, $expectedError) { $clientOpts = ['host' => '127.0.0.1', 'port' => 1]; diff --git a/tests/Monolog/Handler/ElasticsearchHandlerTest.php b/tests/Monolog/Handler/ElasticsearchHandlerTest.php index cd2cf7b92..b63b4ab0a 100644 --- a/tests/Monolog/Handler/ElasticsearchHandlerTest.php +++ b/tests/Monolog/Handler/ElasticsearchHandlerTest.php @@ -19,10 +19,13 @@ use Elastic\Elasticsearch\Client as Client8; use Elasticsearch\ClientBuilder; use Elastic\Elasticsearch\ClientBuilder as ClientBuilder8; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; /** * @group Elasticsearch */ +#[CoversClass(ElasticsearchHandler::class)] class ElasticsearchHandlerTest extends TestCase { protected Client|Client8 $client; @@ -57,9 +60,6 @@ public function tearDown(): void unset($this->client); } - /** - * @covers Monolog\Handler\ElasticsearchHandler::setFormatter - */ public function testSetFormatter() { $handler = new ElasticsearchHandler($this->client); @@ -70,9 +70,6 @@ public function testSetFormatter() $this->assertEquals('type_new', $handler->getFormatter()->getType()); } - /** - * @covers Monolog\Handler\ElasticsearchHandler::setFormatter - */ public function testSetFormatterInvalid() { $handler = new ElasticsearchHandler($this->client); @@ -84,10 +81,6 @@ public function testSetFormatterInvalid() $handler->setFormatter($formatter); } - /** - * @covers Monolog\Handler\ElasticsearchHandler::__construct - * @covers Monolog\Handler\ElasticsearchHandler::getOptions - */ public function testOptions() { $expected = [ @@ -105,10 +98,7 @@ public function testOptions() $this->assertEquals($expected, $handler->getOptions()); } - /** - * @covers Monolog\Handler\ElasticsearchHandler::bulkSend - * @dataProvider providerTestConnectionErrors - */ + #[DataProvider('providerTestConnectionErrors')] public function testConnectionErrors($ignore, $expectedError) { $hosts = ['http://127.0.0.1:1']; diff --git a/tests/Monolog/Handler/FlowdockHandlerTest.php b/tests/Monolog/Handler/FlowdockHandlerTest.php index 0623ef1ff..d7d28189c 100644 --- a/tests/Monolog/Handler/FlowdockHandlerTest.php +++ b/tests/Monolog/Handler/FlowdockHandlerTest.php @@ -78,13 +78,12 @@ private function createHandler($token = 'myToken') $this->handler->expects($this->any()) ->method('fsockopen') - ->will($this->returnValue($this->res)); + ->willReturn($this->res); $this->handler->expects($this->any()) ->method('streamSetTimeout') - ->will($this->returnValue(true)); + ->willReturn(true); $this->handler->expects($this->any()) - ->method('closeSocket') - ->will($this->returnValue(true)); + ->method('closeSocket'); $this->handler->setFormatter(new FlowdockFormatter('test_source', 'source@test.com')); } diff --git a/tests/Monolog/Handler/HandlerWrapperTest.php b/tests/Monolog/Handler/HandlerWrapperTest.php index f5c924640..166fcd0bf 100644 --- a/tests/Monolog/Handler/HandlerWrapperTest.php +++ b/tests/Monolog/Handler/HandlerWrapperTest.php @@ -12,6 +12,7 @@ namespace Monolog\Handler; use Monolog\Test\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\MockObject\MockObject; /** @@ -45,11 +46,8 @@ public static function trueFalseDataProvider(): array ]; } - /** - * @param $result - * @dataProvider trueFalseDataProvider - */ - public function testIsHandling($result) + #[DataProvider('trueFalseDataProvider')] + public function testIsHandling(bool $result) { $record = $this->getRecord(); $this->handler->expects($this->once()) @@ -60,11 +58,8 @@ public function testIsHandling($result) $this->assertEquals($result, $this->wrapper->isHandling($record)); } - /** - * @param $result - * @dataProvider trueFalseDataProvider - */ - public function testHandle($result) + #[DataProvider('trueFalseDataProvider')] + public function testHandle(bool $result) { $record = $this->getRecord(); $this->handler->expects($this->once()) @@ -75,11 +70,8 @@ public function testHandle($result) $this->assertEquals($result, $this->wrapper->handle($record)); } - /** - * @param $result - * @dataProvider trueFalseDataProvider - */ - public function testHandleBatch($result) + #[DataProvider('trueFalseDataProvider')] + public function testHandleBatch(bool $result) { $records = $this->getMultipleRecords(); $this->handler->expects($this->once()) diff --git a/tests/Monolog/Handler/InsightOpsHandlerTest.php b/tests/Monolog/Handler/InsightOpsHandlerTest.php index b867a0b83..99a53536d 100644 --- a/tests/Monolog/Handler/InsightOpsHandlerTest.php +++ b/tests/Monolog/Handler/InsightOpsHandlerTest.php @@ -73,12 +73,11 @@ private function createHandler() $this->handler->expects($this->any()) ->method('fsockopen') - ->will($this->returnValue($this->resource)); + ->willReturn($this->resource); $this->handler->expects($this->any()) ->method('streamSetTimeout') - ->will($this->returnValue(true)); + ->willReturn(true); $this->handler->expects($this->any()) - ->method('closeSocket') - ->will($this->returnValue(true)); + ->method('closeSocket'); } } diff --git a/tests/Monolog/Handler/LogEntriesHandlerTest.php b/tests/Monolog/Handler/LogEntriesHandlerTest.php index fa1d44840..629b2349a 100644 --- a/tests/Monolog/Handler/LogEntriesHandlerTest.php +++ b/tests/Monolog/Handler/LogEntriesHandlerTest.php @@ -77,12 +77,11 @@ private function createHandler() $this->handler->expects($this->any()) ->method('fsockopen') - ->will($this->returnValue($this->res)); + ->willReturn($this->res); $this->handler->expects($this->any()) ->method('streamSetTimeout') - ->will($this->returnValue(true)); + ->willReturn(true); $this->handler->expects($this->any()) - ->method('closeSocket') - ->will($this->returnValue(true)); + ->method('closeSocket'); } } diff --git a/tests/Monolog/Handler/LogmaticHandlerTest.php b/tests/Monolog/Handler/LogmaticHandlerTest.php index e35f86acd..a65b52521 100644 --- a/tests/Monolog/Handler/LogmaticHandlerTest.php +++ b/tests/Monolog/Handler/LogmaticHandlerTest.php @@ -77,12 +77,11 @@ private function createHandler() $this->handler->expects($this->any()) ->method('fsockopen') - ->will($this->returnValue($this->res)); + ->willReturn($this->res); $this->handler->expects($this->any()) ->method('streamSetTimeout') - ->will($this->returnValue(true)); + ->willReturn(true); $this->handler->expects($this->any()) - ->method('closeSocket') - ->will($this->returnValue(true)); + ->method('closeSocket'); } } diff --git a/tests/Monolog/Handler/MailHandlerTest.php b/tests/Monolog/Handler/MailHandlerTest.php index 3c4a5805a..ebf5c11df 100644 --- a/tests/Monolog/Handler/MailHandlerTest.php +++ b/tests/Monolog/Handler/MailHandlerTest.php @@ -21,11 +21,11 @@ class MailHandlerTest extends TestCase */ public function testHandleBatch() { - $formatter = $this->createMock('Monolog\\Formatter\\FormatterInterface'); + $formatter = $this->createMock('Monolog\Formatter\FormatterInterface'); $formatter->expects($this->once()) ->method('formatBatch'); // Each record is formatted - $handler = $this->getMockForAbstractClass('Monolog\\Handler\\MailHandler', [], '', true, true, true, ['send', 'write']); + $handler = $this->createPartialMock('Monolog\Handler\MailHandler', ['send', 'write']); $handler->expects($this->once()) ->method('send'); $handler->expects($this->never()) @@ -47,7 +47,7 @@ public function testHandleBatchNotSendsMailIfMessagesAreBelowLevel() $this->getRecord(Level::Info, 'information'), ]; - $handler = $this->getMockForAbstractClass('Monolog\\Handler\\MailHandler'); + $handler = $this->createPartialMock('Monolog\Handler\MailHandler', ['send']); $handler->expects($this->never()) ->method('send'); $handler->setLevel(Level::Error); @@ -60,16 +60,15 @@ public function testHandleBatchNotSendsMailIfMessagesAreBelowLevel() */ public function testHandle() { - $handler = $this->getMockForAbstractClass('Monolog\\Handler\\MailHandler'); + $handler = $this->createPartialMock('Monolog\Handler\MailHandler', ['send']); $handler->setFormatter(new \Monolog\Formatter\LineFormatter); - $record = $this->getRecord(); - $records = [$record]; - $records[0]['formatted'] = '['.$record->datetime.'] test.WARNING: test [] []'."\n"; + $record = $this->getRecord(message: 'test handle'); + $record->formatted = '['.$record->datetime.'] test.WARNING: test handle [] []'."\n"; $handler->expects($this->once()) ->method('send') - ->with($records[0]['formatted'], $records); + ->with($record->formatted, [$record]); $handler->handle($record); } diff --git a/tests/Monolog/Handler/MongoDBHandlerTest.php b/tests/Monolog/Handler/MongoDBHandlerTest.php index 6f293f11e..d02d32f6b 100644 --- a/tests/Monolog/Handler/MongoDBHandlerTest.php +++ b/tests/Monolog/Handler/MongoDBHandlerTest.php @@ -43,7 +43,7 @@ public function testHandleWithLibraryClient() $mongodb->expects($this->once()) ->method('selectCollection') ->with('db', 'collection') - ->will($this->returnValue($collection)); + ->willReturn($collection); $record = $this->getRecord(); $expected = $record->toArray(); diff --git a/tests/Monolog/Handler/NoopHandlerTest.php b/tests/Monolog/Handler/NoopHandlerTest.php index 0546c0c9a..dd47a8713 100644 --- a/tests/Monolog/Handler/NoopHandlerTest.php +++ b/tests/Monolog/Handler/NoopHandlerTest.php @@ -13,24 +13,21 @@ use Monolog\Level; use Monolog\Test\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; /** * @covers Monolog\Handler\NoopHandler::handle */ class NoopHandlerTest extends TestCase { - /** - * @dataProvider logLevelsProvider - */ + #[DataProvider('logLevelsProvider')] public function testIsHandling(Level $level) { $handler = new NoopHandler(); $this->assertTrue($handler->isHandling($this->getRecord($level))); } - /** - * @dataProvider logLevelsProvider - */ + #[DataProvider('logLevelsProvider')] public function testHandle(Level $level) { $handler = new NoopHandler(); diff --git a/tests/Monolog/Handler/PHPConsoleHandlerTest.php b/tests/Monolog/Handler/PHPConsoleHandlerTest.php index 3dc3d18e9..e1ceb829a 100644 --- a/tests/Monolog/Handler/PHPConsoleHandlerTest.php +++ b/tests/Monolog/Handler/PHPConsoleHandlerTest.php @@ -20,6 +20,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\MockObject\MockObject; /** @@ -99,7 +100,7 @@ protected function initConnectorMock() $connector->expects($this->any()) ->method('isActiveClient') - ->will($this->returnValue(true)); + ->willReturn(true); return $connector; } @@ -246,9 +247,7 @@ public static function provideConnectorMethodsOptionsSets() ]; } - /** - * @dataProvider provideConnectorMethodsOptionsSets - */ + #[DataProvider('provideConnectorMethodsOptionsSets')] public function testOptionCallsConnectorMethod($option, $method, $value, $isArgument = true) { $expectCall = $this->connector->expects($this->once())->method($method); @@ -275,9 +274,7 @@ public static function provideDumperOptionsValues() ]; } - /** - * @dataProvider provideDumperOptionsValues - */ + #[DataProvider('provideDumperOptionsValues')] public function testDumperOptions($option, $dumperProperty, $value) { new PHPConsoleHandler([$option => $value], $this->connector); diff --git a/tests/Monolog/Handler/ProcessHandlerTest.php b/tests/Monolog/Handler/ProcessHandlerTest.php index bf3c90751..2d7003a6b 100644 --- a/tests/Monolog/Handler/ProcessHandlerTest.php +++ b/tests/Monolog/Handler/ProcessHandlerTest.php @@ -13,6 +13,7 @@ use Monolog\Test\TestCase; use Monolog\Level; +use PHPUnit\Framework\Attributes\DataProvider; class ProcessHandlerTest extends TestCase { @@ -76,11 +77,10 @@ public static function invalidCommandProvider(): array } /** - * @dataProvider invalidCommandProvider - * @param mixed $invalidCommand * @covers Monolog\Handler\ProcessHandler::guardAgainstInvalidCommand */ - public function testConstructWithInvalidCommandThrowsInvalidArgumentException($invalidCommand, $expectedExcep) + #[DataProvider('invalidCommandProvider')] + public function testConstructWithInvalidCommandThrowsInvalidArgumentException(mixed $invalidCommand, string $expectedExcep) { $this->expectException($expectedExcep); new ProcessHandler($invalidCommand, Level::Debug); @@ -99,10 +99,10 @@ public static function invalidCwdProvider(): array } /** - * @dataProvider invalidCwdProvider * @param mixed $invalidCwd * @covers Monolog\Handler\ProcessHandler::guardAgainstInvalidCwd */ + #[DataProvider('invalidCwdProvider')] public function testConstructWithInvalidCwdThrowsInvalidArgumentException($invalidCwd, $expectedExcep) { $this->expectException($expectedExcep); @@ -136,7 +136,7 @@ public function testStartupWithFailingToSelectErrorStreamThrowsUnexpectedValueEx $handler->expects($this->once()) ->method('selectErrorStream') - ->will($this->returnValue(false)); + ->willReturn(false); $this->expectException(\UnexpectedValueException::class); /** @var ProcessHandler $handler */ @@ -170,7 +170,7 @@ public function testWritingWithErrorsOnStdOutOfProcessThrowsInvalidArgumentExcep $handler->expects($this->exactly(2)) ->method('readProcessErrors') - ->willReturnOnConsecutiveCalls('', $this->returnValue('some fake error message here')); + ->willReturnOnConsecutiveCalls('', 'some fake error message here'); $this->expectException(\UnexpectedValueException::class); /** @var ProcessHandler $handler */ diff --git a/tests/Monolog/Handler/PsrHandlerTest.php b/tests/Monolog/Handler/PsrHandlerTest.php index 8a1ff25ee..2af8ca9b7 100644 --- a/tests/Monolog/Handler/PsrHandlerTest.php +++ b/tests/Monolog/Handler/PsrHandlerTest.php @@ -14,6 +14,7 @@ use Monolog\Level; use Monolog\Test\TestCase; use Monolog\Formatter\LineFormatter; +use PHPUnit\Framework\Attributes\DataProvider; /** * @covers Monolog\Handler\PsrHandler::handle @@ -28,9 +29,7 @@ public static function logLevelProvider() ); } - /** - * @dataProvider logLevelProvider - */ + #[DataProvider('logLevelProvider')] public function testHandlesAllLevels(string $levelName, Level $level) { $message = 'Hello, world! ' . $level->value; diff --git a/tests/Monolog/Handler/PushoverHandlerTest.php b/tests/Monolog/Handler/PushoverHandlerTest.php index 4c3c9481f..5010e8a19 100644 --- a/tests/Monolog/Handler/PushoverHandlerTest.php +++ b/tests/Monolog/Handler/PushoverHandlerTest.php @@ -136,13 +136,12 @@ private function createHandler($token = 'myToken', $user = 'myUser', $title = 'M $this->handler->expects($this->any()) ->method('fsockopen') - ->will($this->returnValue($this->res)); + ->willReturn($this->res); $this->handler->expects($this->any()) ->method('streamSetTimeout') - ->will($this->returnValue(true)); + ->willReturn(true); $this->handler->expects($this->any()) - ->method('closeSocket') - ->will($this->returnValue(true)); + ->method('closeSocket'); $this->handler->setFormatter($this->getIdentityFormatter()); } diff --git a/tests/Monolog/Handler/RedisHandlerTest.php b/tests/Monolog/Handler/RedisHandlerTest.php index 806919e69..dcb4aabdc 100644 --- a/tests/Monolog/Handler/RedisHandlerTest.php +++ b/tests/Monolog/Handler/RedisHandlerTest.php @@ -78,19 +78,19 @@ public function testRedisHandleCapped() // Redis uses multi $redis->expects($this->once()) ->method('multi') - ->will($this->returnSelf()); + ->willReturnSelf(); $redis->expects($this->once()) ->method('rPush') - ->will($this->returnSelf()); + ->willReturnSelf(); $redis->expects($this->once()) ->method('lTrim') - ->will($this->returnSelf()); + ->willReturnSelf(); $redis->expects($this->once()) ->method('exec') - ->will($this->returnSelf()); + ->willReturnSelf(); $record = $this->getRecord(Level::Warning, 'test', ['data' => new \stdClass, 'foo' => 34]); @@ -101,32 +101,32 @@ public function testRedisHandleCapped() public function testPredisHandleCapped() { - $redis = $this->createPartialMock('Predis\Client', ['transaction']); - - $redisTransaction = $this->getMockBuilder('Predis\Client') - ->disableOriginalConstructor() - ->addMethods(['rPush', 'lTrim']) - ->getMock(); - - $redisTransaction->expects($this->once()) - ->method('rPush') - ->will($this->returnSelf()); - - $redisTransaction->expects($this->once()) - ->method('lTrim') - ->will($this->returnSelf()); - - // Redis uses multi - $redis->expects($this->once()) - ->method('transaction') - ->will($this->returnCallback(function ($cb) use ($redisTransaction) { - $cb($redisTransaction); - })); + $redis = new class extends \Predis\Client { + public array $testResults = []; + public function rpush(...$args) { + $this->testResults[] = ['rpush', ...$args]; + return $this; + } + public function ltrim(...$args) { + $this->testResults[] = ['ltrim', ...$args]; + return $this; + } + public function transaction(...$args) { + $this->testResults[] = ['transaction start']; + return ($args[0])($this); + } + }; $record = $this->getRecord(Level::Warning, 'test', ['data' => new \stdClass, 'foo' => 34]); $handler = new RedisHandler($redis, 'key', Level::Debug, true, 10); $handler->setFormatter(new LineFormatter("%message%")); $handler->handle($record); + + self::assertsame([ + ['transaction start'], + ['rpush', 'key', 'test'], + ['ltrim', 'key', -10, -1], + ], $redis->testResults); } } diff --git a/tests/Monolog/Handler/RotatingFileHandlerTest.php b/tests/Monolog/Handler/RotatingFileHandlerTest.php index 7611a53fd..f7f35533f 100644 --- a/tests/Monolog/Handler/RotatingFileHandlerTest.php +++ b/tests/Monolog/Handler/RotatingFileHandlerTest.php @@ -13,6 +13,7 @@ use InvalidArgumentException; use Monolog\Test\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; /** * @covers Monolog\Handler\RotatingFileHandler @@ -106,9 +107,7 @@ public function testRotationCreatesNewFile() $this->assertEquals('test', file_get_contents($log)); } - /** - * @dataProvider rotationTests - */ + #[DataProvider('rotationTests')] public function testRotation($createFile, $dateFormat, $timeCallback) { touch($old1 = __DIR__.'/Fixtures/foo-'.date($dateFormat, $timeCallback(-1)).'.rot'); @@ -176,9 +175,7 @@ private function createDeep($file) return $file; } - /** - * @dataProvider rotationWithFolderByDateTests - */ + #[DataProvider('rotationWithFolderByDateTests')] public function testRotationWithFolderByDate($createFile, $dateFormat, $timeCallback) { $old1 = $this->createDeep(__DIR__.'/Fixtures/'.date($dateFormat, $timeCallback(-1)).'/foo.rot'); @@ -238,9 +235,7 @@ public static function rotationWithFolderByDateTests() ]; } - /** - * @dataProvider dateFormatProvider - */ + #[DataProvider('dateFormatProvider')] public function testAllowOnlyFixedDefinedDateFormats($dateFormat, $valid) { $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2); @@ -279,9 +274,7 @@ public static function dateFormatProvider() ]; } - /** - * @dataProvider filenameFormatProvider - */ + #[DataProvider('filenameFormatProvider')] public function testDisallowFilenameFormatsWithoutDate($filenameFormat, $valid) { $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2); @@ -307,9 +300,7 @@ public static function filenameFormatProvider() ]; } - /** - * @dataProvider rotationWhenSimilarFilesExistTests - */ + #[DataProvider('rotationWhenSimilarFilesExistTests')] public function testRotationWhenSimilarFileNamesExist($dateFormat) { touch($old1 = __DIR__.'/Fixtures/foo-foo-'.date($dateFormat).'.rot'); diff --git a/tests/Monolog/Handler/Slack/SlackRecordTest.php b/tests/Monolog/Handler/Slack/SlackRecordTest.php index d59de1779..0d1eaf3c8 100644 --- a/tests/Monolog/Handler/Slack/SlackRecordTest.php +++ b/tests/Monolog/Handler/Slack/SlackRecordTest.php @@ -13,10 +13,10 @@ use Monolog\Level; use Monolog\Test\TestCase; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; -/** - * @coversDefaultClass Monolog\Handler\Slack\SlackRecord - */ +#[CoversClass(SlackRecord::class)] class SlackRecordTest extends TestCase { public static function dataGetAttachmentColor() @@ -33,10 +33,7 @@ public static function dataGetAttachmentColor() ]; } - /** - * @dataProvider dataGetAttachmentColor - * @covers ::getAttachmentColor - */ + #[DataProvider('dataGetAttachmentColor')] public function testGetAttachmentColor(Level $logLevel, string $expectedColour) { $slackRecord = new SlackRecord(); @@ -78,9 +75,7 @@ public static function dataStringify(): array ]; } - /** - * @dataProvider dataStringify - */ + #[DataProvider('dataStringify')] public function testStringify($fields, $expectedResult) { $slackRecord = new SlackRecord( @@ -162,17 +157,17 @@ public function testTextEqualsFormatterOutput() $formatter ->expects($this->any()) ->method('format') - ->will($this->returnCallback(function ($record) { + ->willReturnCallback(function ($record) { return $record->message . 'test'; - })); + }); $formatter2 = $this->createMock('Monolog\\Formatter\\FormatterInterface'); $formatter2 ->expects($this->any()) ->method('format') - ->will($this->returnCallback(function ($record) { + ->willReturnCallback(function ($record) { return $record->message . 'test1'; - })); + }); $message = 'Test message'; $record = new SlackRecord(null, null, false, null, false, false, [], $formatter); diff --git a/tests/Monolog/Handler/SlackHandlerTest.php b/tests/Monolog/Handler/SlackHandlerTest.php index 7832739f9..6e5be8396 100644 --- a/tests/Monolog/Handler/SlackHandlerTest.php +++ b/tests/Monolog/Handler/SlackHandlerTest.php @@ -15,6 +15,7 @@ use Monolog\Level; use Monolog\Formatter\LineFormatter; use Monolog\Handler\Slack\SlackRecord; +use PHPUnit\Framework\Attributes\DataProvider; /** * @author Greg Kedzierski @@ -93,9 +94,7 @@ public function testWriteContentWithEmoji() $this->assertMatchesRegularExpression('/icon_emoji=%3Aalien%3A/', $content); } - /** - * @dataProvider provideLevelColors - */ + #[DataProvider('provideLevelColors')] public function testWriteContentWithColors($level, $expectedColor) { $this->createHandler(); @@ -145,13 +144,12 @@ private function createHandler($token = 'myToken', $channel = 'channel1', $usern $this->handler->expects($this->any()) ->method('fsockopen') - ->will($this->returnValue($this->res)); + ->willReturn($this->res); $this->handler->expects($this->any()) ->method('streamSetTimeout') - ->will($this->returnValue(true)); + ->willReturn(true); $this->handler->expects($this->any()) - ->method('closeSocket') - ->will($this->returnValue(true)); + ->method('closeSocket'); $this->handler->setFormatter($this->getIdentityFormatter()); } diff --git a/tests/Monolog/Handler/SocketHandlerTest.php b/tests/Monolog/Handler/SocketHandlerTest.php index bb7ab9ba2..c8a2c7735 100644 --- a/tests/Monolog/Handler/SocketHandlerTest.php +++ b/tests/Monolog/Handler/SocketHandlerTest.php @@ -97,7 +97,7 @@ public function testExceptionIsThrownOnFsockopenError() $this->setMockHandler(['fsockopen']); $this->handler->expects($this->once()) ->method('fsockopen') - ->will($this->returnValue(false)); + ->willReturn(false); $this->expectException(\UnexpectedValueException::class); @@ -109,7 +109,7 @@ public function testExceptionIsThrownOnPfsockopenError() $this->setMockHandler(['pfsockopen']); $this->handler->expects($this->once()) ->method('pfsockopen') - ->will($this->returnValue(false)); + ->willReturn(false); $this->handler->setPersistent(true); @@ -123,7 +123,7 @@ public function testExceptionIsThrownIfCannotSetTimeout() $this->setMockHandler(['streamSetTimeout']); $this->handler->expects($this->once()) ->method('streamSetTimeout') - ->will($this->returnValue(false)); + ->willReturn(false); $this->expectException(\UnexpectedValueException::class); @@ -136,7 +136,7 @@ public function testExceptionIsThrownIfCannotSetChunkSize() $this->handler->setChunkSize(8192); $this->handler->expects($this->once()) ->method('streamSetChunkSize') - ->will($this->returnValue(false)); + ->willReturn(false); $this->expectException(\UnexpectedValueException::class); @@ -158,7 +158,7 @@ public function testWriteFailsOnIfFwriteReturnsFalse() $this->handler->expects($this->exactly(2)) ->method('fwrite') - ->will($this->returnCallback($callback)); + ->willReturnCallback($callback); $this->expectException(\RuntimeException::class); @@ -180,10 +180,10 @@ public function testWriteFailsIfStreamTimesOut() $this->handler->expects($this->exactly(1)) ->method('fwrite') - ->will($this->returnCallback($callback)); + ->willReturnCallback($callback); $this->handler->expects($this->exactly(1)) ->method('streamGetMetadata') - ->will($this->returnValue(['timed_out' => true])); + ->willReturn(['timed_out' => true]); $this->expectException(\RuntimeException::class); @@ -203,10 +203,10 @@ public function testWriteFailsOnIncompleteWrite() $this->handler->expects($this->exactly(1)) ->method('fwrite') - ->will($this->returnCallback($callback)); + ->willReturnCallback($callback); $this->handler->expects($this->exactly(1)) ->method('streamGetMetadata') - ->will($this->returnValue(['timed_out' => false])); + ->willReturn(['timed_out' => false]); $this->expectException(\RuntimeException::class); @@ -238,7 +238,7 @@ public function testWriteWithMock() $this->handler->expects($this->exactly(2)) ->method('fwrite') - ->will($this->returnCallback($callback)); + ->willReturnCallback($callback); $this->writeRecord('Hello world'); } @@ -268,11 +268,11 @@ public function testAvoidInfiniteLoopWhenNoDataIsWrittenForAWritingTimeoutSecond $this->handler->expects($this->any()) ->method('fwrite') - ->will($this->returnValue(0)); + ->willReturn(0); $this->handler->expects($this->any()) ->method('streamGetMetadata') - ->will($this->returnValue(['timed_out' => false])); + ->willReturn(['timed_out' => false]); $this->handler->setWritingTimeout(1); @@ -311,25 +311,25 @@ private function setMockHandler(array $methods = []) if (!in_array('fsockopen', $methods)) { $this->handler->expects($this->any()) ->method('fsockopen') - ->will($this->returnValue($this->res)); + ->willReturn($this->res); } if (!in_array('pfsockopen', $methods)) { $this->handler->expects($this->any()) ->method('pfsockopen') - ->will($this->returnValue($this->res)); + ->willReturn($this->res); } if (!in_array('streamSetTimeout', $methods)) { $this->handler->expects($this->any()) ->method('streamSetTimeout') - ->will($this->returnValue(true)); + ->willReturn(true); } if (!in_array('streamSetChunkSize', $methods)) { $this->handler->expects($this->any()) ->method('streamSetChunkSize') - ->will($this->returnValue(8192)); + ->willReturn(8192); } $this->handler->setFormatter($this->getIdentityFormatter()); diff --git a/tests/Monolog/Handler/StreamHandlerTest.php b/tests/Monolog/Handler/StreamHandlerTest.php index 4b5cea1f8..882f8d843 100644 --- a/tests/Monolog/Handler/StreamHandlerTest.php +++ b/tests/Monolog/Handler/StreamHandlerTest.php @@ -13,6 +13,7 @@ use Monolog\Test\TestCase; use Monolog\Level; +use PHPUnit\Framework\Attributes\DataProvider; class StreamHandlerTest extends TestCase { @@ -127,9 +128,9 @@ public static function invalidArgumentProvider() } /** - * @dataProvider invalidArgumentProvider * @covers Monolog\Handler\StreamHandler::__construct */ + #[DataProvider('invalidArgumentProvider')] public function testWriteInvalidArgument($invalidArgument) { $this->expectException(\InvalidArgumentException::class); @@ -207,8 +208,8 @@ public function testWriteNonExistingFileResource() /** * @covers Monolog\Handler\StreamHandler::__construct * @covers Monolog\Handler\StreamHandler::write - * @dataProvider provideNonExistingAndNotCreatablePath */ + #[DataProvider('provideNonExistingAndNotCreatablePath')] public function testWriteNonExistingAndNotCreatablePath($nonExistingAndNotCreatablePath) { if (defined('PHP_WINDOWS_VERSION_BUILD')) { @@ -259,9 +260,7 @@ public static function provideMemoryValues() ]; } - /** - * @dataProvider provideMemoryValues - */ + #[DataProvider('provideMemoryValues')] public function testPreventOOMError($phpMemory, $expectedChunkSize): void { $previousValue = ini_set('memory_limit', $phpMemory); diff --git a/tests/Monolog/Handler/TestHandlerTest.php b/tests/Monolog/Handler/TestHandlerTest.php index 4ce7f1be6..d1e1ec32b 100644 --- a/tests/Monolog/Handler/TestHandlerTest.php +++ b/tests/Monolog/Handler/TestHandlerTest.php @@ -13,15 +13,14 @@ use Monolog\Level; use Monolog\Test\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; /** * @covers Monolog\Handler\TestHandler */ class TestHandlerTest extends TestCase { - /** - * @dataProvider methodProvider - */ + #[DataProvider('methodProvider')] public function testHandler($method, Level $level) { $handler = new TestHandler; diff --git a/tests/Monolog/Handler/ZendMonitorHandlerTest.php b/tests/Monolog/Handler/ZendMonitorHandlerTest.php index c79aa4392..6c73eb6c8 100644 --- a/tests/Monolog/Handler/ZendMonitorHandlerTest.php +++ b/tests/Monolog/Handler/ZendMonitorHandlerTest.php @@ -49,11 +49,11 @@ public function testWrite() $formatterMock->expects($this->once()) ->method('format') - ->will($this->returnValue($formatterResult)); + ->willReturn($formatterResult); $zendMonitor->expects($this->once()) ->method('getDefaultFormatter') - ->will($this->returnValue($formatterMock)); + ->willReturn($formatterMock); $zendMonitor->expects($this->once()) ->method('writeZendMonitorCustomEvent') diff --git a/tests/Monolog/LoggerTest.php b/tests/Monolog/LoggerTest.php index 30129e0a8..ffae65587 100644 --- a/tests/Monolog/LoggerTest.php +++ b/tests/Monolog/LoggerTest.php @@ -15,6 +15,7 @@ use Monolog\Processor\WebProcessor; use Monolog\Handler\TestHandler; use Monolog\Test\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; class LoggerTest extends TestCase { @@ -161,7 +162,7 @@ public function testLogNotHandledIfProcessorsArePresent() $logger = new Logger(__METHOD__); $handler = $this->getMockBuilder('Monolog\Handler\HandlerInterface')->getMock(); - $handler->expects($this->once())->method('isHandling')->will($this->returnValue(false)); + $handler->expects($this->once())->method('isHandling')->willReturn(false); $handler->expects($this->never())->method('handle'); $logger->pushProcessor(fn (LogRecord $record) => $record); @@ -283,11 +284,11 @@ public function testProcessorsAreCalledOnlyOnce() $handler = $this->createMock('Monolog\Handler\HandlerInterface'); $handler->expects($this->any()) ->method('isHandling') - ->will($this->returnValue(true)) + ->willReturn(true); ; $handler->expects($this->any()) ->method('handle') - ->will($this->returnValue(true)) + ->willReturn(true); ; $logger->pushHandler($handler); @@ -298,7 +299,7 @@ public function testProcessorsAreCalledOnlyOnce() ; $processor->expects($this->once()) ->method('__invoke') - ->will($this->returnArgument(0)) + ->willReturnArgument(0) ; $logger->pushProcessor($processor); @@ -314,7 +315,7 @@ public function testProcessorsNotCalledWhenNotHandled() $handler = $this->createMock('Monolog\Handler\HandlerInterface'); $handler->expects($this->once()) ->method('isHandling') - ->will($this->returnValue(false)) + ->willReturn(false); ; $logger->pushHandler($handler); $that = $this; @@ -335,29 +336,29 @@ public function testHandlersNotCalledBeforeFirstHandlingWhenProcessorsPresent() $handler1 = $this->createMock('Monolog\Handler\HandlerInterface'); $handler1->expects($this->never()) ->method('isHandling') - ->will($this->returnValue(false)) + ->willReturn(false); ; $handler1->expects($this->once()) ->method('handle') - ->will($this->returnValue(false)) + ->willReturn(false); ; $logger->pushHandler($handler1); $handler2 = $this->createMock('Monolog\Handler\HandlerInterface'); $handler2->expects($this->once()) ->method('isHandling') - ->will($this->returnValue(true)) + ->willReturn(true); ; $handler2->expects($this->once()) ->method('handle') - ->will($this->returnValue(false)) + ->willReturn(false); ; $logger->pushHandler($handler2); $handler3 = $this->createMock('Monolog\Handler\HandlerInterface'); $handler3->expects($this->once()) ->method('isHandling') - ->will($this->returnValue(false)) + ->willReturn(false); ; $handler3->expects($this->never()) ->method('handle') @@ -375,27 +376,27 @@ public function testHandlersNotCalledBeforeFirstHandlingWhenProcessorsPresentWit $handler1 = $this->createMock('Monolog\Handler\HandlerInterface'); $handler1->expects($this->never()) ->method('isHandling') - ->will($this->returnValue(false)) + ->willReturn(false); ; $handler1->expects($this->once()) ->method('handle') - ->will($this->returnValue(false)) + ->willReturn(false); ; $handler2 = $this->createMock('Monolog\Handler\HandlerInterface'); $handler2->expects($this->once()) ->method('isHandling') - ->will($this->returnValue(true)) + ->willReturn(true); ; $handler2->expects($this->once()) ->method('handle') - ->will($this->returnValue(false)) + ->willReturn(false); ; $handler3 = $this->createMock('Monolog\Handler\HandlerInterface'); $handler3->expects($this->once()) ->method('isHandling') - ->will($this->returnValue(false)) + ->willReturn(false); ; $handler3->expects($this->never()) ->method('handle') @@ -417,22 +418,22 @@ public function testBubblingWhenTheHandlerReturnsFalse() $handler1 = $this->createMock('Monolog\Handler\HandlerInterface'); $handler1->expects($this->any()) ->method('isHandling') - ->will($this->returnValue(true)) + ->willReturn(true); ; $handler1->expects($this->once()) ->method('handle') - ->will($this->returnValue(false)) + ->willReturn(false); ; $logger->pushHandler($handler1); $handler2 = $this->createMock('Monolog\Handler\HandlerInterface'); $handler2->expects($this->any()) ->method('isHandling') - ->will($this->returnValue(true)) + ->willReturn(true); ; $handler2->expects($this->once()) ->method('handle') - ->will($this->returnValue(false)) + ->willReturn(false); ; $logger->pushHandler($handler2); @@ -449,7 +450,7 @@ public function testNotBubblingWhenTheHandlerReturnsTrue() $handler1 = $this->createMock('Monolog\Handler\HandlerInterface'); $handler1->expects($this->any()) ->method('isHandling') - ->will($this->returnValue(true)) + ->willReturn(true); ; $handler1->expects($this->never()) ->method('handle') @@ -459,11 +460,11 @@ public function testNotBubblingWhenTheHandlerReturnsTrue() $handler2 = $this->createMock('Monolog\Handler\HandlerInterface'); $handler2->expects($this->any()) ->method('isHandling') - ->will($this->returnValue(true)) + ->willReturn(true); ; $handler2->expects($this->once()) ->method('handle') - ->will($this->returnValue(true)) + ->willReturn(true); ; $logger->pushHandler($handler2); @@ -480,7 +481,7 @@ public function testIsHandling() $handler1 = $this->createMock('Monolog\Handler\HandlerInterface'); $handler1->expects($this->any()) ->method('isHandling') - ->will($this->returnValue(false)) + ->willReturn(false); ; $logger->pushHandler($handler1); @@ -489,7 +490,7 @@ public function testIsHandling() $handler2 = $this->createMock('Monolog\Handler\HandlerInterface'); $handler2->expects($this->any()) ->method('isHandling') - ->will($this->returnValue(true)) + ->willReturn(true); ; $logger->pushHandler($handler2); @@ -497,7 +498,6 @@ public function testIsHandling() } /** - * @dataProvider logMethodProvider * @covers Level::Debug * @covers Level::Info * @covers Level::Notice @@ -507,6 +507,7 @@ public function testIsHandling() * @covers Level::Alert * @covers Level::Emergency */ + #[DataProvider('logMethodProvider')] public function testLogMethods(string $method, Level $expectedLevel) { $logger = new Logger('foo'); @@ -533,9 +534,9 @@ public static function logMethodProvider() } /** - * @dataProvider setTimezoneProvider * @covers Logger::setTimezone */ + #[DataProvider('setTimezoneProvider')] public function testSetTimezone($tz) { $logger = new Logger('foo'); @@ -608,10 +609,10 @@ public function tearDown(): void } /** - * @dataProvider useMicrosecondTimestampsProvider * @covers Logger::useMicrosecondTimestamps * @covers Logger::addRecord */ + #[DataProvider('useMicrosecondTimestampsProvider')] public function testUseMicrosecondTimestamps($micro, $assert, $assertFormat) { if (PHP_VERSION_ID === 70103) { @@ -675,7 +676,7 @@ public function testDefaultHandleException() $handler = $this->getMockBuilder('Monolog\Handler\HandlerInterface')->getMock(); $handler->expects($this->any()) ->method('isHandling') - ->will($this->returnValue(true)) + ->willReturn(true); ; $handler->expects($this->any()) ->method('handle') @@ -704,7 +705,7 @@ public function testCustomHandleException() $handler = $this->getMockBuilder('Monolog\Handler\HandlerInterface')->getMock(); $handler->expects($this->any()) ->method('isHandling') - ->will($this->returnValue(true)) + ->willReturn(true); ; $handler->expects($this->any()) ->method('handle') diff --git a/tests/Monolog/Processor/ClosureContextProcessorTest.php b/tests/Monolog/Processor/ClosureContextProcessorTest.php index 134a992b9..24e35d82c 100644 --- a/tests/Monolog/Processor/ClosureContextProcessorTest.php +++ b/tests/Monolog/Processor/ClosureContextProcessorTest.php @@ -12,6 +12,7 @@ namespace Monolog\Processor; use Monolog\Test\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; class ClosureContextProcessorTest extends TestCase { @@ -24,9 +25,7 @@ public function testReplace() $this->assertSame($context, $record->context); } - /** - * @dataProvider getContexts - */ + #[DataProvider('getContexts')] public function testSkip(array $context) { $processor = new ClosureContextProcessor(); diff --git a/tests/Monolog/Processor/PsrLogMessageProcessorTest.php b/tests/Monolog/Processor/PsrLogMessageProcessorTest.php index 2735935db..fd9bfe998 100644 --- a/tests/Monolog/Processor/PsrLogMessageProcessorTest.php +++ b/tests/Monolog/Processor/PsrLogMessageProcessorTest.php @@ -13,12 +13,11 @@ use Monolog\Level; use Monolog\Test\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; class PsrLogMessageProcessorTest extends TestCase { - /** - * @dataProvider getPairs - */ + #[DataProvider('getPairs')] public function testReplacement($val, $expected) { $proc = new PsrLogMessageProcessor; diff --git a/tests/Monolog/PsrLogCompatTest.php b/tests/Monolog/PsrLogCompatTest.php index a1f523ac8..1d51c55b5 100644 --- a/tests/Monolog/PsrLogCompatTest.php +++ b/tests/Monolog/PsrLogCompatTest.php @@ -15,6 +15,7 @@ use Monolog\Handler\TestHandler; use Monolog\Formatter\LineFormatter; use Monolog\Processor\PsrLogMessageProcessor; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Log\InvalidArgumentException; @@ -63,9 +64,7 @@ public function testImplements() $this->assertInstanceOf(LoggerInterface::class, $this->getLogger()); } - /** - * @dataProvider provideLevelsAndMessages - */ + #[DataProvider('provideLevelsAndMessages')] public function testLogsAtAllLevels($level, $message) { $logger = $this->getLogger(); @@ -170,7 +169,7 @@ protected function createStringable(string $string = ''): MockObject&Stringable ->getMock(); $mock->method('__toString') - ->will($this->returnValue($string)); + ->willReturn($string); return $mock; } diff --git a/tests/Monolog/RegistryTest.php b/tests/Monolog/RegistryTest.php index cacf7855e..ba4353000 100644 --- a/tests/Monolog/RegistryTest.php +++ b/tests/Monolog/RegistryTest.php @@ -11,6 +11,8 @@ namespace Monolog; +use PHPUnit\Framework\Attributes\DataProvider; + class RegistryTest extends \PHPUnit\Framework\TestCase { protected function setUp(): void @@ -19,9 +21,9 @@ protected function setUp(): void } /** - * @dataProvider hasLoggerProvider * @covers Monolog\Registry::hasLogger */ + #[DataProvider('hasLoggerProvider')] public function testHasLogger(array $loggersToAdd, array $loggersToCheck, array $expectedResult) { foreach ($loggersToAdd as $loggerToAdd) { @@ -73,10 +75,10 @@ public function testClearClears() } /** - * @dataProvider removedLoggerProvider * @covers Monolog\Registry::addLogger * @covers Monolog\Registry::removeLogger */ + #[DataProvider('removedLoggerProvider')] public function testRemovesLogger($loggerToAdd, $remove) { Registry::addLogger($loggerToAdd); diff --git a/tests/Monolog/SignalHandlerTest.php b/tests/Monolog/SignalHandlerTest.php index e4e9cffd4..9d833a599 100644 --- a/tests/Monolog/SignalHandlerTest.php +++ b/tests/Monolog/SignalHandlerTest.php @@ -13,6 +13,7 @@ use Monolog\Handler\StreamHandler; use Monolog\Handler\TestHandler; +use PHPUnit\Framework\Attributes\DataProvider; use Psr\Log\LogLevel; use Monolog\Test\TestCase; @@ -122,12 +123,12 @@ public function testRegisterSignalHandler() } /** - * @dataProvider defaultPreviousProvider * @depends testRegisterSignalHandler * @requires function pcntl_fork * @requires function pcntl_sigprocmask * @requires function pcntl_waitpid */ + #[DataProvider('defaultPreviousProvider')] public function testRegisterDefaultPreviousSignalHandler($signo, $callPrevious, $expected) { $this->setSignalHandler($signo, SIG_DFL); @@ -174,10 +175,10 @@ public static function defaultPreviousProvider() } /** - * @dataProvider callablePreviousProvider * @depends testRegisterSignalHandler * @requires function pcntl_signal_get_handler */ + #[DataProvider('callablePreviousProvider')] public function testRegisterCallablePreviousSignalHandler($callPrevious) { $this->setSignalHandler(SIGURG, SIG_IGN); @@ -205,11 +206,11 @@ public static function callablePreviousProvider() } /** - * @dataProvider restartSyscallsProvider * @depends testRegisterDefaultPreviousSignalHandler * @requires function pcntl_fork * @requires function pcntl_waitpid */ + #[DataProvider('restartSyscallsProvider')] public function testRegisterSyscallRestartingSignalHandler($restartSyscalls) { $this->setSignalHandler(SIGURG, SIG_IGN); @@ -259,10 +260,10 @@ public static function restartSyscallsProvider() } /** - * @dataProvider asyncProvider * @depends testRegisterDefaultPreviousSignalHandler * @requires function pcntl_async_signals */ + #[DataProvider('asyncProvider')] public function testRegisterAsyncSignalHandler($initialAsync, $desiredAsync, $expectedBefore, $expectedAfter) { $this->setSignalHandler(SIGURG, SIG_IGN); diff --git a/tests/Monolog/UtilsTest.php b/tests/Monolog/UtilsTest.php index d6e48ed94..1ebff1103 100644 --- a/tests/Monolog/UtilsTest.php +++ b/tests/Monolog/UtilsTest.php @@ -11,11 +11,11 @@ namespace Monolog; +use PHPUnit\Framework\Attributes\DataProvider; + class UtilsTest extends \PHPUnit_Framework_TestCase { - /** - * @dataProvider provideObjects - */ + #[DataProvider('provideObjects')] public function testGetClass(string $expected, object $object) { $this->assertSame($expected, Utils::getClass($object)); @@ -32,9 +32,7 @@ public static function provideObjects() ]; } - /** - * @dataProvider providePathsToCanonicalize - */ + #[DataProvider('providePathsToCanonicalize')] public function testCanonicalizePath(string $expected, string $input) { $this->assertSame($expected, Utils::canonicalizePath($input)); @@ -53,9 +51,7 @@ public static function providePathsToCanonicalize() ]; } - /** - * @dataProvider providesHandleJsonErrorFailure - */ + #[DataProvider('providesHandleJsonErrorFailure')] public function testHandleJsonErrorFailure(int $code, string $msg) { $this->expectException('RuntimeException', $msg); @@ -76,8 +72,8 @@ public static function providesHandleJsonErrorFailure() * @param mixed $in Input * @param mixed $expect Expected output * @covers Monolog\Formatter\NormalizerFormatter::detectAndCleanUtf8 - * @dataProvider providesDetectAndCleanUtf8 */ + #[DataProvider('providesDetectAndCleanUtf8')] public function testDetectAndCleanUtf8($in, $expect) { $reflMethod = new \ReflectionMethod(Utils::class, 'detectAndCleanUtf8'); @@ -106,9 +102,7 @@ public static function providesDetectAndCleanUtf8() ]; } - /** - * @dataProvider providesPcreLastErrorMessage - */ + #[DataProvider('providesPcreLastErrorMessage')] public function testPcreLastErrorMessage(int $code, string $msg) { if (PHP_VERSION_ID >= 80000) { @@ -171,12 +165,8 @@ public static function provideIniValuesToConvertToBytes() ]; } - /** - * @dataProvider provideIniValuesToConvertToBytes - * @param mixed $input - * @param int|false $expected - */ - public function testExpandIniShorthandBytes($input, $expected) + #[DataProvider('provideIniValuesToConvertToBytes')] + public function testExpandIniShorthandBytes(string|null|bool $input, int|false $expected) { $result = Utils::expandIniShorthandBytes($input); $this->assertEquals($expected, $result);