YamlFileLoaderTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Tests\Loader;
  11. use Symfony\Component\Translation\Loader\YamlFileLoader;
  12. use Symfony\Component\Config\Resource\FileResource;
  13. class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected function setUp()
  16. {
  17. if (!class_exists('Symfony\Component\Config\Loader\Loader')) {
  18. $this->markTestSkipped('The "Config" component is not available');
  19. }
  20. if (!class_exists('Symfony\Component\Yaml\Yaml')) {
  21. $this->markTestSkipped('The "Yaml" component is not available');
  22. }
  23. }
  24. public function testLoad()
  25. {
  26. $loader = new YamlFileLoader();
  27. $resource = __DIR__.'/../fixtures/resources.yml';
  28. $catalogue = $loader->load($resource, 'en', 'domain1');
  29. $this->assertEquals(array('foo' => 'bar'), $catalogue->all('domain1'));
  30. $this->assertEquals('en', $catalogue->getLocale());
  31. $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
  32. }
  33. public function testLoadDoesNothingIfEmpty()
  34. {
  35. $loader = new YamlFileLoader();
  36. $resource = __DIR__.'/../fixtures/empty.yml';
  37. $catalogue = $loader->load($resource, 'en', 'domain1');
  38. $this->assertEquals(array(), $catalogue->all('domain1'));
  39. $this->assertEquals('en', $catalogue->getLocale());
  40. $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
  41. }
  42. /**
  43. * @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
  44. */
  45. public function testLoadNonExistingResource()
  46. {
  47. $loader = new YamlFileLoader();
  48. $resource = __DIR__.'/../fixtures/non-existing.yml';
  49. $loader->load($resource, 'en', 'domain1');
  50. }
  51. /**
  52. * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
  53. */
  54. public function testLoadThrowsAnExceptionIfFileNotLocal()
  55. {
  56. $loader = new YamlFileLoader();
  57. $resource = 'http://example.com/resources.yml';
  58. $loader->load($resource, 'en', 'domain1');
  59. }
  60. /**
  61. * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
  62. */
  63. public function testLoadThrowsAnExceptionIfNotAnArray()
  64. {
  65. $loader = new YamlFileLoader();
  66. $resource = __DIR__.'/../fixtures/non-valid.yml';
  67. $loader->load($resource, 'en', 'domain1');
  68. }
  69. }