TestCase.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 isGreaterOrEqualThanIcuVersion($version)
  51. {
  52. $version = $this->normalizeIcuVersion($version);
  53. $icuVersion = $this->normalizeIcuVersion($this->getIntlExtensionIcuVersion());
  54. return $icuVersion >= $version;
  55. }
  56. protected function isLowerThanIcuVersion($version)
  57. {
  58. $version = $this->normalizeIcuVersion($version);
  59. $icuVersion = $this->normalizeIcuVersion($this->getIntlExtensionIcuVersion());
  60. return $icuVersion < $version;
  61. }
  62. protected function normalizeIcuVersion($version)
  63. {
  64. return ((float) $version) * 100;
  65. }
  66. protected function getIntlExtensionIcuVersion()
  67. {
  68. if (isset(self::$icuVersion)) {
  69. return self::$icuVersion;
  70. }
  71. if (!$this->isIntlExtensionLoaded()) {
  72. throw new \RuntimeException('The intl extension is not available');
  73. }
  74. if (defined('INTL_ICU_VERSION')) {
  75. return INTL_ICU_VERSION;
  76. }
  77. $reflector = new \ReflectionExtension('intl');
  78. ob_start();
  79. $reflector->info();
  80. $output = ob_get_clean();
  81. preg_match('/^ICU version => (.*)$/m', $output, $matches);
  82. self::$icuVersion = $matches[1];
  83. return self::$icuVersion;
  84. }
  85. }