respect-validation/tests/unit/Rules/SortedTest.php
2017-11-12 14:22:22 +01:00

120 lines
2.8 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;
use PHPUnit\Framework\TestCase;
/**
* @group rule
* @covers \Respect\Validation\Rules\Sorted
* @covers \Respect\Validation\Exceptions\SortedException
*/
class SortedTest extends TestCase
{
public function testPasses()
{
$arr = [1, 2, 3];
$rule = new Sorted();
$this->assertTrue($rule->validate($arr));
$this->assertTrue($rule->assert($arr));
$this->assertTrue($rule->check($arr));
}
public function testPassesWithEqualValues()
{
$arr = [1, 2, 2, 3];
$rule = new Sorted();
$this->assertTrue($rule->validate($arr));
$this->assertTrue($rule->assert($arr));
$this->assertTrue($rule->check($arr));
}
/**
* @expectedException \Respect\Validation\Exceptions\SortedException
*/
public function testNotPasses()
{
$arr = [1, 2, 4, 3];
$rule = new Sorted();
$this->assertFalse($rule->validate($arr));
$this->assertFalse($rule->check($arr));
}
public function testPassesDescending()
{
$arr = [10, 9, 8];
$rule = new Sorted(null, false);
$this->assertTrue($rule->validate($arr));
$this->assertTrue($rule->assert($arr));
$this->assertTrue($rule->check($arr));
}
public function testPassesDescendingWithEqualValues()
{
$arr = [10, 9, 9, 8];
$rule = new Sorted(null, false);
$this->assertTrue($rule->validate($arr));
$this->assertTrue($rule->assert($arr));
$this->assertTrue($rule->check($arr));
}
public function testPassesByFunction()
{
$arr = [
[
'key' => 1,
],
[
'key' => 2,
],
[
'key' => 5,
],
];
$rule = new Sorted(function ($x) {
return $x['key'];
});
$this->assertTrue($rule->validate($arr));
$this->assertTrue($rule->assert($arr));
$this->assertTrue($rule->check($arr));
}
/**
* @expectedException \Respect\Validation\Exceptions\SortedException
*/
public function testNotPassesByFunction()
{
$arr = [
[
'key' => 1,
],
[
'key' => 8,
],
[
'key' => 5,
],
];
$rule = new Sorted(function ($x) {
return $x['key'];
});
$this->assertFalse($rule->validate($arr));
$this->assertFalse($rule->check($arr));
}
}