forked from zendframework/zendframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OptionalParametersFilter - checks for methods with only optional para…
…meters/no paremeters
- Loading branch information
Showing
2 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
library/Zend/Stdlib/Hydrator/Filter/OptionalParametersFilter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
namespace Zend\Stdlib\Hydrator\Filter; | ||
|
||
use InvalidArgumentException; | ||
use ReflectionException; | ||
use ReflectionMethod; | ||
use ReflectionParameter; | ||
|
||
/** | ||
* Filter that includes methods which have no parameters or only optional parameters | ||
*/ | ||
class OptionalParametersFilter implements FilterInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function filter($property) | ||
{ | ||
try { | ||
$reflectionMethod = new ReflectionMethod($property); | ||
} catch (ReflectionException $exception) { | ||
throw new InvalidArgumentException(sprintf('Method %s doesn\'t exist', $property)); | ||
} | ||
|
||
$mandatoryParameters = array_filter( | ||
$reflectionMethod->getParameters(), | ||
function (ReflectionParameter $parameter) { | ||
return ! $parameter->isOptional(); | ||
} | ||
); | ||
|
||
return empty($mandatoryParameters); | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
tests/ZendTest/Stdlib/Hydrator/Filter/OptionalParametersFilterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace ZendTest\Stdlib\Hydrator\Filter; | ||
|
||
use Zend\Stdlib\Hydrator\Filter\OptionalParametersFilter; | ||
|
||
/** | ||
* Unit tests for {@see \Zend\Stdlib\Hydrator\Filter\OptionalParametersFilter} | ||
* | ||
* @covers \Zend\Stdlib\Hydrator\Filter\OptionalParametersFilter | ||
*/ | ||
class OptionalParametersFilterTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var OptionalParametersFilter | ||
*/ | ||
protected $filter; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function setUp() | ||
{ | ||
$this->filter = new OptionalParametersFilter(); | ||
} | ||
|
||
/** | ||
* Verifies a list of methods against expected results | ||
* | ||
* @param string $method | ||
* @param bool $expectedResult | ||
* | ||
* @dataProvider methodProvider | ||
*/ | ||
public function testMethods($method, $expectedResult) | ||
{ | ||
$this->assertSame($expectedResult, $this->filter->filter($method)); | ||
} | ||
|
||
public function testTriggersExceptionOnUnknownMethod() | ||
{ | ||
$this->setExpectedException('InvalidArgumentException'); | ||
$this->filter->filter(__CLASS__ . '::' . 'nonExistingMethod'); | ||
} | ||
|
||
/** | ||
* Provides a list of methods to be checked against the filter | ||
* | ||
* @return array | ||
*/ | ||
public function methodProvider() | ||
{ | ||
return array( | ||
array(__CLASS__ . '::' . 'methodWithoutParameters', true), | ||
array(__CLASS__ . '::' . 'methodWithSingleMandatoryParameter', false), | ||
array(__CLASS__ . '::' . 'methodWithSingleOptionalParameter', true), | ||
array(__CLASS__ . '::' . 'methodWithMultipleMandatoryParameters', false), | ||
array(__CLASS__ . '::' . 'methodWithMultipleOptionalParameters', true), | ||
); | ||
} | ||
|
||
/** | ||
* Test asset method | ||
*/ | ||
public function methodWithoutParameters() | ||
{ | ||
} | ||
|
||
/** | ||
* Test asset method | ||
*/ | ||
public function methodWithSingleMandatoryParameter($parameter) | ||
{ | ||
} | ||
|
||
/** | ||
* Test asset method | ||
*/ | ||
public function methodWithSingleOptionalParameter($parameter = null) | ||
{ | ||
} | ||
|
||
/** | ||
* Test asset method | ||
*/ | ||
public function methodWithMultipleMandatoryParameters($parameter, $otherParameter) | ||
{ | ||
} | ||
|
||
/** | ||
* Test asset method | ||
*/ | ||
public function methodWithMultipleOptionalParameters($parameter = null, $otherParameter = null) | ||
{ | ||
} | ||
} |