From 34d0c6b640077a1861abe6a338ff0496b9f25339 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 12 Dec 2012 20:43:13 +0100 Subject: [PATCH] [Translation] uniformized the way exception are thrown in LoaderInterface::load() --- .../Component/Translation/CHANGELOG.md | 9 +++++++ .../Exception/ExceptionInterface.php | 23 ++++++++++++++++++ .../Exception/InvalidResourceException.php | 23 ++++++++++++++++++ .../Exception/NotFoundResourceException.php | 23 ++++++++++++++++++ .../Translation/Loader/CsvFileLoader.php | 14 +++++++---- .../Translation/Loader/IcuDatFileLoader.php | 14 +++++++++-- .../Translation/Loader/IcuResFileLoader.php | 14 +++++++++-- .../Translation/Loader/IniFileLoader.php | 10 ++++++-- .../Translation/Loader/LoaderInterface.php | 4 ++++ .../Translation/Loader/MoFileLoader.php | 16 +++++++++---- .../Translation/Loader/PhpFileLoader.php | 10 ++++---- .../Translation/Loader/PoFileLoader.php | 10 ++++++-- .../Loader/QtTranslationsLoader.php | 14 +++++++++-- .../Translation/Loader/XliffFileLoader.php | 16 +++++++------ .../Translation/Loader/YamlFileLoader.php | 19 +++++++++++++-- .../Tests/Loader/CsvFileLoaderTest.php | 4 ++-- .../Tests/Loader/IcuDatFileLoaderTest.php | 24 +++++++++---------- .../Tests/Loader/IcuResFileLoaderTest.php | 10 ++++---- .../Tests/Loader/IniFileLoaderTest.php | 12 +--------- .../Tests/Loader/MoFileLoaderTest.php | 4 ++-- .../Tests/Loader/PhpFileLoaderTest.php | 4 ++-- .../Tests/Loader/PoFileLoaderTest.php | 2 +- .../Tests/Loader/QtTranslationsLoaderTest.php | 2 +- .../Tests/Loader/XliffFileLoaderTest.php | 10 ++++---- .../Tests/Loader/YamlFileLoaderTest.php | 14 +++++++++-- .../resourcebundle/corrupted/resources.dat | 1 + .../Tests/fixtures/resourcebundle/res/en.txt | 3 --- 27 files changed, 229 insertions(+), 80 deletions(-) create mode 100644 src/Symfony/Component/Translation/Exception/ExceptionInterface.php create mode 100644 src/Symfony/Component/Translation/Exception/InvalidResourceException.php create mode 100644 src/Symfony/Component/Translation/Exception/NotFoundResourceException.php create mode 100644 src/Symfony/Component/Translation/Tests/fixtures/resourcebundle/corrupted/resources.dat delete mode 100644 src/Symfony/Component/Translation/Tests/fixtures/resourcebundle/res/en.txt diff --git a/src/Symfony/Component/Translation/CHANGELOG.md b/src/Symfony/Component/Translation/CHANGELOG.md index 72affba57b..1517d0feed 100644 --- a/src/Symfony/Component/Translation/CHANGELOG.md +++ b/src/Symfony/Component/Translation/CHANGELOG.md @@ -1,6 +1,15 @@ CHANGELOG ========= +2.2.0 +----- + + * [BC BREAK] uniformized the exception thrown by the load() method when an error occurs. The load() method now + throws Symfony\Component\Translation\Exception\InvalidResourceException when a resource cannot be found + and Symfony\Component\Translation\Exception\InvalidResourceException when a resource is invalid. + * changed the exception class thrown by some load() methods from \RuntimeException to \InvalidArgumentException + (IcuDatFileLoader, IcuResFileLoader, and QtTranslationsLoader) + 2.1.0 ----- diff --git a/src/Symfony/Component/Translation/Exception/ExceptionInterface.php b/src/Symfony/Component/Translation/Exception/ExceptionInterface.php new file mode 100644 index 0000000000..7757e669a0 --- /dev/null +++ b/src/Symfony/Component/Translation/Exception/ExceptionInterface.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Exception; + +/** + * Exception interface for all exceptions thrown by the component. + * + * @author Fabien Potencier + * + * @api + */ +interface ExceptionInterface +{ +} diff --git a/src/Symfony/Component/Translation/Exception/InvalidResourceException.php b/src/Symfony/Component/Translation/Exception/InvalidResourceException.php new file mode 100644 index 0000000000..6413f1ae79 --- /dev/null +++ b/src/Symfony/Component/Translation/Exception/InvalidResourceException.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Exception; + +/** + * Thrown when a resource cannot be loaded. + * + * @author Fabien Potencier + * + * @api + */ +class InvalidResourceException extends \InvalidArgumentException implements ExceptionInterface +{ +} diff --git a/src/Symfony/Component/Translation/Exception/NotFoundResourceException.php b/src/Symfony/Component/Translation/Exception/NotFoundResourceException.php new file mode 100644 index 0000000000..7826e5ce51 --- /dev/null +++ b/src/Symfony/Component/Translation/Exception/NotFoundResourceException.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Exception; + +/** + * Thrown when a resource does not exist. + * + * @author Fabien Potencier + * + * @api + */ +class NotFoundResourceException extends \InvalidArgumentException implements ExceptionInterface +{ +} diff --git a/src/Symfony/Component/Translation/Loader/CsvFileLoader.php b/src/Symfony/Component/Translation/Loader/CsvFileLoader.php index ce8930f355..bc10188c4c 100644 --- a/src/Symfony/Component/Translation/Loader/CsvFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/CsvFileLoader.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Translation\Loader; +use Symfony\Component\Translation\Exception\InvalidResourceException; +use Symfony\Component\Translation\Exception\NotFoundResourceException; use Symfony\Component\Config\Resource\FileResource; /** @@ -33,16 +35,20 @@ class CsvFileLoader extends ArrayLoader implements LoaderInterface */ public function load($resource, $locale, $domain = 'messages') { - $messages = array(); - if (!stream_is_local($resource)) { - throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $resource)); + throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); } + if (!file_exists($resource)) { + throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); + } + + $messages = array(); + try { $file = new \SplFileObject($resource, 'rb'); } catch (\RuntimeException $e) { - throw new \InvalidArgumentException(sprintf('Error opening file "%s".', $resource)); + throw new NotFoundResourceException(sprintf('Error opening file "%s".', $resource), 0, $e); } $file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY); diff --git a/src/Symfony/Component/Translation/Loader/IcuDatFileLoader.php b/src/Symfony/Component/Translation/Loader/IcuDatFileLoader.php index 83c8abaebe..104883b0c5 100644 --- a/src/Symfony/Component/Translation/Loader/IcuDatFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/IcuDatFileLoader.php @@ -12,6 +12,8 @@ namespace Symfony\Component\Translation\Loader; use Symfony\Component\Translation\MessageCatalogue; +use Symfony\Component\Translation\Exception\InvalidResourceException; +use Symfony\Component\Translation\Exception\NotFoundResourceException; use Symfony\Component\Config\Resource\FileResource; /** @@ -26,12 +28,20 @@ class IcuDatFileLoader extends IcuResFileLoader */ public function load($resource, $locale, $domain = 'messages') { + if (!stream_is_local($resource.'.dat')) { + throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); + } + + if (!file_exists($resource.'.dat')) { + throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); + } + $rb = new \ResourceBundle($locale, $resource); if (!$rb) { - throw new \RuntimeException("cannot load this resource : $resource"); + throw new InvalidResourceException(sprintf('Cannot load resource "%s"', $resource)); } elseif (intl_is_failure($rb->getErrorCode())) { - throw new \RuntimeException($rb->getErrorMessage(), $rb->getErrorCode()); + throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode()); } $messages = $this->flatten($rb); diff --git a/src/Symfony/Component/Translation/Loader/IcuResFileLoader.php b/src/Symfony/Component/Translation/Loader/IcuResFileLoader.php index 9387596ae6..81b8e0fcec 100644 --- a/src/Symfony/Component/Translation/Loader/IcuResFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/IcuResFileLoader.php @@ -12,6 +12,8 @@ namespace Symfony\Component\Translation\Loader; use Symfony\Component\Translation\MessageCatalogue; +use Symfony\Component\Translation\Exception\InvalidResourceException; +use Symfony\Component\Translation\Exception\NotFoundResourceException; use Symfony\Component\Config\Resource\DirectoryResource; /** @@ -26,12 +28,20 @@ class IcuResFileLoader implements LoaderInterface */ public function load($resource, $locale, $domain = 'messages') { + if (!stream_is_local($resource)) { + throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); + } + + if (!is_dir($resource)) { + throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); + } + $rb = new \ResourceBundle($locale, $resource); if (!$rb) { - throw new \RuntimeException("cannot load this resource : $resource"); + throw new InvalidResourceException(sprintf('Cannot load resource "%s"', $resource)); } elseif (intl_is_failure($rb->getErrorCode())) { - throw new \RuntimeException($rb->getErrorMessage(), $rb->getErrorCode()); + throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode()); } $messages = $this->flatten($rb); diff --git a/src/Symfony/Component/Translation/Loader/IniFileLoader.php b/src/Symfony/Component/Translation/Loader/IniFileLoader.php index cd181709eb..616fa7e0e7 100644 --- a/src/Symfony/Component/Translation/Loader/IniFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/IniFileLoader.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Translation\Loader; +use Symfony\Component\Translation\Exception\InvalidResourceException; +use Symfony\Component\Translation\Exception\NotFoundResourceException; use Symfony\Component\Config\Resource\FileResource; /** @@ -25,8 +27,12 @@ class IniFileLoader extends ArrayLoader implements LoaderInterface */ public function load($resource, $locale, $domain = 'messages') { - if (!is_file($resource)) { - throw new \InvalidArgumentException(sprintf('Error opening file "%s".', $resource)); + if (!stream_is_local($resource)) { + throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); + } + + if (!file_exists($resource)) { + throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); } $messages = parse_ini_file($resource, true); diff --git a/src/Symfony/Component/Translation/Loader/LoaderInterface.php b/src/Symfony/Component/Translation/Loader/LoaderInterface.php index d16705d522..4d9965ce99 100644 --- a/src/Symfony/Component/Translation/Loader/LoaderInterface.php +++ b/src/Symfony/Component/Translation/Loader/LoaderInterface.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Translation\Loader; use Symfony\Component\Translation\MessageCatalogue; +use Symfony\Component\Translation\Exception\InvalidResourceException; /** * LoaderInterface is the interface implemented by all translation loaders. @@ -32,6 +33,9 @@ interface LoaderInterface * @return MessageCatalogue A MessageCatalogue instance * * @api + * + * @throws NotFoundResourceException when the resource cannot be found + * @throws InvalidResourceException when the resource cannot be loaded */ public function load($resource, $locale, $domain = 'messages'); } diff --git a/src/Symfony/Component/Translation/Loader/MoFileLoader.php b/src/Symfony/Component/Translation/Loader/MoFileLoader.php index 3b28fcea3a..9d1cacb36e 100644 --- a/src/Symfony/Component/Translation/Loader/MoFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/MoFileLoader.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Translation\Loader; +use Symfony\Component\Translation\Exception\InvalidResourceException; +use Symfony\Component\Translation\Exception\NotFoundResourceException; use Symfony\Component\Config\Resource\FileResource; /** @@ -43,8 +45,12 @@ class MoFileLoader extends ArrayLoader implements LoaderInterface public function load($resource, $locale, $domain = 'messages') { + if (!stream_is_local($resource)) { + throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); + } + if (!file_exists($resource)) { - throw new \InvalidArgumentException(sprintf('File "%s" not found.', $resource)); + throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); } $messages = $this->parse($resource); @@ -56,7 +62,7 @@ class MoFileLoader extends ArrayLoader implements LoaderInterface // not an array if (!is_array($messages)) { - throw new \InvalidArgumentException(sprintf('The file "%s" must contain a valid mo file.', $resource)); + throw new InvalidResourceException(sprintf('The file "%s" must contain a valid mo file.', $resource)); } $catalogue = parent::load($messages, $locale, $domain); @@ -72,7 +78,7 @@ class MoFileLoader extends ArrayLoader implements LoaderInterface * @param resource $resource * * @return array - * @throws \InvalidArgumentException If stream content has an invalid format. + * @throws InvalidResourceException If stream content has an invalid format. */ private function parse($resource) { @@ -81,7 +87,7 @@ class MoFileLoader extends ArrayLoader implements LoaderInterface $stat = fstat($stream); if ($stat['size'] < self::MO_HEADER_SIZE) { - throw new \InvalidArgumentException("MO stream content has an invalid format."); + throw new InvalidResourceException("MO stream content has an invalid format."); } $magic = unpack('V1', fread($stream, 4)); $magic = hexdec(substr(dechex(current($magic)), -8)); @@ -91,7 +97,7 @@ class MoFileLoader extends ArrayLoader implements LoaderInterface } elseif ($magic == self::MO_BIG_ENDIAN_MAGIC) { $isBigEndian = true; } else { - throw new \InvalidArgumentException("MO stream content has an invalid format."); + throw new InvalidResourceException("MO stream content has an invalid format."); } $formatRevision = $this->readLong($stream, $isBigEndian); diff --git a/src/Symfony/Component/Translation/Loader/PhpFileLoader.php b/src/Symfony/Component/Translation/Loader/PhpFileLoader.php index ebb73200e5..4737d1bf96 100644 --- a/src/Symfony/Component/Translation/Loader/PhpFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/PhpFileLoader.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Translation\Loader; +use Symfony\Component\Translation\Exception\InvalidResourceException; +use Symfony\Component\Translation\Exception\NotFoundResourceException; use Symfony\Component\Config\Resource\FileResource; /** @@ -29,12 +31,12 @@ class PhpFileLoader extends ArrayLoader implements LoaderInterface */ 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)) { + throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); } - if (!stream_is_local($resource)) { - throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $resource)); + if (!file_exists($resource)) { + throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); } $messages = require($resource); diff --git a/src/Symfony/Component/Translation/Loader/PoFileLoader.php b/src/Symfony/Component/Translation/Loader/PoFileLoader.php index 686162ac18..58ec6c7a66 100644 --- a/src/Symfony/Component/Translation/Loader/PoFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/PoFileLoader.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Translation\Loader; +use Symfony\Component\Translation\Exception\InvalidResourceException; +use Symfony\Component\Translation\Exception\NotFoundResourceException; use Symfony\Component\Config\Resource\FileResource; /** @@ -21,8 +23,12 @@ class PoFileLoader extends ArrayLoader implements LoaderInterface { public function load($resource, $locale, $domain = 'messages') { + if (!stream_is_local($resource)) { + throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); + } + if (!file_exists($resource)) { - throw new \InvalidArgumentException(sprintf('File "%s" not found.', $resource)); + throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); } $messages = $this->parse($resource); @@ -34,7 +40,7 @@ class PoFileLoader extends ArrayLoader implements LoaderInterface // not an array if (!is_array($messages)) { - throw new \InvalidArgumentException(sprintf('The file "%s" must contain a valid po file.', $resource)); + throw new InvalidResourceException(sprintf('The file "%s" must contain a valid po file.', $resource)); } $catalogue = parent::load($messages, $locale, $domain); diff --git a/src/Symfony/Component/Translation/Loader/QtTranslationsLoader.php b/src/Symfony/Component/Translation/Loader/QtTranslationsLoader.php index bf7f3ce1e8..d15f3ce020 100644 --- a/src/Symfony/Component/Translation/Loader/QtTranslationsLoader.php +++ b/src/Symfony/Component/Translation/Loader/QtTranslationsLoader.php @@ -11,8 +11,10 @@ namespace Symfony\Component\Translation\Loader; -use Symfony\Component\Config\Resource\FileResource; use Symfony\Component\Translation\MessageCatalogue; +use Symfony\Component\Translation\Exception\InvalidResourceException; +use Symfony\Component\Translation\Exception\NotFoundResourceException; +use Symfony\Component\Config\Resource\FileResource; /** * QtTranslationsLoader loads translations from QT Translations XML files. @@ -30,10 +32,18 @@ class QtTranslationsLoader implements LoaderInterface */ public function load($resource, $locale, $domain = 'messages') { + if (!stream_is_local($resource)) { + throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); + } + + if (!file_exists($resource)) { + throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); + } + $dom = new \DOMDocument(); $current = libxml_use_internal_errors(true); if (!@$dom->load($resource, defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0)) { - throw new \RuntimeException(implode("\n", $this->getXmlErrors())); + throw new InvalidResourceException(implode("\n", $this->getXmlErrors())); } $xpath = new \DOMXPath($dom); diff --git a/src/Symfony/Component/Translation/Loader/XliffFileLoader.php b/src/Symfony/Component/Translation/Loader/XliffFileLoader.php index 8e5e0db092..c1e9f1de3a 100644 --- a/src/Symfony/Component/Translation/Loader/XliffFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/XliffFileLoader.php @@ -12,6 +12,8 @@ namespace Symfony\Component\Translation\Loader; use Symfony\Component\Translation\MessageCatalogue; +use Symfony\Component\Translation\Exception\InvalidResourceException; +use Symfony\Component\Translation\Exception\NotFoundResourceException; use Symfony\Component\Config\Resource\FileResource; /** @@ -30,12 +32,12 @@ class XliffFileLoader implements LoaderInterface */ 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)) { + throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); } - if (!stream_is_local($resource)) { - throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $resource)); + if (!file_exists($resource)) { + throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); } $xml = $this->parseFile($resource); @@ -71,7 +73,7 @@ class XliffFileLoader implements LoaderInterface if (!@$dom->loadXML(file_get_contents($file), LIBXML_NONET | (defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0))) { libxml_disable_entity_loader($disableEntities); - throw new \RuntimeException(implode("\n", $this->getXmlErrors($internalErrors))); + throw new InvalidResourceException(implode("\n", $this->getXmlErrors($internalErrors))); } libxml_disable_entity_loader($disableEntities); @@ -80,7 +82,7 @@ class XliffFileLoader implements LoaderInterface if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) { libxml_use_internal_errors($internalErrors); - throw new \RuntimeException('Document types are not allowed.'); + throw new InvalidResourceException('Document types are not allowed.'); } } @@ -100,7 +102,7 @@ class XliffFileLoader implements LoaderInterface $source = str_replace('http://www.w3.org/2001/xml.xsd', $location, $source); if (!@$dom->schemaValidateSource($source)) { - throw new \RuntimeException(implode("\n", $this->getXmlErrors($internalErrors))); + throw new InvalidResourceException(implode("\n", $this->getXmlErrors($internalErrors))); } $dom->normalizeDocument(); diff --git a/src/Symfony/Component/Translation/Loader/YamlFileLoader.php b/src/Symfony/Component/Translation/Loader/YamlFileLoader.php index 4ac885b0a2..232ee2bca3 100644 --- a/src/Symfony/Component/Translation/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/YamlFileLoader.php @@ -11,8 +11,11 @@ namespace Symfony\Component\Translation\Loader; +use Symfony\Component\Translation\Exception\InvalidResourceException; +use Symfony\Component\Translation\Exception\NotFoundResourceException; use Symfony\Component\Config\Resource\FileResource; use Symfony\Component\Yaml\Yaml; +use Symfony\Component\Yaml\Exception\ParseException; /** * YamlFileLoader loads translations from Yaml files. @@ -30,7 +33,19 @@ class YamlFileLoader extends ArrayLoader implements LoaderInterface */ public function load($resource, $locale, $domain = 'messages') { - $messages = Yaml::parse($resource); + if (!stream_is_local($resource)) { + throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); + } + + if (!file_exists($resource)) { + throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); + } + + try { + $messages = Yaml::parse($resource); + } catch (ParseException $e) { + throw new InvalidResourceException('Error parsing YAML.', 0, $e); + } // empty file if (null === $messages) { @@ -39,7 +54,7 @@ class YamlFileLoader extends ArrayLoader implements LoaderInterface // not an array if (!is_array($messages)) { - throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $resource)); + throw new InvalidResourceException(sprintf('The file "%s" must contain a YAML array.', $resource)); } $catalogue = parent::load($messages, $locale, $domain); diff --git a/src/Symfony/Component/Translation/Tests/Loader/CsvFileLoaderTest.php b/src/Symfony/Component/Translation/Tests/Loader/CsvFileLoaderTest.php index b74521bdc9..88570a6024 100644 --- a/src/Symfony/Component/Translation/Tests/Loader/CsvFileLoaderTest.php +++ b/src/Symfony/Component/Translation/Tests/Loader/CsvFileLoaderTest.php @@ -46,7 +46,7 @@ class CsvFileLoaderTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \InvalidArgumentException + * @expectedException Symfony\Component\Translation\Exception\NotFoundResourceException */ public function testLoadNonExistingResource() { @@ -56,7 +56,7 @@ class CsvFileLoaderTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \InvalidArgumentException + * @expectedException Symfony\Component\Translation\Exception\InvalidResourceException */ public function testLoadNonLocalResource() { diff --git a/src/Symfony/Component/Translation/Tests/Loader/IcuDatFileLoaderTest.php b/src/Symfony/Component/Translation/Tests/Loader/IcuDatFileLoaderTest.php index 1e6b3dadf0..e4f0a36daa 100644 --- a/src/Symfony/Component/Translation/Tests/Loader/IcuDatFileLoaderTest.php +++ b/src/Symfony/Component/Translation/Tests/Loader/IcuDatFileLoaderTest.php @@ -25,12 +25,20 @@ class IcuDatFileLoaderTest extends LocalizedTestCase if (!extension_loaded('intl')) { $this->markTestSkipped('This test requires intl extension to work.'); } + } + /** + * @expectedException Symfony\Component\Translation\Exception\InvalidResourceException + */ + public function testLoadInvalidResource() + { + $loader = new IcuDatFileLoader(); + $loader->load(__DIR__.'/../fixtures/resourcebundle/corrupted/resources', 'es', 'domain2'); } public function testDatEnglishLoad() { - // bundled resource is build using pkgdata command which at leas in ICU 4.2 comes in extremely! buggy form + // bundled resource is build using pkgdata command which at least in ICU 4.2 comes in extremely! buggy form // you must specify an temporary build directory which is not the same as current directory and // MUST reside on the same partition. pkgdata -p resources -T /srv -d . packagelist.txt $loader = new IcuDatFileLoader(); @@ -54,21 +62,11 @@ class IcuDatFileLoaderTest extends LocalizedTestCase } /** - * @expectedException \RuntimeException + * @expectedException Symfony\Component\Translation\Exception\NotFoundResourceException */ public function testLoadNonExistingResource() { $loader = new IcuDatFileLoader(); - $resource = __DIR__.'/../fixtures/non-existing.txt'; - $loader->load($resource, 'en', 'domain1'); - } - - /** - * @expectedException \RuntimeException - */ - public function testLoadInvalidResource() - { - $loader = new IcuDatFileLoader(); - $loader->load(__DIR__.'/../fixtures/resourcebundle/res/en.txt', 'en', 'domain1'); + $loader->load(__DIR__.'/../fixtures/non-existing.txt', 'en', 'domain1'); } } diff --git a/src/Symfony/Component/Translation/Tests/Loader/IcuResFileLoaderTest.php b/src/Symfony/Component/Translation/Tests/Loader/IcuResFileLoaderTest.php index cc6e28b3c9..89020e72ae 100644 --- a/src/Symfony/Component/Translation/Tests/Loader/IcuResFileLoaderTest.php +++ b/src/Symfony/Component/Translation/Tests/Loader/IcuResFileLoaderTest.php @@ -25,7 +25,6 @@ class IcuResFileLoaderTest extends LocalizedTestCase if (!extension_loaded('intl')) { $this->markTestSkipped('This test requires intl extension to work.'); } - } public function testLoad() @@ -41,21 +40,20 @@ class IcuResFileLoaderTest extends LocalizedTestCase } /** - * @expectedException \RuntimeException + * @expectedException Symfony\Component\Translation\Exception\NotFoundResourceException */ public function testLoadNonExistingResource() { $loader = new IcuResFileLoader(); - $resource = __DIR__.'/../fixtures/non-existing.txt'; - $loader->load($resource, 'en', 'domain1'); + $loader->load(__DIR__.'/../fixtures/non-existing.txt', 'en', 'domain1'); } /** - * @expectedException \RuntimeException + * @expectedException Symfony\Component\Translation\Exception\InvalidResourceException */ public function testLoadInvalidResource() { $loader = new IcuResFileLoader(); - $loader->load(__DIR__.'/../fixtures/resourcebundle/res/en.txt', 'en', 'domain1'); + $loader->load(__DIR__.'/../fixtures/resourcebundle/corrupted', 'en', 'domain1'); } } diff --git a/src/Symfony/Component/Translation/Tests/Loader/IniFileLoaderTest.php b/src/Symfony/Component/Translation/Tests/Loader/IniFileLoaderTest.php index 43a5e774a7..d95263f775 100644 --- a/src/Symfony/Component/Translation/Tests/Loader/IniFileLoaderTest.php +++ b/src/Symfony/Component/Translation/Tests/Loader/IniFileLoaderTest.php @@ -46,7 +46,7 @@ class IniFileLoaderTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \InvalidArgumentException + * @expectedException Symfony\Component\Translation\Exception\NotFoundResourceException */ public function testLoadNonExistingResource() { @@ -54,14 +54,4 @@ class IniFileLoaderTest extends \PHPUnit_Framework_TestCase $resource = __DIR__.'/../fixtures/non-existing.ini'; $loader->load($resource, 'en', 'domain1'); } - - /** - * @expectedException \InvalidArgumentException - */ - public function testLoadThrowsAnExceptionIfFileNotExists() - { - $loader = new IniFileLoader(); - $resource = __DIR__.'/../fixtures/not-exists.ini'; - $loader->load($resource, 'en', 'domain1'); - } } diff --git a/src/Symfony/Component/Translation/Tests/Loader/MoFileLoaderTest.php b/src/Symfony/Component/Translation/Tests/Loader/MoFileLoaderTest.php index 1cd9543102..b309ba5c17 100644 --- a/src/Symfony/Component/Translation/Tests/Loader/MoFileLoaderTest.php +++ b/src/Symfony/Component/Translation/Tests/Loader/MoFileLoaderTest.php @@ -46,7 +46,7 @@ class MoFileLoaderTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \InvalidArgumentException + * @expectedException Symfony\Component\Translation\Exception\NotFoundResourceException */ public function testLoadNonExistingResource() { @@ -56,7 +56,7 @@ class MoFileLoaderTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \InvalidArgumentException + * @expectedException Symfony\Component\Translation\Exception\InvalidResourceException */ public function testLoadInvalidResource() { diff --git a/src/Symfony/Component/Translation/Tests/Loader/PhpFileLoaderTest.php b/src/Symfony/Component/Translation/Tests/Loader/PhpFileLoaderTest.php index 279c92e090..3272c08569 100644 --- a/src/Symfony/Component/Translation/Tests/Loader/PhpFileLoaderTest.php +++ b/src/Symfony/Component/Translation/Tests/Loader/PhpFileLoaderTest.php @@ -35,7 +35,7 @@ class PhpFileLoaderTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \InvalidArgumentException + * @expectedException Symfony\Component\Translation\Exception\NotFoundResourceException */ public function testLoadNonExistingResource() { @@ -45,7 +45,7 @@ class PhpFileLoaderTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \InvalidArgumentException + * @expectedException Symfony\Component\Translation\Exception\InvalidResourceException */ public function testLoadThrowsAnExceptionIfFileNotLocal() { diff --git a/src/Symfony/Component/Translation/Tests/Loader/PoFileLoaderTest.php b/src/Symfony/Component/Translation/Tests/Loader/PoFileLoaderTest.php index 3f643266ab..e31a3f8683 100644 --- a/src/Symfony/Component/Translation/Tests/Loader/PoFileLoaderTest.php +++ b/src/Symfony/Component/Translation/Tests/Loader/PoFileLoaderTest.php @@ -57,7 +57,7 @@ class PoFileLoaderTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \InvalidArgumentException + * @expectedException Symfony\Component\Translation\Exception\NotFoundResourceException */ public function testLoadNonExistingResource() { diff --git a/src/Symfony/Component/Translation/Tests/Loader/QtTranslationsLoaderTest.php b/src/Symfony/Component/Translation/Tests/Loader/QtTranslationsLoaderTest.php index b58c6ff193..aa1f0c4852 100644 --- a/src/Symfony/Component/Translation/Tests/Loader/QtTranslationsLoaderTest.php +++ b/src/Symfony/Component/Translation/Tests/Loader/QtTranslationsLoaderTest.php @@ -35,7 +35,7 @@ class QtTranslationsLoaderTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \RuntimeException + * @expectedException Symfony\Component\Translation\Exception\NotFoundResourceException */ public function testLoadNonExistingResource() { diff --git a/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php b/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php index 7e80142da9..5f65231f8e 100644 --- a/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php +++ b/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php @@ -43,7 +43,7 @@ class XliffFileLoaderTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \RuntimeException + * @expectedException Symfony\Component\Translation\Exception\InvalidResourceException */ public function testLoadInvalidResource() { @@ -52,7 +52,7 @@ class XliffFileLoaderTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \RuntimeException + * @expectedException Symfony\Component\Translation\Exception\InvalidResourceException */ public function testLoadResourceDoesNotValidate() { @@ -61,7 +61,7 @@ class XliffFileLoaderTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \InvalidArgumentException + * @expectedException Symfony\Component\Translation\Exception\NotFoundResourceException */ public function testLoadNonExistingResource() { @@ -71,7 +71,7 @@ class XliffFileLoaderTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \InvalidArgumentException + * @expectedException Symfony\Component\Translation\Exception\InvalidResourceException */ public function testLoadThrowsAnExceptionIfFileNotLocal() { @@ -81,7 +81,7 @@ class XliffFileLoaderTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \RuntimeException + * @expectedException Symfony\Component\Translation\Exception\InvalidResourceException * @expectedExceptionMessage Document types are not allowed. */ public function testDocTypeIsNotAllowed() diff --git a/src/Symfony/Component/Translation/Tests/Loader/YamlFileLoaderTest.php b/src/Symfony/Component/Translation/Tests/Loader/YamlFileLoaderTest.php index c73fe645b4..35fba47dae 100644 --- a/src/Symfony/Component/Translation/Tests/Loader/YamlFileLoaderTest.php +++ b/src/Symfony/Component/Translation/Tests/Loader/YamlFileLoaderTest.php @@ -50,7 +50,7 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \InvalidArgumentException + * @expectedException Symfony\Component\Translation\Exception\NotFoundResourceException */ public function testLoadNonExistingResource() { @@ -60,7 +60,17 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \InvalidArgumentException + * @expectedException Symfony\Component\Translation\Exception\InvalidResourceException + */ + public function testLoadThrowsAnExceptionIfFileNotLocal() + { + $loader = new YamlFileLoader(); + $resource = 'http://example.com/resources.yml'; + $loader->load($resource, 'en', 'domain1'); + } + + /** + * @expectedException Symfony\Component\Translation\Exception\InvalidResourceException */ public function testLoadThrowsAnExceptionIfNotAnArray() { diff --git a/src/Symfony/Component/Translation/Tests/fixtures/resourcebundle/corrupted/resources.dat b/src/Symfony/Component/Translation/Tests/fixtures/resourcebundle/corrupted/resources.dat new file mode 100644 index 0000000000..391250caa0 --- /dev/null +++ b/src/Symfony/Component/Translation/Tests/fixtures/resourcebundle/corrupted/resources.dat @@ -0,0 +1 @@ +XXX \ No newline at end of file diff --git a/src/Symfony/Component/Translation/Tests/fixtures/resourcebundle/res/en.txt b/src/Symfony/Component/Translation/Tests/fixtures/resourcebundle/res/en.txt deleted file mode 100644 index a8a87e5f44..0000000000 --- a/src/Symfony/Component/Translation/Tests/fixtures/resourcebundle/res/en.txt +++ /dev/null @@ -1,3 +0,0 @@ -en { - foo { "bar" } -} \ No newline at end of file