New tests for exceptions.

This commit is contained in:
Alexandre 2011-02-20 15:03:09 -03:00
parent 91da403137
commit 62f19fe798
5 changed files with 169 additions and 14 deletions

View file

@ -15,7 +15,7 @@ class AbstractNestedException extends ValidationException
public function addRelated(ValidationException $related)
{
$this->related[] = $related;
$this->related[spl_object_hash($related)] = $related;
return $this;
}

View file

@ -17,13 +17,14 @@ class ValidationException extends InvalidArgumentException
protected $id = 'validation';
protected $name = '';
protected $template = '';
protected $params=array();
public static function format($template, array $vars=array())
{
return preg_replace_callback(
'/{{(\w+)}}/',
function($match) use($vars) {
return $vars[$match[1]];
return isset($vars[$match[1]]) ? $vars[$match[1]] : $match[0];
}, $template
);
}
@ -99,7 +100,7 @@ class ValidationException extends InvalidArgumentException
if (!empty($this->template))
return $this->template;
else
return $this->buildTemplate();
return $this->template = $this->buildTemplate();
}
public function hasParam($name)
@ -124,10 +125,15 @@ class ValidationException extends InvalidArgumentException
return $this;
}
public function setParam($key, $value)
{
$this->params[$key] = static::stringify($value);
}
public function setParams(array $params)
{
$this->params = array_map(array(get_called_class(), 'stringify'),
$params);
foreach ($params as $key => $value)
$this->setParam($key, $value);
}
public function setTemplate($template)
@ -139,9 +145,9 @@ class ValidationException extends InvalidArgumentException
{
$templateKey = $this->chooseTemplate();
if (is_null($this->context))
$this->template = static::$defaultTemplates[$templateKey];
return static::$defaultTemplates[$templateKey];
else
$this->template = $this->context->getTemplate($this, $templateKey);
return $this->context->getTemplate($this, $templateKey);
}
protected function guessId()

View file

@ -1,8 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<package packagerversion="1.9.1" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0
http://pear.php.net/dtd/tasks-1.0.xsd
http://pear.php.net/dtd/package-2.0
http://pear.php.net/dtd/package-2.0.xsd">
<package packagerversion="1.9.1" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd">
<name>RespectValidation</name>
<uri>http://github.com/Respect/Validation</uri>
<summary>The most awesome validation engine ever created for PHP</summary>
@ -14,7 +11,7 @@
<active>yes</active>
</lead>
<date>2011-02-20</date>
<time>14:06:50</time>
<time>15:02:52</time>
<version>
<release>0.2</release>
<api>0.2</api>
@ -32,7 +29,7 @@
<file baseinstalldir="Respect/Validation" md5sum="4ef5c6ff21a7382ac26d74354a9c2ece" name="Contexts/AbstractContext.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a99e9fcc3f89d82a4b6dfd0fbe487014" name="Contexts/Form.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="ef1b18cf77f1806f777f0600abde19c1" name="Exceptions/AbstractGroupedException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="040df90f709ce6780c8c4be2f5b708f8" name="Exceptions/AbstractNestedException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="19c0319d71edea33dee50be75e973185" name="Exceptions/AbstractNestedException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="3e4da4e7b629a4889f87517506664bf3" name="Exceptions/AllOfException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="ae818fe09029e856ed02ce70273da0f5" name="Exceptions/AlnumException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a4e7f3830a04d92bf48ae2e88bb8d488" name="Exceptions/AlphaException.php" role="php" />
@ -70,7 +67,7 @@
<file baseinstalldir="Respect/Validation" md5sum="cb0971a4379482977ddf22a675581255" name="Exceptions/RegexException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="d0498462a420d57ca74fa47e3c8c6194" name="Exceptions/SfException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a9d0825a244e0b7d6412777c01367650" name="Exceptions/StringException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a5b6a3c68b826aef74b6fe19c696b2c0" name="Exceptions/ValidationException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="29a4034c405fab7925927a2f9ebc72a9" name="Exceptions/ValidationException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="f7294294fd18031fd9ad746ca4945369" name="Exceptions/ZendException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="67eecfc191e3e8d2b7137ba7bac0c574" name="Rules/AbstractComposite.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a2e9210c6722fa39592ee89d9719df3f" name="Rules/AbstractRelated.php" role="php" />

