ExceptionMapperSpec.php
3.11 KB
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
94
95
96
97
98
99
100
<?php
namespace spec\Cardinity\Http\Guzzle;
use Cardinity\Method\Error;
use Cardinity\Method\MethodInterface;
use Cardinity\Method\Payment\Payment;
use Cardinity\Method\ResultObjectMapperInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use PhpSpec\ObjectBehavior;
class ExceptionMapperSpec extends ObjectBehavior
{
function let(
ClientException $exception,
Request $request,
Response $response,
ResultObjectMapperInterface $resultMapper
) {
$this->beConstructedWith($resultMapper);
$exception->beConstructedWith([
'Message',
$request->getWrappedObject(),
$response->getWrappedObject()
]);
}
function it_maps_expected_exception_code(
ClientException $exception,
Response $response,
MethodInterface $method
) {
$result = [];
$response->getStatusCode()->willReturn(400);
$response->getBody()->willReturn(json_encode($result));
$exception->getResponse()->willReturn($response);
$this->get($exception, $method)->shouldReturnAnInstanceOf('Cardinity\Exception\ValidationFailed');
}
function it_handles_unexpected_exception_code(
ClientException $exception,
Response $response,
MethodInterface $method
) {
$result = [];
$response->getStatusCode()->willReturn(999);
$response->getBody()->willReturn(json_encode($result));
$exception->getResponse()->willReturn($response);
$this->get($exception, $method)->shouldReturnAnInstanceOf('Cardinity\Exception\UnexpectedResponse');
}
function it_maps_error_response_to_error_result_object(
ClientException $exception,
Response $response,
ResultObjectMapperInterface $resultMapper,
MethodInterface $method
) {
$result = ['errors' => ['error' => 'Error string']];
$resultObject = new Error();
$response->getStatusCode()->willReturn(400);
$response->getBody()->willReturn(json_encode($result));
$method->createResultObject()->willReturn($resultObject);
$exception->getResponse()->willReturn($response);
$resultMapper->map($result, $resultObject)->shouldBeCalled()->willReturn($resultObject);
$this->get($exception, $method)->getResult()->shouldReturn($resultObject);
}
function it_maps_declined_response_402_to_payment_result_object(
ClientException $exception,
Response $response,
ResultObjectMapperInterface $resultMapper,
MethodInterface $method
) {
$result = ['error' => 'Error string'];
$resultObject = new Payment();
$response->getStatusCode()->willReturn(402);
$response->getBody()->willReturn(json_encode($result));
$method->createResultObject()->willReturn($resultObject);
$exception->getResponse()->willReturn($response);
$resultMapper->map($result, $resultObject)->shouldBeCalled()->willReturn($resultObject);
$this->get($exception, $method)->getResult()->shouldReturn($resultObject);
}
}