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

Commit

Permalink
Browse files Browse the repository at this point in the history
- Removed unnecessary imports
- Added required imports (in testing)
- Removed extraneous whitespace
- Removed unnecessary dependency (ArrayUtils)
  • Loading branch information
weierophinney committed Oct 1, 2012
1 parent 63efbf9 commit 893ce23
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 55 deletions.
28 changes: 14 additions & 14 deletions src/IsInstanceOf.php
Expand Up @@ -10,13 +10,13 @@
namespace Zend\Validator;

use Traversable;
use Zend\Stdlib\ArrayUtils;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception\InvalidArgumentException;

/**
* @category Zend
* @package Zend_Validate
*/
class IsInstanceOf extends AbstractValidator
{

const NOT_INSTANCE_OF = 'notInstanceOf';

/**
Expand All @@ -25,7 +25,7 @@ class IsInstanceOf extends AbstractValidator
* @var array
*/
protected $messageTemplates = array(
self::NOT_INSTANCE_OF => "The input is not an instance of '%className%'"
self::NOT_INSTANCE_OF => "The input is not an instance of '%className%'",
);

/**
Expand All @@ -48,16 +48,16 @@ class IsInstanceOf extends AbstractValidator
* Sets validator options
*
* @param array|Traversable $options
* @throws Zend\Validator\Exception\InvalidArgumentException
* @throws Exception\InvalidArgumentException
*/
public function __construct ($options = null)
public function __construct($options = null)
{
if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
$options = iterator_to_array($options);
}

// If argument is not an array, consider first argument as class name
if (! is_array($options)) {
if (!is_array($options)) {
$options = func_get_args();

$tmpOptions = array();
Expand All @@ -66,8 +66,8 @@ public function __construct ($options = null)
$options = $tmpOptions;
}

if (! array_key_exists('className', $options)) {
throw new InvalidArgumentException("Missing option 'className'");
if (!array_key_exists('className', $options)) {
throw new Exception\InvalidArgumentException('Missing option "className"');
}

parent::__construct($options);
Expand All @@ -78,7 +78,7 @@ public function __construct ($options = null)
*
* @return string
*/
public function getClassName ()
public function getClassName()
{
return $this->className;
}
Expand All @@ -89,7 +89,7 @@ public function getClassName ()
* @param string $className
* @return self
*/
public function setClassName ($className)
public function setClassName($className)
{
$this->className = $className;
return $this;
Expand All @@ -101,7 +101,7 @@ public function setClassName ($className)
* @param mixed $value
* @return boolean
*/
public function isValid ($value)
public function isValid($value)
{
if ($value instanceof $this->className) {
return true;
Expand Down
69 changes: 28 additions & 41 deletions test/IsInstanceOfTest.php
Expand Up @@ -20,17 +20,12 @@
*/
namespace ZendTest\Validator;

use Zend\Validator, ReflectionClass;

/**
* Test helper
*/

/**
* @see Zend_Validator_IsInstanceOf
*/
use DateTime;
use ReflectionClass;
use Zend\Validator;

/**
* @covers Zend\Validator\IsInstanceOf
* @category Zend
* @package Zend_Validator
* @subpackage UnitTests
Expand All @@ -40,28 +35,24 @@
*/
class IsInstanceOfTest extends \PHPUnit_Framework_TestCase
{

/**
* Ensures that the validator follows expected behavior
*
* @return void
*/
public function testBasic ()
public function testBasic()
{
$validator = new Validator\IsInstanceOf('\DateTime');

$this->assertTrue($validator->isValid(new \DateTime())); // True
$validator = new Validator\IsInstanceOf('DateTime');
$this->assertTrue($validator->isValid(new DateTime())); // True
$this->assertFalse($validator->isValid(null)); // False
$this->assertFalse($validator->isValid($this)); // False

$validator = new Validator\IsInstanceOf('\Exception');

$validator = new Validator\IsInstanceOf('Exception');
$this->assertTrue($validator->isValid(new \Exception())); // True
$this->assertFalse($validator->isValid(null)); // False
$this->assertFalse($validator->isValid($this)); // False

$validator = new Validator\IsInstanceOf('\PHPUnit_Framework_TestCase');

$validator = new Validator\IsInstanceOf('PHPUnit_Framework_TestCase');
$this->assertTrue($validator->isValid($this)); // True
}

Expand All @@ -70,9 +61,9 @@ public function testBasic ()
*
* @return void
*/
public function testGetMessages ()
public function testGetMessages()
{
$validator = new Validator\IsInstanceOf('\DateTime');
$validator = new Validator\IsInstanceOf('DateTime');
$this->assertEquals(array(), $validator->getMessages());
}

Expand All @@ -81,41 +72,37 @@ public function testGetMessages ()
*
* @return void
*/
public function testGetClassName ()
public function testGetClassName()
{
$validator = new Validator\IsInstanceOf('\DateTime');
$this->assertEquals('\DateTime', $validator->getClassName());
$validator = new Validator\IsInstanceOf('DateTime');
$this->assertEquals('DateTime', $validator->getClassName());
}

public function testEqualsMessageTemplates ()
public function testEqualsMessageTemplates()
{
$validator = new Validator\IsInstanceOf('\DateTime');
$validator = new Validator\IsInstanceOf('DateTime');
$reflection = new ReflectionClass($validator);

if (! $reflection->hasProperty('_messageTemplates')) {
return;
}

$property = $reflection->getProperty('_messageTemplates');
$property = $reflection->getProperty('messageTemplates');
$property->setAccessible(true);

$this->assertEquals($property->getValue($validator),
$validator->getOption('messageTemplates'));
$this->assertEquals(
$property->getValue($validator),
$validator->getOption('messageTemplates')
);
}

public function testEqualsMessageVariables ()
public function testEqualsMessageVariables()
{
$validator = new Validator\IsInstanceOf('\DateTime');
$validator = new Validator\IsInstanceOf('\DateTime');
$reflection = new ReflectionClass($validator);

if (! $reflection->hasProperty('_messageVariables')) {
return;
}

$property = $reflection->getProperty('_messageVariables');
$property = $reflection->getProperty('messageVariables');
$property->setAccessible(true);

$this->assertEquals($property->getValue($validator),
$validator->getOption('messageVariables'));
$this->assertEquals(
$property->getValue($validator),
$validator->getOption('messageVariables')
);
}
}

0 comments on commit 893ce23

Please sign in to comment.