PhpDumperTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\PhpDumper;
  13. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. use Symfony\Component\DependencyInjection\Definition;
  16. class PhpDumperTest extends \PHPUnit_Framework_TestCase
  17. {
  18. static protected $fixturesPath;
  19. static public function setUpBeforeClass()
  20. {
  21. self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
  22. }
  23. public function testDump()
  24. {
  25. $dumper = new PhpDumper($container = new ContainerBuilder());
  26. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services1.php', $dumper->dump(), '->dump() dumps an empty container as an empty PHP class');
  27. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services1-1.php', $dumper->dump(array('class' => 'Container', 'base_class' => 'AbstractContainer')), '->dump() takes a class and a base_class options');
  28. $container = new ContainerBuilder();
  29. new PhpDumper($container);
  30. }
  31. public function testDumpOptimizationString()
  32. {
  33. $definition = new Definition();
  34. $definition->setClass('stdClass');
  35. $definition->addArgument(array(
  36. 'only dot' => '.',
  37. 'concatenation as value' => '.\'\'.',
  38. 'concatenation from the start value' => '\'\'.',
  39. '.' => 'dot as a key',
  40. '.\'\'.' => 'concatenation as a key',
  41. '\'\'.' =>'concatenation from the start key',
  42. 'optimize concatenation' => "string1%some_string%string2",
  43. 'optimize concatenation with empty string' => "string1%empty_value%string2",
  44. 'optimize concatenation from the start' => '%empty_value%start',
  45. 'optimize concatenation at the end' => 'end%empty_value%',
  46. ));
  47. $container = new ContainerBuilder();
  48. $container->setDefinition('test', $definition);
  49. $container->setParameter('empty_value', '');
  50. $container->setParameter('some_string', '-');
  51. $container->compile();
  52. $dumper = new PhpDumper($container);
  53. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services10.php', $dumper->dump(), '->dump() dumps an empty container as an empty PHP class');
  54. }
  55. /**
  56. * @expectedException InvalidArgumentException
  57. */
  58. public function testExportParameters()
  59. {
  60. $dumper = new PhpDumper(new ContainerBuilder(new ParameterBag(array('foo' => new Reference('foo')))));
  61. $dumper->dump();
  62. }
  63. public function testAddParameters()
  64. {
  65. $container = include self::$fixturesPath.'/containers/container8.php';
  66. $dumper = new PhpDumper($container);
  67. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services8.php', $dumper->dump(), '->dump() dumps parameters');
  68. }
  69. public function testAddService()
  70. {
  71. $container = include self::$fixturesPath.'/containers/container9.php';
  72. $dumper = new PhpDumper($container);
  73. $this->assertEquals(str_replace('%path%', str_replace('\\','\\\\',self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR), file_get_contents(self::$fixturesPath.'/php/services9.php')), $dumper->dump(), '->dump() dumps services');
  74. $dumper = new PhpDumper($container = new ContainerBuilder());
  75. $container->register('foo', 'FooClass')->addArgument(new \stdClass());
  76. try {
  77. $dumper->dump();
  78. $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  79. } catch (\Exception $e) {
  80. $this->assertInstanceOf('\RuntimeException', $e, '->dump() returns a LogicException if the dump() method has not been overridden by a children class');
  81. $this->assertEquals('Unable to dump a service container if a parameter is an object or a resource.', $e->getMessage(), '->dump() returns a LogicException if the dump() method has not been overridden by a children class');
  82. }
  83. }
  84. public function testOverrideServiceWhenUsingADumpedContainer()
  85. {
  86. require_once self::$fixturesPath.'/php/services9.php';
  87. require_once self::$fixturesPath.'/includes/foo.php';
  88. $container = new \ProjectServiceContainer();
  89. $container->set('bar', $bar = new \stdClass());
  90. $container->setParameter('foo_bar', 'foo_bar');
  91. $this->assertEquals($bar, $container->get('bar'), '->set() overrides an already defined service');
  92. }
  93. public function testOverrideServiceWhenUsingADumpedContainerAndServiceIsUsedFromAnotherOne()
  94. {
  95. require_once self::$fixturesPath.'/php/services9.php';
  96. require_once self::$fixturesPath.'/includes/foo.php';
  97. require_once self::$fixturesPath.'/includes/classes.php';
  98. $container = new \ProjectServiceContainer();
  99. $container->set('bar', $bar = new \stdClass());
  100. $this->assertSame($bar, $container->get('foo')->bar, '->set() overrides an already defined service');
  101. }
  102. }