Fix loaders to check if resource exists.

This commit is contained in:
umpirsky 2012-12-12 17:29:33 +01:00
parent 3140a589d2
commit ac58681ae4
15 changed files with 117 additions and 12 deletions

View File

@ -11,7 +11,6 @@
namespace Symfony\Component\Locale\Tests\Stub; namespace Symfony\Component\Locale\Tests\Stub;
use Symfony\Component\Locale\Locale;
use Symfony\Component\Locale\Stub\StubLocale; use Symfony\Component\Locale\Stub\StubLocale;
use Symfony\Component\Locale\Tests\TestCase as LocaleTestCase; use Symfony\Component\Locale\Tests\TestCase as LocaleTestCase;
@ -22,7 +21,7 @@ class StubLocaleTest extends LocaleTestCase
*/ */
public function testGetDisplayCountriesWithUnsupportedLocale() public function testGetDisplayCountriesWithUnsupportedLocale()
{ {
$countries = StubLocale::getDisplayCountries('pt_BR'); StubLocale::getDisplayCountries('pt_BR');
} }
public function testGetDisplayCountries() public function testGetDisplayCountries()
@ -42,7 +41,7 @@ class StubLocaleTest extends LocaleTestCase
*/ */
public function testGetDisplayLanguagesWithUnsupportedLocale() public function testGetDisplayLanguagesWithUnsupportedLocale()
{ {
$countries = StubLocale::getDisplayLanguages('pt_BR'); StubLocale::getDisplayLanguages('pt_BR');
} }
public function testGetDisplayLanguages() public function testGetDisplayLanguages()
@ -62,7 +61,7 @@ class StubLocaleTest extends LocaleTestCase
*/ */
public function testGetCurrenciesDataWithUnsupportedLocale() public function testGetCurrenciesDataWithUnsupportedLocale()
{ {
$currencies = StubLocale::getCurrenciesData('pt_BR'); StubLocale::getCurrenciesData('pt_BR');
} }
public function testGetCurrenciesData() public function testGetCurrenciesData()
@ -97,7 +96,7 @@ class StubLocaleTest extends LocaleTestCase
*/ */
public function testGetDisplayLocalesWithUnsupportedLocale() public function testGetDisplayLocalesWithUnsupportedLocale()
{ {
$locales = StubLocale::getDisplayLocales('pt'); StubLocale::getDisplayLocales('pt');
} }
public function testGetDisplayLocales() public function testGetDisplayLocales()

View File

@ -43,6 +43,10 @@ class MoFileLoader extends ArrayLoader implements LoaderInterface
public function load($resource, $locale, $domain = 'messages') public function load($resource, $locale, $domain = 'messages')
{ {
if (!file_exists($resource)) {
throw new \InvalidArgumentException(sprintf('File "%s" not found.', $resource));
}
$messages = $this->parse($resource); $messages = $this->parse($resource);
// empty file // empty file

View File

@ -29,6 +29,10 @@ class PhpFileLoader extends ArrayLoader implements LoaderInterface
*/ */
public function load($resource, $locale, $domain = 'messages') public function load($resource, $locale, $domain = 'messages')
{ {
if (!file_exists($resource)) {
throw new \InvalidArgumentException(sprintf('File "%s" not found.', $resource));
}
if (!stream_is_local($resource)) { if (!stream_is_local($resource)) {
throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $resource)); throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $resource));
} }

View File

@ -21,6 +21,10 @@ class PoFileLoader extends ArrayLoader implements LoaderInterface
{ {
public function load($resource, $locale, $domain = 'messages') public function load($resource, $locale, $domain = 'messages')
{ {
if (!file_exists($resource)) {
throw new \InvalidArgumentException(sprintf('File "%s" not found.', $resource));
}
$messages = $this->parse($resource); $messages = $this->parse($resource);
// empty file // empty file

View File

@ -30,6 +30,10 @@ class XliffFileLoader implements LoaderInterface
*/ */
public function load($resource, $locale, $domain = 'messages') public function load($resource, $locale, $domain = 'messages')
{ {
if (!file_exists($resource)) {
throw new \InvalidArgumentException(sprintf('File "%s" not found.', $resource));
}
if (!stream_is_local($resource)) { if (!stream_is_local($resource)) {
throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $resource)); throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $resource));
} }

View File

@ -48,7 +48,7 @@ class CsvFileLoaderTest extends \PHPUnit_Framework_TestCase
/** /**
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
*/ */
public function testLoadThrowsAnExceptionIfFileNotExists() public function testLoadNonExistingResource()
{ {
$loader = new CsvFileLoader(); $loader = new CsvFileLoader();
$resource = __DIR__.'/../fixtures/not-exists.csv'; $resource = __DIR__.'/../fixtures/not-exists.csv';
@ -58,7 +58,7 @@ class CsvFileLoaderTest extends \PHPUnit_Framework_TestCase
/** /**
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
*/ */
public function testLoadThrowsAnExceptionIfFileNotLocal() public function testLoadNonLocalResource()
{ {
$loader = new CsvFileLoader(); $loader = new CsvFileLoader();
$resource = 'http://example.com/resources.csv'; $resource = 'http://example.com/resources.csv';

View File

@ -53,12 +53,22 @@ class IcuDatFileLoaderTest extends LocalizedTestCase
$this->assertEquals(array(new FileResource($resource.'.dat')), $catalogue->getResources()); $this->assertEquals(array(new FileResource($resource.'.dat')), $catalogue->getResources());
} }
/**
* @expectedException \RuntimeException
*/
public function testLoadNonExistingResource()
{
$loader = new IcuDatFileLoader();
$resource = __DIR__.'/../fixtures/non-existing.txt';
$loader->load($resource, 'en', 'domain1');
}
/** /**
* @expectedException \RuntimeException * @expectedException \RuntimeException
*/ */
public function testLoadInvalidResource() public function testLoadInvalidResource()
{ {
$loader = new IcuDatFileLoader(); $loader = new IcuDatFileLoader();
$catalogue = $loader->load(__DIR__.'/../fixtures/resourcebundle/res/en.txt', 'en', 'domain1'); $loader->load(__DIR__.'/../fixtures/resourcebundle/res/en.txt', 'en', 'domain1');
} }
} }

View File

@ -40,12 +40,22 @@ class IcuResFileLoaderTest extends LocalizedTestCase
$this->assertEquals(array(new DirectoryResource($resource)), $catalogue->getResources()); $this->assertEquals(array(new DirectoryResource($resource)), $catalogue->getResources());
} }
/**
* @expectedException \RuntimeException
*/
public function testLoadNonExistingResource()
{
$loader = new IcuResFileLoader();
$resource = __DIR__.'/../fixtures/non-existing.txt';
$loader->load($resource, 'en', 'domain1');
}
/** /**
* @expectedException \RuntimeException * @expectedException \RuntimeException
*/ */
public function testLoadInvalidResource() public function testLoadInvalidResource()
{ {
$loader = new IcuResFileLoader(); $loader = new IcuResFileLoader();
$catalogue = $loader->load(__DIR__.'/../fixtures/resourcebundle/res/en.txt', 'en', 'domain1'); $loader->load(__DIR__.'/../fixtures/resourcebundle/res/en.txt', 'en', 'domain1');
} }
} }

