From b7b049a93a4465b73dda4d6f86015c281a2ddd74 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Wed, 21 Sep 2016 15:48:32 +0200 Subject: [PATCH] Fix validator messages for implicit attributes with errors --- src/Illuminate/Translation/Translator.php | 17 + src/Illuminate/Validation/Validator.php | 4 +- tests/Validation/ValidationValidatorTest.php | 384 ++++++++++--------- 3 files changed, 214 insertions(+), 191 deletions(-) diff --git a/src/Illuminate/Translation/Translator.php b/src/Illuminate/Translation/Translator.php index d68d63c90556..6793e15bed94 100755 --- a/src/Illuminate/Translation/Translator.php +++ b/src/Illuminate/Translation/Translator.php @@ -127,6 +127,23 @@ public function get($key, array $replace = [], $locale = null, $fallback = true) return $line; } + /** + * Add translation lines to the given locale. + * + * @param array $lines + * @param string $locale + * @param string $namespace + * @return void + */ + public function addLines(array $lines, $locale, $namespace = '*') + { + foreach ($lines as $key => $value) { + list($group, $item) = explode('.', $key, 2); + + Arr::set($this->loaded, "$namespace.$group.$locale.$item", $value); + } + } + /** * Retrieve a language line out the loaded array. * diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index 05b2d3ca43e7..413b65941421 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -2242,12 +2242,10 @@ protected function getAttribute($attribute) return $this->customAttributes[$expectedAttributeName]; } - $key = "validation.attributes.{$expectedAttributeName}"; - // We allow for the developer to specify language lines for each of the // attributes allowing for more displayable counterparts of each of // the attributes. This provides the ability for simple formats. - if (($line = $this->translator->trans($key)) !== $key) { + if ($line = Arr::get($this->translator->get('validation.attributes'), $expectedAttributeName)) { return $line; } } diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index ba6e962bf4d2..a8c6a540ab14 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -14,19 +14,19 @@ public function tearDown() public function testSometimesWorksOnNestedArrays() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => ['bar' => ['baz' => '']]], ['foo.bar.baz' => 'sometimes|required']); $this->assertFalse($v->passes()); $this->assertEquals(['foo.bar.baz' => ['Required' => []]], $v->failed()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => ['bar' => ['baz' => 'nonEmpty']]], ['foo.bar.baz' => 'sometimes|required']); $this->assertTrue($v->passes()); } public function testAfterCallbacksAreCalledWithValidatorInstance() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => 'bar', 'baz' => 'boom'], ['foo' => 'Same:baz']); $v->setContainer(new Illuminate\Container\Container); $v->after(function ($validator) { @@ -45,12 +45,12 @@ public function testAfterCallbacksAreCalledWithValidatorInstance() public function testSometimesWorksOnArrays() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => ['bar', 'baz', 'moo']], ['foo' => 'sometimes|required|between:5,10']); $this->assertFalse($v->passes()); $this->assertNotEmpty($v->failed()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => ['bar', 'baz', 'moo', 'pew', 'boom']], ['foo' => 'sometimes|required|between:5,10']); $this->assertTrue($v->passes()); } @@ -60,7 +60,7 @@ public function testSometimesWorksOnArrays() */ public function testValidateThrowsOnFail() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => 'bar'], ['baz' => 'required']); $v->validate(); @@ -68,7 +68,7 @@ public function testValidateThrowsOnFail() public function testValidateDoesntThrowOnPass() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => 'bar'], ['foo' => 'required']); $v->validate(); @@ -76,7 +76,7 @@ public function testValidateDoesntThrowOnPass() public function testHasFailedValidationRules() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => 'bar', 'baz' => 'boom'], ['foo' => 'Same:baz']); $this->assertFalse($v->passes()); $this->assertEquals(['foo' => ['Same' => ['baz']]], $v->failed()); @@ -84,7 +84,7 @@ public function testHasFailedValidationRules() public function testFailingOnce() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => 'bar', 'baz' => 'boom'], ['foo' => 'Bail|Same:baz|In:qux']); $this->assertFalse($v->passes()); $this->assertEquals(['foo' => ['Same' => ['baz']]], $v->failed()); @@ -118,7 +118,7 @@ public function testInValidatableRulesReturnsValid() public function testValidateEmptyStringsAlwaysPasses() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => ''], ['x' => 'size:10|array|integer|min:5']); $this->assertTrue($v->passes()); @@ -126,7 +126,7 @@ public function testValidateEmptyStringsAlwaysPasses() public function testEmptyExistingAttributesAreValidated() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => ''], ['x' => 'array']); $this->assertTrue($v->passes()); @@ -149,7 +149,7 @@ public function testEmptyExistingAttributesAreValidated() public function testNullable() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, [ 'x' => null, 'y' => null, 'z' => null, 'a' => null, 'b' => null, @@ -173,18 +173,19 @@ public function testNullable() public function testProperLanguageLineIsSet() { - $trans = $this->getRealTranslator(); - $trans->addResource('array', ['validation.required' => 'required!'], 'en', 'messages'); + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.required' => 'required!'], 'en'); $v = new Validator($trans, ['name' => ''], ['name' => 'Required']); $this->assertFalse($v->passes()); $v->messages()->setFormat(':message'); + $this->assertEquals('required!', $v->messages()->first('name')); } public function testCustomReplacersAreCalled() { - $trans = $this->getRealTranslator(); - $trans->addResource('array', ['validation.required' => 'foo bar'], 'en', 'messages'); + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.required' => 'foo bar'], 'en'); $v = new Validator($trans, ['name' => ''], ['name' => 'Required']); $v->addReplacer('required', function ($message, $attribute, $rule, $parameters) { return str_replace('bar', 'taylor', $message); @@ -196,8 +197,8 @@ public function testCustomReplacersAreCalled() public function testClassBasedCustomReplacers() { - $trans = $this->getRealTranslator(); - $trans->addResource('array', ['validation.foo' => 'foo!'], 'en', 'messages'); + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.foo' => 'foo!'], 'en'); $v = new Validator($trans, [], ['name' => 'required']); $v->setContainer($container = m::mock('Illuminate\Container\Container')); $v->addReplacer('required', 'Foo@bar'); @@ -210,23 +211,23 @@ public function testClassBasedCustomReplacers() public function testAttributeNamesAreReplaced() { - $trans = $this->getRealTranslator(); - $trans->addResource('array', ['validation.required' => ':attribute is required!'], 'en', 'messages'); + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.required' => ':attribute is required!'], 'en'); $v = new Validator($trans, ['name' => ''], ['name' => 'Required']); $this->assertFalse($v->passes()); $v->messages()->setFormat(':message'); $this->assertEquals('name is required!', $v->messages()->first('name')); - $trans = $this->getRealTranslator(); - $trans->addResource('array', ['validation.required' => ':attribute is required!', 'validation.attributes.name' => 'Name'], 'en', 'messages'); + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.required' => ':attribute is required!', 'validation.attributes.name' => 'Name'], 'en'); $v = new Validator($trans, ['name' => ''], ['name' => 'Required']); $this->assertFalse($v->passes()); $v->messages()->setFormat(':message'); $this->assertEquals('Name is required!', $v->messages()->first('name')); //set customAttributes by setter - $trans = $this->getRealTranslator(); - $trans->addResource('array', ['validation.required' => ':attribute is required!'], 'en', 'messages'); + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.required' => ':attribute is required!'], 'en'); $customAttributes = ['name' => 'Name']; $v = new Validator($trans, ['name' => ''], ['name' => 'Required']); $v->addCustomAttributes($customAttributes); @@ -234,23 +235,23 @@ public function testAttributeNamesAreReplaced() $v->messages()->setFormat(':message'); $this->assertEquals('Name is required!', $v->messages()->first('name')); - $trans = $this->getRealTranslator(); - $trans->addResource('array', ['validation.required' => ':attribute is required!'], 'en', 'messages'); + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.required' => ':attribute is required!'], 'en'); $v = new Validator($trans, ['name' => ''], ['name' => 'Required']); $v->setAttributeNames(['name' => 'Name']); $this->assertFalse($v->passes()); $v->messages()->setFormat(':message'); $this->assertEquals('Name is required!', $v->messages()->first('name')); - $trans = $this->getRealTranslator(); - $trans->addResource('array', ['validation.required' => ':Attribute is required!'], 'en', 'messages'); + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.required' => ':Attribute is required!'], 'en'); $v = new Validator($trans, ['name' => ''], ['name' => 'Required']); $this->assertFalse($v->passes()); $v->messages()->setFormat(':message'); $this->assertEquals('Name is required!', $v->messages()->first('name')); - $trans = $this->getRealTranslator(); - $trans->addResource('array', ['validation.required' => ':ATTRIBUTE is required!'], 'en', 'messages'); + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.required' => ':ATTRIBUTE is required!'], 'en'); $v = new Validator($trans, ['name' => ''], ['name' => 'Required']); $this->assertFalse($v->passes()); $v->messages()->setFormat(':message'); @@ -259,25 +260,25 @@ public function testAttributeNamesAreReplaced() public function testAttributeNamesAreReplacedInArrays() { - $trans = $this->getRealTranslator(); - $trans->addResource('array', ['validation.required' => ':attribute is required!'], 'en', 'messages'); + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.required' => ':attribute is required!'], 'en'); $v = new Validator($trans, ['users' => [['country_code' => 'US'], ['country_code' => null]]], ['users.*.country_code' => 'Required']); $this->assertFalse($v->passes()); $v->messages()->setFormat(':message'); $this->assertEquals('users.1.country_code is required!', $v->messages()->first('users.1.country_code')); - $trans = $this->getRealTranslator(); - $trans->addResource('array', [ + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines([ 'validation.string' => ':attribute must be a string!', 'validation.attributes.name.*' => 'Any name', - ], 'en', 'messages'); + ], 'en'); $v = new Validator($trans, ['name' => ['Jon', 2]], ['name.*' => 'string']); $this->assertFalse($v->passes()); $v->messages()->setFormat(':message'); $this->assertEquals('Any name must be a string!', $v->messages()->first('name.1')); - $trans = $this->getRealTranslator(); - $trans->addResource('array', ['validation.string' => ':attribute must be a string!'], 'en', 'messages'); + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.string' => ':attribute must be a string!'], 'en'); $v = new Validator($trans, ['name' => ['Jon', 2]], ['name.*' => 'string']); $v->setAttributeNames(['name.*' => 'Any name']); $this->assertFalse($v->passes()); @@ -290,37 +291,52 @@ public function testAttributeNamesAreReplacedInArrays() $v->messages()->setFormat(':message'); $this->assertEquals('Any name must be a string!', $v->messages()->first('users.1.name')); - $trans->addResource('array', ['validation.required' => ':attribute is required!'], 'en', 'messages'); + $trans->addLines(['validation.required' => ':attribute is required!'], 'en'); $v = new Validator($trans, ['title' => ['nl' => '', 'en' => 'Hello']], ['title.*' => 'required'], [], ['title.nl' => 'Titel', 'title.en' => 'Title']); $this->assertFalse($v->passes()); $v->messages()->setFormat(':message'); $this->assertEquals('Titel is required!', $v->messages()->first('title.nl')); + + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.required' => ':attribute is required!'], 'en'); + $trans->addLines(['validation.attributes' => ['names.*' => 'names']], 'en'); + $v = new Validator($trans, ['names' => [null, 'name']], ['names.*' => 'Required']); + $v->messages()->setFormat(':message'); + $this->assertEquals('names is required!', $v->messages()->first('names.0')); + + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.required' => ':attribute is required!'], 'en'); + $trans->addLines(['validation.attributes' => ['names.*' => 'names']], 'en'); + $trans->addLines(['validation.attributes' => ['names.0' => 'First name']], 'en'); + $v = new Validator($trans, ['names' => [null, 'name']], ['names.*' => 'Required']); + $v->messages()->setFormat(':message'); + $this->assertEquals('First name is required!', $v->messages()->first('names.0')); } public function testDisplayableValuesAreReplaced() { //required_if:foo,bar - $trans = $this->getRealTranslator(); - $trans->addResource('array', ['validation.required_if' => 'The :attribute field is required when :other is :value.'], 'en', 'messages'); - $trans->addResource('array', ['validation.values.color.1' => 'red'], 'en', 'messages'); + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.required_if' => 'The :attribute field is required when :other is :value.'], 'en'); + $trans->addLines(['validation.values.color.1' => 'red'], 'en'); $v = new Validator($trans, ['color' => '1', 'bar' => ''], ['bar' => 'RequiredIf:color,1']); $this->assertFalse($v->passes()); $v->messages()->setFormat(':message'); $this->assertEquals('The bar field is required when color is red.', $v->messages()->first('bar')); //in:foo,bar,... - $trans = $this->getRealTranslator(); - $trans->addResource('array', ['validation.in' => ':attribute must be included in :values.'], 'en', 'messages'); - $trans->addResource('array', ['validation.values.type.5' => 'Short'], 'en', 'messages'); - $trans->addResource('array', ['validation.values.type.300' => 'Long'], 'en', 'messages'); + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.in' => ':attribute must be included in :values.'], 'en'); + $trans->addLines(['validation.values.type.5' => 'Short'], 'en'); + $trans->addLines(['validation.values.type.300' => 'Long'], 'en'); $v = new Validator($trans, ['type' => '4'], ['type' => 'in:5,300']); $this->assertFalse($v->passes()); $v->messages()->setFormat(':message'); $this->assertEquals('type must be included in Short, Long.', $v->messages()->first('type')); // test addCustomValues - $trans = $this->getRealTranslator(); - $trans->addResource('array', ['validation.in' => ':attribute must be included in :values.'], 'en', 'messages'); + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.in' => ':attribute must be included in :values.'], 'en'); $customValues = [ 'type' => [ '5' => 'Short', @@ -334,8 +350,8 @@ public function testDisplayableValuesAreReplaced() $this->assertEquals('type must be included in Short, Long.', $v->messages()->first('type')); // set custom values by setter - $trans = $this->getRealTranslator(); - $trans->addResource('array', ['validation.in' => ':attribute must be included in :values.'], 'en', 'messages'); + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.in' => ':attribute must be included in :values.'], 'en'); $customValues = [ 'type' => [ '5' => 'Short', @@ -408,13 +424,13 @@ public function testValidationDotCustomDotAnythingCanBeTranslated() public function testInlineValidationMessagesAreRespected() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['name' => ''], ['name' => 'Required'], ['name.required' => 'require it please!']); $this->assertFalse($v->passes()); $v->messages()->setFormat(':message'); $this->assertEquals('require it please!', $v->messages()->first('name')); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['name' => ''], ['name' => 'Required'], ['required' => 'require it please!']); $this->assertFalse($v->passes()); $v->messages()->setFormat(':message'); @@ -423,7 +439,7 @@ public function testInlineValidationMessagesAreRespected() public function testInlineValidationMessagesAreRespectedWithAsterisks() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['name' => ['', '']], [], ['name.*.required' => 'all must be required!']); $v->each('name', 'required|max:255'); $this->assertFalse($v->passes()); @@ -434,7 +450,7 @@ public function testInlineValidationMessagesAreRespectedWithAsterisks() public function testIfRulesAreSuccessfullyAdded() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, [], ['foo' => 'Required']); // foo has required rule $this->assertTrue($v->hasRule('foo', 'Required')); @@ -446,7 +462,7 @@ public function testIfRulesAreSuccessfullyAdded() public function testValidateArray() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => [1, 2, 3]], ['foo' => 'Array']); $this->assertTrue($v->passes()); @@ -457,7 +473,7 @@ public function testValidateArray() public function testValidateFilled() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, [], ['name' => 'filled']); $this->assertTrue($v->passes()); @@ -476,7 +492,7 @@ public function testValidateFilled() public function testValidationStopsAtFailedPresenceCheck() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['name' => null], ['name' => 'Required|string']); $v->passes(); @@ -497,7 +513,7 @@ public function testValidationStopsAtFailedPresenceCheck() public function testValidatePresent() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, [], ['name' => 'present']); $this->assertFalse($v->passes()); @@ -519,7 +535,7 @@ public function testValidatePresent() public function testValidateRequired() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, [], ['name' => 'Required']); $this->assertFalse($v->passes()); @@ -540,7 +556,7 @@ public function testValidateRequired() public function testValidateRequiredWith() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['first' => 'Taylor'], ['last' => 'required_with:first']); $this->assertFalse($v->passes()); @@ -573,7 +589,7 @@ public function testValidateRequiredWith() public function testRequiredWithAll() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['first' => 'foo'], ['last' => 'required_with_all:first,foo']); $this->assertTrue($v->passes()); @@ -583,7 +599,7 @@ public function testRequiredWithAll() public function testValidateRequiredWithout() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['first' => 'Taylor'], ['last' => 'required_without:first']); $this->assertTrue($v->passes()); @@ -637,7 +653,7 @@ public function testValidateRequiredWithout() public function testRequiredWithoutMultiple() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $rules = [ 'f1' => 'required_without:f2,f3', @@ -672,7 +688,7 @@ public function testRequiredWithoutMultiple() public function testRequiredWithoutAll() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $rules = [ 'f1' => 'required_without_all:f2,f3', @@ -707,33 +723,33 @@ public function testRequiredWithoutAll() public function testRequiredIf() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['first' => 'taylor'], ['last' => 'required_if:first,taylor']); $this->assertTrue($v->fails()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['first' => 'taylor', 'last' => 'otwell'], ['last' => 'required_if:first,taylor']); $this->assertTrue($v->passes()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['first' => 'taylor', 'last' => 'otwell'], ['last' => 'required_if:first,taylor,dayle']); $this->assertTrue($v->passes()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['first' => 'dayle', 'last' => 'rees'], ['last' => 'required_if:first,taylor,dayle']); $this->assertTrue($v->passes()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => true], ['bar' => 'required_if:foo,false']); $this->assertTrue($v->passes()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => true], ['bar' => 'required_if:foo,true']); $this->assertTrue($v->fails()); // error message when passed multiple values (required_if:foo,bar,baz) - $trans = $this->getRealTranslator(); - $trans->addResource('array', ['validation.required_if' => 'The :attribute field is required when :other is :value.'], 'en', 'messages'); + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.required_if' => 'The :attribute field is required when :other is :value.'], 'en'); $v = new Validator($trans, ['first' => 'dayle', 'last' => ''], ['last' => 'RequiredIf:first,taylor,dayle']); $this->assertFalse($v->passes()); $this->assertEquals('The last field is required when first is dayle.', $v->messages()->first('last')); @@ -741,29 +757,29 @@ public function testRequiredIf() public function testRequiredUnless() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['first' => 'sven'], ['last' => 'required_unless:first,taylor']); $this->assertTrue($v->fails()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['first' => 'taylor'], ['last' => 'required_unless:first,taylor']); $this->assertTrue($v->passes()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['first' => 'sven', 'last' => 'wittevrongel'], ['last' => 'required_unless:first,taylor']); $this->assertTrue($v->passes()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['first' => 'taylor'], ['last' => 'required_unless:first,taylor,sven']); $this->assertTrue($v->passes()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['first' => 'sven'], ['last' => 'required_unless:first,taylor,sven']); $this->assertTrue($v->passes()); // error message when passed multiple values (required_unless:foo,bar,baz) - $trans = $this->getRealTranslator(); - $trans->addResource('array', ['validation.required_unless' => 'The :attribute field is required unless :other is in :values.'], 'en', 'messages'); + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.required_unless' => 'The :attribute field is required unless :other is in :values.'], 'en'); $v = new Validator($trans, ['first' => 'dayle', 'last' => ''], ['last' => 'RequiredUnless:first,taylor,sven']); $this->assertFalse($v->passes()); $this->assertEquals('The last field is required unless first is in taylor, sven.', $v->messages()->first('last')); @@ -771,7 +787,7 @@ public function testRequiredUnless() public function testFailedFileUploads() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); // If file is not successfully uploaded validation should fail with a // 'uploaded' error message instead of the original rule. @@ -811,30 +827,30 @@ public function testFailedFileUploads() public function testValidateInArray() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => [1, 2, 3], 'bar' => [1, 2]], ['foo.*' => 'in_array:bar.*']); $this->assertFalse($v->passes()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => [1, 2], 'bar' => [1, 2, 3]], ['foo.*' => 'in_array:bar.*']); $this->assertTrue($v->passes()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => [['bar_id' => 5], ['bar_id' => 2]], 'bar' => [['id' => 1, ['id' => 2]]]], ['foo.*.bar_id' => 'in_array:bar.*.id']); $this->assertFalse($v->passes()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => [['bar_id' => 1], ['bar_id' => 2]], 'bar' => [['id' => 1, ['id' => 2]]]], ['foo.*.bar_id' => 'in_array:bar.*.id']); $this->assertTrue($v->passes()); - $trans->addResource('array', ['validation.in_array' => 'The value of :attribute does not exist in :other.'], 'en', 'messages'); + $trans->addLines(['validation.in_array' => 'The value of :attribute does not exist in :other.'], 'en'); $v = new Validator($trans, ['foo' => [1, 2, 3], 'bar' => [1, 2]], ['foo.*' => 'in_array:bar.*']); $this->assertEquals('The value of foo.2 does not exist in bar.*.', $v->messages()->first('foo.2')); } public function testValidateConfirmed() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['password' => 'foo'], ['password' => 'Confirmed']); $this->assertFalse($v->passes()); @@ -850,7 +866,7 @@ public function testValidateConfirmed() public function testValidateSame() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => 'bar', 'baz' => 'boom'], ['foo' => 'Same:baz']); $this->assertFalse($v->passes()); @@ -866,7 +882,7 @@ public function testValidateSame() public function testValidateDifferent() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => 'bar', 'baz' => 'boom'], ['foo' => 'Different:baz']); $this->assertTrue($v->passes()); @@ -882,7 +898,7 @@ public function testValidateDifferent() public function testValidateAccepted() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => 'no'], ['foo' => 'Accepted']); $this->assertFalse($v->passes()); @@ -922,33 +938,33 @@ public function testValidateAccepted() public function testValidateString() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => 'string']); $this->assertTrue($v->passes()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => ['blah' => 'test']], ['x' => 'string']); $this->assertFalse($v->passes()); } public function testValidateJson() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => 'aslksd'], ['foo' => 'json']); $this->assertFalse($v->passes()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => '[]'], ['foo' => 'json']); $this->assertTrue($v->passes()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => '{"name":"John","age":"34"}'], ['foo' => 'json']); $this->assertTrue($v->passes()); } public function testValidateBoolean() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => 'no'], ['foo' => 'Boolean']); $this->assertFalse($v->passes()); @@ -985,7 +1001,7 @@ public function testValidateBoolean() public function testValidateBool() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => 'no'], ['foo' => 'Bool']); $this->assertFalse($v->passes()); @@ -1022,7 +1038,7 @@ public function testValidateBool() public function testValidateNumeric() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => 'asdad'], ['foo' => 'Numeric']); $this->assertFalse($v->passes()); @@ -1038,7 +1054,7 @@ public function testValidateNumeric() public function testValidateInteger() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => 'asdad'], ['foo' => 'Integer']); $this->assertFalse($v->passes()); @@ -1054,7 +1070,7 @@ public function testValidateInteger() public function testValidateInt() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => 'asdad'], ['foo' => 'Int']); $this->assertFalse($v->passes()); @@ -1070,7 +1086,7 @@ public function testValidateInt() public function testValidateDigits() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => '12345'], ['foo' => 'Digits:5']); $this->assertTrue($v->passes()); @@ -1083,7 +1099,7 @@ public function testValidateDigits() $v = new Validator($trans, ['foo' => '2e7'], ['foo' => 'Digits:3']); $this->assertTrue($v->fails()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => '12345'], ['foo' => 'digits_between:1,6']); $this->assertTrue($v->passes()); @@ -1099,7 +1115,7 @@ public function testValidateDigits() public function testValidateSize() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => 'asdad'], ['foo' => 'Size:3']); $this->assertFalse($v->passes()); @@ -1133,7 +1149,7 @@ public function testValidateSize() public function testValidateBetween() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => 'asdad'], ['foo' => 'Between:3,4']); $this->assertFalse($v->passes()); @@ -1173,7 +1189,7 @@ public function testValidateBetween() public function testValidateMin() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => '3'], ['foo' => 'Min:3']); $this->assertFalse($v->passes()); @@ -1207,7 +1223,7 @@ public function testValidateMin() public function testValidateMax() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => 'aslksd'], ['foo' => 'Max:3']); $this->assertFalse($v->passes()); @@ -1249,8 +1265,8 @@ public function testValidateMax() public function testProperMessagesAreReturnedForSizes() { - $trans = $this->getRealTranslator(); - $trans->addResource('array', ['validation.min.numeric' => 'numeric', 'validation.size.string' => 'string', 'validation.max.file' => 'file'], 'en', 'messages'); + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.min.numeric' => 'numeric', 'validation.size.string' => 'string', 'validation.max.file' => 'file'], 'en'); $v = new Validator($trans, ['name' => '3'], ['name' => 'Numeric|Min:5']); $this->assertFalse($v->passes()); $v->messages()->setFormat(':message'); @@ -1272,11 +1288,11 @@ public function testProperMessagesAreReturnedForSizes() public function testValidateIn() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['name' => 'foo'], ['name' => 'In:bar,baz']); $this->assertFalse($v->passes()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['name' => 0], ['name' => 'In:bar,baz']); $this->assertFalse($v->passes()); @@ -1298,7 +1314,7 @@ public function testValidateIn() public function testValidateNotIn() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['name' => 'foo'], ['name' => 'NotIn:bar,baz']); $this->assertTrue($v->passes()); @@ -1308,7 +1324,7 @@ public function testValidateNotIn() public function testValidateDistinct() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => ['foo', 'foo']], ['foo.*' => 'distinct']); $this->assertFalse($v->passes()); @@ -1349,7 +1365,7 @@ public function testValidateDistinct() public function testValidateUnique() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['email' => 'foo'], ['email' => 'Unique:users']); $mock = m::mock('Illuminate\Validation\PresenceVerifierInterface'); $mock->shouldReceive('setConnection')->once()->with(null); @@ -1398,7 +1414,7 @@ public function testValidateUnique() */ public function testValidateUniqueAndExistsSendsCorrectFieldNameToDBWithArrays() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, [['email' => 'foo', 'type' => 'bar']], ['*.email' => 'unique:users', '*.type' => 'exists:user_types']); $mock = m::mock('Illuminate\Validation\PresenceVerifierInterface'); $mock->shouldReceive('setConnection')->twice()->with(null); @@ -1410,7 +1426,7 @@ public function testValidateUniqueAndExistsSendsCorrectFieldNameToDBWithArrays() public function testValidationExists() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['email' => 'foo'], ['email' => 'Exists:users']); $mock = m::mock('Illuminate\Validation\PresenceVerifierInterface'); $mock->shouldReceive('setConnection')->once()->with(null); @@ -1418,7 +1434,7 @@ public function testValidationExists() $v->setPresenceVerifier($mock); $this->assertTrue($v->passes()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['email' => 'foo'], ['email' => 'Exists:users,email,account_id,1,name,taylor']); $mock = m::mock('Illuminate\Validation\PresenceVerifierInterface'); $mock->shouldReceive('setConnection')->once()->with(null); @@ -1450,14 +1466,14 @@ public function testValidationExists() public function testValidationExistsIsNotCalledUnnecessarily() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['id' => 'foo'], ['id' => 'Integer|Exists:users,id']); $mock = m::mock('Illuminate\Validation\PresenceVerifierInterface'); $mock->shouldReceive('getCount')->never(); $v->setPresenceVerifier($mock); $this->assertFalse($v->passes()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['id' => '1'], ['id' => 'Integer|Exists:users,id']); $mock = m::mock('Illuminate\Validation\PresenceVerifierInterface'); $mock->shouldReceive('setConnection')->once()->with(null); @@ -1468,7 +1484,7 @@ public function testValidationExistsIsNotCalledUnnecessarily() public function testValidateIp() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['ip' => 'aslsdlks'], ['ip' => 'Ip']); $this->assertFalse($v->passes()); @@ -1478,7 +1494,7 @@ public function testValidateIp() public function testValidateEmail() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => 'Email']); $this->assertFalse($v->passes()); @@ -1491,7 +1507,7 @@ public function testValidateEmail() */ public function testValidateUrlWithValidUrls($validUrl) { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => $validUrl], ['x' => 'Url']); $this->assertTrue($v->passes()); } @@ -1501,7 +1517,7 @@ public function testValidateUrlWithValidUrls($validUrl) */ public function testValidateUrlWithInvalidUrls($invalidUrl) { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => $invalidUrl], ['x' => 'Url']); $this->assertFalse($v->passes()); } @@ -1766,7 +1782,7 @@ public function invalidUrls() public function testValidateActiveUrl() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => 'active_url']); $this->assertFalse($v->passes()); @@ -1785,7 +1801,7 @@ public function testValidateActiveUrl() public function testValidateImage() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $uploadedFile = [__FILE__, '', null, null, null, true]; $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension'])->setConstructorArgs($uploadedFile)->getMock(); @@ -1825,7 +1841,7 @@ public function testValidateImageDimensions() { // Knowing that demo image.gif has width = 3 and height = 2 $uploadedFile = new \Symfony\Component\HttpFoundation\File\UploadedFile(__DIR__.'/fixtures/image.gif', '', null, null, null, true); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => 'file'], ['x' => 'dimensions']); $this->assertTrue($v->fails()); @@ -1892,7 +1908,7 @@ public function testValidateImageDimensions() */ public function testValidateMimetypes() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $uploadedFile = [__FILE__, '', null, null, null, true]; $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension'])->setConstructorArgs($uploadedFile)->getMock(); @@ -1904,7 +1920,7 @@ public function testValidateMimetypes() public function testValidateMime() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $uploadedFile = [__FILE__, '', null, null, null, true]; $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension'])->setConstructorArgs($uploadedFile)->getMock(); @@ -1926,7 +1942,7 @@ public function testValidateMime() */ public function testValidateFile() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $file = new Symfony\Component\HttpFoundation\File\UploadedFile(__FILE__, '', null, null, null, true); $v = new Validator($trans, ['x' => '1'], ['x' => 'file']); @@ -1939,7 +1955,7 @@ public function testValidateFile() public function testEmptyRulesSkipped() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => ['alpha', [], '']]); $this->assertTrue($v->passes()); @@ -1949,18 +1965,18 @@ public function testEmptyRulesSkipped() public function testAlternativeFormat() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => ['alpha', ['min', 3], ['max', 10]]]); $this->assertTrue($v->passes()); } public function testValidateAlpha() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => 'Alpha']); $this->assertTrue($v->passes()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => 'aslsdlks 1 1'], ['x' => 'Alpha']); @@ -2002,7 +2018,7 @@ public function testValidateAlpha() public function testValidateAlphaNum() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => 'asls13dlks'], ['x' => 'AlphaNum']); $this->assertTrue($v->passes()); @@ -2021,7 +2037,7 @@ public function testValidateAlphaNum() public function testValidateAlphaDash() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => 'asls1-_3dlks'], ['x' => 'AlphaDash']); $this->assertTrue($v->passes()); @@ -2037,7 +2053,7 @@ public function testValidateAlphaDash() public function testValidateTimezone() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => 'India'], ['foo' => 'Timezone']); $this->assertFalse($v->passes()); @@ -2056,7 +2072,7 @@ public function testValidateTimezone() public function testValidateRegex() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => 'asdasdf'], ['x' => 'Regex:/^([a-z])+$/i']); $this->assertTrue($v->passes()); @@ -2076,7 +2092,7 @@ public function testValidateRegex() public function testValidateDateAndFormat() { date_default_timezone_set('UTC'); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => '2000-01-01'], ['x' => 'date']); $this->assertTrue($v->passes()); @@ -2114,7 +2130,7 @@ public function testValidateDateAndFormat() public function testBeforeAndAfter() { date_default_timezone_set('UTC'); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => '2000-01-01'], ['x' => 'Before:2012-01-01']); $this->assertTrue($v->passes()); @@ -2170,7 +2186,7 @@ public function testBeforeAndAfter() public function testBeforeAndAfterWithFormat() { date_default_timezone_set('UTC'); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => '31/12/2000'], ['x' => 'before:31/02/2012']); $this->assertTrue($v->fails()); @@ -2231,35 +2247,35 @@ public function testBeforeAndAfterWithFormat() public function testSometimesAddingRules() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => 'foo'], ['x' => 'Required']); $v->sometimes('x', 'Confirmed', function ($i) { return $i->x == 'foo'; }); $this->assertEquals(['x' => ['Required', 'Confirmed']], $v->getRules()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => 'foo'], ['x' => 'Required']); $v->sometimes('x', 'Confirmed', function ($i) { return $i->x == 'bar'; }); $this->assertEquals(['x' => ['Required']], $v->getRules()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => 'foo'], ['x' => 'Required']); $v->sometimes('x', 'Foo|Bar', function ($i) { return $i->x == 'foo'; }); $this->assertEquals(['x' => ['Required', 'Foo', 'Bar']], $v->getRules()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => 'foo'], ['x' => 'Required']); $v->sometimes('x', ['Foo', 'Bar:Baz'], function ($i) { return $i->x == 'foo'; }); $this->assertEquals(['x' => ['Required', 'Foo', 'Bar:Baz']], $v->getRules()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => [['name' => 'first', 'title' => null]]], []); $v->sometimes('foo.*.name', 'Required|String', function ($i) { return $i['foo'][0]['title'] === null; @@ -2269,8 +2285,8 @@ public function testSometimesAddingRules() public function testCustomValidators() { - $trans = $this->getRealTranslator(); - $trans->addResource('array', ['validation.foo' => 'foo!'], 'en', 'messages'); + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.foo' => 'foo!'], 'en'); $v = new Validator($trans, ['name' => 'taylor'], ['name' => 'foo']); $v->addExtension('foo', function () { return false; @@ -2279,8 +2295,8 @@ public function testCustomValidators() $v->messages()->setFormat(':message'); $this->assertEquals('foo!', $v->messages()->first('name')); - $trans = $this->getRealTranslator(); - $trans->addResource('array', ['validation.foo_bar' => 'foo!'], 'en', 'messages'); + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.foo_bar' => 'foo!'], 'en'); $v = new Validator($trans, ['name' => 'taylor'], ['name' => 'foo_bar']); $v->addExtension('FooBar', function () { return false; @@ -2289,7 +2305,7 @@ public function testCustomValidators() $v->messages()->setFormat(':message'); $this->assertEquals('foo!', $v->messages()->first('name')); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['name' => 'taylor'], ['name' => 'foo_bar']); $v->addExtension('FooBar', function () { return false; @@ -2299,7 +2315,7 @@ public function testCustomValidators() $v->messages()->setFormat(':message'); $this->assertEquals('foo!', $v->messages()->first('name')); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['name' => 'taylor'], ['name' => 'foo_bar']); $v->addExtensions(['FooBar' => function () { return false; @@ -2312,8 +2328,8 @@ public function testCustomValidators() public function testClassBasedCustomValidators() { - $trans = $this->getRealTranslator(); - $trans->addResource('array', ['validation.foo' => 'foo!'], 'en', 'messages'); + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.foo' => 'foo!'], 'en'); $v = new Validator($trans, ['name' => 'taylor'], ['name' => 'foo']); $v->setContainer($container = m::mock('Illuminate\Container\Container')); $v->addExtension('foo', 'Foo@bar'); @@ -2326,8 +2342,8 @@ public function testClassBasedCustomValidators() public function testClassBasedCustomValidatorsUsingConventionalMethod() { - $trans = $this->getRealTranslator(); - $trans->addResource('array', ['validation.foo' => 'foo!'], 'en', 'messages'); + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.foo' => 'foo!'], 'en'); $v = new Validator($trans, ['name' => 'taylor'], ['name' => 'foo']); $v->setContainer($container = m::mock('Illuminate\Container\Container')); $v->addExtension('foo', 'Foo'); @@ -2340,7 +2356,7 @@ public function testClassBasedCustomValidatorsUsingConventionalMethod() public function testCustomImplicitValidators() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, [], ['implicit_rule' => 'foo']); $v->addImplicitExtension('implicit_rule', function () { return true; @@ -2360,7 +2376,7 @@ public function testExceptionThrownOnIncorrectParameterCount() public function testValidateImplicitEachWithAsterisks() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $data = ['foo' => [5, 10, 15]]; // pipe rules fails @@ -2428,7 +2444,7 @@ public function testValidateImplicitEachWithAsterisks() public function testSometimesFailOnEmptyArrayInImplicitRules() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $data = ['names' => [['second' => []]]]; $v = new Validator($trans, $data, ['names.*.second' => 'sometimes|required']); @@ -2437,7 +2453,7 @@ public function testSometimesFailOnEmptyArrayInImplicitRules() public function testValidateImplicitEachWithAsterisksForRequiredNonExistingKey() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $data = ['companies' => ['spark']]; $v = new Validator($trans, $data, ['companies.*.name' => 'required']); @@ -2508,7 +2524,7 @@ public function testValidateImplicitEachWithAsterisksForRequiredNonExistingKey() public function testParsingArrayKeysWithDot() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => ['bar' => ''], 'foo.bar' => 'valid'], ['foo.bar' => 'required']); $this->assertTrue($v->fails()); @@ -2525,7 +2541,7 @@ public function testParsingArrayKeysWithDot() public function testImplicitEachWithAsterisksWithArrayValues() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => [1, 2, 3]], ['foo' => 'size:4']); $this->assertFalse($v->passes()); @@ -2557,7 +2573,7 @@ public function testImplicitEachWithAsterisksWithArrayValues() public function testValidateNestedArrayWithCommonParentChildKey() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $data = [ 'products' => [ @@ -2581,7 +2597,7 @@ public function testValidateNestedArrayWithCommonParentChildKey() public function testValidateNestedArrayWithNonNumericKeys() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $data = [ 'item_amounts' => [ @@ -2595,7 +2611,7 @@ public function testValidateNestedArrayWithNonNumericKeys() public function testValidateImplicitEachWithAsterisksConfirmed() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); // confirmed passes $v = new Validator($trans, ['foo' => [ @@ -2640,7 +2656,7 @@ public function testValidateImplicitEachWithAsterisksConfirmed() public function testValidateImplicitEachWithAsterisksDifferent() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); // different passes $v = new Validator($trans, ['foo' => [ @@ -2681,7 +2697,7 @@ public function testValidateImplicitEachWithAsterisksDifferent() public function testValidateImplicitEachWithAsterisksSame() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); // same passes $v = new Validator($trans, ['foo' => [ @@ -2722,7 +2738,7 @@ public function testValidateImplicitEachWithAsterisksSame() public function testValidateImplicitEachWithAsterisksRequired() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); // required passes $v = new Validator($trans, ['foo' => [ @@ -2761,7 +2777,7 @@ public function testValidateImplicitEachWithAsterisksRequired() public function testValidateImplicitEachWithAsterisksRequiredIf() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); // required_if passes $v = new Validator($trans, ['foo' => [ @@ -2800,7 +2816,7 @@ public function testValidateImplicitEachWithAsterisksRequiredIf() public function testValidateImplicitEachWithAsterisksRequiredUnless() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); // required_unless passes $v = new Validator($trans, ['foo' => [ @@ -2839,7 +2855,7 @@ public function testValidateImplicitEachWithAsterisksRequiredUnless() public function testValidateImplicitEachWithAsterisksRequiredWith() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); // required_with passes $v = new Validator($trans, ['foo' => [ @@ -2884,7 +2900,7 @@ public function testValidateImplicitEachWithAsterisksRequiredWith() public function testValidateImplicitEachWithAsterisksRequiredWithAll() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); // required_with_all passes $v = new Validator($trans, ['foo' => [ @@ -2923,7 +2939,7 @@ public function testValidateImplicitEachWithAsterisksRequiredWithAll() public function testValidateImplicitEachWithAsterisksRequiredWithout() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); // required_without passes $v = new Validator($trans, ['foo' => [ @@ -2962,7 +2978,7 @@ public function testValidateImplicitEachWithAsterisksRequiredWithout() public function testValidateImplicitEachWithAsterisksRequiredWithoutAll() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); // required_without_all passes $v = new Validator($trans, ['foo' => [ @@ -3003,7 +3019,7 @@ public function testValidateImplicitEachWithAsterisksRequiredWithoutAll() public function testValidateImplicitEachWithAsterisksBeforeAndAfter() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => [ ['start' => '2016-04-19', 'end' => '2017-04-19'], @@ -3028,7 +3044,7 @@ public function testValidateImplicitEachWithAsterisksBeforeAndAfter() public function testValidateEachWithNonIndexedArray() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $data = ['foobar' => [ ['key' => 'foo', 'value' => 5], ['key' => 'foo', 'value' => 10], @@ -3046,7 +3062,7 @@ public function testValidateEachWithNonIndexedArray() public function testValidateEachWithNonArrayWithArrayRule() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => 'string'], ['foo' => 'Array']); $v->each('foo', ['min:7|max:13']); $this->assertFalse($v->passes()); @@ -3054,7 +3070,7 @@ public function testValidateEachWithNonArrayWithArrayRule() public function testValidateEachWithNonArrayWithoutArrayRule() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => 'string'], ['foo' => 'numeric']); $v->each('foo', ['min:7|max:13']); $this->assertFalse($v->passes()); @@ -3062,7 +3078,7 @@ public function testValidateEachWithNonArrayWithoutArrayRule() public function testGetLeadingExplicitAttributePath() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, [], []); $method = new ReflectionMethod(Validator::class, 'getLeadingExplicitAttributePath'); @@ -3079,7 +3095,7 @@ public function testExtractDataFromPath() { $method = new ReflectionMethod(Validator::class, 'extractDataFromPath'); $method->setAccessible(true); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, [['email' => 'mail'], ['email' => 'mail2']], []); $this->assertEquals([['email' => 'mail'], ['email' => 'mail2']], $method->invoke($v, null)); @@ -3097,12 +3113,12 @@ public function testInlineMessagesMayUseAsteriskForEachRules() public function testUsingSettersWithImplicitRules() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => ['a', 'b', 'c']], ['foo.*' => 'string']); $v->setData(['foo' => ['a', 'b', 'c', 4]]); $this->assertFalse($v->passes()); - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => ['a', 'b', 'c']], ['foo.*' => 'string']); $v->setRules(['foo.*' => 'integer']); $this->assertFalse($v->passes()); @@ -3110,7 +3126,7 @@ public function testUsingSettersWithImplicitRules() public function testInvalidMethod() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, [ @@ -3142,7 +3158,7 @@ public function testInvalidMethod() public function testValidMethod() { - $trans = $this->getRealTranslator(); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, [ @@ -3183,14 +3199,6 @@ protected function getTranslator() return m::mock('Symfony\Component\Translation\TranslatorInterface'); } - protected function getRealTranslator() - { - $trans = new Symfony\Component\Translation\Translator('en', new Symfony\Component\Translation\MessageSelector); - $trans->addLoader('array', new Symfony\Component\Translation\Loader\ArrayLoader); - - return $trans; - } - public function getIlluminateArrayTranslator() { return new Illuminate\Translation\Translator(