IntlTestHelper.php 4.6 KB

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