-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dontReportExceptionToFile method to endpoints
- Loading branch information
Showing
5 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
46
tests/Endpoints/TestShouldIgnoreLoggersSendRequestActionAssert.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |