Skip to content

Commit

Permalink
Add doc and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bakura10 committed Apr 2, 2014
1 parent a513776 commit 2bbaace
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 76 deletions.
51 changes: 51 additions & 0 deletions docs/04. Controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,57 @@ class TweetListController extends AbstractRestfulController

ZfrRest only supports going back one level in the hierarchy.

## Configuring input filters based on context

Quite often, you need different validation rules depending on the HTTP method. For instance, POST method for creating
a user may require a first name, a last name and a password. However, you want restrict updating (PUT method) to
only password and last name, so that they cannot change their first name.

Zend Framework 2 input filters offer this feature through so-called validation groups. Validation groups allow to
skip unwanted values and do not return them. ZfrRest allows you to do this through a hook called `configureInputFilter`
that you can override in your controller. This method is called by POST and PUT handlers. For instance:

```php
use Zend\InputFilter\InputFilterInterface;

class UserController
{
public function configureInputFilter(InputFilterInterface $inputFilter)
{
$method = strtolower($this->request->getMethod());

// We want to keep all the fields for POST, but limit them for PUT
if ($method === 'put') {
$inputFilter = $inputFilter->setValidationGroup(['last_name', 'password']);
}

return $inputFilter;
}
}
```

You are not restricted to using validation groups. In some cases, you may need a completely different input filter,
because rules are dramatically different. You can do that too:

```php
use Zend\InputFilter\InputFilterInterface;

class UserController
{
public function configureInputFilter(InputFilterInterface $inputFilter)
{
$method = strtolower($this->request->getMethod());

// We want to keep all the fields for POST, but limit them for PUT
if ($method === 'put') {
$inputFilter = new UserUpdateInputFilter();
}

return $inputFilter;
}
}
```

### Navigation

* Continue to [**Built-in listeners**](/docs/05. Built-in listeners.md)
Expand Down
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ If you are looking for some information that is not listed in the documentation,
4. [Controllers](/docs/04. Controllers.md)
1. [Understanding method handlers](/docs/04. Controllers.md#method-handlers)
2. [Configuring controller behaviours](/docs/04. Controllers.md#configuring-controller-behaviours)
3. [Context resource](/docs/04. Controllers.md#context-resource)
4. [Configuring input filters based on context](/docs/04. Controllers.md#configuring-input-filters-based-on-context)

5. [Built-in listeners](/docs/05. Built-in listeners.md)
1. [CreateResourceModelListener](/docs/05. Built-in listeners.md)
Expand Down
36 changes: 0 additions & 36 deletions tests/ZfrRestTest/Asset/Mvc/ControllerWithValidationGroupSpec.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
namespace ZfrRestTest\Mvc\Controller\MethodHandler;

use PHPUnit_Framework_TestCase;
use ZfrRest\Options\ControllerBehavioursOptions;
use ZfrRestTest\Asset\Mvc\ControllerWithValidationGroupSpec;
use ZfrRestTest\Asset\Mvc\DataValidationObject;

/**
Expand Down Expand Up @@ -58,7 +56,10 @@ public function testThrowExceptionIfNoInputFilterNameIsDefined()
$resource->expects($this->once())->method('getMetadata')->will($this->returnValue($metadata));
$metadata->expects($this->once())->method('getInputFilterName')->will($this->returnValue(null));

$this->dataValidation->validateData($resource, []);
$controller = $this->getMock('ZfrRest\Mvc\Controller\AbstractRestfulController');
$controller->expects($this->never())->method('configureInputFilter');

$this->dataValidation->validateData($resource, [], $controller);
}

public function testCanValidateData()
Expand Down Expand Up @@ -86,7 +87,13 @@ public function testCanValidateData()
->method('getValues')
->will($this->returnValue(['filtered']));

$result = $this->dataValidation->validateData($resource, $data);
$controller = $this->getMock('ZfrRest\Mvc\Controller\AbstractRestfulController');
$controller->expects($this->once())
->method('configureInputFilter')
->with($this->isInstanceOf('Zend\InputFilter\InputFilterInterface'))
->will($this->returnArgument(0));

$result = $this->dataValidation->validateData($resource, $data, $controller);

$this->assertEquals(['filtered'], $result);
}
Expand Down Expand Up @@ -116,42 +123,12 @@ public function testThrowExceptionOnFailedValidation()
->with('inputFilter')
->will($this->returnValue($inputFilter));

$this->dataValidation->validateData($resource, $data);
}

public function testCanUseValidationGroup()
{
$resource = $this->getMock('ZfrRest\Resource\ResourceInterface');
$metadata = $this->getMock('ZfrRest\Resource\Metadata\ResourceMetadataInterface');

$resource->expects($this->once())->method('getMetadata')->will($this->returnValue($metadata));
$metadata->expects($this->once())->method('getInputFilterName')->will($this->returnValue('inputFilter'));

$data = ['foo'];

$inputFilter = $this->getMock('Zend\InputFilter\InputFilterInterface');
$inputFilter->expects($this->once())->method('setData')->with($data);
$inputFilter->expects($this->once())
->method('isValid')
->will($this->returnValue(true));

$inputFilter->expects($this->once())
->method('setValidationGroup')
->with(['field']);

$this->inputFilterPluginManager->expects($this->once())
->method('get')
->with('inputFilter')
->will($this->returnValue($inputFilter));

$inputFilter->expects($this->once())
->method('getValues')
->will($this->returnValue(['filtered']));

$controller = new ControllerWithValidationGroupSpec();
$controller = $this->getMock('ZfrRest\Mvc\Controller\AbstractRestfulController');
$controller->expects($this->once())
->method('configureInputFilter')
->with($this->isInstanceOf('Zend\InputFilter\InputFilterInterface'))
->will($this->returnArgument(0));

$result = $this->dataValidation->validateData($resource, $data, $controller, 'post');

$this->assertEquals(['filtered'], $result);
$this->dataValidation->validateData($resource, $data, $controller);
}
}

0 comments on commit 2bbaace

Please sign in to comment.