From 1821a61ddcb54950eb138c25cdb6d45171fbc8ec Mon Sep 17 00:00:00 2001 From: Wilmer Arambula <42547589+terabytesoftw@users.noreply.github.com> Date: Fri, 31 Oct 2025 07:29:29 -0300 Subject: [PATCH] Refactor tests to use `willReturnCallback` with invocation matcher for `withConsecutive` deprecated method. (#20646) --- tests/framework/BaseYiiTest.php | 64 ++++++++---- tests/framework/log/DispatcherTest.php | 75 +++++++++----- tests/framework/log/SyslogTargetTest.php | 52 ++++++++-- tests/framework/log/TargetTest.php | 121 ++++++++++++++--------- 4 files changed, 207 insertions(+), 105 deletions(-) diff --git a/tests/framework/BaseYiiTest.php b/tests/framework/BaseYiiTest.php index e3ef7ed155..178996ab4b 100644 --- a/tests/framework/BaseYiiTest.php +++ b/tests/framework/BaseYiiTest.php @@ -172,27 +172,51 @@ class BaseYiiTest extends TestCase BaseYii::setLogger($logger); - $logger->expects($this->exactly(6)) + /** + * @link https://github.com/sebastianbergmann/phpunit/issues/5063 + */ + $matcher = $this->exactly(6); + $logger + ->expects($matcher) ->method('log') - ->withConsecutive( - [$this->equalTo('info message'), $this->equalTo(Logger::LEVEL_INFO), $this->equalTo('info category')], - [ - $this->equalTo('warning message'), - $this->equalTo(Logger::LEVEL_WARNING), - $this->equalTo('warning category'), - ], - [$this->equalTo('trace message'), $this->equalTo(Logger::LEVEL_TRACE), $this->equalTo('trace category')], - [$this->equalTo('error message'), $this->equalTo(Logger::LEVEL_ERROR), $this->equalTo('error category')], - [ - $this->equalTo('beginProfile message'), - $this->equalTo(Logger::LEVEL_PROFILE_BEGIN), - $this->equalTo('beginProfile category'), - ], - [ - $this->equalTo('endProfile message'), - $this->equalTo(Logger::LEVEL_PROFILE_END), - $this->equalTo('endProfile category'), - ] + ->willReturnCallback( + function (...$parameters) use ($matcher): void { + if ($matcher->getInvocationCount() === 1) { + $this->assertEquals('info message', $parameters[0]); + $this->assertEquals(Logger::LEVEL_INFO, $parameters[1]); + $this->assertEquals('info category', $parameters[2]); + } + + if ($matcher->getInvocationCount() === 2) { + $this->assertEquals('warning message', $parameters[0]); + $this->assertEquals(Logger::LEVEL_WARNING, $parameters[1]); + $this->assertEquals('warning category', $parameters[2]); + } + + if ($matcher->getInvocationCount() === 3) { + $this->assertEquals('trace message', $parameters[0]); + $this->assertEquals(Logger::LEVEL_TRACE, $parameters[1]); + $this->assertEquals('trace category', $parameters[2]); + } + + if ($matcher->getInvocationCount() === 4) { + $this->assertEquals('error message', $parameters[0]); + $this->assertEquals(Logger::LEVEL_ERROR, $parameters[1]); + $this->assertEquals('error category', $parameters[2]); + } + + if ($matcher->getInvocationCount() === 5) { + $this->assertEquals('beginProfile message', $parameters[0]); + $this->assertEquals(Logger::LEVEL_PROFILE_BEGIN, $parameters[1]); + $this->assertEquals('beginProfile category', $parameters[2]); + } + + if ($matcher->getInvocationCount() === 6) { + $this->assertEquals('endProfile message', $parameters[0]); + $this->assertEquals(Logger::LEVEL_PROFILE_END, $parameters[1]); + $this->assertEquals('endProfile category', $parameters[2]); + } + }, ); BaseYii::info('info message', 'info category'); diff --git a/tests/framework/log/DispatcherTest.php b/tests/framework/log/DispatcherTest.php index 938fb1e556..d50128f223 100644 --- a/tests/framework/log/DispatcherTest.php +++ b/tests/framework/log/DispatcherTest.php @@ -187,43 +187,64 @@ namespace yiiunit\framework\log { public function testDispatchWithFakeTarget2ThrowExceptionWhenCollect(): void { static::$microtimeIsMocked = true; + $target1 = $this->createPartialMock(Target::class, ['collect', 'export']); $target2 = $this->createPartialMock(Target::class, ['collect', 'export']); - $target1->expects($this->exactly(2)) + /** + * @link https://github.com/sebastianbergmann/phpunit/issues/5063 + */ + $matcher = $this->exactly(2); + $target1 + ->expects($matcher) ->method('collect') - ->withConsecutive( - [$this->equalTo('messages'), $this->equalTo(true)], - [ - $this->callback(function ($arg) use ($target1) { - if (!isset($arg[0][0], $arg[0][1], $arg[0][2], $arg[0][3])) { - return false; - } + ->willReturnCallback( + function (...$parameters) use ($matcher): void { + if ($matcher->getInvocationCount() === 1) { + $this->assertEquals('messages', $parameters[0]); + $this->assertTrue($parameters[1]); + } - if (strpos($arg[0][0], 'Unable to send log via ' . get_class($target1) . ': Exception (Exception) \'yii\base\UserException\' with message \'some error\'') !== 0) { - return false; - } + if ($matcher->getInvocationCount() === 2) { + $callback = function ($arg) use ($target1): bool { + if (!isset($arg[0][0], $arg[0][1], $arg[0][2], $arg[0][3])) { + return false; + } - if ($arg[0][1] !== Logger::LEVEL_WARNING) { - return false; - } + if ( + strpos( + (string) $arg[0][0], + 'Unable to send log via ' . + get_class($target1) . + ': Exception (Exception) \'yii\base\UserException\' with message \'some error\'' + ) !== 0 + ) { + return false; + } - if ($arg[0][2] !== 'yii\log\Dispatcher::dispatch') { - return false; - } + if ($arg[0][1] !== Logger::LEVEL_WARNING) { + return false; + } - if ($arg[0][3] !== 'time data') { - return false; - } + if ($arg[0][2] !== 'yii\log\Dispatcher::dispatch') { + return false; + } - if ($arg[0][4] !== []) { - return false; - } + if ($arg[0][3] !== 'time data') { + return false; + } - return true; - }), - true, - ] + if ($arg[0][4] !== []) { + return false; + } + + return true; + }; + + $this->assertTrue($callback($parameters[0])); + $this->assertTrue($parameters[1]); + } + }, ); $target2->expects($this->once()) diff --git a/tests/framework/log/SyslogTargetTest.php b/tests/framework/log/SyslogTargetTest.php index 4fbbcc04ee..b4ad5bc3eb 100644 --- a/tests/framework/log/SyslogTargetTest.php +++ b/tests/framework/log/SyslogTargetTest.php @@ -114,16 +114,50 @@ namespace yiiunit\framework\log { [$messages[6], 'formatted message 7'], ]); - $syslogTarget->expects($this->exactly(7)) + /** + * @link https://github.com/sebastianbergmann/phpunit/issues/5063 + */ + $matcher = $this->exactly(7); + $syslogTarget + ->expects($matcher) ->method('syslog') - ->withConsecutive( - [$this->equalTo(LOG_INFO), $this->equalTo('formatted message 1')], - [$this->equalTo(LOG_ERR), $this->equalTo('formatted message 2')], - [$this->equalTo(LOG_WARNING), $this->equalTo('formatted message 3')], - [$this->equalTo(LOG_DEBUG), $this->equalTo('formatted message 4')], - [$this->equalTo(LOG_DEBUG), $this->equalTo('formatted message 5')], - [$this->equalTo(LOG_DEBUG), $this->equalTo('formatted message 6')], - [$this->equalTo(LOG_DEBUG), $this->equalTo('formatted message 7')] + ->willReturnCallback( + function (...$parameters) use ($matcher): void { + if ($matcher->getInvocationCount() === 1) { + $this->assertEquals(LOG_INFO, $parameters[0]); + $this->assertEquals('formatted message 1', $parameters[1]); + } + + if ($matcher->getInvocationCount() === 2) { + $this->assertEquals(LOG_ERR, $parameters[0]); + $this->assertEquals('formatted message 2', $parameters[1]); + } + + if ($matcher->getInvocationCount() === 3) { + $this->assertEquals(LOG_WARNING, $parameters[0]); + $this->assertEquals('formatted message 3', $parameters[1]); + } + + if ($matcher->getInvocationCount() === 4) { + $this->assertEquals(LOG_DEBUG, $parameters[0]); + $this->assertEquals('formatted message 4', $parameters[1]); + } + + if ($matcher->getInvocationCount() === 5) { + $this->assertEquals(LOG_DEBUG, $parameters[0]); + $this->assertEquals('formatted message 5', $parameters[1]); + } + + if ($matcher->getInvocationCount() === 6) { + $this->assertEquals(LOG_DEBUG, $parameters[0]); + $this->assertEquals('formatted message 6', $parameters[1]); + } + + if ($matcher->getInvocationCount() === 7) { + $this->assertEquals(LOG_DEBUG, $parameters[0]); + $this->assertEquals('formatted message 7', $parameters[1]); + } + } ); $syslogTarget->expects($this->once())->method('closelog'); diff --git a/tests/framework/log/TargetTest.php b/tests/framework/log/TargetTest.php index 7728e9ec48..b53b14acf1 100644 --- a/tests/framework/log/TargetTest.php +++ b/tests/framework/log/TargetTest.php @@ -265,25 +265,36 @@ class TargetTest extends TestCase public function testNotBreakProfilingWithFlushWithProfilingEnabled(): void { - $dispatcher = $this->createPartialMock('yii\log\Dispatcher', ['dispatch']); - $dispatcher->expects($this->exactly(2))->method('dispatch')->withConsecutive( - [ - $this->callback(function ($messages) { - return count($messages) === 1 && $messages[0][0] === 'info'; - }), - false - ], - [ - $this->callback(function ($messages) { - return count($messages) === 2 - && $messages[0][0] === 'token.a' - && $messages[0][1] == Logger::LEVEL_PROFILE_BEGIN - && $messages[1][0] === 'token.a' - && $messages[1][1] == Logger::LEVEL_PROFILE_END; - }), - false - ] - ); + $dispatcher = $this->createPartialMock(Dispatcher::class, ['dispatch']); + + /** + * @link https://github.com/sebastianbergmann/phpunit/issues/5063 + */ + $matcher = $this->exactly(2); + $dispatcher + ->expects($matcher) + ->method('dispatch') + ->willReturnCallback( + function (...$parameters) use ($matcher): void { + if ($matcher->getInvocationCount() === 1) { + $callback = fn($messages): bool => count($messages) === 1 && $messages[0][0] === 'info'; + + $this->assertTrue($callback($parameters[0])); + $this->assertFalse($parameters[1]); + } + + if ($matcher->getInvocationCount() === 2) { + $callback = fn($messages): bool => count($messages) === 2 + && $messages[0][0] === 'token.a' + && $messages[0][1] === Logger::LEVEL_PROFILE_BEGIN + && $messages[1][0] === 'token.a' + && $messages[1][1] === Logger::LEVEL_PROFILE_END; + + $this->assertTrue($callback($parameters[0])); + $this->assertFalse($parameters[1]); + } + }, + ); $logger = new Logger([ 'profilingAware' => true, @@ -298,36 +309,48 @@ class TargetTest extends TestCase public function testFlushingWithProfilingEnabledAndOverflow(): void { - $dispatcher = $this->createPartialMock('yii\log\Dispatcher', ['dispatch']); - $dispatcher->expects($this->exactly(3))->method('dispatch')->withConsecutive( - [ - $this->callback(function ($messages) { - return count($messages) === 2 - && $messages[0][0] === 'token.a' - && $messages[0][1] == Logger::LEVEL_PROFILE_BEGIN - && $messages[1][0] === 'token.b' - && $messages[1][1] == Logger::LEVEL_PROFILE_BEGIN; - }), - false - ], - [ - $this->callback(function ($messages) { - return count($messages) === 1 - && $messages[0][0] === 'Number of dangling profiling block messages reached flushInterval value and therefore these were flushed. Please consider setting higher flushInterval value or making profiling blocks shorter.'; - }), - false - ], - [ - $this->callback(function ($messages) { - return count($messages) === 2 - && $messages[0][0] === 'token.b' - && $messages[0][1] == Logger::LEVEL_PROFILE_END - && $messages[1][0] === 'token.a' - && $messages[1][1] == Logger::LEVEL_PROFILE_END; - }), - false - ] - ); + $dispatcher = $this->createPartialMock(Dispatcher::class, ['dispatch']); + + /** + * @link https://github.com/sebastianbergmann/phpunit/issues/5063 + */ + $matcher = $this->exactly(3); + $dispatcher + ->expects($matcher) + ->method('dispatch') + ->willReturnCallback( + function (...$parameters) use ($matcher): void { + if ($matcher->getInvocationCount() === 1) { + $callback = fn($messages): bool => count($messages) === 2 + && $messages[0][0] === 'token.a' + && $messages[0][1] === Logger::LEVEL_PROFILE_BEGIN + && $messages[1][0] === 'token.b' + && $messages[1][1] === Logger::LEVEL_PROFILE_BEGIN; + + $this->assertTrue($callback($parameters[0])); + $this->assertFalse($parameters[1]); + } + + if ($matcher->getInvocationCount() === 2) { + $callback = fn($messages): bool => count($messages) === 1 + && $messages[0][0] === 'Number of dangling profiling block messages reached flushInterval value and therefore these were flushed. Please consider setting higher flushInterval value or making profiling blocks shorter.'; + + $this->assertTrue($callback($parameters[0])); + $this->assertFalse($parameters[1]); + } + + if ($matcher->getInvocationCount() === 3) { + $callback = fn($messages): bool => count($messages) === 2 + && $messages[0][0] === 'token.b' + && $messages[0][1] === Logger::LEVEL_PROFILE_END + && $messages[1][0] === 'token.a' + && $messages[1][1] === Logger::LEVEL_PROFILE_END; + + $this->assertTrue($callback($parameters[0])); + $this->assertFalse($parameters[1]); + } + }, + ); $logger = new Logger([ 'profilingAware' => true,