Skip to content

Commit

Permalink
Refactor test to use custom error handler
Browse files Browse the repository at this point in the history
Replaced the generic warning expectation with a custom error handler in `QueryRepositoryTest`. This change ensures more precise error catching and validation of the user data response.
  • Loading branch information
koriym committed Sep 2, 2024
1 parent 6d16d9e commit 088ff7d
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions tests/QueryRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
use function assert;
use function is_array;
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 @@ -151,10 +154,15 @@ protected function configure(): void
});
$resource = (new Injector($module, $_ENV['TMP_DIR']))->getInstance(ResourceInterface::class);
assert($resource instanceof ResourceInterface);
$this->expectWarning();
$resource->get('app://self/user', ['id' => 1]);
$this->assertSame(2, $GLOBALS['BEAR\QueryRepository\syslog'][0]);
$this->assertContains('Exception: DoctrineNamespaceCacheKey[]', $GLOBALS['BEAR\QueryRepository\syslog'][1]);
$errorCaught = false;
set_error_handler(static function ($errno, $errstr) use (&$errorCaught): void {

Check failure on line 158 in tests/QueryRepositoryTest.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Parameter #1 $callback of function set_error_handler expects (callable(int, string, string, int): bool)|null, Closure(mixed, mixed): void given.

Check failure on line 158 in tests/QueryRepositoryTest.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Parameter #1 $callback of function set_error_handler expects (callable(int, string, string, int): bool)|null, Closure(mixed, mixed): void given.
if ($errno === E_USER_WARNING) {
$errorCaught = true;
}
});
$user = $resource->get('app://self/user', ['id' => 1]);
$this->assertSame('bear', $user->body['name']);

Check failure on line 164 in tests/QueryRepositoryTest.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Cannot access offset 'name' on mixed.

Check failure on line 164 in tests/QueryRepositoryTest.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Cannot access offset 'name' on mixed.
$this->assertTrue($errorCaught);
}

public function testSameResponseButDifferentParameter(): void
Expand Down

0 comments on commit 088ff7d

Please sign in to comment.