IniFileLoaderTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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('array.ini');
  34. $this->assertEquals(array('foo' => 'bar', 'versions' => array(1, 2, 3)), $container->getParameterBag()->all(), '->load() takes a single file name as its first argument');
  35. $container = new ContainerBuilder();
  36. $loader = new IniFileLoader($container, new FileLocator(self::$fixturesPath.'/ini'));
  37. $loader->load('boolean.ini');
  38. $this->assertEquals(array('foo' => true, 'bar' => false, 'boo' => null), $container->getParameterBag()->all());
  39. try {
  40. $loader->load('foo.ini');
  41. $this->fail('->load() throws an InvalidArgumentException if the loaded file does not exist');
  42. } catch (\Exception $e) {
  43. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not exist');
  44. $this->assertStringStartsWith('The file "foo.ini" does not exist (in: ', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not exist');
  45. }
  46. try {
  47. @$loader->load('nonvalid.ini');
  48. $this->fail('->load() throws an InvalidArgumentException if the loaded file is not parseable');
  49. } catch (\Exception $e) {
  50. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file is not parseable');
  51. $this->assertEquals('The "nonvalid.ini" file is not valid.', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file is not parseable');
  52. }
  53. }
  54. /**
  55. * @covers Symfony\Component\DependencyInjection\Loader\IniFileLoader::supports
  56. */
  57. public function testSupports()
  58. {
  59. $loader = new IniFileLoader(new ContainerBuilder(), new FileLocator());
  60. $this->assertTrue($loader->supports('foo.ini'), '->supports() returns true if the resource is loadable');
  61. $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  62. }
  63. }