Skip to content

Commit

Permalink
Merge pull request #164 from zf-fr/default-values
Browse files Browse the repository at this point in the history
Set empty values
  • Loading branch information
bakura10 committed May 16, 2014
2 parents 11c5244 + 0aa91ac commit 730989a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 0.3.1

* ZfrRest now returns input errors correctly if no data was given in the body

## 0.3.0

* Association mapping can now accept one new property: `collectionController`. It allows to map a specific
Expand Down
2 changes: 1 addition & 1 deletion src/ZfrRest/Mvc/Controller/MethodHandler/PostHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function handleMethod(AbstractRestfulController $controller, ResourceInte
}

$singleResource = $resource->getMetadata()->createResource();
$data = json_decode($controller->getRequest()->getContent(), true);
$data = json_decode($controller->getRequest()->getContent(), true) ?: [];

if ($controller->getAutoValidate()) {
$data = $this->validateData($singleResource, $data, $controller);
Expand Down
2 changes: 1 addition & 1 deletion src/ZfrRest/Mvc/Controller/MethodHandler/PutHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function handleMethod(AbstractRestfulController $controller, ResourceInte
throw new MethodNotAllowedException();
}

$data = json_decode($controller->getRequest()->getContent(), true);
$data = json_decode($controller->getRequest()->getContent(), true) ?: [];

if ($controller->getAutoValidate()) {
$data = $this->validateData($resource, $data, $controller);
Expand Down
32 changes: 32 additions & 0 deletions tests/ZfrRestTest/Mvc/Controller/MethodHandler/PostHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,36 @@ public function testThrowMethodNotAllowedIfNoPostMethodIsSet()

$handler->handleMethod($controller, $this->getMock('ZfrRest\Resource\ResourceInterface'));
}

public function testCreateEmptyDataIfNoBodyIsPassed()
{
$controller = $this->getMock('ZfrRest\Mvc\Controller\AbstractRestfulController', ['post']);

$handler = $this->getMock(
'ZfrRest\Mvc\Controller\MethodHandler\PostHandler',
['validateData', 'hydrateData'],
[],
'',
false
);

$singleResource = $this->getMock('ZfrRest\Resource\ResourceInterface');

$metadata = $this->getMock('ZfrRest\Resource\Metadata\ResourceMetadataInterface');
$metadata->expects($this->once())
->method('createResource')
->will($this->returnValue($singleResource));

$resource = $this->getMock('ZfrRest\Resource\ResourceInterface');
$resource->expects($this->once())
->method('getMetadata')
->will($this->returnValue($metadata));

$handler->expects($this->once())
->method('validateData')
->with($singleResource, [], $controller)
->will($this->returnValue([]));

$handler->handleMethod($controller, $resource);
}
}
21 changes: 21 additions & 0 deletions tests/ZfrRestTest/Mvc/Controller/MethodHandler/PutHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,25 @@ public function testThrowMethodNotAllowedIfNoPutMethodIsSet()

$handler->handleMethod($controller, $this->getMock('ZfrRest\Resource\ResourceInterface'));
}

public function testCreateEmptyDataIfNoBodyIsPassed()
{
$controller = $this->getMock('ZfrRest\Mvc\Controller\AbstractRestfulController', ['put']);

$handler = $this->getMock(
'ZfrRest\Mvc\Controller\MethodHandler\PutHandler',
['validateData', 'hydrateData'],
[],
'',
false
);

$resource = $this->getMock('ZfrRest\Resource\ResourceInterface');
$handler->expects($this->once())
->method('validateData')
->with($resource, [], $controller)
->will($this->returnValue([]));

$handler->handleMethod($controller, $resource);
}
}

0 comments on commit 730989a

Please sign in to comment.