PhpDumperTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. class PhpDumperTest extends \PHPUnit_Framework_TestCase
  13. {
  14. static protected $fixturesPath;
  15. static public function setUpBeforeClass()
  16. {
  17. self::$fixturesPath = realpath(__DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/');
  18. }
  19. public function testDump()
  20. {
  21. $dumper = new PhpDumper($container = new Builder());
  22. $this->assertEquals(file_get_contents(self::$fixturesPath.'/php/services1.php'), $dumper->dump(), '->dump() dumps an empty container as an empty PHP class');
  23. $this->assertEquals(file_get_contents(self::$fixturesPath.'/php/services1-1.php'), $dumper->dump(array('class' => 'Container', 'base_class' => 'AbstractContainer')), '->dump() takes a class and a base_class options');
  24. $container = new Builder();
  25. $dumper = new PhpDumper($container);
  26. }
  27. public function testAddParameters()
  28. {
  29. $container = include self::$fixturesPath.'/containers/container8.php';
  30. $dumper = new PhpDumper($container);
  31. $this->assertEquals(file_get_contents(self::$fixturesPath.'/php/services8.php'), $dumper->dump(), '->dump() dumps parameters');
  32. }
  33. public function testAddService()
  34. {
  35. $container = include self::$fixturesPath.'/containers/container9.php';
  36. $dumper = new PhpDumper($container);
  37. $this->assertEquals(str_replace('%path%', self::$fixturesPath.'/includes', file_get_contents(self::$fixturesPath.'/php/services9.php')), $dumper->dump(), '->dump() dumps services');
  38. $dumper = new PhpDumper($container = new Builder());
  39. $container->register('foo', 'FooClass')->addArgument(new \stdClass());
  40. try
  41. {
  42. $dumper->dump();
  43. $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  44. }
  45. catch (\RuntimeException $e)
  46. {
  47. }
  48. }
  49. public function testOverrideServiceWhenUsingADumpedContainer()
  50. {
  51. require_once self::$fixturesPath.'/php/services9.php';
  52. require_once self::$fixturesPath.'/includes/foo.php';
  53. $container = new \ProjectServiceContainer();
  54. $container->setService('bar', $bar = new \stdClass());
  55. $container->setParameter('foo_bar', 'foo_bar');
  56. $this->assertEquals($bar, $container->getBarService(), '->setService() overrides an already defined service');
  57. $this->assertEquals($bar, $container->getService('bar'), '->setService() overrides an already defined service');
  58. }
  59. }