TestCase.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\Tests\Component\Locale;
  11. abstract class TestCase extends \PHPUnit_Framework_TestCase
  12. {
  13. protected static $icuVersion = null;
  14. protected function is32Bit()
  15. {
  16. return PHP_INT_SIZE == 4;
  17. }
  18. protected function is64Bit()
  19. {
  20. return PHP_INT_SIZE == 8;
  21. }
  22. protected function isIntlExtensionLoaded()
  23. {
  24. return extension_loaded('intl');
  25. }
  26. protected function skipIfIntlExtensionIsNotLoaded()
  27. {
  28. if (!$this->isIntlExtensionLoaded()) {
  29. $this->markTestSkipped('The intl extension is not available.');
  30. }
  31. }
  32. protected function skipIfICUVersionIsTooOld()
  33. {
  34. if ($this->isLowerThanIcuVersion('4.0')) {
  35. $this->markTestSkipped('Please upgrade ICU version to 4+');
  36. }
  37. }
  38. protected function skipIfNot32Bit()
  39. {
  40. if (!$this->is32Bit()) {
  41. $this->markTestSkipped('PHP must be compiled in 32 bit mode to run this test');
  42. }
  43. }
  44. protected function skipIfNot64Bit()
  45. {
  46. if (!$this->is64Bit()) {
  47. $this->markTestSkipped('PHP must be compiled in 64 bit mode to run this test');
  48. }
  49. }
  50. protected function isGreaterOrEqualThanPhpVersion($version)
  51. {
  52. return version_compare(\PHP_VERSION, $version, '>=');
  53. }
  54. protected function isGreaterOrEqualThanIcuVersion($version)
  55. {
  56. $version = $this->normalizeIcuVersion($version);
  57. $icuVersion = $this->normalizeIcuVersion($this->getIntlExtensionIcuVersion());
  58. return $icuVersion >= $version;
  59. }
  60. protected function isLowerThanIcuVersion($version)
  61. {
  62. $version = $this->normalizeIcuVersion($version);
  63. $icuVersion = $this->normalizeIcuVersion($this->getIntlExtensionIcuVersion());
  64. return $icuVersion < $version;
  65. }
  66. protected function normalizeIcuVersion($version)
  67. {
  68. return ((float) $version) * 100;
  69. }
  70. protected function getIntlExtensionIcuVersion()
  71. {
  72. if (isset(self::$icuVersion)) {
  73. return self::$icuVersion;
  74. }
  75. if (!$this->isIntlExtensionLoaded()) {
  76. throw new \RuntimeException('The intl extension is not available');
  77. }
  78. if (defined('INTL_ICU_VERSION')) {
  79. return INTL_ICU_VERSION;
  80. }
  81. $reflector = new \ReflectionExtension('intl');
  82. ob_start();
  83. $reflector->info();
  84. $output = ob_get_clean();
  85. preg_match('/^ICU version => (.*)$/m', $output, $matches);
  86. self::$icuVersion = $matches[1];
  87. return self::$icuVersion;
  88. }
  89. }