|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace UnitTestFiles\Test; |
| 4 | + |
| 5 | +use Route4Me\Constants; |
| 6 | +use Route4Me\Route4Me; |
| 7 | +use Route4Me\V5\Orders\CustomData; |
| 8 | +use Route4Me\V5\Orders\CustomField; |
| 9 | +use Route4Me\V5\Orders\GPSCoords; |
| 10 | +use Route4Me\V5\Orders\LocalTimeWindow; |
| 11 | +use Route4Me\V5\Orders\Order; |
| 12 | +use Route4Me\V5\Orders\Orders; |
| 13 | +use Route4Me\V5\Orders\ResponseOrder; |
| 14 | +use Route4Me\V5\Orders\ResponseSearch; |
| 15 | + |
| 16 | +final class OrderUnitTests extends \PHPUnit\Framework\TestCase |
| 17 | +{ |
| 18 | + public static ?string $created_uuid = null; |
| 19 | + public static ?string $created_field_uuid = null; |
| 20 | + |
| 21 | + public static function setUpBeforeClass() : void |
| 22 | + { |
| 23 | + Route4Me::setApiKey(Constants::API_KEY); |
| 24 | + } |
| 25 | + |
| 26 | + public function testCustomDataCanBeCreateEmpty() : void |
| 27 | + { |
| 28 | + $this->assertInstanceOf(CustomData::class, new CustomData()); |
| 29 | + } |
| 30 | + |
| 31 | + public function testCustomDataCanBeCreateFromArray() : void |
| 32 | + { |
| 33 | + $this->assertInstanceOf(CustomData::class, new CustomData([ |
| 34 | + 'barcode' => '1', |
| 35 | + 'airbillno' => '2', |
| 36 | + 'sorted_on_date' => '3', |
| 37 | + 'sorted_on_utc' => 1 |
| 38 | + ])); |
| 39 | + } |
| 40 | + |
| 41 | + public function testCustomFieldCanBeCreateEmpty() : void |
| 42 | + { |
| 43 | + $this->assertInstanceOf(CustomField::class, new CustomField()); |
| 44 | + } |
| 45 | + |
| 46 | + public function testCustomFieldCanBeCreateFromArray() : void |
| 47 | + { |
| 48 | + $this->assertInstanceOf(CustomField::class, new CustomField([ |
| 49 | + 'order_custom_field_uuid' => 'uuid', |
| 50 | + 'order_custom_field_value' => 'value' |
| 51 | + ])); |
| 52 | + } |
| 53 | + |
| 54 | + public function testGPSCoordsCanBeCreateEmpty() : void |
| 55 | + { |
| 56 | + $this->assertInstanceOf(GPSCoords::class, new GPSCoords()); |
| 57 | + } |
| 58 | + |
| 59 | + public function testGPSCoordsCanBeCreateByConstructor() : void |
| 60 | + { |
| 61 | + $coords = new GPSCoords(40.5, 90.0); |
| 62 | + $this->assertInstanceOf(GPSCoords::class, $coords); |
| 63 | + $this->assertEquals($coords->lat, 40.5); |
| 64 | + $this->assertEquals($coords->lng, 90.0); |
| 65 | + } |
| 66 | + |
| 67 | + public function testGPSCoordsCanBeCreateFromArray() : void |
| 68 | + { |
| 69 | + $coords = new GPSCoords([ |
| 70 | + 'lat' => 40.5, |
| 71 | + 'lng' => 90.0 |
| 72 | + ]); |
| 73 | + $this->assertInstanceOf(GPSCoords::class, $coords); |
| 74 | + $this->assertEquals($coords->lat, 40.5); |
| 75 | + $this->assertEquals($coords->lng, 90.0); |
| 76 | + } |
| 77 | + |
| 78 | + public function testLocalTimeWindowCanBeCreateEmpty() : void |
| 79 | + { |
| 80 | + $this->assertInstanceOf(LocalTimeWindow::class, new LocalTimeWindow()); |
| 81 | + } |
| 82 | + |
| 83 | + public function testLocalTimeWindowCanBeCreateByConstructor() : void |
| 84 | + { |
| 85 | + $coords = new LocalTimeWindow(1, 2); |
| 86 | + $this->assertInstanceOf(LocalTimeWindow::class, $coords); |
| 87 | + $this->assertEquals($coords->start, 1); |
| 88 | + $this->assertEquals($coords->end, 2); |
| 89 | + } |
| 90 | + |
| 91 | + public function testLocalTimeWindowCanBeCreateFromArray() : void |
| 92 | + { |
| 93 | + $coords = new LocalTimeWindow([ |
| 94 | + 'start' => 1, |
| 95 | + 'end' => 2 |
| 96 | + ]); |
| 97 | + $this->assertInstanceOf(LocalTimeWindow::class, $coords); |
| 98 | + $this->assertEquals($coords->start, 1); |
| 99 | + $this->assertEquals($coords->end, 2); |
| 100 | + } |
| 101 | + |
| 102 | + public function testOrderCanBeCreateEmpty() : void |
| 103 | + { |
| 104 | + $this->assertInstanceOf(Order::class, new Order()); |
| 105 | + } |
| 106 | + |
| 107 | + public function testOrderCanBeCreateFromArray() : void |
| 108 | + { |
| 109 | + $order = new Order([ |
| 110 | + 'first_name' => 'John', |
| 111 | + 'last_name' => 'Doe', |
| 112 | + |
| 113 | + ]); |
| 114 | + $this->assertInstanceOf(Order::class, $order); |
| 115 | + $this->assertEquals($order->first_name, 'John'); |
| 116 | + $this->assertEquals($order->last_name, 'Doe'); |
| 117 | + } |
| 118 | + |
| 119 | + public function testResponseOrderCanBeCreateEmpty() : void |
| 120 | + { |
| 121 | + $this->assertInstanceOf(Order::class, new Order()); |
| 122 | + } |
| 123 | + |
| 124 | + public function testResponseOrderCanBeCreateFromArray() : void |
| 125 | + { |
| 126 | + $order = new ResponseOrder([ |
| 127 | + 'first_name' => 'John', |
| 128 | + 'last_name' => 'Doe', |
| 129 | + |
| 130 | + ]); |
| 131 | + $this->assertInstanceOf(ResponseOrder::class, $order); |
| 132 | + $this->assertEquals($order->first_name, 'John'); |
| 133 | + $this->assertEquals($order->last_name, 'Doe'); |
| 134 | + } |
| 135 | + |
| 136 | + public function testCreateMustReturnResponseOrder() : void |
| 137 | + { |
| 138 | + $orders = new Orders(); |
| 139 | + $order = $orders->create([ |
| 140 | + 'address_1' => '1358 E Luzerne St, Philadelphia, PA 19124, US', |
| 141 | + 'address_alias' => 'Auto test address', |
| 142 | + 'address_city' => 'Philadelphia', |
| 143 | + 'address_geo' => [ |
| 144 | + 'lat' => 48.335991, |
| 145 | + 'lng' => 31.18287 |
| 146 | + ], |
| 147 | + 'first_name' => 'John', |
| 148 | + 'last_name' => 'Doe', |
| 149 | + |
| 150 | + ]); |
| 151 | + |
| 152 | + $this->assertInstanceOf(ResponseOrder::class, $order); |
| 153 | + $this->assertNotNull($order->first_name); |
| 154 | + $this->assertEquals($order->first_name, 'John'); |
| 155 | + |
| 156 | + self::$created_uuid = $order->order_uuid; |
| 157 | + } |
| 158 | + |
| 159 | + public function testGetMustReturnResponseOrder() : void |
| 160 | + { |
| 161 | + if (self::$created_uuid !== null) { |
| 162 | + $orders = new Orders(); |
| 163 | + $order = $orders->get(self::$created_uuid); |
| 164 | + |
| 165 | + $this->assertInstanceOf(ResponseOrder::class, $order); |
| 166 | + $this->assertNotNull($order->first_name); |
| 167 | + $this->assertEquals($order->first_name, 'John'); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + public function testUpdateMustReturnResponseOrder() : void |
| 172 | + { |
| 173 | + if (self::$created_uuid !== null) { |
| 174 | + $orders = new Orders(); |
| 175 | + $order = $orders->update(self::$created_uuid, ['first_name' => 'Jane']); |
| 176 | + |
| 177 | + $this->assertInstanceOf(ResponseOrder::class, $order); |
| 178 | + $this->assertNotNull($order->first_name); |
| 179 | + $this->assertEquals($order->first_name, 'Jane'); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + public function testSearchMustReturnResponseSearchWithoutParams() : void |
| 184 | + { |
| 185 | + $orders = new Orders(); |
| 186 | + $res = $orders->search(); |
| 187 | + |
| 188 | + $this->assertInstanceOf(ResponseSearch::class, $res); |
| 189 | + $this->assertNotNull($res->total); |
| 190 | + } |
| 191 | + |
| 192 | + public function testSearchMustReturnResponseSearchWithParams() : void |
| 193 | + { |
| 194 | + if (self::$created_uuid !== null) { |
| 195 | + $orders = new Orders(); |
| 196 | + $params = [ |
| 197 | + "filters" => [ |
| 198 | + "order_ids" => [self::$created_uuid] |
| 199 | + ] |
| 200 | + ]; |
| 201 | + $res = $orders->search($params); |
| 202 | + |
| 203 | + $this->assertInstanceOf(ResponseSearch::class, $res); |
| 204 | + $this->assertNotNull($res->total); |
| 205 | + $this->assertNotNull($res->results); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + public function testBatchCreateMustReturnTrue() : void |
| 210 | + { |
| 211 | + $this->markTestSkipped('must be revisited, cannot get back IDs or created Orders.'); |
| 212 | + |
| 213 | + $orders = new Orders(); |
| 214 | + $params = [ |
| 215 | + [ |
| 216 | + 'address_1' => '1358 E Luzerne St, Philadelphia, PA 19124, US', |
| 217 | + 'address_alias' => 'Address for batch workflow 0', |
| 218 | + 'address_city' => 'Philadelphia', |
| 219 | + 'address_geo' => [ |
| 220 | + 'lat' => 48.335991, |
| 221 | + 'lng' => 31.18287 |
| 222 | + ], |
| 223 | + 'first_name' => 'John', |
| 224 | + 'last_name' => 'Doe', |
| 225 | + |
| 226 | + ], [ |
| 227 | + 'address_1' => '1358 E Luzerne St, Philadelphia, PA 19124, US', |
| 228 | + 'address_alias' => 'Address for batch workflow 1', |
| 229 | + 'address_city' => 'Philadelphia', |
| 230 | + 'address_geo' => [ |
| 231 | + 'lat' => 48.335991, |
| 232 | + 'lng' => 31.18287 |
| 233 | + ], |
| 234 | + 'first_name' => 'John', |
| 235 | + 'last_name' => 'Doe', |
| 236 | + |
| 237 | + ] |
| 238 | + ]; |
| 239 | + $res = $orders->batchCreate($params); |
| 240 | + |
| 241 | + $this->assertIsBool($res); |
| 242 | + $this->assertTrue($res); |
| 243 | + } |
| 244 | + |
| 245 | + public function testBatchUpdateMustReturnArray() : void |
| 246 | + { |
| 247 | + $this->markTestSkipped('must be revisited, cannot get back IDs or created Orders.'); |
| 248 | + |
| 249 | + $orders = new Orders(); |
| 250 | + $orderIds = ['', '']; |
| 251 | + $data = [ |
| 252 | + 'first_name' => 'Jane' |
| 253 | + ]; |
| 254 | + $res = $orders->batchUpdate($orderIds, $data); |
| 255 | + |
| 256 | + $this->assertIsArray($res); |
| 257 | + } |
| 258 | + |
| 259 | + public function testBatchUpdateByFiltersMustReturnTrue() : void |
| 260 | + { |
| 261 | + $this->markTestSkipped('must be revisited, cannot get back IDs or created Orders.'); |
| 262 | + |
| 263 | + $orders = new Orders(); |
| 264 | + $params = [ |
| 265 | + 'data' => [ |
| 266 | + 'first_name' => 'John' |
| 267 | + ], |
| 268 | + 'filters' => [ |
| 269 | + 'orderIds' => ['', ''] |
| 270 | + ] |
| 271 | + ]; |
| 272 | + $res = $orders->batchUpdateByFilters($params); |
| 273 | + |
| 274 | + $this->assertIsBool($res); |
| 275 | + $this->assertTrue($res); |
| 276 | + } |
| 277 | + |
| 278 | + public function testBatchDeleteMustReturnTrue() : void |
| 279 | + { |
| 280 | + $this->markTestSkipped('must be revisited, cannot get back IDs or created Orders.'); |
| 281 | + |
| 282 | + $orders = new Orders(); |
| 283 | + $orderIds = ['', '']; |
| 284 | + $res = $orders->batchDelete($orderIds); |
| 285 | + |
| 286 | + $this->assertIsBool($res); |
| 287 | + $this->assertTrue($res); |
| 288 | + } |
| 289 | + |
| 290 | + public function testGetOrderCustomFieldsReturnArray() : void |
| 291 | + { |
| 292 | + $orders = new Orders(); |
| 293 | + $res = $orders->getOrderCustomFields(); |
| 294 | + |
| 295 | + $this->assertIsArray($res); |
| 296 | + } |
| 297 | + |
| 298 | + public function testCreateOrderCustomFieldMustReturnCustomField() : void |
| 299 | + { |
| 300 | + $orders = new Orders(); |
| 301 | + $field = $orders->createOrderCustomField([ |
| 302 | + 'order_custom_field_name' => 'CustomField1', |
| 303 | + 'order_custom_field_label' => 'Custom Field 1', |
| 304 | + 'order_custom_field_type' => 'checkbox', |
| 305 | + 'order_custom_field_type_info' => [ |
| 306 | + 'short_label' => 'cFl1' |
| 307 | + ] |
| 308 | + ]); |
| 309 | + |
| 310 | + $this->assertInstanceOf(CustomField::class, $field); |
| 311 | + $this->assertNotNull($field->order_custom_field_label); |
| 312 | + $this->assertEquals($field->order_custom_field_label, 'Custom Field 1'); |
| 313 | + |
| 314 | + self::$created_field_uuid = $field->order_custom_field_uuid; |
| 315 | + } |
| 316 | + |
| 317 | + public function testUpdateOrderCustomFieldMustReturnCustomField() : void |
| 318 | + { |
| 319 | + if (self::$created_field_uuid !== null) { |
| 320 | + $orders = new Orders(); |
| 321 | + $field = $orders->updateOrderCustomField(self::$created_field_uuid, [ |
| 322 | + 'order_custom_field_label' => 'Custom Field New', |
| 323 | + 'order_custom_field_type' => 'checkbox', |
| 324 | + 'order_custom_field_type_info' => [ |
| 325 | + 'short_label' => 'cFl1' |
| 326 | + ] |
| 327 | + ]); |
| 328 | + |
| 329 | + $this->assertInstanceOf(CustomField::class, $field); |
| 330 | + $this->assertNotNull($field->order_custom_field_label); |
| 331 | + $this->assertEquals($field->order_custom_field_label, 'Custom Field New'); |
| 332 | + } |
| 333 | + } |
| 334 | + |
| 335 | + public function testDeleteOrderCustomFieldMustReturnCustomField() : void |
| 336 | + { |
| 337 | + if (self::$created_field_uuid !== null) { |
| 338 | + $orders = new Orders(); |
| 339 | + $field = $orders->deleteOrderCustomField(self::$created_field_uuid); |
| 340 | + |
| 341 | + $this->assertInstanceOf(CustomField::class, $field); |
| 342 | + self::$created_field_uuid = null; |
| 343 | + } |
| 344 | + } |
| 345 | + |
| 346 | + public function testDeleteMustReturnTrue() : void |
| 347 | + { |
| 348 | + if (self::$created_uuid !== null) { |
| 349 | + $orders = new Orders(); |
| 350 | + $result = $orders->delete(self::$created_uuid); |
| 351 | + |
| 352 | + $this->assertTrue($result); |
| 353 | + self::$created_uuid = null; |
| 354 | + } |
| 355 | + } |
| 356 | + |
| 357 | + public static function tearDownAfterClass() : void |
| 358 | + { |
| 359 | + sleep(1); |
| 360 | + |
| 361 | + if (self::$created_uuid !== null || self::$created_field_uuid !== null) { |
| 362 | + $orders = new Orders(); |
| 363 | + if (self::$created_uuid !== null) { |
| 364 | + $orders->delete(self::$created_uuid); |
| 365 | + } |
| 366 | + if (self::$created_field_uuid !== null) { |
| 367 | + $orders->deleteOrderCustomField(self::$created_field_uuid); |
| 368 | + } |
| 369 | + } |
| 370 | + } |
| 371 | +} |
0 commit comments