Skip to content

Commit 2edc2ad

Browse files
committed
Add dontReportExceptionToFile method to endpoints
1 parent 1922813 commit 2edc2ad

File tree

5 files changed

+140
-2
lines changed

5 files changed

+140
-2
lines changed

src/Endpoints/AbstractEndpoint.php

+15
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ final public function setShouldIgnoreLoggersForExceptions(Closure $closure): sta
4545
return $cloned;
4646
}
4747

48+
final public function dontReportExceptionsToFile(array $exceptions): static
49+
{
50+
return $this->setShouldIgnoreLoggersForExceptions(static function (Throwable $throwable) use (
51+
$exceptions
52+
): array {
53+
foreach ($exceptions as $exception) {
54+
if ($throwable instanceof $exception) {
55+
return [FileLoggerContract::class];
56+
}
57+
}
58+
return [];
59+
});
60+
}
61+
4862
final protected function shouldIgnoreLoggersOnException(): ?Closure
4963
{
5064
return function (Throwable $throwable): array {
@@ -65,6 +79,7 @@ final protected function shouldIgnoreLoggersOnException(): ?Closure
6579
};
6680
}
6781

82+
6883
/**
6984
* Appends to base path in uri. Must start with /.
7085
*/

src/Interfaces/EndpointInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function setShouldIgnoreLoggersForExceptions(Closure $closure): static;
2424
/**
2525
* Returns a copy of endpoint that will not log to file when the request fails with given exception.
2626
*
27-
* @param array<class-string<Throwable>> $exceptions
27+
* @param non-empty-array<class-string<Throwable>> $exceptions
2828
*/
2929
public function dontReportExceptionsToFile(array $exceptions): static;
3030
}

src/Testing/Factories/EndpointDIEntityFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace WrkFlow\ApiSdkBuilder\Testing\Factories;
66

