respect-validation/tests/unit/Rules/JsonTest.php
2015-10-07 01:00:39 -03:00

66 lines
1.6 KiB
PHP

<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
namespace Respect\Validation\Rules;
/**
* @group rule
* @covers Respect\Validation\Rules\Json
* @covers Respect\Validation\Exceptions\JsonException
*/
class JsonTest extends \PHPUnit_Framework_TestCase
{
protected $json;
protected function setUp()
{
$this->json = new Json();
}
/**
* @dataProvider providerForPass
*/
public function testValidJsonsShouldReturnTrue($input)
{
$this->assertTrue($this->json->__invoke($input));
$this->assertTrue($this->json->check($input));
$this->assertTrue($this->json->assert($input));
}
/**
* @expectedException Respect\Validation\Exceptions\JSonException
*/
public function testInvalidJsonsShouldThrowJsonException()
{
$this->assertFalse($this->json->__invoke('{foo:bar}'));
$this->assertFalse($this->json->assert('{foo:bar}'));
}
public function testInvalidJsonsNotBeValid()
{
$this->assertFalse($this->json->validate(''));
}
public function providerForPass()
{
return array(
array('2'),
array('"abc"'),
array('[1,2,3]'),
array('["foo", "bar", "number", 1]'),
array('{"foo": "bar", "number":1}'),
array('[]'),
array('{}'),
array('false'),
array('null'),
);
}
}