Example #1
0
File: Usps.php Project: kingsj/core
 /**
  * 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;
 }
Example #2
0
    $values = array();
    foreach ($fields as $fieldName) {
        $values[$fieldName] = isset($booleanFields[$fieldName]) ? 'Y' === $config->{$fieldName} || 1 === (int) $config->{$fieldName} : $config->{$fieldName};
    }
    $values['dimensions'] = serialize(array($config->length, $config->width, $config->height));
    $yamlFile = __DIR__ . LC_DS . 'post_rebuild.yaml';
    \XLite\Core\Database::getInstance()->loadFixturesFromYaml($yamlFile);
    /** @var \XLite\Model\Repo\Config $repo */
    $repo = \XLite\Core\Database::getRepo('XLite\\Model\\Config');
    $category = 'CDev\\USPS';
    foreach ($values as $name => $value) {
        $repo->createOption(array('category' => $category, 'name' => $name, 'value' => $value));
    }
    foreach (array('length', 'width', 'height') as $name) {
        $option = $repo->findOneBy(array('name' => $name, 'category' => $category));
        if ($option) {
            \XLite\Core\Database::getEM()->remove($option);
        }
    }
    \XLite\Core\Database::getEM()->flush();
    \XLite\Core\Config::updateInstance();
    /** @var \XLite\Model\Repo\Shipping\Method $repo */
    $repo = \XLite\Core\Database::getRepo('XLite\\Model\\Shipping\\Method');
    $carrier = $repo->findOnlineCarrier('usps');
    $processor = new \XLite\Module\CDev\USPS\Model\Shipping\Processor\USPS();
    if ($processor->isConfigured()) {
        $carrier->setAdded(true);
        $carrier->setEnabled(true);
    }
    \XLite\Core\Database::getEM()->flush();
};
Example #3
0
File: USPS.php Project: kingsj/core
 /**
  * testGetRates
  *
  * @return void
  * @see    ____func_see____
  * @since  1.0.0
  */
 public function testGetRates()
 {
     if (!isset($this->testConfig['usps'])) {
         $this->markTestSkipped('U.S.P.S tests are not condifured');
     }
     $processor = new XLite\Module\CDev\USPS\Model\Shipping\Processor\USPS();
     \XLite\Base::getInstance()->config->CDev->USPS->userid = $this->testConfig['usps']['userid'];
     // Test on anonymous order
     $tmpConfig = \XLite\Base::getInstance()->config->Company;
     \XLite\Base::getInstance()->config->Company->location_country = 'US';
     \XLite\Base::getInstance()->config->Company->location_zipcode = '10001';
     $m = $this->getTestOrder(false)->getModifier('shipping', 'SHIPPING')->getModifier();
     $rates = $processor->getRates($m, true);
     $ratesCached = $processor->getRates($m);
     \XLite\Base::getInstance()->config->Company = $tmpConfig;
     $this->assertTrue(is_array($rates), 'getRates() must return an array (#1)');
     // Actually USPS returns 22 methods in the response. We check just >15 to avoid failure if USPS will return other number of methods
     $this->assertGreaterThan(15, count($rates), 'Count of rates checking failed (#1) - ' . $processor->getErrorMsg());
     $this->assertEquals($rates, $ratesCached, 'Cached rates does not match an original rates (#1)');
     foreach ($rates as $rate) {
         $this->assertTrue($rate instanceof \XLite\Model\Shipping\Rate, 'getRates() must return an array of \\XLite\\Model\\Shipping\\Rate instances (#1)');
         $this->assertTrue($rate->getMethod() instanceof \XLite\Model\Shipping\Method, 'Wrong method object returned (#1)');
         $this->assertEquals('usps', $rate->getMethod()->getProcessor(), 'Wrong method returned (processor does not match) (#1)');
         $this->assertNotEquals(0, $rate->getBaseRate(), 'Base rate is zero (#1)');
         $this->assertEquals(0, $rate->getMarkupRate(), 'Markup rate is not zero (#1)');
     }
     // Test on an array with input data
     $data = array('srcAddress' => array('zipcode' => 10001), 'dstAddress' => array('zipcode' => '22044', 'country' => 'US'), 'packages' => array(0 => array('weight' => 10, 'subtotal' => 200)));
     $rates = $processor->getRates($data, true);
     $this->assertTrue(is_array($rates), 'getRates() must return an array (#2)');
     $this->assertGreaterThan(15, count($rates), 'Count of rates checking failed (#2) - ' . $processor->getErrorMsg());
     foreach ($rates as $rate) {
         $this->assertTrue($rate instanceof \XLite\Model\Shipping\Rate, 'getRates() must return an array of \\XLite\\Model\\Shipping\\Rate instances (#2)');
         $this->assertTrue($rate->getMethod() instanceof \XLite\Model\Shipping\Method, 'Wrong method object returned (#2)');
         $this->assertEquals('usps', $rate->getMethod()->getProcessor(), 'Wrong method returned (processor does not match) (#2)');
         $this->assertNotEquals(0, $rate->getBaseRate(), 'Base rate is zero (#2)');
         $this->assertEquals(0, $rate->getMarkupRate(), 'Markup rate is not zero (#2)');
     }
     // Test error of Domestic API
     $tmpCont = \XLite\Base::getInstance()->config->CDev->USPS->container;
     \XLite\Base::getInstance()->config->CDev->USPS->container = 'WRONG';
     $rates = $processor->getRates($data, true);
     $this->assertTrue(is_array($rates), 'getRates() must return an array (111)');
     $this->assertEquals(empty($rates), 'getRates() must return empty array (111)');
     \XLite\Base::getInstance()->config->CDev->USPS->container = $tmpCont;
     // Test on anonymous order if config.Company.location_country != 'US'
     $tmpConfig = \XLite\Base::getInstance()->config->Company;
     \XLite\Base::getInstance()->config->Company->location_country = 'AU';
     $m = $this->getTestOrder(false)->getModifier('shipping', 'SHIPPING')->getModifier();
     $rates = $processor->getRates($m, true);
     \XLite\Base::getInstance()->config->Company = $tmpConfig;
     $this->assertTrue(is_array($rates), 'getRates() must return an array (#3)');
     $this->assertEquals(0, count($rates), 'Count of rates is not match with an expected value (#3) - ' . $processor->getErrorMsg());
     unset($rates);
     // Test International API
     \XLite\Base::getInstance()->config->Company->location_country = 'US';
     \XLite\Base::getInstance()->config->CDev->USPS->gxg = true;
     $methodToDelete = 'I-1-735bf98ee9fbdf9dbb374920def99049';
     $method = \XLite\Core\Database::getRepo('XLite\\Model\\Shipping\\Method')->findOneByCode($methodToDelete);
     $this->assertNotNull($method, 'Method with code ' . $methodToDelete . ' not found in the database');
     \XLite\Core\Database::getRepo('XLite\\Model\\Shipping\\Method')->delete($method);
     $method->detach();
     $method = \XLite\Core\Database::getRepo('XLite\\Model\\Shipping\\Method')->findOneByCode($methodToDelete);
     $this->assertNull($method, 'Method with code ' . $methodToDelete . ' found in the database');
     $data = array('srcAddress' => array('zipcode' => 50001), 'dstAddress' => array('zipcode' => '3456', 'country' => 'GB'), 'packages' => array(0 => array('weight' => 3, 'subtotal' => 200)));
     $rates = $processor->getRates($data, true);
     $this->assertTrue(is_array($rates), 'getRates() must return an array (#4)');
     $this->assertGreaterThan(1, count($rates), 'Count of rates checking failed (#4) - ' . $processor->getErrorMsg());
     foreach ($rates as $rate) {
         $this->assertTrue($rate instanceof \XLite\Model\Shipping\Rate, 'getRates() must return an array of \\XLite\\Model\\Shipping\\Rate instances (#4)');
         $this->assertTrue($rate->getMethod() instanceof \XLite\Model\Shipping\Method, 'Wrong method object returned (#4)');
         $this->assertEquals('usps', $rate->getMethod()->getProcessor(), 'Wrong method returned (processor does not match) (#4)');
         $this->assertNotEquals(0, $rate->getBaseRate(), 'Base rate is zero (#4)');
         $this->assertEquals(0, $rate->getMarkupRate(), 'Markup rate is not zero (#4)');
     }
     // Check that method I-1 returned to the database
     $method = \XLite\Core\Database::getRepo('XLite\\Model\\Shipping\\Method')->findOneByCode($methodToDelete);
     $this->assertNotNull($method, 'Method with code ' . $methodToDelete . ' not restored in the database after request to USPS');
     $this->assertFalse($method->getEnabled(), 'Method with code ' . $methodToDelete . ' restored with enabled status');
     // Test International API error
     \XLite\Base::getInstance()->config->CDev->USPS->mail_type = 'WRONG';
     \XLite\Base::getInstance()->config->CDev->USPS->gxg = false;
     $rates = $processor->getRates($data, true);
     $this->assertTrue(is_array($rates), 'getRates() must return an array (222)');
     $this->assertEquals(empty($rates), 'getRates() must return empty array (222)');
     // Test for wrong input data
     $data = array('something' => 'wrong');
     $rates = $processor->getRates($data, true);
     $this->assertTrue(is_array($rates), 'getRates() must return an array (333)');
     $this->assertEquals(empty($rates), 'getRates() must return empty array (333)');
 }