ConfigDataCollectorTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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\HttpKernel\Tests\DataCollector;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Config\Loader\LoaderInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\DataCollector\ConfigDataCollector;
  16. use Symfony\Component\HttpKernel\Kernel;
  17. class ConfigDataCollectorTest extends TestCase
  18. {
  19. public function testCollect()
  20. {
  21. $kernel = new KernelForTest('test', true);
  22. $c = new ConfigDataCollector();
  23. $c->setKernel($kernel);
  24. $c->collect(new Request(), new Response());
  25. $this->assertSame('test', $c->getEnv());
  26. $this->assertTrue($c->isDebug());
  27. $this->assertSame('config', $c->getName());
  28. $this->assertMatchesRegularExpression('~^'.preg_quote($c->getPhpVersion(), '~').'~', \PHP_VERSION);
  29. $this->assertMatchesRegularExpression('~'.preg_quote((string) $c->getPhpVersionExtra(), '~').'$~', \PHP_VERSION);
  30. $this->assertSame(\PHP_INT_SIZE * 8, $c->getPhpArchitecture());
  31. $this->assertSame(class_exists(\Locale::class, false) && \Locale::getDefault() ? \Locale::getDefault() : 'n/a', $c->getPhpIntlLocale());
  32. $this->assertSame(date_default_timezone_get(), $c->getPhpTimezone());
  33. $this->assertSame(Kernel::VERSION, $c->getSymfonyVersion());
  34. $this->assertSame(4 === Kernel::MINOR_VERSION, $c->isSymfonyLts());
  35. $this->assertNull($c->getToken());
  36. $this->assertSame(\extension_loaded('xdebug'), $c->hasXDebug());
  37. $this->assertSame(\extension_loaded('Zend OPcache') && filter_var(\ini_get('opcache.enable'), \FILTER_VALIDATE_BOOLEAN), $c->hasZendOpcache());
  38. $this->assertSame(\extension_loaded('apcu') && filter_var(\ini_get('apc.enabled'), \FILTER_VALIDATE_BOOLEAN), $c->hasApcu());
  39. $this->assertSame(sprintf('%s.%s', Kernel::MAJOR_VERSION, Kernel::MINOR_VERSION), $c->getSymfonyMinorVersion());
  40. $this->assertContains($c->getSymfonyState(), ['eol', 'eom', 'dev', 'stable']);
  41. $eom = \DateTime::createFromFormat('d/m/Y', '01/'.Kernel::END_OF_MAINTENANCE)->format('F Y');
  42. $eol = \DateTime::createFromFormat('d/m/Y', '01/'.Kernel::END_OF_LIFE)->format('F Y');
  43. $this->assertSame($eom, $c->getSymfonyEom());
  44. $this->assertSame($eol, $c->getSymfonyEol());
  45. }
  46. public function testCollectWithoutKernel()
  47. {
  48. $c = new ConfigDataCollector();
  49. $c->collect(new Request(), new Response());
  50. $this->assertSame('n/a', $c->getEnv());
  51. $this->assertSame('n/a', $c->isDebug());
  52. $this->assertSame('config', $c->getName());
  53. $this->assertMatchesRegularExpression('~^'.preg_quote($c->getPhpVersion(), '~').'~', \PHP_VERSION);
  54. $this->assertMatchesRegularExpression('~'.preg_quote((string) $c->getPhpVersionExtra(), '~').'$~', \PHP_VERSION);
  55. $this->assertSame(\PHP_INT_SIZE * 8, $c->getPhpArchitecture());
  56. $this->assertSame(class_exists(\Locale::class, false) && \Locale::getDefault() ? \Locale::getDefault() : 'n/a', $c->getPhpIntlLocale());
  57. $this->assertSame(date_default_timezone_get(), $c->getPhpTimezone());
  58. $this->assertSame(Kernel::VERSION, $c->getSymfonyVersion());
  59. $this->assertSame(4 === Kernel::MINOR_VERSION, $c->isSymfonyLts());
  60. $this->assertNull($c->getToken());
  61. $this->assertSame(\extension_loaded('xdebug'), $c->hasXDebug());
  62. $this->assertSame(\extension_loaded('Zend OPcache') && filter_var(\ini_get('opcache.enable'), \FILTER_VALIDATE_BOOLEAN), $c->hasZendOpcache());
  63. $this->assertSame(\extension_loaded('apcu') && filter_var(\ini_get('apc.enabled'), \FILTER_VALIDATE_BOOLEAN), $c->hasApcu());
  64. $this->assertSame(sprintf('%s.%s', Kernel::MAJOR_VERSION, Kernel::MINOR_VERSION), $c->getSymfonyMinorVersion());
  65. $this->assertContains($c->getSymfonyState(), ['eol', 'eom', 'dev', 'stable']);
  66. $eom = \DateTime::createFromFormat('d/m/Y', '01/'.Kernel::END_OF_MAINTENANCE)->format('F Y');
  67. $eol = \DateTime::createFromFormat('d/m/Y', '01/'.Kernel::END_OF_LIFE)->format('F Y');
  68. $this->assertSame($eom, $c->getSymfonyEom());
  69. $this->assertSame($eol, $c->getSymfonyEol());
  70. }
  71. }
  72. class KernelForTest extends Kernel
  73. {
  74. public function registerBundles(): iterable
  75. {
  76. }
  77. public function getBundles(): array
  78. {
  79. return [];
  80. }
  81. public function registerContainerConfiguration(LoaderInterface $loader)
  82. {
  83. }
  84. }