I am writing unit tests for an API service in a Symfony2 project. One service method takes an instance of the controller as an argument and processes the requested JSON.
public function getJSONContent(Controller $controller) {
$version = $this->getAPIVersion();
$content = $controller->get("request")->getContent();
if (empty($content)) {
throw new HttpException(400, 'Empty request payload');
}
$jsonContent = json_decode($content, true);
if($jsonContent === null) {
throw new HttpException(400, 'Malformed JSON content received');
}
return $jsonContent;
}
The following is my test:
class ApiTest extends \PHPUnit_Framework_TestCase {
public function testGetJSONContent() {
$stub = $this->getMock('OrganizationController');
$post = 'Testy Test';
$request = $post;
$version = "VersionTest";
$APIService = new APIService();
$stub->expects($this->any())
->method('get')
->will($this->returnValue($post));
$stub->expects($this->any())
->method('getContent')
->will($this->returnValue($request));
$stub->expects($this->any())
->method('getAPIVersion')
->will($this->returnValue($version));
$this->assertEquals('Testy Test', $APIService->getJSONContent($stub));
}
}
My test produces the following error:
Argument 1 passed to Main \ EntityBundle \ Service \ APIService :: getJSONContent () must be an instance of Symfony \ Bundle \ FrameworkBundle \ Controller \ Controller, an instance of Mock_OrganizationController_767eac0e.
My stub obviously isn't fooling anyone, is there any way to fix this?
source
share