mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-19 04:24:12 +00:00
136 lines
4.7 KiB
PHP
136 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\App\Classes\Modules\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 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;
|
|
|
|
class ListOrderTrackingLogicTest extends TestCaseChild
|
|
{
|
|
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 tearDown(): void
|
|
{
|
|
Mockery::close();
|
|
parent::tearDown();
|
|
}
|
|
|
|
|
|
public function test_logic_with_valid_tracking_number()
|
|
{
|
|
// Arrange
|
|
$trackingNo = '123456789';
|
|
$reference = 'X202503131317208';
|
|
$orderId = 1;
|
|
$packingListId = 1;
|
|
|
|
$mockOrder = Mockery::mock('overload:' . Order::class)->makePartial();
|
|
$mockOrder->shouldReceive('where')
|
|
->with('reference', $trackingNo)
|
|
->andReturnSelf();
|
|
$mockOrder->shouldReceive('first')
|
|
->andReturn($mockOrder);
|
|
$mockOrder->id = $orderId;
|
|
$mockOrder->reference = $trackingNo;
|
|
|
|
$mockPackingList = Mockery::mock('overload:' . PackingList::class)->makePartial();
|
|
$mockPackingList->id = $packingListId;
|
|
$mockPackingList->reference = $reference;
|
|
|
|
$mockOrder->shouldReceive('packingLists')->andReturn(collect([$mockPackingList]));
|
|
|
|
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)])]);
|
|
|
|
// Act
|
|
$response = $this->listOrderTrackingLogic->logic($fakeRequest);
|
|
|
|
Log::info("response 1: " . json_encode($response));
|
|
|
|
// Assert
|
|
$this->assertInstanceOf(JsonResponse::class, $response);
|
|
}
|
|
|
|
public function test_logic_with_invalid_tracking_number_for_admin()
|
|
{
|
|
// Arrange
|
|
$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);
|
|
|
|
// Act
|
|
$fakeRequest = $this->listOrderTrackingLogic->logic($request);
|
|
$data = $fakeRequest->getData(true); // returns array
|
|
|
|
// Assert
|
|
$this->assertTrue(empty($data['original']['payload']['data']));
|
|
$this->assertInstanceOf(JsonResponse::class, $fakeRequest);
|
|
}
|
|
|
|
public function test_it_throws_404_when_decryption_fails_for_non_admin()
|
|
{
|
|
// Arrange
|
|
$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"));
|
|
|
|
// Assert
|
|
$this->expectException(AccessForbiddenException::class);
|
|
|
|
// Act
|
|
$this->listOrderTrackingLogic->logic($request);
|
|
}
|
|
}
|