View File

@ -45,6 +45,16 @@ class IniFileLoaderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources()); $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
} }
/**
* @expectedException \InvalidArgumentException
*/
public function testLoadNonExistingResource()
{
$loader = new IniFileLoader();
$resource = __DIR__.'/../fixtures/non-existing.ini';
$loader->load($resource, 'en', 'domain1');
}
/** /**
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
*/ */

View File

@ -45,6 +45,16 @@ class MoFileLoaderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources()); $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
} }
/**
* @expectedException \InvalidArgumentException
*/
public function testLoadNonExistingResource()
{
$loader = new MoFileLoader();
$resource = __DIR__.'/../fixtures/non-existing.mo';
$loader->load($resource, 'en', 'domain1');
}
/** /**
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
*/ */
@ -52,6 +62,6 @@ class MoFileLoaderTest extends \PHPUnit_Framework_TestCase
{ {
$loader = new MoFileLoader(); $loader = new MoFileLoader();
$resource = __DIR__.'/../fixtures/empty.mo'; $resource = __DIR__.'/../fixtures/empty.mo';
$catalogue = $loader->load($resource, 'en', 'domain1'); $loader->load($resource, 'en', 'domain1');
} }
} }

View File

@ -34,6 +34,16 @@ class PhpFileLoaderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources()); $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
} }
/**
* @expectedException \InvalidArgumentException
*/
public function testLoadNonExistingResource()
{
$loader = new PhpFileLoader();
$resource = __DIR__.'/../fixtures/non-existing.php';
$loader->load($resource, 'en', 'domain1');
}
/** /**
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
*/ */

View File

@ -56,6 +56,16 @@ class PoFileLoaderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources()); $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
} }
/**
* @expectedException \InvalidArgumentException
*/
public function testLoadNonExistingResource()
{
$loader = new PoFileLoader();
$resource = __DIR__.'/../fixtures/non-existing.po';
$loader->load($resource, 'en', 'domain1');
}
public function testLoadEmptyTranslation() public function testLoadEmptyTranslation()
{ {
$loader = new PoFileLoader(); $loader = new PoFileLoader();

View File

@ -33,4 +33,14 @@ class QtTranslationsLoaderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('en', $catalogue->getLocale()); $this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources()); $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
} }
/**
* @expectedException \RuntimeException
*/
public function testLoadNonExistingResource()
{
$loader = new QtTranslationsLoader();
$resource = __DIR__.'/../fixtures/non-existing.ts';
$loader->load($resource, 'en', 'domain1');
}
} }

View File

@ -48,7 +48,7 @@ class XliffFileLoaderTest extends \PHPUnit_Framework_TestCase
public function testLoadInvalidResource() public function testLoadInvalidResource()
{ {
$loader = new XliffFileLoader(); $loader = new XliffFileLoader();
$catalogue = $loader->load(__DIR__.'/../fixtures/resources.php', 'en', 'domain1'); $loader->load(__DIR__.'/../fixtures/resources.php', 'en', 'domain1');
} }
/** /**
@ -57,7 +57,17 @@ class XliffFileLoaderTest extends \PHPUnit_Framework_TestCase
public function testLoadResourceDoesNotValidate() public function testLoadResourceDoesNotValidate()
{ {
$loader = new XliffFileLoader(); $loader = new XliffFileLoader();
$catalogue = $loader->load(__DIR__.'/../fixtures/non-valid.xlf', 'en', 'domain1'); $loader->load(__DIR__.'/../fixtures/non-valid.xlf', 'en', 'domain1');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testLoadNonExistingResource()
{
$loader = new XliffFileLoader();
$resource = __DIR__.'/../fixtures/non-existing.xlf';
$loader->load($resource, 'en', 'domain1');
} }
/** /**

View File

@ -49,6 +49,16 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources()); $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
} }
/**
* @expectedException \InvalidArgumentException
*/
public function testLoadNonExistingResource()
{
$loader = new YamlFileLoader();
$resource = __DIR__.'/../fixtures/non-existing.yml';
$loader->load($resource, 'en', 'domain1');
}
/** /**
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
*/ */