public function testRequestToken() { /** * @var $client B2Client */ $client = $this->getMockBuilder('\\B2\\B2Client')->setConstructorArgs(['id', 'key'])->setMethods(['curl'])->getMock(); $client->method('curl')->will($this->returnCallback(function () { $response = new B2Response(); $response->setStatusCode(200); $response->setData(json_encode(['downloadUrl' => 'https://downloadurl', 'apiUrl' => 'api', 'authorizationToken' => 'This is a serverside error message'])); return $response; })); $result = $client->requestToken(); $this->assertTrue($result); $this->assertEquals('https://downloadurl', $client->getDownloadUrl()); }
/** * @param $uri * @param string $method * @param array $headers * @param null $body * @return B2Response */ public function curl($uri, $method = 'GET', $headers = [], $body = null) { $response = new B2Response(); $this->CurlRequest->setOption(CURLOPT_URL, $uri); $this->CurlRequest->setOption(CURLOPT_CUSTOMREQUEST, $method); $this->CurlRequest->setOption(CURLOPT_RETURNTRANSFER, 1); $this->CurlRequest->setOption(CURLOPT_POST, 1); $this->CurlRequest->setOption(CURLOPT_POSTFIELDS, $body); $this->CurlRequest->setOption(CURLOPT_HTTPHEADER, $headers); $this->CurlRequest->setOption(CURLOPT_HEADERFUNCTION, function ($curl, $header) use($response) { $response->addHeader($header); return strlen($header); }); $resp = $this->CurlRequest->execute(); if ($this->CurlRequest->getErrorNo() !== 0) { throw new \RuntimeException('curl error ' . $this->CurlRequest->getError() . '" - Code: ' . $this->CurlRequest->getErrorNo()); } else { $response->setData($resp); $response->setStatusCode($this->CurlRequest->getInfo(CURLINFO_HTTP_CODE)); return $response; } }