Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Unit tests and error handling

  • Loading branch information...
commit 36511a869722469571e3d57aeca16bbf302969e6 1 parent 0e9664a
@iwalz authored
View
2  library/Zend/Stdlib/Hydrator/ClassMethods.php
@@ -78,7 +78,6 @@ public function setOptions($options)
public function setUnderscoreSeparatedKeys($underscoreSeparatedKeys)
{
$this->underscoreSeparatedKeys = $underscoreSeparatedKeys;
-
return $this;
}
@@ -88,7 +87,6 @@ public function setUnderscoreSeparatedKeys($underscoreSeparatedKeys)
public function getUnderscoreSeparatedKeys()
{
return $this->underscoreSeparatedKeys;
-
}
/**
View
41 library/Zend/Stdlib/Hydrator/Filter/FilterComposite.php
@@ -9,6 +9,8 @@
*/
namespace Zend\Stdlib\Hydrator\Filter;
+use Zend\Stdlib\Exception\InvalidArgumentException;
+
/**
* @category Zend
* @package Zend_Stdlib
@@ -36,9 +38,39 @@ class FilterComposite implements FilterInterface
/**
* Define default Filter
+ *
+ * @throws InvalidArgumentException
*/
public function __construct($orFilter = array(), $andFilter = array())
{
+ array_walk($orFilter,
+ function($value, $key) {
+ if(
+ !is_callable($value) &&
+ !$value instanceof FilterInterface
+ ) {
+ throw new InvalidArgumentException(
+ 'The value of ' . $key . ' should be either a callable or ' .
+ 'an instance of Zend\Stdlib\Hydrator\Filter\FilterInterface'
+ );
+ }
+ }
+ );
+
+ array_walk($andFilter,
+ function($value, $key) {
+ if(
+ !is_callable($value) &&
+ !$value instanceof FilterInterface
+ ) {
+ throw new InvalidArgumentException(
+ 'The value of ' . $key . ' should be either a callable or ' .
+ 'an instance of Zend\Stdlib\Hydrator\Filter\FilterInterface'
+ );
+ }
+ }
+ );
+
$this->orFilter = new \ArrayObject($orFilter);
$this->andFilter = new \ArrayObject($andFilter);
}
@@ -62,6 +94,7 @@ public function __construct($orFilter = array(), $andFilter = array())
* @param string $name
* @param callable|FilterInterface $filter
* @param int $condition Can be either FilterComposite::CONDITION_OR or FilterComposite::CONDITION_AND
+ * @throws InvalidArgumentException
*/
public function addFilter($name, $filter, $condition = self::CONDITION_OR)
{
@@ -71,10 +104,14 @@ public function addFilter($name, $filter, $condition = self::CONDITION_OR)
) {
if ($condition === self::CONDITION_OR) {
$this->orFilter[$name] = $filter;
- }
- if ($condition === self::CONDITION_AND) {
+ } elseif ($condition === self::CONDITION_AND) {
$this->andFilter[$name] = $filter;
}
+ } else {
+ throw new InvalidArgumentException(
+ 'The value of ' . $name . ' should be either a callable or ' .
+ 'an instance of Zend\Stdlib\Hydrator\Filter\FilterInterface'
+ );
}
}
View
90 tests/ZendTest/Stdlib/TestAsset/FilterCompositeTest.php
@@ -12,56 +12,56 @@
use Zend\Stdlib\Hydrator\Filter\FilterComposite;
-class ValidationCompositeTest extends \PHPUnit_Framework_TestCase
+class FilterCompositeTest extends \PHPUnit_Framework_TestCase
{
- protected $validatorComposite;
+ protected $filterComposite;
public function setUp()
{
- $this->validatorComposite = new FilterComposite();
+ $this->filterComposite = new FilterComposite();
}
public function testValidationAdd()
{
- $this->assertTrue($this->validatorComposite->filter("foo"));
- $this->validatorComposite->addFilter("has",
+ $this->assertTrue($this->filterComposite->filter("foo"));
+ $this->filterComposite->addFilter("has",
function($property) {
return false;
}
);
- $this->assertFalse($this->validatorComposite->filter("foo"));
+ $this->assertFalse($this->filterComposite->filter("foo"));
}
public function testValidationRemove()
{
- $this->validatorComposite->addFilter("has",
+ $this->filterComposite->addFilter("has",
function($property) {
return false;
}
);
- $this->assertFalse($this->validatorComposite->filter("foo"));
- $this->validatorComposite->removeFilter("has");
- $this->assertTrue($this->validatorComposite->filter("foo"));
+ $this->assertFalse($this->filterComposite->filter("foo"));
+ $this->filterComposite->removeFilter("has");
+ $this->assertTrue($this->filterComposite->filter("foo"));
}
public function testValidationHas()
{
- $this->validatorComposite->addFilter("has",
+ $this->filterComposite->addFilter("has",
function($property) {
return false;
}
);
- $this->assertFalse($this->validatorComposite->filter("foo"));
- $this->assertTrue($this->validatorComposite->hasFilter("has"));
+ $this->assertFalse($this->filterComposite->filter("foo"));
+ $this->assertTrue($this->filterComposite->hasFilter("has"));
}
public function testComplexValidation()
{
- $this->validatorComposite->addFilter("has", new \Zend\Stdlib\Hydrator\Filter\HasFilter());
- $this->validatorComposite->addFilter("get", new \Zend\Stdlib\Hydrator\Filter\GetFilter());
- $this->validatorComposite->addFilter("is", new \Zend\Stdlib\Hydrator\Filter\IsFilter());
+ $this->filterComposite->addFilter("has", new \Zend\Stdlib\Hydrator\Filter\HasFilter());
+ $this->filterComposite->addFilter("get", new \Zend\Stdlib\Hydrator\Filter\GetFilter());
+ $this->filterComposite->addFilter("is", new \Zend\Stdlib\Hydrator\Filter\IsFilter());
- $this->validatorComposite->addFilter("exclude",
+ $this->filterComposite->addFilter("exclude",
function($property) {
$method = substr($property, strpos($property, '::'));
@@ -73,7 +73,59 @@ function($property) {
}, FilterComposite::CONDITION_AND
);
- $this->assertTrue($this->validatorComposite->filter('getFooBar'));
- $this->assertFalse($this->validatorComposite->filter('getServiceLocator'));
+ $this->assertTrue($this->filterComposite->filter('getFooBar'));
+ $this->assertFalse($this->filterComposite->filter('getServiceLocator'));
+ }
+
+ public function testConstructorInjection()
+ {
+ $andCondition = array(
+ 'servicelocator' => function($property) {
+ if($property === 'getServiceLocator') {
+ return false;
+ }
+ return true;
+ },
+ 'foobar' => function($property) {
+ if($property === 'getFooBar') {
+ return false;
+ }
+ return true;
+ }
+ );
+ $orCondition = array(
+ 'has' => new \Zend\Stdlib\Hydrator\Filter\HasFilter(),
+ 'get' => new \Zend\Stdlib\Hydrator\Filter\GetFilter()
+ );
+ $filterComposite = new FilterComposite($orCondition, $andCondition);
+
+ $this->assertFalse($filterComposite->filter('getFooBar'));
+ $this->assertFalse($filterComposite->filter('geTFooBar'));
+ $this->assertFalse($filterComposite->filter('getServiceLocator'));
+ $this->assertTrue($filterComposite->filter('getFoo'));
+ $this->assertTrue($filterComposite->filter('hasFoo'));
+ }
+
+ /**
+ * @expectedException Zend\Stdlib\Exception\InvalidArgumentException
+ * @expectedExceptionMessage The value of test should be either a callable
+ * or an instance of Zend\Stdlib\Hydrator\Filter\FilterInterface
+ */
+ public function testInvalidParameterConstructorInjection()
+ {
+ $andCondition = array('foo' => 'bar');
+ $orCondition = array('test' => 'blubb');
+
+ new FilterComposite($orCondition, $andCondition);
+ }
+
+ /**
+ * @expectedException Zend\Stdlib\Exception\InvalidArgumentException
+ * @expectedExceptionMessage The value of foo should be either a callable
+ * or an instance of Zend\Stdlib\Hydrator\Filter\FilterInterface
+ */
+ public function testInvalidFilterInjection()
+ {
+ $this->filterComposite->addFilter('foo', 'bar');
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.