PhpDumperTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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->assertStringEqualsFile(self::$fixturesPath.'/php/services1.php', $dumper->dump(), '->dump() dumps an empty container as an empty PHP class');
  23. $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');
  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->assertStringEqualsFile(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%', str_replace('\\','\\\\',self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR), 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. $dumper->dump();
  42. $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  43. } catch (\Exception $e) {
  44. $this->assertInstanceOf('\RuntimeException', $e, '->dump() returns a LogicException if the dump() method has not been overriden by a children class');
  45. $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');
  46. }
  47. }
  48. public function testOverrideServiceWhenUsingADumpedContainer()
  49. {
  50. require_once self::$fixturesPath.'/php/services9.php';
  51. require_once self::$fixturesPath.'/includes/foo.php';
  52. $container = new \ProjectServiceContainer();
  53. $container->setService('bar', $bar = new \stdClass());
  54. $container->setParameter('foo_bar', 'foo_bar');
  55. $this->assertEquals($bar, $container->getBarService(), '->setService() overrides an already defined service');
  56. $this->assertEquals($bar, $container->getService('bar'), '->setService() overrides an already defined service');
  57. }
  58. }