-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiExceptionTest.php
93 lines (78 loc) · 3.69 KB
/
ApiExceptionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
declare(strict_types=1);
namespace Ankurk91\StripeExceptions\Tests;
use Ankurk91\StripeExceptions\ApiException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Log;
use PHPUnit\Framework\Attributes\CoversClass;
#[CoversClass(ApiException::class)]
#[CoversClass(\Ankurk91\StripeExceptions\StripeServiceProvider::class)]
class ApiExceptionTest extends TestCase
{
public function testItReturnResponse()
{
$stripeException = new \Stripe\Exception\InvalidRequestException("Invalid Request");
$stripeException->setStripeCode('invalid_request');
$exception = new ApiException($stripeException);
$response = $exception->render(new Request());
$this->assertInstanceOf(\Illuminate\Http\JsonResponse::class, $response);
$this->assertEquals($response->getStatusCode(), 400);
$this->assertEquals($response->getData()->message, Lang::get('stripe::exceptions.api.invalid_request'));
$this->assertEquals($response->getData()->stripe?->code, $stripeException->getStripeCode());
}
public function testItShouldReport()
{
$originalException = new \Stripe\Exception\InvalidRequestException();
$exception = new ApiException($originalException);
$this->assertTrue($exception->shouldReport($originalException));
$handler = $this->app->make(\Illuminate\Foundation\Exceptions\Handler::class);
Log::shouldReceive('error')->once();
$handler->report($exception);
}
public function testItShouldNotReport()
{
$originalException = new \Stripe\Exception\ApiConnectionException();
$exception = new ApiException($originalException);
$this->assertFalse($exception->shouldReport($originalException));
$handler = $this->app->make(\Illuminate\Foundation\Exceptions\Handler::class);
Log::shouldReceive('error')->never();
$handler->report($exception);
}
public function testAlwaysReportWhenDebugIsTrue()
{
Config::set('app.debug', true);
$originalException = new \Stripe\Exception\ApiConnectionException();
$exception = new ApiException($originalException);
$this->assertTrue($exception->shouldReport($originalException));
$handler = $this->app->make(\Illuminate\Foundation\Exceptions\Handler::class);
Log::shouldReceive('error')->once();
$handler->report($exception);
}
public function testCardExceptionJsonBody()
{
$stripeException = \Stripe\Exception\CardException::factory(
"Card error",
400,
[],
['error' => ['message' => 'Your card was declined.']]
);
$stripeException->setStripeCode('invalid_card');
$stripeException->setDeclineCode('card_expired');
$exception = new ApiException($stripeException);
$response = $exception->render(new Request());
$this->assertEquals($response->getStatusCode(), 400);
$this->assertEquals($response->getData()->message, 'Your card was declined.');
$this->assertEquals($response->getData()->stripe?->code, $stripeException->getStripeCode());
$this->assertEquals($response->getData()->stripe?->declined_code, $stripeException->getDeclineCode());
}
public function testUnknownException()
{
$exception = new ApiException(new \Exception());
$response = $exception->render(new Request());
$this->assertInstanceOf(\Illuminate\Http\JsonResponse::class, $response);
$this->assertEquals($response->getStatusCode(), 500);
$this->assertEquals($response->getData()->message, Lang::get('stripe::exceptions.api.unknown'));
}
}