TestCase.php 2.2 KB

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