YamlDumperTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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\Components\DependencyInjection\Dumper;
  10. use Symfony\Components\DependencyInjection\Builder;
  11. use Symfony\Components\DependencyInjection\Dumper\YamlDumper;
  12. class YamlDumperTest extends \PHPUnit_Framework_TestCase
  13. {
  14. static protected $fixturesPath;
  15. static public function setUpBeforeClass()
  16. {
  17. self::$fixturesPath = realpath(__DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/');
  18. }
  19. public function testDump()
  20. {
  21. $dumper = new YamlDumper($container = new Builder());
  22. $this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services1.yml', $dumper->dump(), '->dump() dumps an empty container as an empty YAML file');
  23. $container = new Builder();
  24. $dumper = new YamlDumper($container);
  25. }
  26. public function testAddParameters()
  27. {
  28. $container = include self::$fixturesPath.'/containers/container8.php';
  29. $dumper = new YamlDumper($container);
  30. $this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services8.yml', $dumper->dump(), '->dump() dumps parameters');
  31. }
  32. public function testAddService()
  33. {
  34. $container = include self::$fixturesPath.'/containers/container9.php';
  35. $dumper = new YamlDumper($container);
  36. $this->assertEquals(str_replace('%path%', self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR, file_get_contents(self::$fixturesPath.'/yaml/services9.yml')), $dumper->dump(), '->dump() dumps services');
  37. $dumper = new YamlDumper($container = new Builder());
  38. $container->register('foo', 'FooClass')->addArgument(new \stdClass());
  39. try {
  40. $dumper->dump();
  41. $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  42. } catch (\Exception $e) {
  43. $this->assertInstanceOf('\RuntimeException', $e, '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  44. $this->assertEquals('Unable to dump a service container if a parameter is an object or a resource.', $e->getMessage(), '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  45. }
  46. }
  47. }