Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/5364' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Jan 3, 2014
2 parents 81fbb16 + 1962fd4 commit b5e5b5b
Show file tree
Hide file tree
Showing 10 changed files with 453 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Guard/AllGuardsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Stdlib\Guard;

/**
* An aggregate for all guard traits
*/
trait AllGuardsTrait
{
use ArrayOrTraversableGuardTrait;
use EmptyGuardTrait;
use NullGuardTrait;
}
41 changes: 41 additions & 0 deletions src/Guard/ArrayOrTraversableGuardTrait.php
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-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Stdlib\Guard;

use Traversable;

/**
* Provide a guard method for array or Traversable data
*/
trait ArrayOrTraversableGuardTrait
{
/**
* Verifies that the data is an array or Traversable
*
* @param mixed $data the data to verify
* @param string $dataName the data name
* @param string $exceptionClass FQCN for the exception
* @throws \Exception
*/
protected function guardForArrayOrTraversable(
$data,
$dataName = 'Argument',
$exceptionClass = 'Zend\Stdlib\Exception\InvalidArgumentException'
) {
if (!is_array($data) && !($data instanceof Traversable)) {
$message = sprintf(
"%s must be an array or Traversable, [%s] given",
$dataName,
is_object($data) ? get_class($data) : gettype($data)
);
throw new $exceptionClass($message);
}
}
}
35 changes: 35 additions & 0 deletions src/Guard/EmptyGuardTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Stdlib\Guard;

/**
* Provide a guard method against empty data
*/
trait EmptyGuardTrait
{
/**
* Verify that the data is not empty
*
* @param mixed $data the data to verify
* @param string $dataName the data name
* @param string $exceptionClass FQCN for the exception
* @throws \Exception
*/
protected function guardAgainstEmpty(
$data,
$dataName = 'Argument',
$exceptionClass = 'Zend\Stdlib\Exception\InvalidArgumentException'
) {
if (empty($data)) {
$message = sprintf('%s cannot be empty', $dataName);
throw new $exceptionClass($message);
}
}
}
85 changes: 85 additions & 0 deletions src/Guard/GuardUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Stdlib\Guard;

use Traversable;

/**
* Static guard helper class
*
* Bridges the gap for allowing refactoring until traits can be used by default.
*
* @deprecated
*/
abstract class GuardUtils
{
const DEFAULT_EXCEPTION_CLASS = 'Zend\Stdlib\Exception\InvalidArgumentException';

/**
* Verifies that the data is an array or Traversable
*
* @param mixed $data the data to verify
* @param string $dataName the data name
* @param string $exceptionClass FQCN for the exception
* @throws \Exception
*/
public static function guardForArrayOrTraversable(
$data,
$dataName = 'Argument',
$exceptionClass = self::DEFAULT_EXCEPTION_CLASS
) {
if (!is_array($data) && !($data instanceof Traversable)) {
$message = sprintf(
'%s must be an array or Traversable, [%s] given',
$dataName,
is_object($data) ? get_class($data) : gettype($data)
);
throw new $exceptionClass($message);
}
}

/**
* Verify that the data is not empty
*
* @param mixed $data the data to verify
* @param string $dataName the data name
* @param string $exceptionClass FQCN for the exception
* @throws \Exception
*/
public static function guardAgainstEmpty(
$data,
$dataName = 'Argument',
$exceptionClass = self::DEFAULT_EXCEPTION_CLASS
) {
if (empty($data)) {
$message = sprintf('%s cannot be empty', $dataName);
throw new $exceptionClass($message);
}
}

/**
* Verify that the data is not null
*
* @param mixed $data the data to verify
* @param string $dataName the data name
* @param string $exceptionClass FQCN for the exception
* @throws \Exception
*/
public static function guardAgainstNull(
$data,
$dataName = 'Argument',
$exceptionClass = self::DEFAULT_EXCEPTION_CLASS
) {
if (null === $data) {
$message = sprintf('%s cannot be null', $dataName);
throw new $exceptionClass($message);
}
}
}
35 changes: 35 additions & 0 deletions src/Guard/NullGuardTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Stdlib\Guard;

