From 17cd8bd9f2b8197c1088d70feef2f3ab6e264b66 Mon Sep 17 00:00:00 2001 From: "@ChristelLoftus" <christel@bob.co.za> Date: Tue, 3 Sep 2024 12:57:26 +0200 Subject: [PATCH] remove old tracking classes and functions --- Model/Carrier/BobGo.php | 269 ------------------ .../DataProviders/Tracking/ChangeTitle.php | 31 -- Plugin/Block/Tracking/PopUpDeliveryDate.php | 53 ---- .../Tracking/ChangeTitleTest.php | 62 ---- .../Block/Tracking/PopUpDeliveryDateTest.php | 92 ------ 5 files changed, 507 deletions(-) delete mode 100644 Plugin/Block/DataProviders/Tracking/ChangeTitle.php delete mode 100644 Plugin/Block/Tracking/PopUpDeliveryDate.php delete mode 100644 Test/Unit/Plugin/Block/DataProviders/Tracking/ChangeTitleTest.php delete mode 100644 Test/Unit/Plugin/Block/Tracking/PopUpDeliveryDateTest.php diff --git a/Model/Carrier/BobGo.php b/Model/Carrier/BobGo.php index 386e4cb..2809b13 100644 --- a/Model/Carrier/BobGo.php +++ b/Model/Carrier/BobGo.php @@ -527,141 +527,6 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car } } - /** - * Get tracking - * - * @param string|string[] $trackings - * @return \Magento\Shipping\Model\Tracking\Result|null - */ - public function getTracking($trackings) - { - $this->setTrackingRequest(); // Ensure this method is correctly defined - - if (!is_array($trackings)) { - $trackings = [$trackings]; - } - - foreach ($trackings as $tracking) { - $this->_getXMLTracking([$tracking]); // Ensure _getXMLTracking processes tracking correctly - } - - return $this->_result; // Ensure _result is a \Magento\Shipping\Model\Tracking\Result - } - - /** - * Set tracking request - * - * @return void - */ - protected function setTrackingRequest() - { - $r = new \Magento\Framework\DataObject(); - - $account = $this->getConfigData('account'); - $r->setData('account', $account); // Using setData with the key 'account' - - $this->_rawTrackingRequest = $r; - } - - /** - * Get tracking request - * - * @return \Magento\Framework\DataObject|null - */ - protected function getTrackingRequest(): ?\Magento\Framework\DataObject - { - return $this->_rawTrackingRequest; - } - - /** - * Send request for tracking - * - * @param string[] $tracking - * @return void - */ - protected function _getXMLTracking($tracking) - { - $this->_parseTrackingResponse($tracking); - } - - /** - * Parse tracking response - * - * @param string|array<int,string> $trackingValue - * @return void - */ - protected function _parseTrackingResponse($trackingValue) - { - $result = $this->getResult(); - $carrierTitle = $this->getConfigData('title'); - $counter = 0; - - if (!is_array($trackingValue)) { - $trackingValue = [$trackingValue]; - } - - foreach ($trackingValue as $trackingReference) { - $tracking = $this->_trackStatusFactory->create(); - - $tracking->setCarrier(self::CODE); - $tracking->setCarrierTitle($carrierTitle); - - // Adjust as needed based on the environment - $tracking->setUrl(UData::TRACKING . $trackingReference); - $tracking->setTracking($trackingReference); - $tracking->addData($this->processTrackingDetails($trackingReference)); - - if ($result) { - $result->append($tracking); - $counter++; - } - } - - // Tracking Details Not Available - if ($counter === 0) { - $this->appendTrackingError( - $trackingValue[0] ?? '', - (string)__('For some reason we can\'t retrieve tracking info right now.') - ); - } - } - - /** - * Get tracking response - * - * @return string - */ - public function getResponse(): string - { - $statuses = ''; - - // If $_result is of type \Magento\Shipping\Model\Tracking\Result, handle it - if ($this->_result instanceof \Magento\Shipping\Model\Tracking\Result) { - if ($trackings = $this->_result->getAllTrackings()) { - foreach ($trackings as $tracking) { - if ($data = $tracking->getAllData()) { - if (!empty($data['status'])) { - $statuses .= __($data['status']) . "\n<br/>"; - } else { - $statuses .= __('Empty response') . "\n<br/>"; - } - } - } - } - } - -// // Handle \Magento\Shipping\Model\Rate\Result if needed -// if ($this->_result instanceof \Magento\Shipping\Model\Rate\Result) { -// // Implement the logic for Rate\Result if applicable -// } - - if (trim($statuses) === '') { - $statuses = (string)__('Empty response'); - } - - return $statuses; - } - /** * Get allowed shipping methods * @@ -773,55 +638,6 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car return $data; } - /** - * Parse track details response from Bob Go. - * - * @param string $trackInfo - * @return array<string, array<int, array<string, string>>> - * @SuppressWarnings(PHPMD.CyclomaticComplexity) - * @SuppressWarnings(PHPMD.NPathComplexity) - */ - private function processTrackingDetails(string $trackInfo): array - { - $result = [ - 'shippeddate' => [], // Initializing as an array of arrays - 'deliverydate' => [], // Initializing as an array of arrays - 'deliverytime' => [], // Initializing as an array of arrays - 'deliverylocation' => [], // Initializing as an array of arrays - 'weight' => [], // Initializing as an array of arrays - 'progressdetail' => [], // This will be populated with an array of arrays - ]; - - $result = $this->_requestTracking($trackInfo, $result); - - return $result; - } - - /** - * Append error message to rate result instance. - * - * @param string $trackingValue - * @param string $errorMessage - * @return void - */ - private function appendTrackingError(string $trackingValue, string $errorMessage): void - { - $error = $this->_trackErrorFactory->create(); - $error->setCarrier(self::CODE); - $error->setCarrierTitle($this->getConfigData('title')); - $error->setTracking($trackingValue); - $error->setErrorMessage($errorMessage); - - $result = $this->getResult(); - - if ($result !== null) { - $result->append($error); - } else { - // Handle the case where $result is null, such as logging an error - $this->_logger->error('Failed to append tracking error: Result object is null.'); - } - } - /** * Format a date to 'd M Y'. * @@ -883,26 +699,6 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car } } - /** - * Perform API Request for Shipment Tracking to Bob Go API and return response. - * - * @param string $trackInfo The tracking information or tracking ID. - * @param array<string,array<int,array<string,string>>> $result The result array to be - * populated with tracking details. - * @return array<string, array<int, array<string, string>>> The updated result array with tracking details. - */ - private function _requestTracking(string $trackInfo, array $result): array - { - $response = $this->trackBobGoShipment($trackInfo); - - // Validate that the response is an array and contains at least one element - if (is_array($response) && isset($response[0]) && is_array($response[0])) { - $result = $this->prepareActivity($response[0], $result); - } - - return $result; - } - /** * Format rates from Bob Go API response and append to rate result instance of carrier. * @@ -976,35 +772,6 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car } } - /** - * Prepare received checkpoints and activity from Bob Go Shipment Tracking API. - * - * @param array<string,mixed> $response The API response containing tracking checkpoints. - * @param array<string,array<int,array<string,string>>> $result The result array to be - * populated with activity details. - * @return array<string, array<int, array<string, string>>> The updated result array with activity details. - */ - private function prepareActivity(array $response, array $result): array - { - if (isset($response['checkpoints']) && is_array($response['checkpoints'])) { - foreach ($response['checkpoints'] as $checkpoint) { - if (is_array($checkpoint) && - isset($checkpoint['status'], $checkpoint['time']) && - is_string($checkpoint['status']) && - is_string($checkpoint['time']) - ) { - $result['progressdetail'][] = [ - 'activity' => $checkpoint['status'], - 'deliverydate' => $this->formatDate($checkpoint['time']), - 'deliverytime' => $this->formatTime($checkpoint['time']), - ]; - } - } - } - - return $result; - } - /** * Get Working Days between time of checkout and delivery date (min and max). * @@ -1037,21 +804,6 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car return $no_days - $weekends; } - /** - * Curl request to Bob Go Shipment Tracking API. - * - * @param string $trackInfo The tracking information or tracking ID. - * @return mixed The decoded API response. - */ - private function trackBobGoShipment(string $trackInfo): mixed - { - $this->curl->get(UData::TRACKING . $trackInfo); - - $response = $this->curl->getBody(); - - return json_decode($response, true); - } - /** * Build the payload for Bob Go API request and return the response. * @@ -1199,27 +951,6 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car return $itemsArray; } - /** - * Checks if the required data fields are present in the request. - * - * @param \Magento\Framework\DataObject $request The data object containing the request information. - * @return bool True if all required fields are present, otherwise false. - */ - public function hasRequiredData(\Magento\Framework\DataObject $request): bool - { - $requiredFields = [ - 'dest_country_id', - 'dest_region_id', - ]; - - foreach ($requiredFields as $field) { - if (!$request->getData($field)) { - return false; - } - } - return true; - } - /** * Trigger a test for rates. * diff --git a/Plugin/Block/DataProviders/Tracking/ChangeTitle.php b/Plugin/Block/DataProviders/Tracking/ChangeTitle.php deleted file mode 100644 index a035d23..0000000 --- a/Plugin/Block/DataProviders/Tracking/ChangeTitle.php +++ /dev/null @@ -1,31 +0,0 @@ -<?php - -namespace BobGroup\BobGo\Plugin\Block\DataProviders\Tracking; - -use BobGroup\BobGo\Model\Carrier; -use Magento\Shipping\Model\Tracking\Result\Status; -use Magento\Shipping\Block\DataProviders\Tracking\DeliveryDateTitle as Subject; - -/** - * Plugin to change delivery date title with bobgo customized value - */ - -class ChangeTitle -{ - /** - * Title modification in case if bobgo used as carrier - * - * @param Subject $subject - * @param \Magento\Framework\Phrase|string $result - * @param Status $trackingStatus - * @return \Magento\Framework\Phrase|string - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - public function afterGetTitle(Subject $subject, $result, Status $trackingStatus) - { - if ($trackingStatus->getCarrier() === \BobGroup\BobGo\Model\Carrier\BobGo::CODE) { - $result = __('Expected delivery:'); - } - return $result; - } -} diff --git a/Plugin/Block/Tracking/PopUpDeliveryDate.php b/Plugin/Block/Tracking/PopUpDeliveryDate.php deleted file mode 100644 index bb37528..0000000 --- a/Plugin/Block/Tracking/PopUpDeliveryDate.php +++ /dev/null @@ -1,53 +0,0 @@ -<?php - -namespace BobGroup\BobGo\Plugin\Block\Tracking; - -use Magento\Shipping\Block\Tracking\Popup; -use Magento\Shipping\Model\Tracking\Result\Status; - -/* - * Plugin to update delivery date value in case if Bob Go is a carrier used - */ -class PopupDeliveryDate -{ - /** - * Bob Go carrier code - */ - private const BOB_GO_CARRIER_CODE = 'bobgo_carrier_code'; // Replace with your actual carrier code - - /** - * Show only date for expected delivery in case if Bob Go is a carrier - * - * @param Popup $subject - * @param string $result - * @param string $date - * @param string $time - * @return string - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - public function afterFormatDeliveryDateTime(Popup $subject, $result, $date, $time) - { - if ($this->getCarrier($subject) === self::BOB_GO_CARRIER_CODE) { - $result = $subject->formatDeliveryDate($date); - } - return $result; - } - - /** - * Retrieve carrier name from tracking info - * - * @param Popup $subject - * @return string - */ - private function getCarrier(Popup $subject): string - { - foreach ($subject->getTrackingInfo() as $trackingData) { - foreach ($trackingData as $trackingInfo) { - if ($trackingInfo instanceof Status) { - return $trackingInfo->getCarrier() ?? ''; - } - } - } - return ''; - } -} diff --git a/Test/Unit/Plugin/Block/DataProviders/Tracking/ChangeTitleTest.php b/Test/Unit/Plugin/Block/DataProviders/Tracking/ChangeTitleTest.php deleted file mode 100644 index 4d8bd9d..0000000 --- a/Test/Unit/Plugin/Block/DataProviders/Tracking/ChangeTitleTest.php +++ /dev/null @@ -1,62 +0,0 @@ -<?php - -namespace BobGroup\BobGo\Test\Unit\Plugin\Block\DataProviders\Tracking; - -use BobGroup\BobGo\Plugin\Block\DataProviders\Tracking\ChangeTitle; -use BobGroup\BobGo\Model\Carrier\BobGo; -use Magento\Shipping\Model\Tracking\Result\Status; -use Magento\Shipping\Block\DataProviders\Tracking\DeliveryDateTitle as Subject; -use PHPUnit\Framework\TestCase; - -class ChangeTitleTest extends TestCase -{ - /** - * @var ChangeTitle - */ - private $plugin; - - protected function setUp(): void - { - // Instantiate the ChangeTitle plugin - $this->plugin = new ChangeTitle(); - } - - public function testAfterGetTitleWithBobGoCarrier(): void - { - // Create a custom Status object with BobGo carrier - $status = $this->getMockBuilder(Status::class) - ->setMethods(['getCarrier']) - ->getMock(); - - $status->method('getCarrier')->willReturn(BobGo::CODE); - - // Mock the Subject class - $subjectMock = $this->createMock(Subject::class); - - // Call the plugin method afterGetTitle - $result = $this->plugin->afterGetTitle($subjectMock, 'Original Title', $status); - - // Assert that the title was changed for BobGo carrier - $this->assertEquals('Expected delivery:', $result); - } - - public function testAfterGetTitleWithOtherCarrier(): void - { - // Create a custom Status object with a different carrier - $status = $this->getMockBuilder(Status::class) - ->setMethods(['getCarrier']) - ->getMock(); - - $status->method('getCarrier')->willReturn('other_carrier_code'); - - // Mock the Subject class - $subjectMock = $this->createMock(Subject::class); - - // Call the plugin method afterGetTitle - $originalTitle = 'Original Title'; - $result = $this->plugin->afterGetTitle($subjectMock, $originalTitle, $status); - - // Assert that the title was not changed for other carriers - $this->assertEquals($originalTitle, $result); - } -} diff --git a/Test/Unit/Plugin/Block/Tracking/PopUpDeliveryDateTest.php b/Test/Unit/Plugin/Block/Tracking/PopUpDeliveryDateTest.php deleted file mode 100644 index 6c01770..0000000 --- a/Test/Unit/Plugin/Block/Tracking/PopUpDeliveryDateTest.php +++ /dev/null @@ -1,92 +0,0 @@ -<?php - -namespace BobGroup\BobGo\Test\Unit\Plugin\Block\Tracking; - -use BobGroup\BobGo\Plugin\Block\Tracking\PopupDeliveryDate; -use Magento\Shipping\Block\Tracking\Popup; -use Magento\Shipping\Model\Tracking\Result\Status; -use PHPUnit\Framework\TestCase; - -class PopupDeliveryDateTest extends TestCase -{ - /** - * @var PopupDeliveryDate - */ - private $plugin; - - protected function setUp(): void - { - // Instantiate the PopupDeliveryDate plugin - $this->plugin = new PopupDeliveryDate(); - } - - public function testAfterFormatDeliveryDateTimeWithBobGoCarrier(): void - { - // Create an instance of the Status class - $status = new Status(); - $status->setCarrier('bobgo_carrier_code'); - - // Mock the Popup class - $popupMock = $this->createMock(Popup::class); - $popupMock->method('getTrackingInfo')->willReturn([ - ['tracking_info' => $status], - ]); - - // Mock the formatDeliveryDate method - $popupMock->method('formatDeliveryDate') - ->with('2024-08-19') - ->willReturn('Aug 19, 2024'); - - // Call the plugin method afterFormatDeliveryDateTime - $result = $this->plugin->afterFormatDeliveryDateTime( - $popupMock, - 'Aug 19, 2024 10:00 AM', - '2024-08-19', - '10:00 AM' - ); - - // Assert that the time was stripped for BobGo carrier - $this->assertEquals('Aug 19, 2024', $result); - } - - public function testAfterFormatDeliveryDateTimeWithOtherCarrier(): void - { - // Create an instance of the Status class - $status = new Status(); - $status->setCarrier('other_carrier_code'); - - // Mock the Popup class - $popupMock = $this->createMock(Popup::class); - $popupMock->method('getTrackingInfo')->willReturn([ - ['tracking_info' => $status], - ]); - - // Call the plugin method afterFormatDeliveryDateTime - $result = $this->plugin->afterFormatDeliveryDateTime( - $popupMock, - 'Aug 19, 2024 10:00 AM', - '2024-08-19', - '10:00 AM' - ); - - // Assert that the time remains unchanged for other carriers - $this->assertEquals('Aug 19, 2024 10:00 AM', $result); - } - - public function testGetCarrierWithNoTrackingInfo(): void - { - // Mock the Popup class with no tracking info - $popupMock = $this->createMock(Popup::class); - $popupMock->method('getTrackingInfo')->willReturn([]); - - // Call the getCarrier method directly - $reflection = new \ReflectionClass($this->plugin); - $method = $reflection->getMethod('getCarrier'); - $method->setAccessible(true); - - $result = $method->invokeArgs($this->plugin, [$popupMock]); - - // Assert that an empty string is returned when no tracking info is available - $this->assertEquals('', $result); - } -} -- GitLab