aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/symfony/intl/Symfony/Component/Intl/Util/IntlTestHelper.php
blob: cace36c6f5bedf24790f12ab24341e35b514d629 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Intl\Util;

use Symfony\Component\Intl\Intl;

/**
 * Helper class for preparing test cases that rely on the Intl component.
 *
 * Any test that tests functionality relying on either the intl classes or
 * the resource bundle data should call either of the methods
 * {@link requireIntl()} or {@link requireFullIntl()}. Calling
 * {@link requireFullIntl()} is only necessary if you use functionality in the
 * test that is not provided by the stub intl implementation.
 *
 * @author Bernhard Schussek <bschussek@gmail.com>
 */
class IntlTestHelper
{
    /**
     * Should be called before tests that work fine with the stub implementation.
     *
     * @param \PhpUnit_Framework_TestCase $testCase
     */
    public static function requireIntl(\PhpUnit_Framework_TestCase $testCase)
    {
        // We only run tests if the version is *one specific version*.
        // This condition is satisfied if
        //
        //   * the intl extension is loaded with version Intl::getIcuStubVersion()
        //   * the intl extension is not loaded

        if (IcuVersion::compare(Intl::getIcuVersion(), Intl::getIcuStubVersion(), '!=', 1)) {
            $testCase->markTestSkipped('Please change ICU version to ' . Intl::getIcuStubVersion());
        }

        if (IcuVersion::compare(Intl::getIcuDataVersion(), Intl::getIcuStubVersion(), '!=', 1)) {
            $testCase->markTestSkipped('Please change the Icu component to version 1.0.x or 1.' . IcuVersion::normalize(Intl::getIcuStubVersion(), 1) . '.x');
        }

        // Normalize the default locale in case this is not done explicitly
        // in the test
        \Locale::setDefault('en');

        // Consequently, tests will
        //
        //   * run only for one ICU version (see Intl::getIcuStubVersion())
        //     there is no need to add control structures to your tests that
        //     change the test depending on the ICU version.
        //
        // Tests should only rely on functionality that is implemented in the
        // stub classes.
    }

    /**
     * Should be called before tests that require a feature-complete intl
     * implementation.
     *
     * @param \PhpUnit_Framework_TestCase $testCase
     */
    public static function requireFullIntl(\PhpUnit_Framework_TestCase $testCase)
    {
        // We only run tests if the intl extension is loaded...
        if (!Intl::isExtensionLoaded()) {
            $testCase->markTestSkipped('The intl extension is not available.');
        }

        // ... and only if the version is *one specific version* ...
        if (IcuVersion::compare(Intl::getIcuVersion(), Intl::getIcuStubVersion(), '!=', 1)) {
            $testCase->markTestSkipped('Please change ICU version to ' . Intl::getIcuStubVersion());
        }

        // ... and only if the data in the Icu component matches that version.
        if (IcuVersion::compare(Intl::getIcuDataVersion(), Intl::getIcuStubVersion(), '!=', 1)) {
            $testCase->markTestSkipped('Please change the Icu component to version 1.0.x or 1.' . IcuVersion::normalize(Intl::getIcuStubVersion(), 1) . '.x');
        }

        // Normalize the default locale in case this is not done explicitly
        // in the test
        \Locale::setDefault('en');

        // Consequently, tests will
        //
        //   * run only for one ICU version (see Intl::getIcuStubVersion())
        //     there is no need to add control structures to your tests that
        //     change the test depending on the ICU version.
        //   * always use the C intl classes
        //   * always use the binary resource bundles (any locale is allowed)
    }

    /**
     * Skips the test unless the current system has a 32bit architecture.
     *
     * @param \PhpUnit_Framework_TestCase $testCase
     */
    public static function require32Bit(\PhpUnit_Framework_TestCase $testCase)
    {
        if (4 !== PHP_INT_SIZE) {
            $testCase->markTestSkipped('PHP must be compiled in 32 bit mode to run this test');
        }
    }

    /**
     * Skips the test unless the current system has a 64bit architecture.
     *
     * @param \PhpUnit_Framework_TestCase $testCase
     */
    public static function require64Bit(\PhpUnit_Framework_TestCase $testCase)
    {
        if (8 !== PHP_INT_SIZE) {
            $testCase->markTestSkipped('PHP must be compiled in 64 bit mode to run this test');
        }
    }

    /**
     * Must not be instantiated.
     */
    private function __construct() {}
}