Exemplo n.º 1
0
 public function testQuote()
 {
     if (self::$_photoUrl) {
         $endpoint = 'api/rest/customconfigurable/quote';
         $url = $this->_callbackUrl . $endpoint;
         $options = $this->_model->getOptions();
         $params = array();
         foreach ($options as $code => $option) {
             if ($option['required']) {
                 $value = reset($option['values']);
                 $params[$code] = (int) $value['id'];
             }
         }
         $params[$this->_photoCode] = self::$_photoUrl;
         $data = json_encode($params);
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_POST, true);
         curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
         curl_setopt($ch, CURLOPT_HEADER, 1);
         curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data)));
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         $response = curl_exec($ch);
         $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
         $headerStr = substr($response, 0, $headerSize);
         $headersAr = Mage::helper('aydus_customconfigurable')->getHeadersArray($headerStr);
         curl_close($ch);
         $gotLocation = false;
         if (is_array($headersAr) && count($headersAr) > 0) {
             $location = '';
             foreach ($headersAr as $headerAr) {
                 foreach ($headerAr as $headerKey => $headerValue) {
                     if ($headerKey == 'Location') {
                         $location = $headerValue;
                         $parsedUrlAr = parse_url($location);
                         if (is_array($parsedUrlAr) && count($parsedUrlAr) > 0) {
                             if (is_numeric(strpos($location, $url))) {
                                 $gotLocation = true;
                             }
                         }
                     }
                 }
             }
         }
         $this->assertTrue($gotLocation);
         $hash = substr($location, strlen($url) + 1);
         $hash = substr($hash, 0, -1);
         $getQuoteUrl = $url . '/' . $hash;
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $getQuoteUrl);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         $response = curl_exec($ch);
         curl_close($ch);
         $params = json_decode($response, true);
         $this->assertEquals($hash, $params['hash']);
         $quoteId = (int) $params['quote'];
         $gotQuote = $quoteId > 0 ? true : false;
         $this->assertTrue($gotQuote);
         self::$_quoteId = $quoteId;
     }
 }