IniFileLoaderTest.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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\Tests\Component\DependencyInjection\Loader;
  11. use Symfony\Component\DependencyInjection\ContainerBuilder;
  12. use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
  13. use Symfony\Component\Config\FileLocator;
  14. class IniFileLoaderTest extends \PHPUnit_Framework_TestCase
  15. {
  16. static protected $fixturesPath;
  17. static public function setUpBeforeClass()
  18. {
  19. self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
  20. }
  21. /**
  22. * @covers Symfony\Component\DependencyInjection\Loader\IniFileLoader::__construct
  23. * @covers Symfony\Component\DependencyInjection\Loader\IniFileLoader::load
  24. */
  25. public function testLoader()
  26. {
  27. $container = new ContainerBuilder();
  28. $loader = new IniFileLoader($container, new FileLocator(self::$fixturesPath.'/ini'));
  29. $loader->load('parameters.ini');
  30. $this->assertEquals(array('foo' => 'bar', 'bar' => '%foo%'), $container->getParameterBag()->all(), '->load() takes a single file name as its first argument');
  31. $container = new ContainerBuilder();
  32. $loader = new IniFileLoader($container, new FileLocator(self::$fixturesPath.'/ini'));
  33. $loader->load('boolean.ini');
  34. $this->assertEquals(array('foo' => true, 'bar' => false, 'boo' => null), $container->getParameterBag()->all());
  35. try {
  36. $loader->load('foo.ini');
  37. $this->fail('->load() throws an InvalidArgumentException if the loaded file does not exist');
  38. } catch (\Exception $e) {
  39. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not exist');
  40. $this->assertStringStartsWith('The file "foo.ini" does not exist (in: ', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not exist');
  41. }
  42. try {
  43. @$loader->load('nonvalid.ini');
  44. $this->fail('->load() throws an InvalidArgumentException if the loaded file is not parseable');
  45. } catch (\Exception $e) {
  46. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file is not parseable');
  47. $this->assertEquals('The "nonvalid.ini" file is not valid.', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file is not parseable');
  48. }
  49. }
  50. /**
  51. * @covers Symfony\Component\DependencyInjection\Loader\IniFileLoader::supports
  52. */
  53. public function testSupports()
  54. {
  55. $loader = new IniFileLoader(new ContainerBuilder(), new FileLocator());
  56. $this->assertTrue($loader->supports('foo.ini'), '->supports() returns true if the resource is loadable');
  57. $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  58. }
  59. }