YamlDumperTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\Component\DependencyInjection\Tests\Dumper;
  11. use Symfony\Component\DependencyInjection\ContainerBuilder;
  12. use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
  13. class YamlDumperTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected static $fixturesPath;
  16. protected function setUp()
  17. {
  18. if (!class_exists('Symfony\Component\Yaml\Yaml')) {
  19. $this->markTestSkipped('The "Yaml" component is not available');
  20. }
  21. }
  22. public static function setUpBeforeClass()
  23. {
  24. self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
  25. }
  26. public function testDump()
  27. {
  28. $dumper = new YamlDumper($container = new ContainerBuilder());
  29. $this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services1.yml', $dumper->dump(), '->dump() dumps an empty container as an empty YAML file');
  30. $container = new ContainerBuilder();
  31. $dumper = new YamlDumper($container);
  32. }
  33. public function testAddParameters()
  34. {
  35. $container = include self::$fixturesPath.'/containers/container8.php';
  36. $dumper = new YamlDumper($container);
  37. $this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services8.yml', $dumper->dump(), '->dump() dumps parameters');
  38. }
  39. public function testAddService()
  40. {
  41. $container = include self::$fixturesPath.'/containers/container9.php';
  42. $dumper = new YamlDumper($container);
  43. $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');
  44. $dumper = new YamlDumper($container = new ContainerBuilder());
  45. $container->register('foo', 'FooClass')->addArgument(new \stdClass());
  46. try {
  47. $dumper->dump();
  48. $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  49. } catch (\Exception $e) {
  50. $this->assertInstanceOf('\RuntimeException', $e, '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  51. $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');
  52. }
  53. }
  54. }