Skip to content

Commit

Permalink
Merge pull request #574 from FriendsOfCake/issue-572
Browse files Browse the repository at this point in the history
Ensure response's status code doesn't get set to null.
  • Loading branch information
ADmad authored Jan 31, 2018
2 parents c0139f2 + 969fb6c commit babfb5d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/Listener/ApiListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,13 @@ public function respond(Event $event)
return null;
}

$response = $this->render($event->subject())
->withStatus($apiConfig['code']);
$response = $this->render($event->subject());

return $response;
if (empty($apiConfig['code'])) {
return $response;
}

return $response->withStatus($apiConfig['code']);
}

/**
Expand Down
53 changes: 53 additions & 0 deletions tests/TestCase/Listener/ApiListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,59 @@ public function testResponse()
$listener->respond($event);
}

/**
* testResponseWithStatusCodeNotSpecified
*
* @return void
* @see https://github.com/FriendsOfCake/crud/issues/572
*/
public function testResponseWithStatusCodeNotSpecified()
{
$action = $this
->getMockBuilder('\Crud\Action\ViewAction')
->disableOriginalConstructor()
->setMethods(['getConfig'])
->getMock();

$response = $this
->getMockBuilder(Response::class)
->setMethods(['withStatus'])
->getMock();

$subject = $this
->getMockBuilder('\Crud\Event\Subject')
->getMock();
$subject->success = true;

$event = new \Cake\Event\Event('Crud.afterSave', $subject);

$listener = $this
->getMockBuilder('\Crud\Listener\ApiListener')
->disableOriginalConstructor()
->setMethods(['_action', 'render'])
->getMock();
$listener
->expects($this->next($listener))
->method('_action')
->with()
->will($this->returnValue($action));
$action
->expects($this->next($action))
->method('getConfig')
->with('api.success')
->will($this->returnValue(null));
$listener
->expects($this->next($listener))
->method('render')
->with($subject)
->will($this->returnValue($response));
$response
->expects($this->never())
->method('withStatus');

$response = $listener->respond($event);
}

/**
* Test response method with exception config
*
Expand Down

0 comments on commit babfb5d

Please sign in to comment.