-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'ktmd' of https://github.com/afiqiqmal/parcel-track
- Loading branch information
Showing
6 changed files
with
133 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: hafiq | ||
* Date: 01/05/2018 | ||
* Time: 11:02 PM | ||
*/ | ||
|
||
namespace afiqiqmal\ParcelTrack\Tracker; | ||
|
||
use Carbon\Carbon; | ||
|
||
class KTMD extends BaseTracker | ||
{ | ||
protected $url = "http://parcel.ktmd.com.my/ops/ws/track_consignment.php"; | ||
protected $source = "KTM Distribution Sdn Bhd"; | ||
protected $code = "ktmd"; | ||
protected $method = PARCEL_METHOD_POST; | ||
|
||
public function setTrackingNumber($refNum) | ||
{ | ||
parent::setTrackingNumber($refNum); | ||
return [ | ||
'consignments' => $refNum, | ||
]; | ||
} | ||
|
||
public function getHeader() | ||
{ | ||
return [ | ||
'Accept' => 'application/json' | ||
]; | ||
} | ||
|
||
public function rawOutput() | ||
{ | ||
return false; | ||
} | ||
|
||
public function startCrawl($result) | ||
{ | ||
$finalOutput = []; | ||
if (isset($result['body'])) { | ||
$output = $result['body']; | ||
if ($output[0]['date'] == '-') { | ||
return $this->buildResponse($result, []); | ||
} | ||
$output = array_reverse($output); | ||
foreach ($output as $key => $item) { | ||
$data = []; | ||
$date = trim($item['date']); | ||
$parcel = Carbon::createFromFormat("Y-m-d", $date); | ||
|
||
$data['date'] = $parcel->toDateTimeString(); | ||
$data['timestamp'] = $parcel->timestamp; | ||
$data['process'] = trim_spaces($item['description']); | ||
$data['type'] = $this->distinguishProcess(trim_spaces($item['description']), $item == reset($output)); | ||
$data['event'] = isset($item['location']) ? trim_spaces($item['location']) : null; | ||
|
||
$finalOutput[] = $data; | ||
} | ||
|
||
return $this->buildResponse($result, $finalOutput, 200, false); | ||
} | ||
|
||
return $this->buildResponse($result, []); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
namespace Tests; | ||
|
||
require_once __DIR__ .'/../vendor/autoload.php'; | ||
|
||
use afiqiqmal\ParcelTrack\Tracker\CityLink; | ||
use afiqiqmal\ParcelTrack\Tracker\DHL; | ||
use PHPUnit\Framework\TestCase; | ||
/** | ||
* RequestTest.php | ||
* to test function in Request class | ||
*/ | ||
class KTMBTest extends TestCase | ||
{ | ||
function testKTMBSuccess() | ||
{ | ||
$result = parcel_track()->dhlExpress()->setTrackingNumber("103154269")->fetch(); | ||
|
||
$this->assertTrue(true); | ||
$this->assertEquals(200, $result['code']); | ||
} | ||
|
||
function testKTMBEmptySuccess() | ||
{ | ||
$result = parcel_track()->dhlExpress()->setTrackingNumber("103154269AAAA")->fetch(); | ||
|
||
$this->assertTrue(count($result['tracker']['checkpoints']) == 0); | ||
$this->assertEquals(200, $result['code']); | ||
} | ||
|
||
function testKTMBFailed() | ||
{ | ||
$result = parcel_track()->setTrackingNumber("103154269")->fetch(); | ||
$this->assertTrue($result['error']); | ||
$this->assertEquals(400, $result['code']); | ||
} | ||
|
||
function testKTMBCheckCarrier() | ||
{ | ||
$result = parcel_track()->setTrackingNumber("103154269")->checkCourier(); | ||
$this->assertFalse($result['error']); | ||
$this->assertTrue(in_array((new DHL())->getSourceName(), $result['possible_courier'])); | ||
} | ||
} |