/** * doActionTest * * @return void */ protected function doActionTest() { $postedData = \XLite\Core\Request::getInstance()->getData(); // Generate input data array for rates calculator $data = array(); $errorFields = array(); if (isset($postedData['sourceZipcode']) && !empty($postedData['sourceZipcode'])) { $data['srcAddress']['zipcode'] = $postedData['sourceZipcode']; } else { $data['srcAddress']['zipcode'] = \XLite\Core\Config::getInstance()->Company->location_zipcode; } if (isset($postedData['destinationZipcode']) && !empty($postedData['destinationZipcode'])) { $data['dstAddress']['zipcode'] = $postedData['destinationZipcode']; } else { $errorFields[] = 'destinationZipcode'; } if (isset($postedData['destinationCountry']) && !empty($postedData['destinationCountry'])) { $data['dstAddress']['country'] = $postedData['destinationCountry']; } else { $errorFields[] = 'destinationCountry'; } $package = array(); if (isset($postedData['weight']) && 0 < doubleval($postedData['weight'])) { $package['weight'] = doubleval($postedData['weight']); } else { $package['weight'] = 1; } if (isset($postedData['subtotal']) && 0 < doubleval($postedData['subtotal'])) { $package['subtotal'] = doubleval($postedData['subtotal']); } else { $package['subtotal'] = 1; } $data['packages'] = array($package); echo '<h2>Input data</h2>'; ob_start(); print_r($data); $dataStr = '<pre>' . ob_get_contents() . '</pre>'; ob_clean(); echo $dataStr; if (empty($errorFields)) { // Get rates $usps = new \XLite\Module\CDev\USPS\Model\Shipping\Processor\USPS(); $startTime = microtime(true); $rates = $usps->getRates($data, true); $proceedTime = microtime(true) - $startTime; $errorMsg = $usps->getErrorMsg(); if (!isset($errorMsg)) { if (!empty($rates)) { // Rates have been successfully calculated, display them echo '<h2>Rates:</h2>'; foreach ($rates as $rate) { echo sprintf('%s (%0.2f)<br>', $rate->getMethodName(), $rate->getBaseRate()); } echo sprintf('<br /><i>Time elapsed: %0.3f seconds</i>', $proceedTime); } else { $errorMsg = static::t('There are no rates available for specified source/destination and/or package measurements/weight.'); } } } else { $errorMsg = static::t('The following expected input data have wrong format or empty: ' . implode(', ', $errorFields)); } if (!empty($errorMsg)) { echo '<h3>' . $errorMsg . '</h3>'; } if (isset($usps)) { $cmLog = $usps->getApiCommunicationLog(); } if (isset($cmLog)) { echo '<h2>Communication log</h2>'; ob_start(); echo 'API URL: ' . $usps->getApiURL() . PHP_EOL . PHP_EOL; foreach ($cmLog as $log) { print_r($log); echo PHP_EOL . '<hr />' . PHP_EOL; } $msg = '<pre>' . ob_get_contents() . '</pre>'; ob_clean(); echo $msg; } die; }
/** * testGetApiUrl * * @return void * @see ____func_see____ * @since 1.0.0 */ public function testGetApiUrl() { $processor = new XLite\Module\CDev\USPS\Model\Shipping\Processor\USPS(); $defaultHost = 'testing.shippingapis.com'; $tmpServerName = \XLite\Core\Config::getInstance()->CDev->USPS->server_name; $tmpServerPath = \XLite\Core\Config::getInstance()->CDev->USPS->server_path; \XLite\Core\Config::getInstance()->CDev->USPS->server_path = 'path'; \XLite\Core\Config::getInstance()->CDev->USPS->server_name = 'host'; $this->assertEquals('http://host/path', $processor->getApiURL()); \XLite\Core\Config::getInstance()->CDev->USPS->server_name = 'http://host'; $this->assertEquals('http://' . $defaultHost . '/path', $processor->getApiURL()); \XLite\Core\Config::getInstance()->CDev->USPS->server_name = $tmpServerName; \XLite\Core\Config::getInstance()->CDev->USPS->server_path = $tmpServerPath; }