Skip to content

Commit 40304de

Browse files
committed
Throw 500 error on no matching mock
1 parent 62f6795 commit 40304de

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"require": {
1212
"php": "^7.2",
1313
"api-clients/middleware": "^4.0",
14+
"api-clients/psr7-http-status-exceptions": "^1.0",
1415
"api-clients/transport": "^3.1",
1516
"psr/http-message": "^1.0",
1617
"ringcentral/psr7": "^1.2"

composer.lock

Lines changed: 43 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/MockMiddleware.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55
use ApiClients\Foundation\Middleware\ErrorTrait;
66
use ApiClients\Foundation\Middleware\MiddlewareInterface;
77
use ApiClients\Foundation\Middleware\PostTrait;
8+
use ApiClients\Tools\Psr7\HttpStatusExceptions\InternalServerErrorException;
89
use Psr\Http\Message\RequestInterface;
910
use React\Promise\CancellablePromiseInterface;
1011
use function React\Promise\reject;
1112
use function React\Promise\resolve;
13+
use RingCentral\Psr7\Response;
1214

1315
final class MockMiddleware implements MiddlewareInterface
1416
{
1517
use PostTrait;
1618
use ErrorTrait;
19+
private const ERROR_MESSAGE = 'No matching mocks found';
1720
/** @var MockInterface[] */
1821
private $mocks = [];
1922

@@ -41,6 +44,6 @@ public function pre(
4144
}
4245
}
4346

44-
return reject(new \RuntimeException('No matching mocks found'));
47+
return reject(InternalServerErrorException::create(new Response(500, [], self::ERROR_MESSAGE), new \RuntimeException(self::ERROR_MESSAGE)));
4548
}
4649
}

tests/MockMiddlewareTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
use ApiClients\Middleware\Mock\MockMiddleware;
66
use ApiClients\Middleware\Mock\PathMock;
7+
use ApiClients\Tools\Psr7\HttpStatusExceptions\InternalServerErrorException;
78
use ApiClients\Tools\TestUtilities\TestCase;
89
use function Clue\React\Block\await;
910
use React\EventLoop\Factory;
1011
use RingCentral\Psr7\Request;
1112
use RingCentral\Psr7\Response;
12-
use RuntimeException;
1313

1414
/**
1515
* @internal
@@ -18,7 +18,8 @@ class MockMiddlewareTest extends TestCase
1818
{
1919
public function testNoMatchingMock(): void
2020
{
21-
self::expectException(RuntimeException::class);
21+
self::expectException(InternalServerErrorException::class);
22+
self::expectExceptionCode(500);
2223

2324
$middleware = new MockMiddleware();
2425
$request = new Request('GET', 'https://example.com', [], '');

0 commit comments

Comments
 (0)