CrossCheckTest.php 3.6 KB

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