PhpDumperTest.php 4.0 KB

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