Skip to content
This repository has been archived by the owner on Jun 23, 2021. It is now read-only.

Commit

Permalink
III-3386 Refactor the unit tests to take into account the UncaughtErr…
Browse files Browse the repository at this point in the history
…orHandler
  • Loading branch information
LucWollants committed Sep 25, 2020
1 parent aacb551 commit f5cca2c
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions tests/ApiErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,36 @@

use Exception;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject;
use Sentry\State\HubInterface;

class ApiErrorHandlerTest extends TestCase
{
/** @var HubInterface|PHPUnit_Framework_MockObject_MockObject */
private $sentryHub;

/** @var UncaughtErrorHandler */
private $uncaughtErrorHandler;

protected function setUp(): void
{
parent::setUp();

$this->sentryHub = $this->createMock(HubInterface::class);
$this->uncaughtErrorHandler = new UncaughtErrorHandler($this->sentryHub);
}

/**
* @test
*/
public function it_creates_api_problem_from_exception()
{
$exception = new Exception('Exception message', 500);

$errorHandler = new ApiErrorHandler();
$errorHandler = new ApiErrorHandler($this->uncaughtErrorHandler);
$this->sentryHub->expects($this->once())
->method('captureException');

$apiProblemResponse = $errorHandler->__invoke($exception);

$this->assertEquals(
Expand All @@ -31,7 +50,10 @@ public function it_creates_api_problem_with_400_status_if_exception_has_no_statu
{
$exception = new Exception('Exception message');

$errorHandler = new ApiErrorHandler();
$errorHandler = new ApiErrorHandler($this->uncaughtErrorHandler);
$this->sentryHub->expects($this->once())
->method('captureException');

$apiProblemResponse = $errorHandler->__invoke($exception);

$this->assertEquals(
Expand All @@ -41,6 +63,26 @@ public function it_creates_api_problem_with_400_status_if_exception_has_no_statu
$this->assertEquals(400, $apiProblemResponse->getStatusCode());
}

/**
* @test
*/
public function it_does_not_capture_404_to_sentry()
{
$exception = new Exception('Exception message', 404);

$errorHandler = new ApiErrorHandler($this->uncaughtErrorHandler);
$this->sentryHub->expects($this->never())
->method('captureException');

$apiProblemResponse = $errorHandler->__invoke($exception);

$this->assertEquals(
'{"title":"Exception message","type":"about:blank","status":404}',
$apiProblemResponse->getContent()
);
$this->assertEquals(404, $apiProblemResponse->getStatusCode());
}

/**
* @test
*/
Expand All @@ -50,7 +92,10 @@ public function it_strips_URL_CALLED_from_api_problem_message_title()
// of everything after URL CALLED
$exception = new Exception('Exception message URL CALLED: https://acc.uitid.be/uitid/rest/uitpas/cultureevent/de343e38-d656-4928-96bc-55578e0d94ec/cardsystems POST DATA: cardSystemId=3');

$errorHandler = new ApiErrorHandler();
$errorHandler = new ApiErrorHandler($this->uncaughtErrorHandler);
$this->sentryHub->expects($this->once())
->method('captureException');

$apiProblemResponse = $errorHandler->__invoke($exception);

$this->assertEquals(
Expand Down

0 comments on commit f5cca2c

Please sign in to comment.