/**
  * @depends testConfiglessConstructor
  */
 public function testConstructor()
 {
     $connection = new Connection('https://*****:*****@test.freshdesk.com');
     $contact = new Contact($connection);
     $sectionGetters = $this->configlessProvider();
     $sectionGetters = array_pop($sectionGetters);
     foreach ($sectionGetters as $section) {
         $section = Rest::GetSection($section);
         if ($section instanceof Contact) {
             $this->assertEquals($contact, $section);
         }
         $configs = $this->getConnection($section, $contact);
         $this->assertEquals($configs['expected'], $configs['instance']);
         $this->assertEquals($configs['instance']->getBaseUrl(), $connection->getBaseUrl());
         $this->assertEquals($configs['expected']->getBaseUrl(), $connection->getBaseUrl());
         $this->assertEquals($configs['expected']->getDomain(), $connection->getDomain());
         $this->assertEquals($configs['instance']->getDomain(), $connection->getDomain());
         $this->assertEquals($configs['instance']->getScheme(), $connection->getScheme());
         $this->assertEquals($configs['expected']->getScheme(), $connection->getScheme());
         $this->assertEquals($configs['instance']->getUserName(), $connection->getUserName());
         $this->assertEquals($configs['expected']->getUserName(), $connection->getUserName());
         $this->assertEquals($configs['expected']->getPassword(), $connection->getPassword());
         $this->assertEquals($configs['instance']->getPassword(), $connection->getPassword());
     }
 }
 /**
  * @dataProvider setUrlStringProvider
  */
 public function testUrlSetterError($url, $success = true)
 {
     if ($success === false) {
         $this->setExpectedException('RuntimeException');
         $this->instance->setByUrl($url);
     } else {
         $this->instance->setByUrl($url);
         $parsed = parse_url($url);
         $this->assertEquals($parsed['pass'], $this->instance->getPassword(), sprintf('%s expected to be equal to %s', $this->instance->getPassword(), $parsed['pass']));
         $this->assertEquals($parsed['scheme'] . '://', $this->instance->getScheme(), sprintf('%s expected to be equal to %s', $this->instance->getScheme(), $parsed['scheme'] . '://'));
         $this->assertEquals($parsed['host'], $this->instance->getDomain(), sprintf('%s expected to be equal to %s', $this->instance->getDomain(), $parsed['host']));
         $this->assertEquals($parsed['user'], $this->instance->getUserName(), sprintf('%s expected to be equal to %s', $this->instance->getUserName(), $parsed['user']));
     }
 }
示例#3
0
 /**
  * @param $urlMinusDomain - should start with /... example /solutions/categories.xml
  * @param $method - should be either GET, POST, PUT (and theoretically DELETE but that's untested).
  * @param string $postData - only specified if $method == POST or PUT
  * @param $debugMode {bool} optional - prints the request and response with headers
  * @return string
  * @throws \RuntimeException
  * @throws \InvalidArgumentException
  */
 protected function restCall($urlMinusDomain, $method, $postData = '', $debugMode = false)
 {
     if ($urlMinusDomain[0] !== '/') {
         $urlMinusDomain = '/' . $urlMinusDomain;
     }
     $url = $this->config->getScheme() . $this->config->getDomain() . $urlMinusDomain;
     $opts = array(\CURLOPT_USERPWD => $this->config->getUsername() . ':' . $this->config->getPassword(), \CURLOPT_HTTPHEADER => array('Content-type: application/json'), \CURLOPT_RETURNTRANSFER => true, \CURLOPT_HTTPAUTH => \CURLAUTH_BASIC, \CURLOPT_SSL_VERIFYHOST => 0, \CURLOPT_SSL_VERIFYPEER => 0);
     if ($this->proxyServer) {
         $opts[\CURLOPT_PROXY] = $this->proxyServer;
     }
     if ($debugMode) {
         // CURLOPT_VERBOSE: TRUE to output verbose information. Writes output to STDERR,
         // or the file specified using CURLOPT_STDERR.
         $opts[\CURLOPT_STDERR] = fopen('php://temp', 'rw+');
         $opts[\CURLOPT_VERBOSE] = true;
     }
     switch (strtoupper(trim($method))) {
         case self::METHOD_POST:
             if (empty($postData)) {
                 $opts[\CURLOPT_HTTPHEADER][] = 'Content-length: 0';
             }
             //According to the initial wrapper, length should is not required here...
             //else
             //$opts[\CURLOPT_HTTPHEADER][] = 'Content-length: '.strlen($postData);
             $opts[\CURLOPT_POST] = true;
             $opts[\CURLOPT_POSTFIELDS] = $postData;
             break;
         case self::METHOD_PUT:
             $opts[\CURLOPT_CUSTOMREQUEST] = 'PUT';
             $opts[\CURLOPT_POSTFIELDS] = $postData;
             break;
         case self::METHOD_DEL:
             $opts[\CURLOPT_CUSTOMREQUEST] = 'DELETE';
             break;
         case self::METHOD_GET:
             $opts[\CURLOPT_POST] = false;
             break;
         default:
             if ($debugMode) {
                 fclose($opts[\CURLOPT_STDERR]);
             }
             //close stream, we have an error
             throw new \InvalidArgumentException(sprintf('Method "%s" is not a valid method, use %s::METHOD_* constants', $method, __CLASS__));
     }
     $ch = curl_init($url);
     if (!is_resource($ch)) {
         if ($debugMode) {
             fclose($opts[\CURLOPT_STDERR]);
         }
         //close stream
         throw new \RuntimeException('Could not init curl request');
     }
     if (!curl_setopt_array($ch, $opts)) {
         if ($debugMode) {
             fclose($opts[\CURLOPT_STDERR]);
         }
         throw new \RuntimeException('Could not set curl options');
     }
     $this->lastHttpResponseText = $httpResponse = curl_exec($ch);
     $this->lastHttpStatusCode = $httpCode = (int) curl_getinfo($ch, \CURLINFO_HTTP_CODE);
     if ($httpCode < 200 || $httpCode > 299) {
         if (!$debugMode) {
             curl_close($ch);
             //close curl
             throw new \RuntimeException(sprintf('%s action to %s returned unexpected HTTP code (%d), repsonse: %s', $method, $url, $httpCode, $httpResponse));
         }
     }
     if ($debugMode) {
         if (rewind($opts[\CURLOPT_STDERR])) {
             $this->debugLogs[] = array('URL' => $url, 'Method' => $method, 'HTTPCode' => $httpCode, 'Stream' => stream_get_contents($opts[\CURLOPT_STDERR]));
         } else {
             $this->debugLogs[] = array('URL' => $url, 'Method' => $method, 'HTTPCode' => $httpCode, 'Stream' => 'ERROR: rewind stream failed!');
         }
         fclose($opts[\CURLOPT_STDERR]);
     }
     curl_close($ch);
     return $httpResponse;
 }