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()); if ($statusCode === 200) { $api->expects($this->once())->method('send')->with()->will($this->returnValue($response)); } else { if ($statusCode === null) { $exception = new \Guzzle\Http\Exception\CurlException(); } elseif ($statusCode === 422) { $exception = new \Guzzle\Http\Exception\ClientErrorResponseException(); $exception->setResponse($response); } else { $exception = new \Guzzle\Http\Exception\ServerErrorResponseException(); $exception->setResponse($response); } $api->expects($this->once())->method('send')->with()->will($this->throwException($exception)); } return $api; }
/** * @return \PHPUnit_Framework_MockObject_MockObject */ protected function getMockedRestClientWith404Response() { $resquestMock = $this->getMock('\\Guzzle\\Http\\Message\\Request', array('setAuth', 'send'), array(), '', false); $resquestMock->expects($this->once())->method('setAuth')->will($this->returnCallback(function () use($resquestMock) { return $resquestMock; })); $self = $this; $resquestMock->expects($this->once())->method('send')->will($this->returnCallback(function () use($self) { /** @var $responsetMock \Guzzle\Http\Message\Response */ $responseMock = $self->getMock('\\Guzzle\\Http\\Message\\Response', array(), array(), '', false); $responseMock->expects($self->once())->method('getStatusCode')->will($self->returnValue(404)); $exception = new \Guzzle\Http\Exception\ClientErrorResponseException(); $exception->setResponse($responseMock); throw $exception; })); $restClient = $this->getMock('\\Guzzle\\Http\\Client', array(), array(), '', false); $restClient->expects($this->once())->method('setDefaultHeaders')->will($this->returnValue($restClient)); $restClient->expects($this->once())->method('setBaseUrl')->will($this->returnValue($restClient)); $restClient->expects($this->once())->method('get')->will($this->returnValue($resquestMock)); return $restClient; }