Skip to content

Commit 97a3a2a

Browse files
author
Amro Khaled | Commentatk
committed
♻️ Better exceptions handling
Seperate data mock to json files
1 parent 5913f7c commit 97a3a2a

File tree

14 files changed

+227
-138
lines changed

14 files changed

+227
-138
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ Integrate [Like4Card](http://like4card.com/) api with Laravel.
1414
- [Get all orders](#get-all-orders)
1515
- [Get an order info](#get-an-order-info)
1616
- [Create new order](#create-new-order)
17+
- [API Exceptions](#api-exceptions)
18+
- [Wrong credentials](#wrong-credentials)
19+
- [example](#example)
20+
- [Products not available](#products-not-available)
21+
- [example](#example-1)
1722
- [License](#license)
1823

1924
## Installation
@@ -257,6 +262,38 @@ $response = Like4Card::createOrder($product_id, $local_id);
257262
| :-------- | :--------------------------- |
258263
| response | 1 for success, 0 for failure |
259264

265+
## API Exceptions
266+
267+
If the API response is **0**, the service throw an exception. It could be on of following
268+
269+
### Wrong credentials
270+
271+
It been thrown if you enter wrong deviceId, username, password or securityCode.
272+
273+
#### example
274+
275+
```php
276+
try {
277+
$response = Like4Card::balance();
278+
} catch (\CodeBugLab\Like4Card\Exceptions\WrongCredentialsException $ex) {
279+
echo $ex->getMessage(); // "Incorrect Login - invalid email or password"
280+
}
281+
```
282+
283+
### Products not available
284+
285+
It's been thrown if no products found with given **category id** or **products ids array**
286+
287+
#### example
288+
289+
```php
290+
try {
291+
$data = Like4Card::getProductsByCategoryId(1);
292+
} catch (\CodeBugLab\Like4Card\Exceptions\ProductsNotFoundException $ex) {
293+
echo $ex->getMessage(); // "No available products"
294+
}
295+
```
296+
260297
## License
261298

262299
Laravel Like4Card is a free software distributed under the terms of the MIT license.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace CodeBugLab\Like4Card\Exceptions;
4+
5+
6+
use Exception;
7+
8+
class ProductsNotFoundException extends Exception
9+
{
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace CodeBugLab\Like4Card\Exceptions;
4+
5+
6+
use Exception;
7+
8+
class WrongCredentialsException extends Exception
9+
{
10+
}

src/Services/Like4CardAPI.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,17 @@ class Like4CardAPI implements Like4CardInterface
2121
*/
2222
protected const API_URL = "https://taxes.like4app.com/online/";
2323

24-
function __construct()
25-
{
26-
//
27-
}
28-
2924
/**
3025
* Send POST cURL request to like4card server.
3126
*
3227
* @param string $url
3328
* @param array $json
3429
* @return object
3530
*/
36-
protected static function cURL($url, $json = [])
31+
private static function cURL($url, $json = [])
3732
{
33+
dd('there');
34+
3835
$ch = curl_init();
3936

4037
$headers = array();

tests/Feature/ProductsRequestsTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace CodeBugLab\Like4Card\Tests\Feature;
44

5+
use CodeBugLab\Like4Card\Exceptions\ProductsNotFoundException;
56
use CodeBugLab\Like4Card\Tests\TestCase;
67
use CodeBugLab\Like4Card\Models\Like4CardProduct as Product;
78

@@ -34,11 +35,11 @@ public function test_it_saves_products_came_from_api_with_passed_product_id()
3435
]);
3536
}
3637

37-
public function test_it_returns_error_message_if_products_not_found()
38+
public function test_it_throws_an_exception_when_no_products_found()
3839
{
39-
$products = $this->like4Card::products();
40+
$this->expectException(ProductsNotFoundException::class);
41+
$this->expectExceptionMessage("No available products");
4042

41-
$this->assertFalse(isset($products->data));
42-
$this->assertEquals("No available products", $products->message);
43+
$this->like4Card::exceptionTestCase('no_products');
4344
}
4445
}

tests/Mock/Like4CardAPIMock.php

Lines changed: 56 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -2,161 +2,89 @@
22

33
namespace CodeBugLab\Like4Card\Tests\Mock;
44

5-
use CodeBugLab\Like4Card\Contracts\Like4CardInterface;
5+
use Exception;
6+
use CodeBugLab\Like4Card\Services\Like4CardAPI;
7+
use CodeBugLab\Like4Card\Exceptions\ProductsNotFoundException;
8+
use CodeBugLab\Like4Card\Exceptions\WrongCredentialsException;
69

7-
class Like4CardAPIMock implements Like4CardInterface
10+
class Like4CardAPIMock extends Like4CardAPI
811
{
12+
private static function cURL($url, $json = [])
13+
{
14+
$response = self::prepareCURL($url, $json);
15+
16+
if ($response->response > 0) {
17+
return $response;
18+
}
19+
20+
switch (optional($response)->message) {
21+
case "Incorrect Login - invalid email or password":
22+
throw new WrongCredentialsException($response->message);
23+
case "No available products":
24+
throw new ProductsNotFoundException($response->message);
25+
default:
26+
throw new Exception(optional($response)->message ?? "Unknown error");
27+
}
28+
}
29+
30+
private static function prepareCURL($url, $json)
31+
{
32+
return json_decode(
33+
file_get_contents(
34+
sprintf("%s/data/%s.json", __DIR__, $url)
35+
)
36+
);
37+
}
38+
939
public static function balance()
1040
{
11-
return json_decode('{
12-
"response": 1,
13-
"userId": "1",
14-
"balance": "100",
15-
"currency": "SAR"
16-
}');
41+
return self::cURL('check_balance');
1742
}
1843

1944
public static function categories()
2045
{
21-
return json_decode('{
22-
"response": 1,
23-
"data": [
24-
{
25-
"id": "59",
26-
"categoryParentId": "0",
27-
"categoryName": "iTunes",
28-
"amazonImage": "https://likecard-space.fra1.digitaloceanspaces.com/categories/f33b9-it.png",
29-
"childs": [
30-
{
31-
"id": "151",
32-
"categoryParentId": "59",
33-
"categoryName": "British iTunes",
34-
"amazonImage": "https://likecard-space.fra1.digitaloceanspaces.com/categories/bb24a-bcd74-netherland.jpg"
35-
}
36-
]
37-
}
38-
]
39-
}')->data;
46+
return optional(self::cURL('categories'))->data;
4047
}
4148

42-
public static function products(array $ids = null)
49+
public static function products(array $ids)
4350
{
44-
if (!$ids) {
45-
return json_decode('{
46-
"response": 0,
47-
"message": "No available products"
48-
}');
49-
}
50-
51-
return json_decode('{
52-
"response": 1,
53-
"data": [
54-
{
55-
"productId": "693",
56-
"categoryId": "267",
57-
"productName": "mobilyTest",
58-
"productPrice": 0.02,
59-
"productImage": "https://likecard-space.fra1.digitaloceanspaces.com/products/066ce-x50.jpg",
60-
"productCurrency": "SAR",
61-
"optionalFieldsExist": 1,
62-
"productOptionalFields": [
63-
{
64-
"id": 332,
65-
"required": "1",
66-
"defaultValue": "",
67-
"hint": "USER ID",
68-
"label": "USER ID",
69-
"fieldTypeId": "10",
70-
"fieldCode": "userid",
71-
"optionalFieldID": "14",
72-
"options": []
73-
}
74-
],
75-
"sellPrice": "0.02",
76-
"available": true,
77-
"vatPercentage": 0
78-
}
79-
]
80-
}')->data;
51+
return optional(
52+
self::cURL(
53+
"products",
54+
[
55+
["ids[]" => implode(",", $ids)]
56+
]
57+
)
58+
)->data;
8159
}
8260

8361
public static function getProductsByCategoryId(int $category_id)
8462
{
85-
return json_decode('{
86-
"response": 1,
87-
"data": [
88-
{
89-
"productId": "693",
90-
"categoryId": "267",
91-
"productName": "mobilyTest",
92-
"productPrice": 0.02,
93-
"productImage": "https://likecard-space.fra1.digitaloceanspaces.com/products/066ce-x50.jpg",
94-
"productCurrency": "SAR",
95-
"optionalFieldsExist": 1,
96-
"productOptionalFields": [
97-
{
98-
"id": 332,
99-
"required": "1",
100-
"defaultValue": "",
101-
"hint": "USER ID",
102-
"label": "USER ID",
103-
"fieldTypeId": "10",
104-
"fieldCode": "userid",
105-
"optionalFieldID": "14",
106-
"options": []
107-
}
108-
],
109-
"sellPrice": "0.02",
110-
"available": true,
111-
"vatPercentage": 0
112-
}
113-
]
114-
}')->data;
63+
return optional(
64+
self::cURL(
65+
"products",
66+
["categoryId" => $category_id]
67+
)
68+
)->data;
11569
}
11670

11771
public static function orders($options = [])
11872
{
119-
return json_decode('{
120-
"response": 1,
121-
"data": [
122-
{
123-
"orderNumber": "12637610",
124-
"orderFinalTotal": "1.05",
125-
"currencySymbol": "SAR",
126-
"orderCreateDate": "2020/01/06 12:07",
127-
"orderCurrentStatus": "completed",
128-
"orderPaymentMethod": "Pocket"
129-
}
130-
]
131-
}')->data;
73+
return optional(self::cURL('orders'))->data;
13274
}
13375

13476
public static function order(int $id)
13577
{
136-
return json_decode('{
137-
"response": 1,
138-
"orderNumber": "12319604",
139-
"orderFinalTotal": "100",
140-
"currencySymbol": "SAR",
141-
"orderCreateDate": "2019-12-25 06:57",
142-
"orderPaymentMethod": "Pocket",
143-
"orderCurrentStatus": "completed",
144-
"serials": [
145-
{
146-
"productId": "376",
147-
"productName": "test-itunes1",
148-
"productImage": "https://likecard-space.fra1.digitaloceanspaces.com/products/4b09d-5656b-buy-one-get-one-2.png",
149-
"serialId": "11562121",
150-
"serialCode": "U0IycUdUWktsL25UaGhOc2JBMmtTUT09",
151-
"serialNumber": "",
152-
"validTo": "25/03/2020"
153-
}
154-
]
155-
}');
78+
return self::cURL('order');
15679
}
15780

15881
public static function createOrder(int $product_id, int $local_id)
15982
{
16083
// want to know what the response looks like in real example
16184
}
85+
86+
public static function exceptionTestCase(string $test_case)
87+
{
88+
return self::cURL($test_case);
89+
}
16290
}

tests/Mock/data/categories.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"response": 1,
3+
"data": [
4+
{
5+
"id": "59",
6+
"categoryParentId": "0",
7+
"categoryName": "iTunes",
8+
"amazonImage": "https://likecard-space.fra1.digitaloceanspaces.com/categories/f33b9-it.png",
9+
"childs": [
10+
{
11+
"id": "151",
12+
"categoryParentId": "59",
13+
"categoryName": "British iTunes",
14+
"amazonImage": "https://likecard-space.fra1.digitaloceanspaces.com/categories/bb24a-bcd74-netherland.jpg"
15+
}
16+
]
17+
}
18+
]
19+
}

tests/Mock/data/check_balance.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"response": 1,
3+
"userId": "1",
4+
"balance": "100",
5+
"currency": "SAR"
6+
}

tests/Mock/data/no_products.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"response": 0,
3+
"message": "No available products"
4+
}

tests/Mock/data/order.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"response": 1,
3+
"orderNumber": "12319604",
4+
"orderFinalTotal": "100",
5+
"currencySymbol": "SAR",
6+
"orderCreateDate": "2019-12-25 06:57",
7+
"orderPaymentMethod": "Pocket",
8+
"orderCurrentStatus": "completed",
9+
"serials": [
10+
{
11+
"productId": "376",
12+
"productName": "test-itunes1",
13+
"productImage": "https://likecard-space.fra1.digitaloceanspaces.com/products/4b09d-5656b-buy-one-get-one-2.png",
14+
"serialId": "11562121",
15+
"serialCode": "U0IycUdUWktsL25UaGhOc2JBMmtTUT09",
16+
"serialNumber": "",
17+
"validTo": "25/03/2020"
18+
}
19+
]
20+
}

0 commit comments

Comments
 (0)