PhpDumperTest.php 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\PhpDumper;
  12. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  13. use Symfony\Component\DependencyInjection\Reference;
  14. class PhpDumperTest 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 PhpDumper($container = new ContainerBuilder());
  24. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services1.php', $dumper->dump(), '->dump() dumps an empty container as an empty PHP class');
  25. $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');
  26. $container = new ContainerBuilder();
  27. $dumper = new PhpDumper($container);
  28. }
  29. /**
  30. * @expectedException InvalidArgumentException
  31. */
  32. public function testExportParameters()
  33. {
  34. $dumper = new PhpDumper($container = new ContainerBuilder(new ParameterBag(array('foo' => new Reference('foo')))));
  35. $dumper->dump();
  36. }
  37. public function testAddParameters()
  38. {
  39. $container = include self::$fixturesPath.'/containers/container8.php';
  40. $dumper = new PhpDumper($container);
  41. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services8.php', $dumper->dump(), '->dump() dumps parameters');
  42. }
  43. public function testAddService()
  44. {
  45. $container = include self::$fixturesPath.'/containers/container9.php';
  46. $dumper = new PhpDumper($container);
  47. $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');
  48. $dumper = new PhpDumper($container = new ContainerBuilder());
  49. $container->register('foo', 'FooClass')->addArgument(new \stdClass());
  50. try {
  51. $dumper->dump();
  52. $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  53. } catch (\Exception $e) {
  54. $this->assertInstanceOf('\RuntimeException', $e, '->dump() returns a LogicException if the dump() method has not been overriden by a children class');
  55. $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 overriden by a children class');
  56. }
  57. }
  58. public function testOverrideServiceWhenUsingADumpedContainer()
  59. {
  60. require_once self::$fixturesPath.'/php/services9.php';
  61. require_once self::$fixturesPath.'/includes/foo.php';
  62. $container = new \ProjectServiceContainer();
  63. $container->set('bar', $bar = new \stdClass());
  64. $container->setParameter('foo_bar', 'foo_bar');
  65. $this->assertEquals($bar, $container->get('bar'), '->set() overrides an already defined service');
  66. }
  67. public function testOverrideServiceWhenUsingADumpedContainerAndServiceIsUsedFromAnotherOne()
  68. {
  69. require_once self::$fixturesPath.'/php/services9.php';
  70. require_once self::$fixturesPath.'/includes/foo.php';
  71. require_once self::$fixturesPath.'/includes/classes.php';
  72. $container = new \ProjectServiceContainer();
  73. $container->set('bar', $bar = new \stdClass());
  74. $this->assertSame($bar, $container->get('foo')->bar, '->set() overrides an already defined service');
  75. }
  76. }