CakePhp4 - 如何在 Phpunit 测试用例中断言已执行查询的计数?
CakePhp4 - How to assert a count of executed queries in a Phpunit TestCase?
在测试用例中,我想断言已执行查询的计数。我应该使用 DebugKit 还是 ConnectionManager ?我找到了一些旧 CakePhp 的答案,但 none CakePhp 4.x.
你有收据吗?
谢谢和干杯
您必须设置专用记录器。
$cm = ConnectionManager::get('default');
$oldLogger = $cm->getLogger();
$cm->setLogger( new class implements \Psr\Log\LoggerInterface {
use \Psr\Log\LoggerTrait ;
public $count = 0 ;
public function log($level, $message, array $context = array()){
$this->count ++ ;
//echo "\n", $level,' ',$message, "\n";
}
});
// Do some queries ...
// Print the count
echo 'Count: ', $cm->getLogger()->count, "\n" ;
// And don't forget to restore the initial logger.
$cm->setLogger( $oldLogger );
您可以从此代码创建一个专用的 class,有更多选项:-)
在测试用例中,我想断言已执行查询的计数。我应该使用 DebugKit 还是 ConnectionManager ?我找到了一些旧 CakePhp 的答案,但 none CakePhp 4.x.
你有收据吗?
谢谢和干杯
您必须设置专用记录器。
$cm = ConnectionManager::get('default');
$oldLogger = $cm->getLogger();
$cm->setLogger( new class implements \Psr\Log\LoggerInterface {
use \Psr\Log\LoggerTrait ;
public $count = 0 ;
public function log($level, $message, array $context = array()){
$this->count ++ ;
//echo "\n", $level,' ',$message, "\n";
}
});
// Do some queries ...
// Print the count
echo 'Count: ', $cm->getLogger()->count, "\n" ;
// And don't forget to restore the initial logger.
$cm->setLogger( $oldLogger );
您可以从此代码创建一个专用的 class,有更多选项:-)