Skip to content

Commit

Permalink
chore: initial 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
fyvri committed Dec 7, 2024
0 parents commit f1adf57
Show file tree
Hide file tree
Showing 9 changed files with 379 additions and 0 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: "Static Analysis"

on:
push:
paths:
- .github/workflows/static-analysis.yml
- composer.*
- src/**
- tests/**

pull_request:
paths:
- .github/workflows/static-analysis.yml
- composer.*
- src/**
- tests/**

jobs:
phpunit:
name: PHP ${{ matrix.php }} - ${{ matrix.stability }}
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
php: [8.1, 8.2, 8.3, 8.4]
stability: [prefer-stable]

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none

- name: Install Composer dependencies
run: composer install --no-progress --no-suggest

- name: Run PHPUnit
run: vendor/bin/phpunit --testdox
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/vendor
/coverage
composer.phar
composer.lock
/.phpunit.cache
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Azis Alvriyanto <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Shopee Express Waybill

[![Donate](https://img.shields.io/badge/donate-saweria-orange.svg)](https://saweria.co/azisalvriyanto)
</br>
[![Latest Stable Version](https://img.shields.io/packagist/v/azis-alvriyanto/shopee-express-waybill.svg)](https://packagist.org/packages/azis-alvriyanto/shopee-express-waybill)
[![PHP from Stable Version](https://img.shields.io/packagist/php-v/azis-alvriyanto/shopee-express-waybill)](https://www.php.net)
[![Static Analysis](https://github.com/fyvri/shopee-express-waybill/actions/workflows/static-analysis.yml/badge.svg)](https://github.com/fyvri/shopee-express-waybill/actions/workflows/static-analysis.yml)
</br>
[![Total Downloads](https://poser.pugx.org/azis-alvriyanto/shopee-express-waybill/downloads)](https://packagist.org/packages/azis-alvriyanto/shopee-express-waybill/stats)
[![License](https://poser.pugx.org/azis-alvriyanto/shopee-express-waybill/license)](https://github.com/fyvri/shopee-express-waybill/blob/HEAD/LICENSE.md)

## Requirements

- [PHP >= 8.1](http://php.net/)
- A cup of coffee :coffee:

## Installation

```bash
composer require azis-alvriyanto/shopee-express-waybill
```

## Usage

```php
use AzisAlvriyanto\ShopeeExpressWaybill\ShopeeExpressWaybill;

// Initialize
$shopeeExpressWaybill = new ShopeeExpressWaybill();

// Example: Check shipping number
$response = $shopeeExpressWaybill->check('SPXID133333333337');

// Output response
if ($response->success) {
echo "Success: " . $response->message;
print_r($response->data);
} else {
echo "Error: " . $response->message;
}
```

## Testing

To run the tests, use PHPUnit. Ensure PHPUnit is installed as a development dependency:

```bash
composer install --dev
composer test
```

## API Documentation

Server API ready to use publicly in
[https://shopee-express-waybill.membasuh.com](https://shopee-express-waybill.membasuh.com)
or you can get postman collection [here](https://documenter.getpostman.com/view/6937269/2sA3s4nAx2).

## Contributing

Contributions are welcome! Please fork the repository and submit a pull request.

## Credits

- [Kyle Archangel](https://github.com/kylearchangel)
- [Ingin Menjadi Programmer Handal, Namun Enggan Ngoding](https://facebook.com/groups/programmerhandal/)
- [All Contributors](https://github.com/fyvri/shopee-express-waybill/graphs/contributors)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
32 changes: 32 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "azis-alvriyanto/shopee-express-waybill",
"description": "Shopee Express Waybill",
"keywords": [
"shopee",
"cek-resi",
"shopee-express",
"shopee-express-waybill"
],
"require": {
"php": "^8.1"
},
"license": "MIT",
"autoload": {
"psr-4": {
"AzisAlvriyanto\\ShopeeExpressWaybill\\": "src/"
}
},
"authors": [
{
"name": "Azis Alvriyanto",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"require-dev": {
"phpunit/phpunit": "^9.5|^10|^11|^12"
},
"scripts": {
"test": "phpunit"
}
}
23 changes: 23 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/phpunit.xsd"
backupGlobals="false"
bootstrap="vendor/autoload.php"
colors="true"
processIsolation="false"
stopOnFailure="false"
executionOrder="random"
failOnWarning="true"
failOnRisky="true"
failOnEmptyTestSuite="true"
beStrictAboutOutputDuringTests="true"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
>
<testsuites>
<testsuite name="Azis Alvriyanto Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
41 changes: 41 additions & 0 deletions src/CurlWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace AzisAlvriyanto\ShopeeExpressWaybill;

class CurlWrapper
{
public function init($url)
{
return curl_init($url);
}

public function setopt($ch, $option, $value)
{
return curl_setopt($ch, $option, $value);
}

public function exec($ch)
{
return curl_exec($ch);
}

public function errno($ch)
{
return curl_errno($ch);
}

public function error($ch)
{
return curl_error($ch);
}

public function getinfo($ch, $option)
{
return curl_getinfo($ch, $option);
}

public function close($ch)
{
return curl_close($ch);
}
}
61 changes: 61 additions & 0 deletions src/ShopeeExpressWaybill.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace AzisAlvriyanto\ShopeeExpressWaybill;

class ShopeeExpressWaybill
{
protected $curl;

public function __construct($curl = null)
{
$this->curl = $curl ?? new CurlWrapper();
}

public function check(string $shippingNumber): object
{
$apiUrl = 'https://spx.co.id/api/v2/fleet_order/tracking/search';
$currentUnixTime = floor(time());
$url = $apiUrl . '?sls_tracking_number=' . $shippingNumber . '|' . $currentUnixTime . '' . hash('sha256', $shippingNumber . '' . $currentUnixTime . 'MGViZmZmZTYzZDJhNDgxY2Y1N2ZlN2Q1ZWJkYzlmZDY=');

$ch = $this->curl->init($url);
$this->curl->setopt($ch, CURLOPT_RETURNTRANSFER, true);
$this->curl->setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$this->curl->setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$res = $this->curl->exec($ch);
if ($this->curl->errno($ch)) {
$res = [
'success' => false,
'message' => $this->curl->error($ch) ?? 'Sorry, looks like there are some errors detected, please try again',
'data' => null,
];
} else {
$responseCode = $this->curl->getinfo($ch, CURLINFO_HTTP_CODE);
if ($responseCode == 200) {
$res = json_decode($res, true);
if (isset($res['retcode']) && $res['retcode'] === 0) {
$res = [
'success' => true,
'message' => 'Shipping number found',
'data' => $res['data'],
];
} else {
$res = [
'success' => false,
'message' => 'Shipping number not found',
'data' => null,
];
}
} else {
$res = [
'success' => false,
'message' => 'Failed to retrieve data, HTTP status code: ' . $responseCode,
'data' => null,
];
}
}
$this->curl->close($ch);

return (object) $res;
}
}
84 changes: 84 additions & 0 deletions tests/ShopeeExpressWaybillTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

use PHPUnit\Framework\TestCase;
use AzisAlvriyanto\ShopeeExpressWaybill\ShopeeExpressWaybill;
use AzisAlvriyanto\ShopeeExpressWaybill\CurlWrapper;

class ShopeeExpressWaybillTest extends TestCase
{
private $mockCurl;

protected function setUp(): void
{
parent::setUp();

$this->mockCurl = $this->getMockBuilder(CurlWrapper::class)
->onlyMethods(['init', 'setopt', 'exec', 'errno', 'error', 'getinfo', 'close'])
->getMock();

$this->mockCurl->method('init')->willReturn(true);
}

public function testCheckSuccessfulResponse()
{
$this->mockCurl->method('exec')->willReturn(json_encode(['retcode' => 0, 'data' => 'some_tracking_data']));
$this->mockCurl->method('errno')->willReturn(0);
$this->mockCurl->method('error')->willReturn('');
$this->mockCurl->method('getinfo')->willReturn(200);

$shopeeExpressWaybill = new ShopeeExpressWaybill($this->mockCurl);
$response = $shopeeExpressWaybill->check('valid_tracking_number');

$this->assertIsObject($response);
$this->assertTrue($response->success);
$this->assertEquals('Shipping number found', $response->message);
$this->assertEquals('some_tracking_data', $response->data);
}

public function testCheckTrackingNotFound()
{
$this->mockCurl->method('exec')->willReturn(json_encode(['retcode' => 1]));
$this->mockCurl->method('errno')->willReturn(0);
$this->mockCurl->method('error')->willReturn('');
$this->mockCurl->method('getinfo')->willReturn(200);

$shopeeExpressWaybill = new ShopeeExpressWaybill($this->mockCurl);
$response = $shopeeExpressWaybill->check('invalid_tracking_number');

$this->assertIsObject($response);
$this->assertFalse($response->success);
$this->assertEquals('Shipping number not found', $response->message);
$this->assertNull($response->data);
}

public function testCheckCurlError()
{
$this->mockCurl->method('exec')->willReturn(false);
$this->mockCurl->method('errno')->willReturn(28); // Timeout error code
$this->mockCurl->method('error')->willReturn('Timeout was reached');

$shopeeExpressWaybill = new ShopeeExpressWaybill($this->mockCurl);
$response = $shopeeExpressWaybill->check('any_tracking_number');

$this->assertIsObject($response);
$this->assertFalse($response->success);
$this->assertEquals('Timeout was reached', $response->message);
$this->assertNull($response->data);
}

public function testCheckHttpErrorResponse()
{
$this->mockCurl->method('exec')->willReturn(json_encode(['retcode' => 0, 'data' => null]));
$this->mockCurl->method('errno')->willReturn(0);
$this->mockCurl->method('error')->willReturn('');
$this->mockCurl->method('getinfo')->willReturn(404); // Not Found HTTP status

$shopeeExpressWaybill = new ShopeeExpressWaybill($this->mockCurl);
$response = $shopeeExpressWaybill->check('non_existent_tracking_number');

$this->assertIsObject($response);
$this->assertFalse($response->success);
$this->assertEquals('Failed to retrieve data, HTTP status code: 404', $response->message);
$this->assertNull($response->data);
}
}

0 comments on commit f1adf57

Please sign in to comment.