YamlDumperTest.php 2.4 KB

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