YamlDumperTest.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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\Dumper;
  11. use Symfony\Component\DependencyInjection\ContainerBuilder;
  12. use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
  13. use Symfony\Component\DependencyInjection\InterfaceInjector;
  14. class YamlDumperTest extends \PHPUnit_Framework_TestCase
  15. {
  16. static protected $fixturesPath;
  17. static public function setUpBeforeClass()
  18. {
  19. self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
  20. }
  21. public function testDump()
  22. {
  23. $dumper = new YamlDumper($container = new ContainerBuilder());
  24. $this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services1.yml', $dumper->dump(), '->dump() dumps an empty container as an empty YAML file');
  25. $container = new ContainerBuilder();
  26. $dumper = new YamlDumper($container);
  27. }
  28. public function testAddParameters()
  29. {
  30. $container = include self::$fixturesPath.'/containers/container8.php';
  31. $dumper = new YamlDumper($container);
  32. $this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services8.yml', $dumper->dump(), '->dump() dumps parameters');
  33. }
  34. public function testAddService()
  35. {
  36. $container = include self::$fixturesPath.'/containers/container9.php';
  37. $dumper = new YamlDumper($container);
  38. $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');
  39. $dumper = new YamlDumper($container = new ContainerBuilder());
  40. $container->register('foo', 'FooClass')->addArgument(new \stdClass());
  41. try {
  42. $dumper->dump();
  43. $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  44. } catch (\Exception $e) {
  45. $this->assertInstanceOf('\RuntimeException', $e, '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  46. $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');
  47. }
  48. }
  49. public function testInterfaceInjectors()
  50. {
  51. $interfaceInjector = new InterfaceInjector('FooClass');
  52. $interfaceInjector->addMethodCall('setBar', array('someValue'));
  53. $container = include self::$fixturesPath.'/containers/interfaces1.php';
  54. $container->addInterfaceInjector($interfaceInjector);
  55. $dumper = new YamlDumper($container);
  56. $classBody = $dumper->dump();
  57. //TODO: find a better way to test dumper
  58. //var_dump($classBody);
  59. $this->assertEquals("parameters:
  60. cla: Fo
  61. ss: Class
  62. interfaces:
  63. FooClass:
  64. calls:
  65. - [setBar, [someValue]]
  66. services:
  67. foo:
  68. class: %cla%o%ss%
  69. ", $classBody);
  70. }
  71. }