Skip to content

Commit

Permalink
Handle warnings in QueryRepositoryTest
Browse files Browse the repository at this point in the history
Add error handler to catch E_USER_WARNING in QueryRepositoryTest. Introduced set_error_handler and restore_error_handler to properly manage warnings during tests.
  • Loading branch information
koriym committed Sep 3, 2024
1 parent 6fee7b3 commit 378e720
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions tests/QueryRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand All @@ -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
Expand Down

0 comments on commit 378e720

Please sign in to comment.