View file

@ -0,0 +1,48 @@
<?php
namespace Respect\Validation\Exceptions;
class AbstractNestedExceptionTest extends \PHPUnit_Framework_TestCase
{
public function testAddRelated()
{
$x = new AttributeException;
$int = new IntException;
$x->addRelated($int);
$this->assertEquals(1, count($x->getRelated(true)));
}
public function testAddRelatedIdentity()
{
$x = new AttributeException;
$int = new IntException;
$x->addRelated($int);
$x->addRelated($int);
$x->addRelated($int);
$this->assertEquals(1, count($x->getRelated(true)));
}
public function testFindRelated()
{
$foo = new AttributeException;
$bar = new AttributeException;
$baz = new AttributeException;
$bat = new AttributeException;
$foo->configure('foo');
$bar->configure('bar');
$baz->configure('baz');
$bat->configure('bat');
$foo->addRelated($bar);
$bar->addRelated($baz);
$baz->addRelated($bat);
$this->assertSame($bar, $foo->findRelated('bar'));
$this->assertSame($baz, $foo->findRelated('baz'));
$this->assertSame($baz, $foo->findRelated('bar', 'baz'));
$this->assertSame($baz, $foo->findRelated('baz'));
$this->assertSame($bat, $foo->findRelated('bar', 'bat'));
$this->assertSame(false, $foo->findRelated('none'));
$this->assertSame(false, $foo->findRelated('bar', 'none'));
}
}

View file

@ -0,0 +1,104 @@
<?php
namespace Respect\Validation\Exceptions;
class ValidationExceptionTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider providerForFormat
*/
public function testFormat($template, $result, $vars)
{
$this->assertEquals(
$result,
ValidationException::format($template, $vars)
);
}
/**
* @dataProvider providerForStringify
*/
public function testStringify($input, $result)
{
$this->assertEquals(
$result,
ValidationException::stringify($input)
);
}
public function testGetMainMessage()
{
$x = new ValidationException();
$x->configure('foo', array('bar' => 1, 'baz' => 2));
$x->setTemplate('{{name}} {{bar}} {{baz}}');
$this->assertEquals(
'foo 1 2',
$x->getMainMessage()
);
}
public function testGetTemplateSet()
{
$x = new ValidationException();
$x->configure('bar');
$x->setTemplate('foo');
$this->assertEquals('foo', $x->getTemplate());
}
public function testGetTemplateChosen()
{
$x = new ValidationException();
$x->configure('bar');
$this->assertEquals(
ValidationException::$defaultTemplates[0],
$x->getTemplate()
);
}
/**
* @dataProvider providerForStringify
*/
public function testSetParam($input, $expected)
{
$x = new ValidationException;
$x->setParam('foo', $input);
$this->assertEquals(
$expected,
$x->getParam('foo')
);
}
public function providerForStringify()
{
return array(
array('foo', 'foo'),
array(123, '123'),
array(array(), "Array"),
array(new \stdClass, "Object of class stdClass"),
array($x = new \DateTime, $x->format('Y-m-d H:i:s')),
);
}
public function providerForFormat()
{
return array(
array(
'{{foo}} {{bar}} {{baz}}',
'hello world respect',
array('foo' => 'hello', 'bar' => 'world', 'baz' => 'respect')
),
array(
'{{foo}} {{bar}} {{baz}}',
'hello {{bar}} respect',
array('foo' => 'hello', 'baz' => 'respect')
),
array(
'{{foo}} {{bar}} {{baz}}',
'hello {{bar}} respect',
array('foo' => 'hello', 'bot' => 111, 'baz' => 'respect')
)
);
}
}