YamlDumperTest.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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\Component\DependencyInjection\Dumper;
  10. use Symfony\Component\DependencyInjection\ContainerBuilder;
  11. use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
  12. use Symfony\Component\DependencyInjection\InterfaceInjector;
  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. public function testInterfaceInjectors()
  49. {
  50. $interfaceInjector = new InterfaceInjector('FooClass');
  51. $interfaceInjector->addMethodCall('setBar', array('someValue'));
  52. $container = include self::$fixturesPath.'/containers/interfaces1.php';
  53. $container->addInterfaceInjector($interfaceInjector);
  54. $dumper = new YamlDumper($container);
  55. $classBody = $dumper->dump();
  56. //TODO: find a better way to test dumper
  57. //var_dump($classBody);
  58. $this->assertEquals("parameters:
  59. cla: Fo
  60. ss: Class
  61. interfaces:
  62. FooClass:
  63. calls:
  64. - [setBar, [someValue]]
  65. services:
  66. foo:
  67. class: %cla%o%ss%
  68. ", $classBody);
  69. }
  70. }