1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace QuickPay\Tests; |
4
|
|
|
|
5
|
|
|
use QuickPay\API\Client; |
6
|
|
|
use QuickPay\API\Request; |
7
|
|
|
use QuickPay\API\Response; |
8
|
|
|
|
9
|
|
|
class RequestTest extends \PHPUnit_Framework_TestCase |
10
|
|
|
{ |
11
|
|
|
protected $request; |
12
|
|
|
|
13
|
|
|
public function setUp() |
14
|
|
|
{ |
15
|
|
|
$this->client = new Client(); |
|
|
|
|
16
|
|
|
$this->request = new Request($this->client); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function testResponseInstance() |
20
|
|
|
{ |
21
|
|
|
$pingResponse = $this->request->get('/ping'); |
22
|
|
|
|
23
|
|
|
$this->assertTrue(($pingResponse instanceof Response)); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testBadAuthentication() |
27
|
|
|
{ |
28
|
|
|
$client = new Client(':foo'); |
29
|
|
|
$request = new Request($client); |
30
|
|
|
|
31
|
|
|
$response = $request->get('/ping'); |
32
|
|
|
|
33
|
|
|
$this->assertEquals(401, $response->httpStatus()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testSuccessfulGetResponse() |
37
|
|
|
{ |
38
|
|
|
$pingResponse = $this->request->get('/ping'); |
39
|
|
|
|
40
|
|
|
$this->assertTrue($pingResponse->isSuccess()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testFailedGetResponse() |
44
|
|
|
{ |
45
|
|
|
$pingResponse = $this->request->get('/foobar'); |
46
|
|
|
|
47
|
|
|
$this->assertFalse($pingResponse->isSuccess()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testSuccesfulPostResponse() |
51
|
|
|
{ |
52
|
|
|
$pingResponse = $this->request->post('/ping'); |
53
|
|
|
|
54
|
|
|
$this->assertTrue($pingResponse->isSuccess()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testFailedPostResponse() |
58
|
|
|
{ |
59
|
|
|
$pingResponse = $this->request->post('/foobar'); |
60
|
|
|
|
61
|
|
|
$this->assertFalse($pingResponse->isSuccess()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Test function added to make sure that issue gh-54 is fixed. |
66
|
|
|
*/ |
67
|
|
|
public function testBasket() |
68
|
|
|
{ |
69
|
|
|
$basket = []; |
70
|
|
|
$basket[0] = [ |
71
|
|
|
'qty' => 1, |
72
|
|
|
'item_no' => 2, |
73
|
|
|
'item_name' => 'Test 1', |
74
|
|
|
'item_price' => 100, |
75
|
|
|
'vat_rate' => 0.25, |
76
|
|
|
]; |
77
|
|
|
$basket[1] = [ |
78
|
|
|
'qty' => 1, |
79
|
|
|
'item_no' => 2, |
80
|
|
|
'item_name' => 'Test 2', |
81
|
|
|
'item_price' => 100, |
82
|
|
|
'vat_rate' => 0.25, |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
$form = [ |
86
|
|
|
'currency' => 'DKK', |
87
|
|
|
'order_id' => 1, |
88
|
|
|
'basket' => $basket, |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
$query = $this->request->httpBuildQuery($form); |
92
|
|
|
|
93
|
|
|
$expected = 'currency=DKK&order_id=1&basket[][qty]=1&basket[][item_no]=2&basket[][item_name]=Test 1&basket[][item_price]=100&basket[][vat_rate]=0.25&basket[][qty]=1&basket[][item_no]=2&basket[][item_name]=Test 2&basket[][item_price]=100&basket[][vat_rate]=0.25'; |
94
|
|
|
|
95
|
|
|
$this->assertEquals(urldecode($query), $expected); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testStandardHeaders() |
99
|
|
|
{ |
100
|
|
|
$pingResponse = $this->request->get('/ping'); |
|
|
|
|
101
|
|
|
$this->assertContains('Accept: application/json', curl_getinfo($this->client->ch, CURLINFO_HEADER_OUT)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testAdditionalHeadersOnInit() |
105
|
|
|
{ |
106
|
|
|
$extra_headers = array('SpecialVar: foo'); |
107
|
|
|
$client = new Client(null, $extra_headers); |
108
|
|
|
$request = new Request($client); |
109
|
|
|
$pingResponse = $request->get('/ping'); |
|
|
|
|
110
|
|
|
$this->assertContains($extra_headers[0], curl_getinfo($client->ch, CURLINFO_HEADER_OUT)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testAdditionalHeadersAfterInit() |
114
|
|
|
{ |
115
|
|
|
$extra_headers = array('NewVar: foo'); |
116
|
|
|
$this->client->setHeaders($extra_headers); |
117
|
|
|
$request = new Request($this->client); |
118
|
|
|
$pingResponse = $request->get('/ping'); |
|
|
|
|
119
|
|
|
$this->assertContains($extra_headers[0], curl_getinfo($this->client->ch, CURLINFO_HEADER_OUT)); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: