PhpDumperTest.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\Components\DependencyInjection\Dumper;
  10. use Symfony\Components\DependencyInjection\Builder;
  11. use Symfony\Components\DependencyInjection\Dumper\PhpDumper;
  12. use Symfony\Components\DependencyInjection\ParameterBag\ParameterBag;
  13. use Symfony\Components\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 Builder());
  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 Builder();
  27. $dumper = new PhpDumper($container);
  28. }
  29. /**
  30. * @expectedException InvalidArgumentException
  31. */
  32. public function testExportParameters()
  33. {
  34. $dumper = new PhpDumper($container = new Builder(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 Builder());
  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->getBarService(), '->set() overrides an already defined service');
  66. $this->assertEquals($bar, $container->get('bar'), '->set() overrides an already defined service');
  67. }
  68. }