7+
use WrkFlow\ApiSdkBuilder\Contracts\SendRequestActionContract;
78
use WrkFlow\ApiSdkBuilder\Entities\EndpointDIEntity;
89
use WrkFlow\ApiSdkBuilder\Interfaces\ApiInterface;
910
use WrkFlow\ApiSdkBuilder\Testing\ApiMock;
@@ -13,7 +14,7 @@ final class EndpointDIEntityFactory
1314
{
1415
public static function make(
1516
ApiInterface $api = new ApiMock(),
16-
SendTestRequestActionAssert $sendAssert = new SendTestRequestActionAssert()
17+
SendRequestActionContract $sendAssert = new SendTestRequestActionAssert()
1718
): EndpointDIEntity {
1819
return new EndpointDIEntity(api: $api, sendRequestAction: $sendAssert);
1920
}
+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WrkFlow\ApiSdkBuilderTests\Endpoints;
6+
7+
use Closure;
8+
use PHPUnit\Framework\TestCase;
9+
use WrkFlow\ApiSdkBuilder\Exceptions\ApiException;
10+
use WrkFlow\ApiSdkBuilder\Log\Contracts\FileLoggerContract;
11+
use WrkFlow\ApiSdkBuilder\Testing\Exceptions\TestRequestSentException;
12+
use WrkFlow\ApiSdkBuilder\Testing\Factories\EndpointDIEntityFactory;
13+
use WrkFlow\ApiSdkBuilderTests\TestApi\Endpoints\Json\JsonEndpoint;
14+
15+
final class AbstractEndpointTest extends TestCase
16+
{
17+
/**
18+
* @return array<string|int, array{0: Closure(static):void}>
19+
*/
20+
public function dataDontReportToExceptionsToFile(): array
21+
{
22+
return [
23+
'returns empty array if not set' => [
24+
static fn (self $self) => $self->assertTestShouldIgnoreLoggers(
25+
assert: new TestShouldIgnoreLoggersSendRequestActionAssert(
26+
testException: new ApiException(),
27+
expectedIgnoreLoggers: [],
28+
),
29+
onEndpoint: static fn (JsonEndpoint $endpoint) => $endpoint,
30+
),
31+
],
32+
'returns empty array if exception does not match' => [
33+
static fn (self $self) => $self->assertTestShouldIgnoreLoggers(
34+
assert: new TestShouldIgnoreLoggersSendRequestActionAssert(
35+
testException: new ApiException(),
36+
expectedIgnoreLoggers: [],
37+
),
38+
onEndpoint: static fn (JsonEndpoint $endpoint) => $endpoint
39+
->dontReportExceptionsToFile(exceptions: [TestRequestSentException::class]),
40+
),
41+
],
42+
'returns empty array if exception matches' => [
43+
static fn (self $self) => $self->assertTestShouldIgnoreLoggers(
44+
assert: new TestShouldIgnoreLoggersSendRequestActionAssert(
45+
testException: new ApiException(),
46+
expectedIgnoreLoggers: [FileLoggerContract::class],
47+
),
48+
onEndpoint: static fn (JsonEndpoint $endpoint) => $endpoint
49+
->dontReportExceptionsToFile(exceptions: [ApiException::class]),
50+
),
51+
],
52+
];
53+
}
54+
55+
56+
/**
57+
* @param Closure(static):void $assert
58+
*
59+
* @dataProvider dataDontReportToExceptionsToFile
60+
*/
61+
public function testDontReportToExceptionsToFile(Closure $assert): void
62+
{
63+
$assert($this);
64+
}
65+
66+
public function assertTestShouldIgnoreLoggers(
67+
TestShouldIgnoreLoggersSendRequestActionAssert $assert,
68+
Closure $onEndpoint,
69+
): void {
70+
$this->expectException(TestRequestSentException::class);
71+
72+
$endpoint = new JsonEndpoint(di: EndpointDIEntityFactory::make(sendAssert: $assert));
73+
($onEndpoint($endpoint))
74+
->success();
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WrkFlow\ApiSdkBuilderTests\Endpoints;
6+
7+
use Closure;
8+
use PHPUnit\Framework\Assert;
9+
use Psr\Http\Message\RequestInterface;
10+
use Psr\Http\Message\ResponseInterface;
11+
use Psr\Http\Message\StreamInterface;
12+
use Throwable;
13+
use WrkFlow\ApiSdkBuilder\Contracts\SendRequestActionContract;
14+
use WrkFlow\ApiSdkBuilder\Interfaces\ApiInterface;
15+
use WrkFlow\ApiSdkBuilder\Interfaces\OptionsInterface;
16+
use WrkFlow\ApiSdkBuilder\Log\Interfaces\ApiLoggerInterface;
17+
use WrkFlow\ApiSdkBuilder\Responses\AbstractResponse;
18+
use WrkFlow\ApiSdkBuilder\Testing\Exceptions\TestRequestSentException;
19+
20+
final class TestShouldIgnoreLoggersSendRequestActionAssert implements SendRequestActionContract
21+
{
22+
/**
23+
* @param array<class-string<ApiLoggerInterface>> $expectedIgnoreLoggers
24+
*/
25+
public function __construct(
26+
private readonly Throwable $testException,
27+
private readonly array $expectedIgnoreLoggers,
28+
) {
29+
}
30+
31+
public function execute(
32+
ApiInterface $api,
33+
RequestInterface $request,
34+
string $responseClass,
35+
StreamInterface|string|OptionsInterface|null $body = null,
36+
array $headers = [],
37+
?int $expectedResponseStatusCode = null,
38+
?ResponseInterface $fakedResponse = null,
39+
Closure $shouldIgnoreLoggersOnError = null
40+
): AbstractResponse {
41+
Assert::assertNotNull($shouldIgnoreLoggersOnError, 'AbstractEndpoint always builds closure');
42+
Assert::assertEquals($this->expectedIgnoreLoggers, $shouldIgnoreLoggersOnError($this->testException));
43+
44+
throw new TestRequestSentException();
45+
}
46+
}

0 commit comments

Comments
 (0)