|
2 | 2 |
|
3 | 3 | namespace CodeBugLab\Like4Card\Tests\Mock; |
4 | 4 |
|
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; |
6 | 9 |
|
7 | | -class Like4CardAPIMock implements Like4CardInterface |
| 10 | +class Like4CardAPIMock extends Like4CardAPI |
8 | 11 | { |
| 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 | + |
9 | 39 | public static function balance() |
10 | 40 | { |
11 | | - return json_decode('{ |
12 | | - "response": 1, |
13 | | - "userId": "1", |
14 | | - "balance": "100", |
15 | | - "currency": "SAR" |
16 | | - }'); |
| 41 | + return self::cURL('check_balance'); |
17 | 42 | } |
18 | 43 |
|
19 | 44 | public static function categories() |
20 | 45 | { |
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; |
40 | 47 | } |
41 | 48 |
|
42 | | - public static function products(array $ids = null) |
| 49 | + public static function products(array $ids) |
43 | 50 | { |
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; |
81 | 59 | } |
82 | 60 |
|
83 | 61 | public static function getProductsByCategoryId(int $category_id) |
84 | 62 | { |
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; |
115 | 69 | } |
116 | 70 |
|
117 | 71 | public static function orders($options = []) |
118 | 72 | { |
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; |
132 | 74 | } |
133 | 75 |
|
134 | 76 | public static function order(int $id) |
135 | 77 | { |
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'); |
156 | 79 | } |
157 | 80 |
|
158 | 81 | public static function createOrder(int $product_id, int $local_id) |
159 | 82 | { |
160 | 83 | // want to know what the response looks like in real example |
161 | 84 | } |
| 85 | + |
| 86 | + public static function exceptionTestCase(string $test_case) |
| 87 | + { |
| 88 | + return self::cURL($test_case); |
| 89 | + } |
162 | 90 | } |
0 commit comments