Skip to content

Commit

Permalink
Fix phpunit deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Apr 12, 2024
1 parent 5b99025 commit b127292
Show file tree
Hide file tree
Showing 39 changed files with 220 additions and 302 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 7 additions & 3 deletions tests/Monolog/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]);
Expand All @@ -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);
Expand All @@ -68,9 +73,8 @@ protected function getPrivatePropertyValue($instance, $property)
return $prop->getValue($instance);
}

/**
* @dataProvider fatalHandlerProvider
*/
#[DataProvider('fatalHandlerProvider')]
#[WithoutErrorHandler]
public function testFatalHandler(
$level,
$reservedMemorySize,
Expand Down
23 changes: 11 additions & 12 deletions tests/Monolog/Formatter/LineFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Monolog\Test\TestCase;
use Monolog\Level;
use PHPUnit\Framework\Attributes\DataProvider;
use RuntimeException;

/**
Expand Down Expand Up @@ -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();
Expand All @@ -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',
],
];
}
Expand Down
10 changes: 2 additions & 8 deletions tests/Monolog/Formatter/MongoDBFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use MongoDB\BSON\UTCDateTime;
use Monolog\Level;
use Monolog\Test\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;

/**
* @author Florian Plattner <me@florianplattner.de>
Expand All @@ -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);
Expand Down
11 changes: 2 additions & 9 deletions tests/Monolog/Formatter/SyslogFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions tests/Monolog/Handler/AbstractHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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()]);
Expand All @@ -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)));
}
Expand All @@ -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)));
Expand Down
22 changes: 11 additions & 11 deletions tests/Monolog/Handler/AbstractProcessingHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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)));
}

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

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

Expand All @@ -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)));
}
Expand All @@ -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' => '',
Expand All @@ -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']));
Expand All @@ -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;

Expand All @@ -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);

Expand All @@ -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());
}
}
8 changes: 4 additions & 4 deletions tests/Monolog/Handler/AmqpHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
Expand Down
5 changes: 2 additions & 3 deletions tests/Monolog/Handler/ChromePHPHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Monolog\Test\TestCase;
use Monolog\Level;
use PHPUnit\Framework\Attributes\DataProvider;

/**
* @covers Monolog\Handler\ChromePHPHandler
Expand All @@ -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;
Expand Down
16 changes: 1 addition & 15 deletions tests/Monolog/Handler/DynamoDbHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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')
Expand Down
3 changes: 2 additions & 1 deletion tests/Monolog/Handler/ElasticaHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Elastica\Client;
use Elastica\Request;
use Elastica\Response;
use PHPUnit\Framework\Attributes\DataProvider;

/**
* @group Elastica
Expand Down Expand Up @@ -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];
Expand Down
Loading

0 comments on commit b127292

Please sign in to comment.