IntlTestHelper.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\Component\Intl\Util;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Intl\Intl;
  13. /**
  14. * Helper class for preparing test cases that rely on the Intl component.
  15. *
  16. * Any test that tests functionality relying on either the intl classes or
  17. * the resource bundle data should call either of the methods
  18. * {@link requireIntl()} or {@link requireFullIntl()}. Calling
  19. * {@link requireFullIntl()} is only necessary if you use functionality in the
  20. * test that is not provided by the stub intl implementation.
  21. *
  22. * @author Bernhard Schussek <bschussek@gmail.com>
  23. */
  24. class IntlTestHelper
  25. {
  26. /**
  27. * Should be called before tests that work fine with the stub implementation.
  28. */
  29. public static function requireIntl(TestCase $testCase, $minimumIcuVersion = null)
  30. {
  31. if (null === $minimumIcuVersion) {
  32. $minimumIcuVersion = Intl::getIcuStubVersion();
  33. }
  34. // We only run tests if the version is *one specific version*.
  35. // This condition is satisfied if
  36. //
  37. // * the intl extension is loaded with version Intl::getIcuStubVersion()
  38. // * the intl extension is not loaded
  39. if ($minimumIcuVersion && IcuVersion::compare(Intl::getIcuVersion(), $minimumIcuVersion, '<', 1)) {
  40. $testCase->markTestSkipped('ICU version '.$minimumIcuVersion.' is required.');
  41. }
  42. // Normalize the default locale in case this is not done explicitly
  43. // in the test
  44. \Locale::setDefault('en');
  45. // Consequently, tests will
  46. //
  47. // * run only for one ICU version (see Intl::getIcuStubVersion())
  48. // there is no need to add control structures to your tests that
  49. // change the test depending on the ICU version.
  50. //
  51. // Tests should only rely on functionality that is implemented in the
  52. // stub classes.
  53. }
  54. /**
  55. * Should be called before tests that require a feature-complete intl
  56. * implementation.
  57. */
  58. public static function requireFullIntl(TestCase $testCase, $minimumIcuVersion = null)
  59. {
  60. // We only run tests if the intl extension is loaded...
  61. if (!Intl::isExtensionLoaded()) {
  62. $testCase->markTestSkipped('Extension intl is required.');
  63. }
  64. self::requireIntl($testCase, $minimumIcuVersion);
  65. // Consequently, tests will
  66. //
  67. // * run only for one ICU version (see Intl::getIcuStubVersion())
  68. // there is no need to add control structures to your tests that
  69. // change the test depending on the ICU version.
  70. // * always use the C intl classes
  71. }
  72. /**
  73. * Skips the test unless the current system has a 32bit architecture.
  74. */
  75. public static function require32Bit(TestCase $testCase)
  76. {
  77. if (4 !== \PHP_INT_SIZE) {
  78. $testCase->markTestSkipped('PHP 32 bit is required.');
  79. }
  80. }
  81. /**
  82. * Skips the test unless the current system has a 64bit architecture.
  83. */
  84. public static function require64Bit(TestCase $testCase)
  85. {
  86. if (8 !== \PHP_INT_SIZE) {
  87. $testCase->markTestSkipped('PHP 64 bit is required.');
  88. }
  89. }
  90. /**
  91. * Must not be instantiated.
  92. */
  93. private function __construct()
  94. {
  95. }
  96. }