/
githubmirror
/
framework
Обзор
Документация
Войти
/
githubmirror
/
framework
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
13.x
tests/Validation/ValidationExcludeIfTest.php
90 строк
3 KB
Lucas Michot
[13.x] Enforce stricter assertions (#59749)
17 апр 2026, 16:15
Не верифицирован
17 апр 2026, 16:15
92fecdd
Код
Авторство
О чём код?
<?php namespace Illuminate\Tests\Validation; use Exception; use Illuminate\Translation\ArrayLoader; use Illuminate\Translation\Translator; use Illuminate\Validation\Rules\ExcludeIf; use Illuminate\Validation\Validator; use InvalidArgumentException; use PHPUnit\Framework\TestCase; use stdClass; class ValidationExcludeIfTest extends TestCase { public function testItReturnsStringVersionOfRuleWhenCast() { $rule = new ExcludeIf(function () { return true; }); $this->assertSame('exclude', (string) $rule); $rule = new ExcludeIf(function () { return false; }); $this->assertSame('', (string) $rule); $rule = new ExcludeIf(true); $this->assertSame('exclude', (string) $rule); $rule = new ExcludeIf(false); $this->assertSame('', (string) $rule); } public function testItValidatesCallableAndBooleanAreAcceptableArguments() { new ExcludeIf(false); new ExcludeIf(true); new ExcludeIf(fn () => true); foreach ([1, 1.1, 'phpinfo', new stdClass, null] as $condition) { try { new ExcludeIf($condition); $this->fail('The ExcludeIf constructor must not accept '.gettype($condition)); } catch (InvalidArgumentException $exception) { $this->assertSame('The provided condition must be a callable or boolean.', $exception->getMessage()); } } } public function testItThrowsExceptionIfRuleIsNotSerializable() { $this->expectException(Exception::class); serialize(new ExcludeIf(function () { return true; })); } public function testExcludeIfRuleValidation() { $ruleTrue = new ExcludeIf(true); $ruleFalse = new ExcludeIf(false); $trans = new Translator(new ArrayLoader, 'en'); $data = ['foo' => 'FOO', 'bar' => 'BAR']; $v = new Validator($trans, $data, ['foo' => $ruleTrue, 'bar' => 'nullable']); $this->assertTrue($v->passes()); $this->assertSame(['bar' => 'BAR'], $v->validated()); $v = new Validator($trans, $data, ['foo' => (string) $ruleTrue, 'bar' => 'nullable']); $this->assertTrue($v->passes()); $this->assertSame(['bar' => 'BAR'], $v->validated()); $v = new Validator($trans, $data, ['foo' => [$ruleTrue], 'bar' => 'nullable']); $this->assertTrue($v->passes()); $this->assertSame(['bar' => 'BAR'], $v->validated()); $v = new Validator($trans, $data, ['foo' => $ruleFalse, 'bar' => 'nullable']); $this->assertTrue($v->passes()); $this->assertSame($data, $v->validated()); } }