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
|
<?php
namespace Illuminate\Tests\Validation;
use Illuminate\Tests\Validation\fixtures\Values;
use Illuminate\Translation\ArrayLoader;
use Illuminate\Translation\Translator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\In;
use Illuminate\Validation\Validator;
use PHPUnit\Framework\TestCase;
include_once 'Enums.php';
class ValidationInRuleTest extends TestCase
{
public function testItCorrectlyFormatsAStringVersionOfTheRule()
{
$rule = new In(['Laravel', 'Framework', 'PHP']);
$this->assertSame('in:"Laravel","Framework","PHP"', (string) $rule);
$rule = new In(collect(['Taylor', 'Michael', 'Tim']));
$this->assertSame('in:"Taylor","Michael","Tim"', (string) $rule);
$rule = new In(['Life, the Universe and Everything', 'this is a "quote"']);
$this->assertSame('in:"Life, the Universe and Everything","this is a ""quote"""', (string) $rule);
$rule = Rule::in(collect([1, 2, 3, 4]));
$this->assertSame('in:"1","2","3","4"', (string) $rule);
$rule = Rule::in(collect([1, 2, 3, 4]));
$this->assertSame('in:"1","2","3","4"', (string) $rule);
$rule = new In(["a,b\nc,d"]);
$this->assertSame("in:\"a,b\nc,d\"", (string) $rule);
$rule = Rule::in([1, 2, 3, 4]);
$this->assertSame('in:"1","2","3","4"', (string) $rule);
$rule = Rule::in(collect([1, 2, 3, 4]));
$this->assertSame('in:"1","2","3","4"', (string) $rule);
$rule = Rule::in(new Values);
$this->assertSame('in:"1","2","3","4"', (string) $rule);
$rule = Rule::in('1', '2', '3', '4');
$this->assertSame('in:"1","2","3","4"', (string) $rule);
$rule = new In('1', '2', '3', '4');
$this->assertSame('in:"1","2","3","4"', (string) $rule);
$rule = Rule::in([StringStatus::done]);
$this->assertSame('in:"done"', (string) $rule);
$rule = Rule::in([IntegerStatus::done]);
$this->assertSame('in:"2"', (string) $rule);
$rule = Rule::in([PureEnum::one]);
$this->assertSame('in:"one"', (string) $rule);
}
public function testInRuleValidation()
{
$trans = new Translator(new ArrayLoader, 'en');
$v = new Validator($trans, ['x' => 'foo'], ['x' => Rule::in('foo', 'bar')]);
$this->assertTrue($v->passes());
$v = new Validator($trans, ['x' => 'foo'], ['x' => (string) Rule::in('foo', 'bar')]);
$this->assertTrue($v->passes());
$v = new Validator($trans, ['x' => 'foo'], ['x' => [Rule::in('bar', 'baz')]]);
$this->assertFalse($v->passes());
$v = new Validator($trans, ['x' => 'foo'], ['x' => ['required', Rule::in('foo', 'bar')]]);
$this->assertTrue($v->passes());
}
}
|