TestCase.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 isGreaterOrEqualThanIcuVersion($version)
  39. {
  40. $version = $this->normalizeIcuVersion($version);
  41. $icuVersion = $this->normalizeIcuVersion($this->getIntlExtensionIcuVersion());
  42. return $icuVersion >= $version;
  43. }
  44. protected function isLowerThanIcuVersion($version)
  45. {
  46. $version = $this->normalizeIcuVersion($version);
  47. $icuVersion = $this->normalizeIcuVersion($this->getIntlExtensionIcuVersion());
  48. return $icuVersion < $version;
  49. }
  50. protected function normalizeIcuVersion($version)
  51. {
  52. return ((float) $version) * 100;
  53. }
  54. protected function getIntlExtensionIcuVersion()
  55. {
  56. if (isset(self::$icuVersion)) {
  57. return self::$icuVersion;
  58. }
  59. if (!$this->isIntlExtensionLoaded()) {
  60. throw new \RuntimeException('The intl extension is not available');
  61. }
  62. ob_start();
  63. phpinfo(INFO_MODULES);
  64. $output = ob_get_clean();
  65. preg_match('/^ICU version => (.*)$/m', $output, $matches);
  66. self::$icuVersion = $matches[1];
  67. return self::$icuVersion;
  68. }
  69. }