missing language file now raises InvalidArgumentException

This commit is contained in:
Marcos Neves 2013-08-19 12:31:11 -03:00
parent 367ec9a7c7
commit f793bb7555
2 changed files with 18 additions and 2 deletions

View File

@ -1,6 +1,8 @@
<?php <?php
namespace Valitron; namespace Valitron;
use InvalidArgumentException;
/** /**
* Validation Class * Validation Class
* *
@ -45,7 +47,12 @@ class Validator
$langDir = $langDir ?: static::langDir(); $langDir = $langDir ?: static::langDir();
// Load language file in directory // Load language file in directory
static::$_ruleMessages = include rtrim($langDir, '/') . '/' . $lang . '.php'; $langFile = rtrim($langDir, '/') . '/' . $lang . '.php';
if ( stream_resolve_include_path($langFile) ) {
static::$_ruleMessages = include $langFile;
} else {
throw new InvalidArgumentException("fail to load language file '$langFile'");
}
} }
/** /**

View File

@ -1,7 +1,7 @@
<?php <?php
use Valitron\Validator; use Valitron\Validator;
class StaticLangTest extends BaseTestCase class LangTest extends BaseTestCase
{ {
protected function getLangDir() protected function getLangDir()
{ {
@ -41,4 +41,13 @@ class StaticLangTest extends BaseTestCase
$validator = new Validator(array()); $validator = new Validator(array());
$this->assertEquals(realpath($this->getLangDir()), realpath(Validator::langDir())); $this->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');
}
} }