/**
* Provide a guard method against null data
*/
trait NullGuardTrait
{
/**
* Verify that the data is not null
*
* @param mixed $data the data to verify
* @param string $dataName the data name
* @param string $exceptionClass FQCN for the exception
* @throws \Exception
*/
protected function guardAgainstNull(
$data,
$dataName = 'Argument',
$exceptionClass = 'Zend\Stdlib\Exception\InvalidArgumentException'
) {
if (null === $data) {
$message = sprintf('%s cannot be null', $dataName);
throw new $exceptionClass($message);
}
}
}
51 changes: 51 additions & 0 deletions test/Guard/ArrayOrTraversableGuardTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Stdlib\Guard;

use PHPUnit_Framework_TestCase as TestCase;
use ZendTest\Stdlib\TestAsset\GuardedObject;
use Zend\Stdlib\ArrayObject;

/**
* @requires PHP 5.4
* @covers Zend\Stdlib\Guard\ArrayOrTraversableGuardTrait
*/
class ArrayOrTraversableGuardTraitTest extends TestCase
{
public function setUp()
{
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
$this->markTestSkipped('Only valid for php >= 5.4');
}
}

public function testGuardForArrayOrTraversableThrowsException()
{
$object = new GuardedObject;
$this->setExpectedException(
'Zend\Stdlib\Exception\InvalidArgumentException',
'Argument must be an array or Traversable, [string] given'
);
$object->setArrayOrTraversable('');
}

public function testGuardForArrayOrTraversableAllowsArray()
{
$object = new GuardedObject;
$this->assertNull($object->setArrayOrTraversable(array()));
}

public function testGuardForArrayOrTraversableAllowsTraversable()
{
$object = new GuardedObject;
$traversable = new ArrayObject;
$this->assertNull($object->setArrayOrTraversable($traversable));
}
}
43 changes: 43 additions & 0 deletions test/Guard/EmptyGuardTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Stdlib\Guard;

use PHPUnit_Framework_TestCase as TestCase;
use ZendTest\Stdlib\TestAsset\GuardedObject;

/**
* @requires PHP 5.4
* @covers Zend\Stdlib\Guard\EmptyGuardTrait
*/
class EmptyGuardTraitTest extends TestCase
{
public function setUp()
{
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
$this->markTestSkipped('Only valid for php >= 5.4');
}
}

public function testGuardAgainstEmptyThrowsException()
{
$object = new GuardedObject;
$this->setExpectedException(
'Zend\Stdlib\Exception\InvalidArgumentException',
'Argument cannot be empty'
);
$object->setNotEmpty('');
}

public function testGuardAgainstEmptyAllowsNonEmptyString()
{
$object = new GuardedObject;
$this->assertNull($object->setNotEmpty('foo'));
}
}
68 changes: 68 additions & 0 deletions test/Guard/GuardUtilsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Stdlib\Guard;

use PHPUnit_Framework_TestCase as TestCase;
use Zend\Stdlib\Guard\GuardUtils;
use Zend\Stdlib\ArrayObject;

/**
* @covers Zend\Stdlib\Guard\GuardUtils
*/
class GuardUtilsTest extends TestCase
{
public function testGuardForArrayOrTraversableThrowsException()
{
$this->setExpectedException(
'Zend\Stdlib\Exception\InvalidArgumentException',
'Argument must be an array or Traversable, [string] given'
);
GuardUtils::guardForArrayOrTraversable('');
}

public function testGuardForArrayOrTraversableAllowsArray()
{
$this->assertNull(GuardUtils::guardForArrayOrTraversable(array()));
}

public function testGuardForArrayOrTraversableAllowsTraversable()
{
$traversable = new ArrayObject;
$this->assertNull(GuardUtils::guardForArrayOrTraversable($traversable));
}

public function testGuardAgainstEmptyThrowsException()
{
$this->setExpectedException(
'Zend\Stdlib\Exception\InvalidArgumentException',
'Argument cannot be empty'
);
GuardUtils::guardAgainstEmpty('');
}

public function testGuardAgainstEmptyAllowsNonEmptyString()
{
$this->assertNull(GuardUtils::guardAgainstEmpty('foo'));
}

public function testGuardAgainstNullThrowsException()
{
$this->setExpectedException(
'Zend\Stdlib\Exception\InvalidArgumentException',
'Argument cannot be null'
);
GuardUtils::guardAgainstNull(null);
}

public function testGuardAgainstNullAllowsNonNull()
{
$this->assertNull(GuardUtils::guardAgainstNull('foo'));
}
}
Loading

0 comments on commit b5e5b5b

Please sign in to comment.