IniFileLoaderTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Tests\Component\DependencyInjection\Loader;
  10. use Symfony\Component\DependencyInjection\ContainerBuilder;
  11. use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
  12. class IniFileLoaderTest extends \PHPUnit_Framework_TestCase
  13. {
  14. static protected $fixturesPath;
  15. static public function setUpBeforeClass()
  16. {
  17. self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
  18. }
  19. /**
  20. * @covers Symfony\Component\DependencyInjection\Loader\IniFileLoader::__construct
  21. * @covers Symfony\Component\DependencyInjection\Loader\IniFileLoader::load
  22. */
  23. public function testLoader()
  24. {
  25. $container = new ContainerBuilder();
  26. $loader = new IniFileLoader($container, self::$fixturesPath.'/ini');
  27. $loader->load('parameters.ini');
  28. $this->assertEquals(array('foo' => 'bar', 'bar' => '%foo%'), $container->getParameterBag()->all(), '->load() takes a single file name as its first argument');
  29. try {
  30. $loader->load('foo.ini');
  31. $this->fail('->load() throws an InvalidArgumentException if the loaded file does not exist');
  32. } catch (\Exception $e) {
  33. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not exist');
  34. $this->assertStringStartsWith('The file "foo.ini" does not exist (in: ', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not exist');
  35. }
  36. try {
  37. @$loader->load('nonvalid.ini');
  38. $this->fail('->load() throws an InvalidArgumentException if the loaded file is not parseable');
  39. } catch (\Exception $e) {
  40. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file is not parseable');
  41. $this->assertEquals('The nonvalid.ini file is not valid.', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file is not parseable');
  42. }
  43. }
  44. /**
  45. * @covers Symfony\Component\DependencyInjection\Loader\IniFileLoader::supports
  46. */
  47. public function testSupports()
  48. {
  49. $loader = new IniFileLoader(new ContainerBuilder());
  50. $this->assertTrue($loader->supports('foo.ini'), '->supports() returns true if the resource is loadable');
  51. $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  52. }
  53. }