1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
<?php
namespace Tests\Unit\Rules;
use Illuminate\Support\Carbon;
use Illuminate\Translation\ArrayLoader;
use Illuminate\Translation\Translator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Date;
use Illuminate\Validation\Validator;
use PHPUnit\Framework\TestCase;
class ValidationDateRuleTest extends TestCase
{
public function testDefaultDateRule()
{
$rule = Rule::date();
$this->assertEquals('date', (string) $rule);
$rule = new Date;
$this->assertSame('date', (string) $rule);
}
public function testDateFormatRule()
{
$rule = Rule::date()->format('d/m/Y');
$this->assertEquals('date|date_format:d/m/Y', (string) $rule);
}
public function testAfterTodayRule()
{
$rule = Rule::date()->afterToday();
$this->assertEquals('date|after:today', (string) $rule);
$rule = Rule::date()->todayOrAfter();
$this->assertEquals('date|after_or_equal:today', (string) $rule);
}
public function testBeforeTodayRule()
{
$rule = Rule::date()->beforeToday();
$this->assertEquals('date|before:today', (string) $rule);
$rule = Rule::date()->todayOrBefore();
$this->assertEquals('date|before_or_equal:today', (string) $rule);
}
public function testAfterSpecificDateRule()
{
$rule = Rule::date()->after(Carbon::parse('2024-01-01'));
$this->assertEquals('date|after:2024-01-01', (string) $rule);
}
public function testBeforeSpecificDateRule()
{
$rule = Rule::date()->before(Carbon::parse('2024-01-01'));
$this->assertEquals('date|before:2024-01-01', (string) $rule);
}
public function testAfterOrEqualSpecificDateRule()
{
$rule = Rule::date()->afterOrEqual(Carbon::parse('2024-01-01'));
$this->assertEquals('date|after_or_equal:2024-01-01', (string) $rule);
}
public function testBeforeOrEqualSpecificDateRule()
{
$rule = Rule::date()->beforeOrEqual(Carbon::parse('2024-01-01'));
$this->assertEquals('date|before_or_equal:2024-01-01', (string) $rule);
}
public function testBetweenDatesRule()
{
$rule = Rule::date()->between(Carbon::parse('2024-01-01'), Carbon::parse('2024-02-01'));
$this->assertEquals('date|after:2024-01-01|before:2024-02-01', (string) $rule);
}
public function testBetweenOrEqualDatesRule()
{
$rule = Rule::date()->betweenOrEqual('2024-01-01', '2024-02-01');
$this->assertEquals('date|after_or_equal:2024-01-01|before_or_equal:2024-02-01', (string) $rule);
}
public function testChainedRules()
{
$rule = Rule::date('Y-m-d H:i:s')
->format('Y-m-d')
->after('2024-01-01 00:00:00')
->before('2025-01-01 00:00:00');
$this->assertEquals('date|date_format:Y-m-d|after:2024-01-01 00:00:00|before:2025-01-01 00:00:00', (string) $rule);
$rule = Rule::date()
->format('Y-m-d')
->when(true, function ($rule) {
$rule->after('2024-01-01');
})
->unless(true, function ($rule) {
$rule->before('2025-01-01');
});
$this->assertSame('date|date_format:Y-m-d|after:2024-01-01', (string) $rule);
}
public function testDateValidation()
{
$trans = new Translator(new ArrayLoader, 'en');
$rule = Rule::date();
$validator = new Validator(
$trans,
['date' => 'not a date'],
['date' => $rule]
);
$this->assertSame(
$trans->get('validation.date'),
$validator->errors()->first('date')
);
$validator = new Validator(
$trans,
['date' => '2024-01-01'],
['date' => $rule]
);
$this->assertEmpty($validator->errors()->first('date'));
$rule = Rule::date()->between('2024-01-01', '2025-01-01');
$validator = new Validator(
$trans,
['date' => '2024-02-01'],
['date' => (string) $rule]
);
$this->assertEmpty($validator->errors()->first('date'));
$rule = Rule::date()->between('2024/01/01', '2024/02/01')->format('Y/m/d');
$validator = new Validator(
$trans,
['date' => '2024/01/15'],
['date' => [$rule]]
);
$this->assertEmpty($validator->errors()->first('date'));
}
}
|