Added vendor/ directory for Composer's installed files
This commit is contained in:
68
vendor/alexgarrett/violin/tests/MessageBagTest.php
vendored
Executable file
68
vendor/alexgarrett/violin/tests/MessageBagTest.php
vendored
Executable file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
use Violin\Support\MessageBag;
|
||||
|
||||
class MessageBagTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $mb;
|
||||
|
||||
protected $messages = [
|
||||
'name' => [
|
||||
'You must fill in the name field.',
|
||||
'Your name must only be letters.',
|
||||
],
|
||||
'username' => [
|
||||
'Your username is required.',
|
||||
'Your username must be alphanumeric (with dashes and underscores permitted)'
|
||||
],
|
||||
'password' => [
|
||||
'Your password must be greater than 6 characters.'
|
||||
]
|
||||
];
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->mb = new MessageBag($this->messages);
|
||||
}
|
||||
|
||||
public function testMessageBagContainsCorrectKeys()
|
||||
{
|
||||
$keys = $this->mb->keys();
|
||||
|
||||
$this->assertContains('name', $keys);
|
||||
$this->assertContains('username', $keys);
|
||||
$this->assertContains('password', $keys);
|
||||
}
|
||||
|
||||
public function testFirstErrorForFieldExists()
|
||||
{
|
||||
$this->assertEquals(
|
||||
$this->mb->first('name'),
|
||||
$this->messages['name'][0]
|
||||
);
|
||||
}
|
||||
|
||||
public function testAllErrorsForFieldExist()
|
||||
{
|
||||
$this->assertEquals(
|
||||
$this->mb->get('name'),
|
||||
$this->messages['name']
|
||||
);
|
||||
}
|
||||
|
||||
public function testAllErrorsExist()
|
||||
{
|
||||
$flatMessages = iterator_to_array(new RecursiveIteratorIterator(
|
||||
new RecursiveArrayIterator($this->messages)
|
||||
), false);
|
||||
|
||||
$this->assertEquals($flatMessages, $this->mb->all());
|
||||
}
|
||||
|
||||
public function testMessageBagHasError()
|
||||
{
|
||||
$this->assertTrue($this->mb->has('name'));
|
||||
$this->assertTrue($this->mb->has('username'));
|
||||
$this->assertTrue($this->mb->has('password'));
|
||||
}
|
||||
}
|
567
vendor/alexgarrett/violin/tests/RulesTest.php
vendored
Executable file
567
vendor/alexgarrett/violin/tests/RulesTest.php
vendored
Executable file
@@ -0,0 +1,567 @@
|
||||
<?php
|
||||
|
||||
use Violin\Rules\IpRule;
|
||||
use Violin\Rules\IntRule;
|
||||
use Violin\Rules\UrlRule;
|
||||
use Violin\Rules\MaxRule;
|
||||
use Violin\Rules\MinRule;
|
||||
use Violin\Rules\BoolRule;
|
||||
use Violin\Rules\DateRule;
|
||||
use Violin\Rules\RegexRule;
|
||||
use Violin\Rules\AlnumRule;
|
||||
use Violin\Rules\AlphaRule;
|
||||
use Violin\Rules\EmailRule;
|
||||
use Violin\Rules\ArrayRule;
|
||||
use Violin\Rules\NumberRule;
|
||||
use Violin\Rules\CheckedRule;
|
||||
use Violin\Rules\BetweenRule;
|
||||
use Violin\Rules\MatchesRule;
|
||||
use Violin\Rules\RequiredRule;
|
||||
use Violin\Rules\AlnumDashRule;
|
||||
|
||||
use Violin\Support\MessageBag;
|
||||
|
||||
class RulesTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testBetweenRule()
|
||||
{
|
||||
$betweenRule = new BetweenRule;
|
||||
|
||||
$this->assertFalse(
|
||||
$betweenRule->run('5', [], [10, 15])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$betweenRule->run(5, [], [10, 15])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$betweenRule->run('100', [], [100, 500])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$betweenRule->run(499, [], [100, 500])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$betweenRule->run('300', [], [100, 500])
|
||||
);
|
||||
}
|
||||
|
||||
public function testIntRule()
|
||||
{
|
||||
$intRule = new IntRule;
|
||||
|
||||
$this->assertFalse(
|
||||
$intRule->run('two', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$intRule->run('2', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$intRule->run(10, [], [])
|
||||
);
|
||||
}
|
||||
|
||||
public function testNumberRule()
|
||||
{
|
||||
$numberRule = new NumberRule;
|
||||
|
||||
$this->assertFalse(
|
||||
$numberRule->run('dale', [], [])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$numberRule->run('', [], [])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$numberRule->run('three', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$numberRule->run('1', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$numberRule->run('3.14159265359', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$numberRule->run(3.14159265359, [], [])
|
||||
);
|
||||
}
|
||||
|
||||
public function testMatchesRule()
|
||||
{
|
||||
$matchesRule = new MatchesRule;
|
||||
|
||||
$this->assertFalse(
|
||||
$matchesRule->run('cats', [
|
||||
'password' => 'cats',
|
||||
'password_again' => 'catz',
|
||||
], ['password_again'])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$matchesRule->run('cats', [
|
||||
'password' => 'cats',
|
||||
'password_again' => 'cats',
|
||||
], ['password_again'])
|
||||
);
|
||||
}
|
||||
|
||||
public function testRequiredRule()
|
||||
{
|
||||
$requiredRule = new RequiredRule;
|
||||
|
||||
$this->assertFalse(
|
||||
$requiredRule->run('', [], [])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$requiredRule->run(' ', [], [])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$requiredRule->run(' ', [], [])
|
||||
); // Contains whitespace character
|
||||
|
||||
$this->assertTrue(
|
||||
$requiredRule->run('cats', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$requiredRule->run(' cats ', [], [])
|
||||
);
|
||||
}
|
||||
|
||||
public function testAlnumRule()
|
||||
{
|
||||
$alnumRule = new AlnumRule;
|
||||
|
||||
$this->assertFalse(
|
||||
$alnumRule->run('cats_123', [], [])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$alnumRule->run('cats-_123', [], [])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$alnumRule->run('cats123!', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$alnumRule->run('cats123', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$alnumRule->run('cats', [], [])
|
||||
);
|
||||
}
|
||||
|
||||
public function testAlnumDashRule()
|
||||
{
|
||||
$alnumDashRule = new AlnumDashRule;
|
||||
|
||||
$this->assertFalse(
|
||||
$alnumDashRule->run('cats123!', [], [])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$alnumDashRule->run('cats(123)', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$alnumDashRule->run('cats_123', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$alnumDashRule->run('i_love-cats', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$alnumDashRule->run('cat__love', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$alnumDashRule->run('cat--love', [], [])
|
||||
);
|
||||
}
|
||||
|
||||
public function testAlphaRule()
|
||||
{
|
||||
$alphaRule = new AlphaRule;
|
||||
|
||||
$this->assertFalse(
|
||||
$alphaRule->run('cats123', [], [])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$alphaRule->run('cats!', [], [])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$alphaRule->run(' cats ', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$alphaRule->run('cats', [], [])
|
||||
);
|
||||
}
|
||||
|
||||
public function testArrayRule()
|
||||
{
|
||||
$arrayRule = new ArrayRule;
|
||||
|
||||
$this->assertFalse(
|
||||
$arrayRule->run('not an array', [], [])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$arrayRule->run("['not', 'an', 'array']", [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$arrayRule->run(['an', 'array'], [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$arrayRule->run([], [], [])
|
||||
);
|
||||
}
|
||||
|
||||
public function testBoolRule()
|
||||
{
|
||||
$boolRule = new BoolRule;
|
||||
|
||||
$this->assertFalse(
|
||||
$boolRule->run('false', [], [])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$boolRule->run('true', [], [])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$boolRule->run(1, [], [])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$boolRule->run(0, [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$boolRule->run(true, [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$boolRule->run(false, [], [])
|
||||
);
|
||||
}
|
||||
|
||||
public function testEmailRule()
|
||||
{
|
||||
$emailRule = new EmailRule;
|
||||
|
||||
$this->assertFalse(
|
||||
$emailRule->run('ilove@', [], [])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$emailRule->run('ilove@cats', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$emailRule->run('ilove@cats.com', [], [])
|
||||
);
|
||||
}
|
||||
|
||||
public function testIpRule()
|
||||
{
|
||||
$ipRule = new IpRule;
|
||||
|
||||
$this->assertFalse(
|
||||
$ipRule->run('127', [], [])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$ipRule->run('127.0.0', [], [])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$ipRule->run('www.duckduckgo.com', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$ipRule->run('0.0.0.0', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$ipRule->run('127.0.0.1', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$ipRule->run('FE80:0000:0000:0000:0202:B3FF:FE1E:8329', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$ipRule->run('FE80::0202:B3FF:FE1E:8329', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$ipRule->run('::1', [], [])
|
||||
);
|
||||
}
|
||||
|
||||
public function testMaxRuleWithNumbers()
|
||||
{
|
||||
$maxRule = new MaxRule;
|
||||
|
||||
$this->assertFalse(
|
||||
$maxRule->run('100', [], ['10', 'number'])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$maxRule->run(100, [], ['99', 'number'])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$maxRule->run(3.14, [], ['3.10', 'number'])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$maxRule->run('50', [], ['100', 'number'])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$maxRule->run(50, [], ['100', 'number'])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$maxRule->run('5.5', [], ['100', 'number'])
|
||||
);
|
||||
}
|
||||
|
||||
public function testMinRuleWithNumbers()
|
||||
{
|
||||
$minRule = new MinRule;
|
||||
|
||||
$this->assertFalse(
|
||||
$minRule->run('10', [], ['100', 'number'])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$minRule->run(99, [], ['100', 'number'])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$minRule->run(3.10, [], ['3.14', 'number'])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$minRule->run('100', [], ['50', 'number'])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$minRule->run(100, [], ['50', 'number'])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$minRule->run('100', [], ['5.5', 'number'])
|
||||
);
|
||||
}
|
||||
|
||||
public function testMaxRuleWithStrings()
|
||||
{
|
||||
$maxRule = new MaxRule;
|
||||
|
||||
$this->assertFalse(
|
||||
$maxRule->run('william', [], ['5'])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$maxRule->run('100000', [], ['5'])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$maxRule->run('billy', [], ['5'])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$maxRule->run('100', [], ['5'])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$maxRule->run('99999', [], ['5'])
|
||||
);
|
||||
}
|
||||
|
||||
public function testMinRuleWithStrings()
|
||||
{
|
||||
$minRule = new MinRule;
|
||||
|
||||
$this->assertFalse(
|
||||
$minRule->run('billy', [], ['10'])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$minRule->run('5', [], ['10'])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$minRule->run('10', [], ['10'])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$minRule->run('william', [], ['5'])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$minRule->run('99999', [], ['5'])
|
||||
);
|
||||
}
|
||||
|
||||
public function testUrlRule()
|
||||
{
|
||||
$urlRule = new UrlRule;
|
||||
|
||||
$this->assertFalse(
|
||||
$urlRule->run('www.com', [], [])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$urlRule->run('duckduckgo.com', [], [])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$urlRule->run('www.duckduckgo', [], [])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$urlRule->run('127.0.0.1', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$urlRule->run('http://www.duckduckgo.com', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$urlRule->run('http://127.0.0.1', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$urlRule->run('ftp://127.0.0.1', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$urlRule->run('ssl://codecourse.com', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$urlRule->run('ssl://127.0.0.1', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$urlRule->run('http://codecourse.com', [], [])
|
||||
);
|
||||
}
|
||||
|
||||
public function testDateRule()
|
||||
{
|
||||
$dateRule = new DateRule;
|
||||
|
||||
$this->assertFalse(
|
||||
$dateRule->run('', [], [])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$dateRule->run('0000-00-00', [], [])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$dateRule->run('40th November 1989', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$dateRule->run('16th November 1989', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$dateRule->run('1989-11-16', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$dateRule->run('16-11-1989', [], [])
|
||||
);
|
||||
|
||||
$dateTime = new DateTime('2 days ago');
|
||||
|
||||
$this->assertFalse(
|
||||
$dateRule->run($dateTime->format('x y z'), [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$dateRule->run($dateTime->format('d M Y'), [], [])
|
||||
);
|
||||
}
|
||||
|
||||
public function testCheckedRule()
|
||||
{
|
||||
$checkedRule = new CheckedRule;
|
||||
|
||||
$this->assertFalse(
|
||||
$checkedRule->run('', [], [])
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$checkedRule->run(' ', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$checkedRule->run('on', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$checkedRule->run('yes', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$checkedRule->run(1, [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$checkedRule->run('1', [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$checkedRule->run(true, [], [])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$checkedRule->run('true', [], [])
|
||||
);
|
||||
}
|
||||
|
||||
public function testRegexRule()
|
||||
{
|
||||
$regexRule = new RegexRule;
|
||||
|
||||
$exampleRegex = '/b[aeiou]g/';
|
||||
|
||||
$this->assertFalse(
|
||||
$regexRule->run('banter', [], [$exampleRegex])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$regexRule->run('bag', [], [$exampleRegex])
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$regexRule->run('big', [], [$exampleRegex])
|
||||
);
|
||||
}
|
||||
}
|
272
vendor/alexgarrett/violin/tests/ValidatorTest.php
vendored
Executable file
272
vendor/alexgarrett/violin/tests/ValidatorTest.php
vendored
Executable file
@@ -0,0 +1,272 @@
|
||||
<?php
|
||||
|
||||
use Violin\Violin;
|
||||
|
||||
class ValidatorTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $v;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->v = new Violin;
|
||||
}
|
||||
|
||||
public function testBasicValidValidation()
|
||||
{
|
||||
$this->v->validate([
|
||||
'first_name' => ['Billy', 'required|alpha|max(20)'],
|
||||
'last_name' => ['Garrett', 'required|alpha|max(20)'],
|
||||
'email|Email' => ['billy@codecourse.com', 'required|email'],
|
||||
'password' => ['ilovecats', 'required'],
|
||||
'password_again' => ['ilovecats', 'required|matches(password)']
|
||||
]);
|
||||
|
||||
$this->assertTrue($this->v->passes());
|
||||
$this->assertFalse($this->v->fails());
|
||||
|
||||
$input = [
|
||||
'first_name' => 'Billy',
|
||||
'last_name' => 'Garrett',
|
||||
'email|Email' => 'billy@codecourse.com',
|
||||
'password' => 'ilovecats',
|
||||
'password' => 'ilovecats'
|
||||
];
|
||||
|
||||
$rules = [
|
||||
'first_name' => 'required|alpha|max(20)',
|
||||
'last_name' => 'required|alpha|max(20)',
|
||||
'email' => 'required|email',
|
||||
'password' => 'required',
|
||||
'password_again' => 'required|matches(password)'
|
||||
];
|
||||
|
||||
$this->v->validate($input, $rules);
|
||||
|
||||
$this->assertTrue($this->v->passes());
|
||||
$this->assertFalse($this->v->fails());
|
||||
}
|
||||
|
||||
public function testBasicInvalidValidation()
|
||||
{
|
||||
$this->v->validate([
|
||||
'first_name' => ['Billy', 'required|alpha|max(20)'],
|
||||
'last_name' => ['', 'required|alpha|max(20)'],
|
||||
'email' => ['billy@codecourse', 'required|email'],
|
||||
'password' => ['ilovecats', 'required'],
|
||||
'password_again' => ['ilovecatsanddogs' , 'required|matches(password)']
|
||||
]);
|
||||
|
||||
$this->assertTrue($this->v->fails());
|
||||
$this->assertFalse($this->v->passes());
|
||||
}
|
||||
|
||||
public function testRuleMessage()
|
||||
{
|
||||
$this->v->addRuleMessage('required', 'This field is required!');
|
||||
|
||||
$this->v->validate([
|
||||
'username' => ['', 'required']
|
||||
]);
|
||||
|
||||
$this->assertEquals(
|
||||
$this->v->errors()->first('username'),
|
||||
'This field is required!'
|
||||
);
|
||||
}
|
||||
|
||||
public function testReplaceMessageFormatOnError()
|
||||
{
|
||||
$this->v->addRule('testRule', function($value, $input, $args) {
|
||||
return false;
|
||||
});
|
||||
|
||||
$this->v->addRuleMessage('testRule', 'We got {$#} arguments: {$*}.');
|
||||
|
||||
$this->v->validate([
|
||||
'age' => [0, 'testRule(1, 2, 3)']
|
||||
]);
|
||||
|
||||
$this->assertEquals(
|
||||
$this->v->errors()->first(),
|
||||
'We got 3 arguments: 1, 2, 3.'
|
||||
);
|
||||
}
|
||||
|
||||
public function testRuleMessages()
|
||||
{
|
||||
$this->v->addRuleMessages([
|
||||
'required' => 'This field is required!',
|
||||
'alpha' => 'Only alpha characters please!',
|
||||
'email' => 'Enter a valid email!'
|
||||
]);
|
||||
|
||||
$this->v->validate([
|
||||
'username' => ['', 'required|alpha'],
|
||||
'name' => ['123', 'alpha'],
|
||||
'email' => ['notanemail', 'required|email']
|
||||
]);
|
||||
|
||||
$errors = $this->v->errors();
|
||||
|
||||
$this->assertEquals(
|
||||
$errors->get('username'),
|
||||
['This field is required!']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
$errors->first('name'),
|
||||
'Only alpha characters please!'
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
$errors->first('email'),
|
||||
'Enter a valid email!'
|
||||
);
|
||||
}
|
||||
|
||||
public function testFieldMessage()
|
||||
{
|
||||
$this->v->addFieldMessage('username', 'required', 'We need a username, please.');
|
||||
|
||||
$this->v->validate([
|
||||
'username' => ['', 'required']
|
||||
]);
|
||||
|
||||
$this->assertEquals(
|
||||
$this->v->errors()->first('username'),
|
||||
'We need a username, please.'
|
||||
);
|
||||
}
|
||||
|
||||
public function testFieldMessages()
|
||||
{
|
||||
$this->v->addFieldMessages([
|
||||
'username' => [
|
||||
'required' => 'We need a username, please.'
|
||||
],
|
||||
'email' => [
|
||||
'required' => 'How do you expect us to contact you without an email?'
|
||||
]
|
||||
]);
|
||||
|
||||
$this->v->validate([
|
||||
'username' => ['', 'required|alpha'],
|
||||
'email' => ['', 'required|email']
|
||||
]);
|
||||
|
||||
$errors = $this->v->errors();
|
||||
|
||||
$this->assertEquals(
|
||||
$errors->get('username'),
|
||||
['We need a username, please.']
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
$errors->first('email'),
|
||||
'How do you expect us to contact you without an email?'
|
||||
);
|
||||
}
|
||||
|
||||
public function testPassingCustomRule()
|
||||
{
|
||||
$this->v->addRule('isbanana', function($value, $input, $args) {
|
||||
return $value === 'banana';
|
||||
});
|
||||
|
||||
$this->v->validate([
|
||||
'fruit' => ['apple', 'isbanana']
|
||||
]);
|
||||
|
||||
$this->assertFalse($this->v->passes());
|
||||
}
|
||||
|
||||
public function testFailingCustomRule()
|
||||
{
|
||||
$this->v->addRule('isbanana', function($value, $input, $args) {
|
||||
return $value === 'banana';
|
||||
});
|
||||
|
||||
$this->v->validate([
|
||||
'fruit' => ['banana', 'isbanana']
|
||||
]);
|
||||
|
||||
$this->assertTrue($this->v->passes());
|
||||
}
|
||||
|
||||
public function testMultipleCustomRules()
|
||||
{
|
||||
$this->v->addRule('isbanana', function($value, $input, $args) {
|
||||
return $value === 'banana';
|
||||
});
|
||||
|
||||
$this->v->addRule('isapple', function($value, $input, $args) {
|
||||
return $value === 'apple';
|
||||
});
|
||||
|
||||
$this->v->validate([
|
||||
'fruit_one' => ['banana', 'isbanana'],
|
||||
'fruit_two' => ['apple', 'isapple']
|
||||
]);
|
||||
|
||||
$this->assertTrue($this->v->passes());
|
||||
}
|
||||
|
||||
public function testPassingCustomRuleWithArguments()
|
||||
{
|
||||
$this->v->addRule('isoneof', function($value, $input, $args) {
|
||||
return in_array($value, $args);
|
||||
});
|
||||
|
||||
$this->v->validate([
|
||||
'items' => ['seeds', 'isoneof(seeds, nuts, fruit)']
|
||||
]);
|
||||
|
||||
$this->assertTrue($this->v->passes());
|
||||
}
|
||||
|
||||
public function testFailingCustomRuleWithArguments()
|
||||
{
|
||||
$this->v->addRule('isoneof', function($value, $input, $args) {
|
||||
return in_array($value, $args);
|
||||
});
|
||||
|
||||
$this->v->validate([
|
||||
'items' => ['burger', 'isoneof(seeds, nuts, fruit)']
|
||||
]);
|
||||
|
||||
$this->assertFalse($this->v->passes());
|
||||
}
|
||||
|
||||
public function testValidationWithAliases()
|
||||
{
|
||||
$this->v->addFieldMessages([
|
||||
'username_box' => [
|
||||
'required' => 'We need a username in the {field} field, please.'
|
||||
]
|
||||
]);
|
||||
|
||||
$this->v->validate([
|
||||
'username_box|Username' => ['', 'required'],
|
||||
'password' => ['secret', 'required|alpha']
|
||||
]);
|
||||
|
||||
$errors = $this->v->errors();
|
||||
|
||||
$this->assertFalse($this->v->passes());
|
||||
$this->assertTrue($this->v->fails());
|
||||
$this->assertEquals(
|
||||
$errors->first('username_box'),
|
||||
'We need a username in the Username field, please.'
|
||||
);
|
||||
}
|
||||
|
||||
public function testSkippingOtherRulesIfNotRequired()
|
||||
{
|
||||
$this->v->validate([
|
||||
'username' => ['alex', 'required|alpha'],
|
||||
'email' => ['', 'alpha|email']
|
||||
]);
|
||||
|
||||
$this->assertEmpty($this->v->errors()->all());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user