forked from yabacon/paystack-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponseTest.php
More file actions
129 lines (109 loc) · 3.62 KB
/
ResponseTest.php
File metadata and controls
129 lines (109 loc) · 3.62 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace Yabacon\Paystack\Tests\Http;
use Yabacon\Paystack\Http\Response;
use Yabacon\Paystack\Exception\ApiException;
class ResponseTest extends \PHPUnit\Framework\TestCase
{
public function testInitialize()
{
$r = new Response();
$this->assertNotNull($r);
}
public function testWrapUpForApiOkayNoStatusWithErrors()
{
$r = new Response();
$r->okay = true;
$r->forApi = true;
$r->body = '{"message": "A validation error has occured","errors": {"email"';
$r->body .= ': [{"rule": "required","message": "Email is required"},{"rule"';
$r->body .= ': "email","message": "Email must be valid"}],"name": [{"rule":';
$r->body .= ' "required","message": "Name is required"}]}}';
try {
$resp = $r->wrapUp();
} catch (\Exception $e) {
$this->assertInstanceOf(ApiException::class, $e);
$this->assertInstanceOf(\stdClass::class, $e->getResponseObject()->errors);
}
}
public function testWrapUpForApiOkay()
{
$r = new Response();
$r->okay = true;
$r->forApi = true;
$r->body = '{"status":"1","data":{"status":"okay"}}';
$resp = $r->wrapUp();
$this->assertEquals('okay', $resp->data->status);
}
public function testWrapUpForApiOkayStatusFalse()
{
$r = new Response();
$r->okay = true;
$r->forApi = true;
$r->body = '{"status":"0","message":"I failed on Api"}';
$this->expectException(ApiException::class);
$resp = $r->wrapUp();
}
public function testWrapUpForApiOkayNoStatus()
{
$r = new Response();
$r->okay = true;
$r->forApi = true;
$r->body = '{"message":"I failed on Api"}';
try {
$resp = $r->wrapUp();
} catch (\Exception $e) {
$this->assertInstanceOf(ApiException::class, $e);
$this->assertEquals("Paystack Request failed with response: 'I failed on Api'", $e->getMessage());
}
}
public function testWrapUpForApiOkayNoStatusNoMessage()
{
$r = new Response();
$r->okay = true;
$r->forApi = true;
$r->body = '{"errors":"I failed on Api"}';
try {
$resp = $r->wrapUp();
} catch (\Exception $e) {
$this->assertInstanceOf(ApiException::class, $e);
$expectedResponse = "Paystack Request failed with response: '{\"errors\":\"I failed on Api\"}'";
$this->assertEquals($expectedResponse, $e->getMessage());
}
}
public function testWrapUpForApiOkayBadJSON()
{
$r = new Response();
$r->okay = true;
$r->forApi = true;
$r->body = 'API didn\'t give JSON';
$this->expectException(ApiException::class);
$resp = $r->wrapUp();
}
public function testWrapUpForApiNotOkay()
{
$r = new Response();
$r->okay = false;
$r->forApi = true;
$r->body = 'I failed before hitting API';
$this->expectException(\Exception::class);
$resp = $r->wrapUp();
}
public function testWrapUpNotForApiOkay()
{
$r = new Response();
$r->okay = true;
$r->forApi = false;
$r->body = 'I was not sent to API';
$resp = $r->wrapUp();
$this->assertEquals('I was not sent to API', $resp);
}
public function testWrapUpNotForApiNotOkay()
{
$r = new Response();
$r->okay = false;
$r->forApi = false;
$r->body = 'I was not sent to API but I still failed';
$resp = $r->wrapUp();
$this->assertFalse($resp);
}
}