TestCase.php 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. if (defined('INTL_ICU_VERSION')) {
  63. return INTL_ICU_VERSION;
  64. }
  65. $reflector = new \ReflectionExtension('intl');
  66. ob_start();
  67. $reflector->info();
  68. $output = ob_get_clean();
  69. preg_match('/^ICU version => (.*)$/m', $output, $matches);
  70. self::$icuVersion = $matches[1];
  71. return self::$icuVersion;
  72. }
  73. }