-
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 'ups' of https://github.com/afiqiqmal/parcel-track
- Loading branch information
Showing
7 changed files
with
144 additions
and
3 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,76 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: hafiq | ||
* Date: 01/05/2018 | ||
* Time: 11:02 PM | ||
*/ | ||
|
||
namespace afiqiqmal\ParcelTrack\Tracker; | ||
|
||
use Carbon\Carbon; | ||
use function foo\func; | ||
use Symfony\Component\DomCrawler\Crawler; | ||
|
||
class UPS extends BaseTracker | ||
{ | ||
protected $url = "https://wwwapps.ups.com/WebTracking/track"; | ||
protected $source = "United Parcel Service (M) Sdn Bhd (Main)"; | ||
protected $code = "ups"; | ||
protected $method = PARCEL_METHOD_POST; | ||
|
||
public function setTrackingNumber($refNum) | ||
{ | ||
parent::setTrackingNumber($refNum); | ||
return [ | ||
'trackNums' => $refNum, | ||
'track.x' => 'Track' | ||
]; | ||
} | ||
|
||
public function startCrawl($result) | ||
{ | ||
if (isset($result['body'])) { | ||
$crawler = new Crawler($result['body']); | ||
|
||
$count = $crawler->filter('.module3 table tr:not(:first-child)')->count(); | ||
$crawlerResult = $crawler->filter('.module3 table tr:not(:first-child)')->each(function (Crawler $node, $i) use ($count) { | ||
$result = $node->filter('td')->each(function (Crawler $node, $x) use ($i, $count) { | ||
return trim_spaces($node->text()); | ||
}); | ||
|
||
$data = []; | ||
$currentDate = null; | ||
foreach ($result as $key => $item) { | ||
if ($key == 0) { | ||
$data['event'] = $item; | ||
} | ||
if ($key == 1) { | ||
$currentDate = $item; | ||
} | ||
if ($key == 2) { | ||
try { | ||
$dates = Carbon::createFromFormat("d/m/Y H:i", $currentDate . ' ' . $item); | ||
$data['date'] = $dates->toDateTimeString(); | ||
$data['timestamp'] = $dates->timestamp; | ||
} catch (\Exception $exception) { | ||
$data['date'] = null; | ||
$data['timestamp'] = 0; | ||
} | ||
} | ||
|
||
if ($key == 3) { | ||
$data['process'] = $item; | ||
$data['type'] = $this->distinguishProcess($item, $i == ($count - 1)); | ||
} | ||
} | ||
|
||
return $data; | ||
}); | ||
|
||
return $this->buildResponse($result, $crawlerResult); | ||
} | ||
|
||
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
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,43 @@ | ||
<?php | ||
namespace Tests; | ||
|
||
require_once __DIR__ .'/../vendor/autoload.php'; | ||
|
||
use afiqiqmal\ParcelTrack\Tracker\UPS; | ||
use PHPUnit\Framework\TestCase; | ||
/** | ||
* RequestTest.php | ||
* to test function in Request class | ||
*/ | ||
class UPSTest extends TestCase | ||
{ | ||
function testUPSSuccess() | ||
{ | ||
$result = parcel_track()->ups()->setTrackingNumber("1Z0V255F0498628539")->fetch(); | ||
|
||
$this->assertTrue(true); | ||
$this->assertEquals(200, $result['code']); | ||
} | ||
|
||
function testUPSEmptySuccess() | ||
{ | ||
$result = parcel_track()->ups()->setTrackingNumber("")->fetch(); | ||
|
||
$this->assertTrue(count($result['tracker']['checkpoints']) == 0); | ||
$this->assertEquals(200, $result['code']); | ||
} | ||
|
||
function testUPSFailed() | ||
{ | ||
$result = parcel_track()->setTrackingNumber("1Z0V255F04986285")->fetch(); | ||
$this->assertTrue($result['error']); | ||
$this->assertEquals(400, $result['code']); | ||
} | ||
|
||
function testUPSCheckCarrier() | ||
{ | ||
$result = parcel_track()->setTrackingNumber("1Z0V255F0498628539")->checkCourier(); | ||
$this->assertFalse($result['error']); | ||
$this->assertTrue(in_array((new UPS())->getSourceName(), $result['possible_courier'])); | ||
} | ||
} |