Refactor tests to use willReturnCallback with invocation matcher for withConsecutive deprecated method. (#20646)

This commit is contained in:
Wilmer Arambula
2025-10-31 07:29:29 -03:00
committed by GitHub
parent c311720922
commit 1821a61ddc
4 changed files with 207 additions and 105 deletions

View File

@@ -172,27 +172,51 @@ class BaseYiiTest extends TestCase
BaseYii::setLogger($logger); 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') ->method('log')
->withConsecutive( ->willReturnCallback(
[$this->equalTo('info message'), $this->equalTo(Logger::LEVEL_INFO), $this->equalTo('info category')], function (...$parameters) use ($matcher): void {
[ if ($matcher->getInvocationCount() === 1) {
$this->equalTo('warning message'), $this->assertEquals('info message', $parameters[0]);
$this->equalTo(Logger::LEVEL_WARNING), $this->assertEquals(Logger::LEVEL_INFO, $parameters[1]);
$this->equalTo('warning category'), $this->assertEquals('info category', $parameters[2]);
], }
[$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')], if ($matcher->getInvocationCount() === 2) {
[ $this->assertEquals('warning message', $parameters[0]);
$this->equalTo('beginProfile message'), $this->assertEquals(Logger::LEVEL_WARNING, $parameters[1]);
$this->equalTo(Logger::LEVEL_PROFILE_BEGIN), $this->assertEquals('warning category', $parameters[2]);
$this->equalTo('beginProfile category'), }
],
[ if ($matcher->getInvocationCount() === 3) {
$this->equalTo('endProfile message'), $this->assertEquals('trace message', $parameters[0]);
$this->equalTo(Logger::LEVEL_PROFILE_END), $this->assertEquals(Logger::LEVEL_TRACE, $parameters[1]);
$this->equalTo('endProfile category'), $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'); BaseYii::info('info message', 'info category');

View File

@@ -187,43 +187,64 @@ namespace yiiunit\framework\log {
public function testDispatchWithFakeTarget2ThrowExceptionWhenCollect(): void public function testDispatchWithFakeTarget2ThrowExceptionWhenCollect(): void
{ {
static::$microtimeIsMocked = true; static::$microtimeIsMocked = true;
$target1 = $this->createPartialMock(Target::class, ['collect', 'export']); $target1 = $this->createPartialMock(Target::class, ['collect', 'export']);
$target2 = $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') ->method('collect')
->withConsecutive( ->willReturnCallback(
[$this->equalTo('messages'), $this->equalTo(true)], function (...$parameters) use ($matcher): void {
[ if ($matcher->getInvocationCount() === 1) {
$this->callback(function ($arg) use ($target1) { $this->assertEquals('messages', $parameters[0]);
if (!isset($arg[0][0], $arg[0][1], $arg[0][2], $arg[0][3])) { $this->assertTrue($parameters[1]);
return false; }
}
if (strpos($arg[0][0], 'Unable to send log via ' . get_class($target1) . ': Exception (Exception) \'yii\base\UserException\' with message \'some error\'') !== 0) { if ($matcher->getInvocationCount() === 2) {
return false; $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) { if (
return false; 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') { if ($arg[0][1] !== Logger::LEVEL_WARNING) {
return false; return false;
} }
if ($arg[0][3] !== 'time data') { if ($arg[0][2] !== 'yii\log\Dispatcher::dispatch') {
return false; return false;
} }
if ($arg[0][4] !== []) { if ($arg[0][3] !== 'time data') {
return false; return false;
} }
return true; if ($arg[0][4] !== []) {
}), return false;
true, }
]
return true;
};
$this->assertTrue($callback($parameters[0]));
$this->assertTrue($parameters[1]);
}
},
); );
$target2->expects($this->once()) $target2->expects($this->once())

View File

@@ -114,16 +114,50 @@ namespace yiiunit\framework\log {
[$messages[6], 'formatted message 7'], [$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') ->method('syslog')
->withConsecutive( ->willReturnCallback(
[$this->equalTo(LOG_INFO), $this->equalTo('formatted message 1')], function (...$parameters) use ($matcher): void {
[$this->equalTo(LOG_ERR), $this->equalTo('formatted message 2')], if ($matcher->getInvocationCount() === 1) {
[$this->equalTo(LOG_WARNING), $this->equalTo('formatted message 3')], $this->assertEquals(LOG_INFO, $parameters[0]);
[$this->equalTo(LOG_DEBUG), $this->equalTo('formatted message 4')], $this->assertEquals('formatted message 1', $parameters[1]);
[$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')] 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'); $syslogTarget->expects($this->once())->method('closelog');

View File

@@ -265,25 +265,36 @@ class TargetTest extends TestCase
public function testNotBreakProfilingWithFlushWithProfilingEnabled(): void public function testNotBreakProfilingWithFlushWithProfilingEnabled(): void
{ {
$dispatcher = $this->createPartialMock('yii\log\Dispatcher', ['dispatch']); $dispatcher = $this->createPartialMock(Dispatcher::class, ['dispatch']);
$dispatcher->expects($this->exactly(2))->method('dispatch')->withConsecutive(
[ /**
$this->callback(function ($messages) { * @link https://github.com/sebastianbergmann/phpunit/issues/5063
return count($messages) === 1 && $messages[0][0] === 'info'; */
}), $matcher = $this->exactly(2);
false $dispatcher
], ->expects($matcher)
[ ->method('dispatch')
$this->callback(function ($messages) { ->willReturnCallback(
return count($messages) === 2 function (...$parameters) use ($matcher): void {
&& $messages[0][0] === 'token.a' if ($matcher->getInvocationCount() === 1) {
&& $messages[0][1] == Logger::LEVEL_PROFILE_BEGIN $callback = fn($messages): bool => count($messages) === 1 && $messages[0][0] === 'info';
&& $messages[1][0] === 'token.a'
&& $messages[1][1] == Logger::LEVEL_PROFILE_END; $this->assertTrue($callback($parameters[0]));
}), $this->assertFalse($parameters[1]);
false }
]
); 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([ $logger = new Logger([
'profilingAware' => true, 'profilingAware' => true,
@@ -298,36 +309,48 @@ class TargetTest extends TestCase
public function testFlushingWithProfilingEnabledAndOverflow(): void public function testFlushingWithProfilingEnabledAndOverflow(): void
{ {
$dispatcher = $this->createPartialMock('yii\log\Dispatcher', ['dispatch']); $dispatcher = $this->createPartialMock(Dispatcher::class, ['dispatch']);
$dispatcher->expects($this->exactly(3))->method('dispatch')->withConsecutive(
[ /**
$this->callback(function ($messages) { * @link https://github.com/sebastianbergmann/phpunit/issues/5063
return count($messages) === 2 */
&& $messages[0][0] === 'token.a' $matcher = $this->exactly(3);
&& $messages[0][1] == Logger::LEVEL_PROFILE_BEGIN $dispatcher
&& $messages[1][0] === 'token.b' ->expects($matcher)
&& $messages[1][1] == Logger::LEVEL_PROFILE_BEGIN; ->method('dispatch')
}), ->willReturnCallback(
false function (...$parameters) use ($matcher): void {
], if ($matcher->getInvocationCount() === 1) {
[ $callback = fn($messages): bool => count($messages) === 2
$this->callback(function ($messages) { && $messages[0][0] === 'token.a'
return count($messages) === 1 && $messages[0][1] === Logger::LEVEL_PROFILE_BEGIN
&& $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.'; && $messages[1][0] === 'token.b'
}), && $messages[1][1] === Logger::LEVEL_PROFILE_BEGIN;
false
], $this->assertTrue($callback($parameters[0]));
[ $this->assertFalse($parameters[1]);
$this->callback(function ($messages) { }
return count($messages) === 2
&& $messages[0][0] === 'token.b' if ($matcher->getInvocationCount() === 2) {
&& $messages[0][1] == Logger::LEVEL_PROFILE_END $callback = fn($messages): bool => count($messages) === 1
&& $messages[1][0] === 'token.a' && $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.';
&& $messages[1][1] == Logger::LEVEL_PROFILE_END;
}), $this->assertTrue($callback($parameters[0]));
false $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([ $logger = new Logger([
'profilingAware' => true, 'profilingAware' => true,