From 378e7203fc8f18c3066c917e9a6b1af0f69f212f Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Wed, 4 Sep 2024 02:58:31 +0900 Subject: [PATCH] Handle warnings in QueryRepositoryTest Add error handler to catch E_USER_WARNING in QueryRepositoryTest. Introduced set_error_handler and restore_error_handler to properly manage warnings during tests. --- tests/QueryRepositoryTest.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tests/QueryRepositoryTest.php b/tests/QueryRepositoryTest.php index ad19351d..b65546fb 100644 --- a/tests/QueryRepositoryTest.php +++ b/tests/QueryRepositoryTest.php @@ -25,9 +25,13 @@ use function assert; use function is_array; +use function restore_error_handler; use function serialize; +use function set_error_handler; use function unserialize; +use const E_USER_WARNING; + class QueryRepositoryTest extends TestCase { private ResourceInterface $resource; @@ -143,6 +147,15 @@ public function testErrorInCacheRead(): void $namespace = 'FakeVendor\HelloWorld'; $module = ModuleFactory::getInstance($namespace); + $errorCaught = false; + set_error_handler(static function (int $errno, string $errstr, string $errfile, int $errline) use (&$errorCaught): bool { + unset($errstr, $errfile, $errline); + if ($errno === E_USER_WARNING) { + $errorCaught = true; + } + + return true; // PHP に通常のエラーハンドリングを停止させるために true を返す + }); $module->override(new class extends AbstractModule { protected function configure(): void { @@ -152,8 +165,8 @@ protected function configure(): void $resource = (new Injector($module, $_ENV['TMP_DIR']))->getInstance(ResourceInterface::class); assert($resource instanceof ResourceInterface); $resource->get('app://self/user', ['id' => 1]); - $this->assertSame(2, $GLOBALS['BEAR\QueryRepository\syslog'][0]); - $this->assertContains('Exception: DoctrineNamespaceCacheKey[]', $GLOBALS['BEAR\QueryRepository\syslog'][1]); + $this->assertTrue($errorCaught, 'E_USER_WARNING should have been caught.'); + restore_error_handler(); } public function testSameResponseButDifferentParameter(): void