respect-validation/tests/unit/Rules/SortedTest.php
Henrique Moody 688fbde552
Make PHPUnit tests final
Whenever is possible it is better to declare our classes as final. The
PHPUnit tests should not be extended, therefore there is no reason for
them to not be final.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2019-02-09 14:32:12 +01:00

145 lines
3 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.
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Test\TestCase;
/**
* @group rule
* @covers \Respect\Validation\Exceptions\SortedException
* @covers \Respect\Validation\Rules\Sorted
*
* @author Gabriel Caruso <carusogabriel34@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Mikhail Vyrtsev <reeywhaar@gmail.com>
*/
final class SortedTest extends TestCase
{
/**
* @test
*/
public function passes(): void
{
$arr = [1, 2, 3];
$rule = new Sorted();
self::assertTrue($rule->validate($arr));
$rule->assert($arr);
$rule->check($arr);
}
/**
* @test
*/
public function passesWithEqualValues(): void
{
$arr = [1, 2, 2, 3];
$rule = new Sorted();
self::assertTrue($rule->validate($arr));
$rule->assert($arr);
$rule->check($arr);
}
/**
* @expectedException \Respect\Validation\Exceptions\SortedException
*
* @test
*/
public function notPasses(): void
{
$arr = [1, 2, 4, 3];
$rule = new Sorted();
self::assertFalse($rule->validate($arr));
$rule->check($arr);
}
/**
* @test
*/
public function passesDescending(): void
{
$arr = [10, 9, 8];
$rule = new Sorted(null, false);
self::assertTrue($rule->validate($arr));
$rule->assert($arr);
$rule->check($arr);
}
/**
* @test
*/
public function passesDescendingWithEqualValues(): void
{
$arr = [10, 9, 9, 8];
$rule = new Sorted(null, false);
self::assertTrue($rule->validate($arr));
$rule->assert($arr);
$rule->check($arr);
}
/**
* @test
*/
public function passesByFunction(): void
{
$arr = [
[
'key' => 1,
],
[
'key' => 2,
],
[
'key' => 5,
],
];
$rule = new Sorted(static function ($x) {
return $x['key'];
});
self::assertTrue($rule->validate($arr));
$rule->assert($arr);
$rule->check($arr);
}
/**
* @expectedException \Respect\Validation\Exceptions\SortedException
*
* @test
*/
public function notPassesByFunction(): void
{
$arr = [
[
'key' => 1,
],
[
'key' => 8,
],
[
'key' => 5,
],
];
$rule = new Sorted(static function ($x) {
return $x['key'];
});
self::assertFalse($rule->validate($arr));
$rule->check($arr);
}
}