IniFileLoaderTest.php 2.7 KB

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