-
Notifications
You must be signed in to change notification settings - Fork 2
/
SaleskingLiveInvoiceTest.php
282 lines (247 loc) · 8.02 KB
/
SaleskingLiveInvoiceTest.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
/**
* @version 1.0.0
* @package SalesKing PHP SDK Tests
* @license MIT License; see LICENSE
* @copyright Copyright (C) 2012 David Jardin
* @link http://www.salesking.eu
*/
require_once (dirname(__FILE__).'/../src/salesking.php');
/**
* Test class for Salesking with real connection
*/
class SaleskingLiveInvoiceTest extends PHPUnit_Framework_TestCase
{
/**
* @var Salesking
*/
protected $object;
/**
* @var array curl options
*/
public $curl_options = array(
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 5,
CURLOPT_USERAGENT => 'salesking-sdk-tests-1.0',
CURLOPT_SSL_VERIFYPEER => false,
CURLINFO_HEADER_OUT => true
);
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
// first, lets check weather a livetest_credentials.php exists
if(!file_exists(dirname(__FILE__)."/test_config.php")) {
$this->markTestSkipped("No connection credentials provided");
}
// if it exists, lets require it and set up our config object
require_once("test_config.php");
$config = sk_basic_auth_config();
// now we set up curl to determine whether we're online or not and we have a valid url
$curl = curl_init();
$options = $this->curl_options;
$options[CURLOPT_URL] = $config['sk_url'];
curl_setopt_array($curl, $options);
$result = curl_exec($curl);
// lets have a look on our result
if($result === false) {
// we're not online, so lets close the connection to avoid memory leaks and throw a message
curl_close($curl);
$this->markTestSkipped("can't connect to SalesKing server");
}
// close the connection
curl_close($curl);
// assign our object for the tests
$this->object = new Salesking($config);
}
/**
* Creates a client + Address + order, copies the order to an invoice and opens the invoices
* Watch it all data is beeing deleted at the end of the test!!!
* @group live-invoice
*/
public function testCreateInvoiceWithDifferentItemTypes()
{
// lets create a client
$client = $this->object->getObject("contact");
$client->type = "Client";
$client->organisation = "PHP-SDK-Testing Company";
$client->last_name= "Joe";
// create a client object object
try {
$client->save();
}
catch (SaleskingException $e) {
$this->fail("Could not create contact object");
}
// a line item
$line_item = $this->object->getObject('line_item');
$line_item->type = "LineItem";
$line_item->position = 1;
$line_item->name = "Stuff";
$line_item->price_single = 1;
// a divider item
$divider_item = $this->object->getObject('divider_item');
$divider_item->type = "DividerItem";
$divider_item->position = 2;
$divider_item->name = "Divider Text";
// a subtotal item
$sub_total_item = $this->object->getObject('sub_total_item');
$sub_total_item->type = "SubTotalItem";
$sub_total_item->position = 3;
$sub_total_item->name = "Sum of the items above";
// the invoice
$doc = $this->object->getObject('invoice');
$doc->contact_id = $client->id;
$doc->items = array($line_item->getData(), $divider_item->getData(), $sub_total_item->getData());
try{
$doc->save();
}
catch (SaleskingException $e) {
// $e->errors->body->errors
$this->fail("Could not create invoice object");
}
$this->assertEquals("LineItem",$doc->items[0]->line_item->type);
$this->assertEquals("DividerItem",$doc->items[1]->divider_item->type);
$this->assertEquals("SubTotalItem",$doc->items[2]->sub_total_item->type);
// DELETE added data
try{
$doc->delete();
$client->delete();
}
catch (SaleskingException $e) {
$this->fail("Could not delete objects");
}
}
/**
* Creates a client + Address + order, copies the order to an invoice and opens the invoices
* Watch it all data is beeing deleted at the end of the test!!!
* @group live-invoice
*/
public function testCreateInvoiceFromOrder()
{
// lets create a client
$client = $this->object->getObject("contact");
$client->type = "Client";
$client->organisation = "PHP-SDK-Testing Company";
$client->last_name= "Joe";
$client->first_name ="Orderexample";
$client->phone_home="123";
$address = $this->object->getObject("address");
$address->address1= 'My Street 34';
$address->city = 'A City'; # required
$address->zip = '081569';
$client->addresses =array($address->getData());
// create a client object object
try {
$client->save();
}
catch (SaleskingException $e) {
$this->fail("Could not create contact object");
}
// a line item
$item = $this->object->getObject('line_item');
$item->type = "LineItem";
$item->position = 1;
$item->name = "Stuff";
$item->price_single = 1;
// and an order
$order = $this->object->getObject('order');
$order->contact_id = $client->id;
$order->items = array($item->getData());
$order->status = "open";
try{
$order->save();
}
catch (SaleskingException $e) {
// $e->errors->body->errors
$this->fail("Could not create order object");
}
// create invoice
$response = $this->object->request('/api/invoices','POST',
json_encode(array(
"source" => $order->id
))
);
if($response['code'] != 201) {
throw new SaleskingException("CREATEINVOICEERROR", "Could not create invoice");
}
$invoice = $this->object->getObject("invoice");
$invoice->load($response['body']->invoice->id);
// change invoice number and status
$invoice->status = "closed";
$invoice->number = "99999";
try{
$invoice->save();
}
catch (SaleskingException $e) {
$this->fail("Could not change invoice number");
}
// DELETE added data
try{
$invoice->status = "draft";
$invoice->save();
$order->status = "draft";
$order->save();
$invoice->delete();
$order->delete();
$client->delete();
}
catch (SaleskingException $e) {
$this->fail("Could not delete objects");
}
}
public function testPrintInvoice()
{
// lets create a client
$client = $this->object->getObject("contact");
$client->type = "Client";
$client->organisation = "PHP-SDK-Testing Company";
$client->last_name= "Joe";
// create a client object object
try {
$client->save();
}
catch (SaleskingException $e) {
$this->fail("Could not create contact object");
}
// a line item
$line_item = $this->object->getObject('line_item');
$line_item->type = "LineItem";
$line_item->position = 1;
$line_item->name = "Stuff";
$line_item->price_single = 1;
// the invoice
$doc = $this->object->getObject('invoice');
$doc->contact_id = $client->id;
$doc->items = array($line_item->getData());
try{
$doc->save();
}
catch (SaleskingException $e) {
// $e->errors->body->errors
$this->fail("Could not create invoice object");
}
$data = array(
'template_id' => 'aLb3zogCir4BaAabxfpGMl', // ad default template on the dev server
'base64' => '1'
);
$response = $this->object->request('/api/invoices/' . $doc->id . '/print', 'POST', json_encode($data));
$attachment = $response['body']->attachment;
$this->assertEquals("application/pdf",$attachment->content_type);
$this->assertEquals($doc->id, $attachment->related_object_id);
// the PDF as base64 encoded string, since we requested it with the data above
$this->assertNotNull($attachment->base64);
// DELETE added data
try{
$doc->delete();
$client->delete();
}
catch (SaleskingException $e) {
$this->fail("Could not delete objects");
}
}
}
?>