protected function createApiMock($response, $statusCode = 200) { $jsonFile = $this->createJsonFile(); $jobsMethods = array('collectCloverXml', 'getJsonFile', 'collectGitInfo', 'collectEnvVars', 'dumpJsonFile', 'send'); $api = $this->getMockBuilder('Satooshi\\Bundle\\CoverallsV1Bundle\\Api\\Jobs')->disableOriginalConstructor()->setMethods($jobsMethods)->getMock(); $api->expects($this->once())->method('collectCloverXml')->with()->will($this->returnSelf()); $api->expects($this->once())->method('getJsonFile')->with()->will($this->returnValue($jsonFile)); $api->expects($this->once())->method('collectGitInfo')->with()->will($this->returnSelf()); $api->expects($this->once())->method('collectEnvVars')->with($this->equalTo($_SERVER))->will($this->returnSelf()); $api->expects($this->once())->method('dumpJsonFile')->with()->will($this->returnSelf()); $request = $this->getMockBuilder('\\GuzzleHttp\\Psr7\\Request')->setConstructorArgs(['POST', '/'])->getMock(); if ($statusCode === 200) { $api->expects($this->once())->method('send')->with()->will($this->returnValue($response)); } else { if ($statusCode === null) { $exception = \GuzzleHttp\Exception\ConnectException::create($request); } elseif ($statusCode === 422) { $exception = \GuzzleHttp\Exception\ClientException::create($request, $response); } else { $exception = \GuzzleHttp\Exception\ServerException::create($request, $response); } $api->expects($this->once())->method('send')->with()->will($this->throwException($exception)); } return $api; }
/** * @covers ::execute */ public function testExecuteWithResponseFailure() { $code = 1337; $message = 'hello world'; $this->setExpectedException(RuntimeException::class, $message, $code); $request = $this->getMock(RequestInterface::class); $response = $this->createResponse(400, ['error' => ['code' => $code, 'message' => $message]]); $exception = ClientException::create($request, $response); $this->internalClient->expects($this->once())->method('post')->will($this->throwException($exception)); $this->client->execute(static::SQL, ['foo' => 'bar']); }
private function createClientException($httpCode) { return function (Request $request) use($httpCode) { // Capture Request object so we can throw an ClientException $response = new Response($httpCode); throw ClientException::create($request, $response); }; }
/** * @covers ::execute */ public function testExecuteWithResponseFailure() { $code = 1337; $message = 'hello world'; $this->setExpectedException(RuntimeException::class, $message, $code); $request = $this->getMock(RequestInterface::class); $request->method('getUri')->willReturn($this->getMock(UriInterface::class)); $response = $this->createResponse(400, ['error' => ['code' => $code, 'message' => $message]]); $exception = ClientException::create($request, $response); $this->server->expects($this->once())->method('doRequest')->will($this->throwException($exception)); $this->client->execute('SELECT ? FROM', ['foo']); }