mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-30 01:44:05 +00:00
155 lines
5.5 KiB
PHP
155 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Orders\ControllersLogic;
|
|
|
|
use App\Classes\Exceptions\AccessForbiddenException;
|
|
use App\Classes\Modules\Orders\ControllersLogic\ListOrderTrackingLogic;
|
|
use App\Classes\Modules\Orders\Services\ListsOrderTracking;
|
|
use App\Classes\Modules\Orders\Standards\Rules\CanListTrackings;
|
|
use App\Models\Order;
|
|
use App\Models\PackingList;
|
|
use App\Models\Company;
|
|
use App\Models\CompanyModule;
|
|
use App\Classes\ValueObjects\Constants\OrderType;
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Classes\ValueObjects\Constants\BusinessType;
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Contracts\Encryption\DecryptException;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Artisan;
|
|
use Illuminate\Support\Facades\Crypt;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Mockery;
|
|
use Tests\TestCaseChild;
|
|
|
|
use PHPUnit\Framework\Attributes\Group;
|
|
|
|
#[Group('feature')]
|
|
#[Group('orders')]
|
|
#[Group('controllers_logic')]
|
|
#[Group('ok_to_run')]
|
|
class ListOrderTrackingLogicTest extends TestCaseChild
|
|
{
|
|
// use DatabaseTransactions;
|
|
|
|
protected $listsOrderTrackingMock;
|
|
protected $listOrderTrackingLogic;
|
|
protected $canListTrackingsMock;
|
|
|
|
public function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->canListTrackingsMock = Mockery::mock(CanListTrackings::class);
|
|
$this->listsOrderTrackingMock = Mockery::mock(ListsOrderTracking::class);
|
|
$this->listOrderTrackingLogic = new ListOrderTrackingLogic($this->canListTrackingsMock, $this->listsOrderTrackingMock);
|
|
}
|
|
|
|
public function test_logic_with_valid_tracking_number()
|
|
{
|
|
// A R R A N G E
|
|
$trackingNo = '123456789';
|
|
$reference = 'X202503131317208';
|
|
|
|
$company = new Company();
|
|
$company->name = 'Test Company';
|
|
$company->reference = 'TEST-' . uniqid();
|
|
$company->status = ApprovalStatus::APPROVED;
|
|
$company->save();
|
|
|
|
$companyModule = new CompanyModule();
|
|
$companyModule->company_id = $company->id;
|
|
$companyModule->type = BusinessType::IMPORTER;
|
|
$companyModule->reference = 'TEST-IMP-' . uniqid();
|
|
$companyModule->name = 'Test Company Module';
|
|
$companyModule->unity_hash_id = '123456789';
|
|
$companyModule->unity_signature = '123456789';
|
|
$companyModule->status = ApprovalStatus::APPROVED;
|
|
$companyModule->save();
|
|
|
|
$order = new Order();
|
|
$order->reference = $trackingNo;
|
|
$order->type = OrderType::SHARED_CONTAINER;
|
|
$order->status = ApprovalStatus::APPROVED;
|
|
$order->company_module_id = $companyModule->id;
|
|
$order->save();
|
|
|
|
$packingList = new PackingList();
|
|
$packingList->reference = $reference;
|
|
$packingList->owner_type = Order::class;
|
|
$packingList->owner_id = $order->id;
|
|
$packingList->save();
|
|
|
|
Artisan::shouldReceive('call')
|
|
->once()
|
|
->withArgs(['process-yd-by-traking-no-data-command', ['trackingNo' => $reference]])
|
|
->andReturn(0);
|
|
|
|
$this->listsOrderTrackingMock->shouldReceive('deserializeFilters')
|
|
->andReturn(['tracking_no' => $trackingNo]);
|
|
|
|
$this->listsOrderTrackingMock->shouldReceive('execute')
|
|
->andReturn(collect([]));
|
|
|
|
$fakeRequest = Request::create('/tracking', 'GET', ['filters' => json_encode(['tracking_no' => Crypt::encryptString($trackingNo)])]);
|
|
|
|
// A C T
|
|
$response = $this->listOrderTrackingLogic->logic($fakeRequest);
|
|
|
|
Log::info("response 1: " . json_encode($response));
|
|
|
|
// A S S E R T
|
|
$this->assertInstanceOf(JsonResponse::class, $response);
|
|
}
|
|
|
|
public function test_logic_with_invalid_tracking_number_for_admin()
|
|
{
|
|
// A R R A N G E
|
|
$user = (object) ['type' => ['ADMIN']];
|
|
Auth::shouldReceive('user')->andReturn($user);
|
|
|
|
$this->listsOrderTrackingMock->shouldReceive('deserializeFilters')
|
|
->andReturn(['tracking_no' => Crypt::encryptString('invalid')]);
|
|
|
|
$this->listsOrderTrackingMock->shouldReceive('execute')
|
|
->andReturn(collect([]));
|
|
|
|
$request = Request::create('/tracking', 'GET', ['filters' => json_encode(['tracking_no' => Crypt::encryptString('invalid')])]);
|
|
|
|
Artisan::shouldReceive('call')
|
|
->once()
|
|
->withArgs(['process-yd-by-traking-no-data-command', ['trackingNo' => 'invalid']])
|
|
->andReturn(0);
|
|
|
|
// A C T
|
|
$fakeRequest = $this->listOrderTrackingLogic->logic($request);
|
|
$data = $fakeRequest->getData(true); // returns array
|
|
|
|
// A S S E R T
|
|
$this->assertTrue(empty($data['original']['payload']['data']));
|
|
$this->assertInstanceOf(JsonResponse::class, $fakeRequest);
|
|
}
|
|
|
|
public function test_it_throws_404_when_decryption_fails_for_non_admin()
|
|
{
|
|
// A R R A N G E
|
|
$user = (object) ['type' => ['REGULAR_USER']];
|
|
Auth::shouldReceive('user')->andReturn($user);
|
|
|
|
Crypt::shouldReceive('decryptString')->andThrow(DecryptException::class);
|
|
$request = Request::create('/tracking', 'GET', ['filters' => json_encode(['tracking_no' => 'test'])]);
|
|
|
|
$this->canListTrackingsMock
|
|
->shouldReceive('passes')
|
|
->andThrow(new AccessForbiddenException("You don't have permission to perform this action"));
|
|
|
|
// A S S E R T
|
|
$this->expectException(AccessForbiddenException::class);
|
|
|
|
// A C T
|
|
$this->listOrderTrackingLogic->logic($request);
|
|
}
|
|
}
|