IntegrationTestKernel.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Bundle\FixturesBundle\Tests;
  4. use Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle;
  5. use Doctrine\Bundle\FixturesBundle\Tests\Fixtures\FooBundle\FooBundle;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Component\Config\Loader\LoaderInterface;
  8. use Symfony\Component\DependencyInjection\ContainerBuilder;
  9. use Symfony\Component\HttpKernel\Kernel;
  10. use function rand;
  11. use function sys_get_temp_dir;
  12. class IntegrationTestKernel extends Kernel
  13. {
  14. /** @var callable */
  15. private $servicesCallback;
  16. /** @var int */
  17. private $randomKey;
  18. public function __construct(string $environment, bool $debug)
  19. {
  20. $this->randomKey = rand(100, 999);
  21. parent::__construct($environment, $debug);
  22. }
  23. protected function getContainerClass() : string
  24. {
  25. return 'test' . $this->randomKey . parent::getContainerClass();
  26. }
  27. public function registerBundles() : array
  28. {
  29. return [
  30. new DoctrineFixturesBundle(),
  31. new FooBundle(),
  32. ];
  33. }
  34. public function addServices(callable $callback) : void
  35. {
  36. $this->servicesCallback = $callback;
  37. }
  38. public function registerContainerConfiguration(LoaderInterface $loader) : void
  39. {
  40. $loader->load(function (ContainerBuilder $c) : void {
  41. if (! $c->hasDefinition('kernel')) {
  42. $c->register('kernel', static::class)
  43. ->setSynthetic(true)
  44. ->setPublic(true);
  45. }
  46. $c->register('doctrine', ManagerRegistry::class);
  47. $callback = $this->servicesCallback;
  48. $callback($c);
  49. $c->addObjectResource($this);
  50. });
  51. }
  52. public function getCacheDir() : string
  53. {
  54. return sys_get_temp_dir() . '/doctrine_fixtures_bundle' . $this->randomKey;
  55. }
  56. public function getLogDir() : string
  57. {
  58. return sys_get_temp_dir();
  59. }
  60. }