IniFileLoaderTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. try {
  32. $loader->load('foo.ini');
  33. $this->fail('->load() throws an InvalidArgumentException if the loaded file does not exist');
  34. } catch (\Exception $e) {
  35. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not exist');
  36. $this->assertStringStartsWith('The file "foo.ini" does not exist (in: ', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not exist');
  37. }
  38. try {
  39. @$loader->load('nonvalid.ini');
  40. $this->fail('->load() throws an InvalidArgumentException if the loaded file is not parseable');
  41. } catch (\Exception $e) {
  42. $this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file is not parseable');
  43. $this->assertEquals('The nonvalid.ini file is not valid.', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file is not parseable');
  44. }
  45. }
  46. /**
  47. * @covers Symfony\Component\DependencyInjection\Loader\IniFileLoader::supports
  48. */
  49. public function testSupports()
  50. {
  51. $loader = new IniFileLoader(new ContainerBuilder(), new FileLocator());
  52. $this->assertTrue($loader->supports('foo.ini'), '->supports() returns true if the resource is loadable');
  53. $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  54. }
  55. }