From ca4f6737dc884e6d2b759c55edd6324a0adbb4a3 Mon Sep 17 00:00:00 2001 From: Marcos Neves Date: Sat, 17 Aug 2013 12:14:19 -0300 Subject: [PATCH 1/3] add test to show the bug --- tests/Valitron/StaticVsInstanceTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 tests/Valitron/StaticVsInstanceTest.php diff --git a/tests/Valitron/StaticVsInstanceTest.php b/tests/Valitron/StaticVsInstanceTest.php new file mode 100644 index 0000000..17d5d8a --- /dev/null +++ b/tests/Valitron/StaticVsInstanceTest.php @@ -0,0 +1,13 @@ +assertEquals('ar', Validator::lang(), + 'lang defined statically should not be override by instance lang'); + } +} From 367ec9a7c79ea928eadc2ea9e6954cd50091da71 Mon Sep 17 00:00:00 2001 From: Marcos Neves Date: Sat, 17 Aug 2013 12:41:58 -0300 Subject: [PATCH 2/3] fix bug moving message loading to constructor and replace require with include to avoid fatal error --- src/Valitron/Validator.php | 8 ++------ tests/Valitron/StaticVsInstanceTest.php | 4 ++-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index cb7cc8e..04a6244 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -44,8 +44,8 @@ class Validator // set langDir in the follow order: constructor param, static::$_langDir, default to package lang dir $langDir = $langDir ?: static::langDir(); - static::langDir($langDir); - static::lang($lang); + // Load language file in directory + static::$_ruleMessages = include rtrim($langDir, '/') . '/' . $lang . '.php'; } /** @@ -55,10 +55,6 @@ class Validator { if($lang !== null) { static::$_lang = $lang; - - // Load language file in directory - $langDir = static::langDir(); - static::$_ruleMessages = require rtrim($langDir, '/') . '/' . $lang . '.php'; } return static::$_lang ?: 'en'; } diff --git a/tests/Valitron/StaticVsInstanceTest.php b/tests/Valitron/StaticVsInstanceTest.php index 17d5d8a..c83dad4 100644 --- a/tests/Valitron/StaticVsInstanceTest.php +++ b/tests/Valitron/StaticVsInstanceTest.php @@ -3,11 +3,11 @@ use Valitron\Validator; class StaticVsInstanceTest extends BaseTestCase { - public function testInstanceOverrideStatic() + public function testInstanceOverrideStaticLang() { Validator::lang('ar'); new Validator(array(), array(), 'en'); $this->assertEquals('ar', Validator::lang(), - 'lang defined statically should not be override by instance lang'); + 'instance defined lang should not replace static global lang'); } } From f793bb75551cfa2611db2f0a707dcd011428b140 Mon Sep 17 00:00:00 2001 From: Marcos Neves Date: Mon, 19 Aug 2013 12:31:11 -0300 Subject: [PATCH 3/3] missing language file now raises InvalidArgumentException --- src/Valitron/Validator.php | 9 ++++++++- tests/Valitron/{StaticLangTest.php => LangTest.php} | 11 ++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) rename tests/Valitron/{StaticLangTest.php => LangTest.php} (77%) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 04a6244..e25de20 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -1,6 +1,8 @@ assertEquals(realpath($this->getLangDir()), realpath(Validator::langDir())); } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage fail to load language file '/this/dir/does/not/exists/en.php' + */ + public function testLangException() + { + new Validator(array(), array(), 'en', '/this/dir/does/not/exists'); + } }