Skip to content

Commit

Permalink
Add dontReportExceptionToFile method to endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
pionl committed Oct 2, 2023
1 parent 1922813 commit 2edc2ad
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/Endpoints/AbstractEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ final public function setShouldIgnoreLoggersForExceptions(Closure $closure): sta
return $cloned;
}

final public function dontReportExceptionsToFile(array $exceptions): static
{
return $this->setShouldIgnoreLoggersForExceptions(static function (Throwable $throwable) use (
$exceptions
): array {
foreach ($exceptions as $exception) {
if ($throwable instanceof $exception) {
return [FileLoggerContract::class];
}
}
return [];
});
}

final protected function shouldIgnoreLoggersOnException(): ?Closure
{
return function (Throwable $throwable): array {
Expand All @@ -65,6 +79,7 @@ final protected function shouldIgnoreLoggersOnException(): ?Closure
};
}


/**
* Appends to base path in uri. Must start with /.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces/EndpointInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function setShouldIgnoreLoggersForExceptions(Closure $closure): static;
/**
* Returns a copy of endpoint that will not log to file when the request fails with given exception.
*
* @param array<class-string<Throwable>> $exceptions
* @param non-empty-array<class-string<Throwable>> $exceptions
*/
public function dontReportExceptionsToFile(array $exceptions): static;
}
3 changes: 2 additions & 1 deletion src/Testing/Factories/EndpointDIEntityFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace WrkFlow\ApiSdkBuilder\Testing\Factories;

use WrkFlow\ApiSdkBuilder\Contracts\SendRequestActionContract;
use WrkFlow\ApiSdkBuilder\Entities\EndpointDIEntity;
use WrkFlow\ApiSdkBuilder\Interfaces\ApiInterface;
use WrkFlow\ApiSdkBuilder\Testing\ApiMock;
Expand All @@ -13,7 +14,7 @@ final class EndpointDIEntityFactory
{
public static function make(
ApiInterface $api = new ApiMock(),
SendTestRequestActionAssert $sendAssert = new SendTestRequestActionAssert()
SendRequestActionContract $sendAssert = new SendTestRequestActionAssert()
): EndpointDIEntity {
return new EndpointDIEntity(api: $api, sendRequestAction: $sendAssert);
}
Expand Down
76 changes: 76 additions & 0 deletions tests/Endpoints/AbstractEndpointTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace WrkFlow\ApiSdkBuilderTests\Endpoints;

use Closure;
use PHPUnit\Framework\TestCase;
use WrkFlow\ApiSdkBuilder\Exceptions\ApiException;
use WrkFlow\ApiSdkBuilder\Log\Contracts\FileLoggerContract;
use WrkFlow\ApiSdkBuilder\Testing\Exceptions\TestRequestSentException;
use WrkFlow\ApiSdkBuilder\Testing\Factories\EndpointDIEntityFactory;
use WrkFlow\ApiSdkBuilderTests\TestApi\Endpoints\Json\JsonEndpoint;

final class AbstractEndpointTest extends TestCase
{
/**
* @return array<string|int, array{0: Closure(static):void}>
*/
public function dataDontReportToExceptionsToFile(): array
{
return [
'returns empty array if not set' => [
static fn (self $self) => $self->assertTestShouldIgnoreLoggers(
assert: new TestShouldIgnoreLoggersSendRequestActionAssert(
testException: new ApiException(),
expectedIgnoreLoggers: [],
),
onEndpoint: static fn (JsonEndpoint $endpoint) => $endpoint,
),
],
'returns empty array if exception does not match' => [
static fn (self $self) => $self->assertTestShouldIgnoreLoggers(
assert: new TestShouldIgnoreLoggersSendRequestActionAssert(
testException: new ApiException(),
expectedIgnoreLoggers: [],
),
onEndpoint: static fn (JsonEndpoint $endpoint) => $endpoint
->dontReportExceptionsToFile(exceptions: [TestRequestSentException::class]),
),
],
'returns empty array if exception matches' => [
static fn (self $self) => $self->assertTestShouldIgnoreLoggers(
assert: new TestShouldIgnoreLoggersSendRequestActionAssert(
testException: new ApiException(),
expectedIgnoreLoggers: [FileLoggerContract::class],
),
onEndpoint: static fn (JsonEndpoint $endpoint) => $endpoint
->dontReportExceptionsToFile(exceptions: [ApiException::class]),
),
],
];
}


/**
* @param Closure(static):void $assert
*
* @dataProvider dataDontReportToExceptionsToFile
*/
public function testDontReportToExceptionsToFile(Closure $assert): void
{
$assert($this);
}

public function assertTestShouldIgnoreLoggers(
TestShouldIgnoreLoggersSendRequestActionAssert $assert,
Closure $onEndpoint,
): void {
$this->expectException(TestRequestSentException::class);

$endpoint = new JsonEndpoint(di: EndpointDIEntityFactory::make(sendAssert: $assert));
($onEndpoint($endpoint))
->success();
}
}
46 changes: 46 additions & 0 deletions tests/Endpoints/TestShouldIgnoreLoggersSendRequestActionAssert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace WrkFlow\ApiSdkBuilderTests\Endpoints;

use Closure;
use PHPUnit\Framework\Assert;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Throwable;
use WrkFlow\ApiSdkBuilder\Contracts\SendRequestActionContract;
use WrkFlow\ApiSdkBuilder\Interfaces\ApiInterface;
use WrkFlow\ApiSdkBuilder\Interfaces\OptionsInterface;
use WrkFlow\ApiSdkBuilder\Log\Interfaces\ApiLoggerInterface;
use WrkFlow\ApiSdkBuilder\Responses\AbstractResponse;
use WrkFlow\ApiSdkBuilder\Testing\Exceptions\TestRequestSentException;

final class TestShouldIgnoreLoggersSendRequestActionAssert implements SendRequestActionContract
{
/**
* @param array<class-string<ApiLoggerInterface>> $expectedIgnoreLoggers
*/
public function __construct(
private readonly Throwable $testException,
private readonly array $expectedIgnoreLoggers,
) {
}

public function execute(
ApiInterface $api,
RequestInterface $request,
string $responseClass,
StreamInterface|string|OptionsInterface|null $body = null,
array $headers = [],
?int $expectedResponseStatusCode = null,
?ResponseInterface $fakedResponse = null,
Closure $shouldIgnoreLoggersOnError = null
): AbstractResponse {
Assert::assertNotNull($shouldIgnoreLoggersOnError, 'AbstractEndpoint always builds closure');
Assert::assertEquals($this->expectedIgnoreLoggers, $shouldIgnoreLoggersOnError($this->testException));

throw new TestRequestSentException();
}
}

0 comments on commit 2edc2ad

Please sign in to comment.