From 676e80dd8ee6b61a1f2c0d29190e0d153aaf5887 Mon Sep 17 00:00:00 2001 From: Marcos Neves Date: Tue, 20 Aug 2013 21:25:14 -0300 Subject: [PATCH 1/4] add failing tests before fix addRule --- tests/Valitron/StaticVsInstanceTest.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/Valitron/StaticVsInstanceTest.php b/tests/Valitron/StaticVsInstanceTest.php index c83dad4..38a2851 100644 --- a/tests/Valitron/StaticVsInstanceTest.php +++ b/tests/Valitron/StaticVsInstanceTest.php @@ -10,4 +10,26 @@ class StaticVsInstanceTest extends BaseTestCase $this->assertEquals('ar', Validator::lang(), 'instance defined lang should not replace static global lang'); } + + /** + * Rules messages added with Validator::addRule are replaced after creating validator instance + */ + public function testRuleMessagesReplacedAfterConstructor() + { + $customMessage = 'custom message'; + $ruleName = 'foo'; + Validator::addRule($ruleName, function() {}, $customMessage); + + $prop = new ReflectionProperty('Valitron\Validator', '_ruleMessages'); + $prop->setAccessible(true); + $messages = $prop->getValue(); + + $this->assertEquals($customMessage, $messages[$ruleName]); + + new Validator(array(), array()); + + $messages = $prop->getValue(); + $this->assertArrayHasKey($ruleName, $messages); + $this->assertEquals($customMessage, $messages[$ruleName]); + } } From cf12d976a61159c6190920837f9930b6d9342702 Mon Sep 17 00:00:00 2001 From: Marcos Neves Date: Tue, 20 Aug 2013 22:03:17 -0300 Subject: [PATCH 2/4] fix addRule by merging the loaded language with the current messages --- src/Valitron/Validator.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index e25de20..5c324d6 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -49,7 +49,8 @@ class Validator // Load language file in directory $langFile = rtrim($langDir, '/') . '/' . $lang . '.php'; if ( stream_resolve_include_path($langFile) ) { - static::$_ruleMessages = include $langFile; + $langMessages = include $langFile; + static::$_ruleMessages = array_merge(static::$_ruleMessages, $langMessages); } else { throw new InvalidArgumentException("fail to load language file '$langFile'"); } From 8ec9d8affe0efbdcb41395575a126ee8bfc54d79 Mon Sep 17 00:00:00 2001 From: Marcos Neves Date: Wed, 21 Aug 2013 06:38:33 -0300 Subject: [PATCH 3/4] refactor test to use public interface instead of protected data by reflection --- tests/Valitron/BaseTestCase.php | 1 + tests/Valitron/StaticVsInstanceTest.php | 21 ++++++++------------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/tests/Valitron/BaseTestCase.php b/tests/Valitron/BaseTestCase.php index 0dec6c4..9754f01 100644 --- a/tests/Valitron/BaseTestCase.php +++ b/tests/Valitron/BaseTestCase.php @@ -11,6 +11,7 @@ class BaseTestCase extends \PHPUnit_Framework_TestCase { $this->resetProperty('_lang'); $this->resetProperty('_langDir'); + $this->resetProperty('_rules', array()); $this->resetProperty('_ruleMessages', array()); } diff --git a/tests/Valitron/StaticVsInstanceTest.php b/tests/Valitron/StaticVsInstanceTest.php index 38a2851..1f00a23 100644 --- a/tests/Valitron/StaticVsInstanceTest.php +++ b/tests/Valitron/StaticVsInstanceTest.php @@ -17,19 +17,14 @@ class StaticVsInstanceTest extends BaseTestCase public function testRuleMessagesReplacedAfterConstructor() { $customMessage = 'custom message'; - $ruleName = 'foo'; + $ruleName = 'customRule'; + $fieldName = 'fieldName'; Validator::addRule($ruleName, function() {}, $customMessage); - - $prop = new ReflectionProperty('Valitron\Validator', '_ruleMessages'); - $prop->setAccessible(true); - $messages = $prop->getValue(); - - $this->assertEquals($customMessage, $messages[$ruleName]); - - new Validator(array(), array()); - - $messages = $prop->getValue(); - $this->assertArrayHasKey($ruleName, $messages); - $this->assertEquals($customMessage, $messages[$ruleName]); + $v = new Validator(array($fieldName => $fieldName)); + $v->rule($ruleName, $fieldName); + $v->validate(); + $messages = $v->errors(); + $this->assertArrayHasKey($fieldName, $messages); + $this->assertEquals(ucfirst("$fieldName $customMessage"), $messages[$fieldName][0]); } } From 120975b49c22beec8cba81c9388225003e79c0bb Mon Sep 17 00:00:00 2001 From: Marcos Neves Date: Wed, 21 Aug 2013 06:42:48 -0300 Subject: [PATCH 4/4] change test comment to state the fixing instead of the error --- tests/Valitron/StaticVsInstanceTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Valitron/StaticVsInstanceTest.php b/tests/Valitron/StaticVsInstanceTest.php index 1f00a23..1601189 100644 --- a/tests/Valitron/StaticVsInstanceTest.php +++ b/tests/Valitron/StaticVsInstanceTest.php @@ -12,7 +12,7 @@ class StaticVsInstanceTest extends BaseTestCase } /** - * Rules messages added with Validator::addRule are replaced after creating validator instance + * Fix bug where rules messages added with Validator::addRule were replaced after creating validator instance */ public function testRuleMessagesReplacedAfterConstructor() {