CrossCheckTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\Component\DependencyInjection\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Config\FileLocator;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. class CrossCheckTest extends TestCase
  15. {
  16. protected static $fixturesPath;
  17. public static function setUpBeforeClass()
  18. {
  19. self::$fixturesPath = __DIR__.'/Fixtures/';
  20. require_once self::$fixturesPath.'/includes/classes.php';
  21. require_once self::$fixturesPath.'/includes/foo.php';
  22. }
  23. /**
  24. * @dataProvider crossCheckLoadersDumpers
  25. */
  26. public function testCrossCheck($fixture, $type)
  27. {
  28. $loaderClass = 'Symfony\\Component\\DependencyInjection\\Loader\\'.ucfirst($type).'FileLoader';
  29. $dumperClass = 'Symfony\\Component\\DependencyInjection\\Dumper\\'.ucfirst($type).'Dumper';
  30. $tmp = tempnam(sys_get_temp_dir(), 'sf');
  31. copy(self::$fixturesPath.'/'.$type.'/'.$fixture, $tmp);
  32. $container1 = new ContainerBuilder();
  33. $loader1 = new $loaderClass($container1, new FileLocator());
  34. $loader1->load($tmp);
  35. $dumper = new $dumperClass($container1);
  36. file_put_contents($tmp, $dumper->dump());
  37. $container2 = new ContainerBuilder();
  38. $loader2 = new $loaderClass($container2, new FileLocator());
  39. $loader2->load($tmp);
  40. unlink($tmp);
  41. $this->assertEquals($container2->getAliases(), $container1->getAliases(), 'loading a dump from a previously loaded container returns the same container');
  42. $this->assertEquals($container2->getDefinitions(), $container1->getDefinitions(), 'loading a dump from a previously loaded container returns the same container');
  43. $this->assertEquals($container2->getParameterBag()->all(), $container1->getParameterBag()->all(), '->getParameterBag() returns the same value for both containers');
  44. $this->assertEquals(serialize($container2), serialize($container1), 'loading a dump from a previously loaded container returns the same container');
  45. $services1 = array();
  46. foreach ($container1 as $id => $service) {
  47. $services1[$id] = serialize($service);
  48. }
  49. $services2 = array();
  50. foreach ($container2 as $id => $service) {
  51. $services2[$id] = serialize($service);
  52. }
  53. unset($services1['service_container'], $services2['service_container']);
  54. $this->assertEquals($services2, $services1, 'Iterator on the containers returns the same services');
  55. }
  56. public function crossCheckLoadersDumpers()
  57. {
  58. return array(
  59. array('services1.xml', 'xml'),
  60. array('services2.xml', 'xml'),
  61. array('services6.xml', 'xml'),
  62. array('services8.xml', 'xml'),
  63. array('services9.xml', 'xml'),
  64. array('services1.yml', 'yaml'),
  65. array('services2.yml', 'yaml'),
  66. array('services6.yml', 'yaml'),
  67. array('services8.yml', 'yaml'),
  68. array('services9.yml', 'yaml'),
  69. );
  70. }
  71. }