xref: /PHP-7.3/ext/intl/tests/ut_common.inc (revision f9a3519f)
1<?php
2/*
3 * Run unit test in OO- and in procedural mode.
4 * Then compare the outputs.
5 * It they're equal then show one of them.
6 * Otherwise indicate an error.
7 */
8function ut_run()
9{
10    // Run unit test in OO mode.
11    $GLOBALS['oo-mode'] = true;
12    $oo_result = ut_main();
13
14    // Run unit test in procedural mode.
15    $GLOBALS['oo-mode'] = false;
16    $proc_result = ut_main();
17
18    // Show error if the APIs produce different results.
19    if( $proc_result !== $oo_result )
20    {
21          echo "ERROR: OO- and procedural APIs produce different results!\n";
22          echo "OO API output:\n";
23          echo str_repeat( '=', 78 ) . "\n";
24          echo $oo_result;
25          echo str_repeat( '=', 78 ) . "\n";
26          echo "procedural API output:\n";
27          echo str_repeat( '=', 78 ) . "\n";
28          echo $proc_result;
29          echo str_repeat( '=', 78 ) . "\n";
30          return;
31    }
32
33    // Else, if the results are equal, show one of them.
34    echo $proc_result;
35}
36
37function dump( $val )
38{
39    return var_export( $val, true );
40}
41
42/*
43 * Wrappers around Collator methods to run them in either OO- or procedural mode.
44 */
45
46function ut_coll_create( $locale )
47{
48    return $GLOBALS['oo-mode'] ? Collator::create( $locale ) : collator_create( $locale );
49}
50function ut_coll_compare( $coll, $str1, $str2 )
51{
52    return $GLOBALS['oo-mode'] ? $coll->compare( $str1, $str2 ) : collator_compare( $coll, $str1, $str2 );
53}
54function ut_coll_sort( $coll, &$arr, $sort_flag = Collator::SORT_REGULAR )
55{
56    return $GLOBALS['oo-mode'] ? $coll->sort( $arr, $sort_flag ) : collator_sort( $coll, $arr, $sort_flag );
57}
58function ut_coll_sort_with_sort_keys( $coll, &$arr )
59{
60    return $GLOBALS['oo-mode'] ? $coll->sortWithSortKeys( $arr ) : collator_sort_with_sort_keys( $coll, $arr );
61}
62function ut_coll_get_sort_key( $coll, $str )
63{
64    return $GLOBALS['oo-mode'] ? $coll->getSortKey( $str ) : collator_get_sort_key( $coll, $str );
65}
66function ut_coll_asort( $coll, &$arr, $sort_flag = Collator::SORT_REGULAR )
67{
68    return $GLOBALS['oo-mode'] ? $coll->asort( $arr, $sort_flag ) : collator_asort( $coll, $arr, $sort_flag );
69}
70function ut_coll_get_locale( $coll, $type )
71{
72    return $GLOBALS['oo-mode'] ? $coll->getLocale( $type ) : collator_get_locale( $coll, $type );
73}
74function ut_coll_get_display_name( $obj_loc, $disp_loc )
75{
76    return $GLOBALS['oo-mode'] ? Collator::getDisplayName( $obj_loc, $disp_loc ) : collator_get_display_name( $obj_loc, $disp_loc );
77}
78function ut_coll_get_available_locales()
79{
80    return $GLOBALS['oo-mode'] ? Collator::getAvailableLocales() : collator_get_available_locales();
81}
82function ut_coll_get_attribute( $coll, $attr )
83{
84    return $GLOBALS['oo-mode'] ? $coll->getAttribute( $attr ) : collator_get_attribute( $coll, $attr );
85}
86function ut_coll_get_strength( $coll )
87{
88    return $GLOBALS['oo-mode'] ? $coll->getStrength() : collator_get_strength( $coll );
89}
90function ut_coll_set_strength( $coll, $strength )
91{
92    return $GLOBALS['oo-mode'] ? $coll->setStrength( $strength ) : collator_set_strength( $coll, $strength );
93}
94function ut_coll_set_attribute( $coll, $attr, $val )
95{
96    return $GLOBALS['oo-mode'] ? $coll->setAttribute( $attr, $val ) : collator_set_attribute( $coll, $attr, $val );
97}
98function ut_coll_get_variable_top( $coll )
99{
100    return $GLOBALS['oo-mode'] ? $coll->getVariableTop() : collator_get_variable_top( $coll );
101}
102function ut_coll_set_variable_top( $coll, $var_top )
103{
104    return $GLOBALS['oo-mode'] ? $coll->setVariableTop( $var_top ) : collator_set_variable_top( $coll, $var_top );
105}
106function ut_coll_restore_variable_top( $coll, $var_top )
107{
108    return $GLOBALS['oo-mode'] ? $coll->restoreVariableTop( $var_top ) : collator_restore_variable_top( $coll, $var_top );
109}
110function ut_coll_get_error_code( $coll )
111{
112    return $GLOBALS['oo-mode'] ? $coll->getErrorCode() : collator_get_error_code( $coll );
113}
114function ut_coll_get_error_message( $coll )
115{
116    return $GLOBALS['oo-mode'] ? $coll->getErrorMessage() : collator_get_error_message( $coll );
117}
118function ut_coll_get_default()
119{
120    return $GLOBALS['oo-mode'] ? Collator::getDefault() : collator_get_default();
121}
122function ut_coll_set_default( $coll )
123{
124    return $GLOBALS['oo-mode'] ? Collator::setDefault( $coll ) : collator_set_default( $coll );
125}
126
127/*
128 * Wrappers around NumberFormatter methods to run them in either OO- or procedural mode.
129 */
130
131// FIXME: incomplete list
132
133function ut_nfmt_create( $locale, $style, $pattern = null )
134{
135    if ($GLOBALS['oo-mode']) {
136        try {
137            return new NumberFormatter( $locale, $style, $pattern );
138        } catch (Exception $e) {
139            return NULL;
140        }
141    } else {
142        return numfmt_create( $locale, $style, $pattern );
143    }
144}
145function ut_nfmt_format( $fmt, $number, $type = null )
146{
147    return $GLOBALS['oo-mode'] ? $fmt->format( $number, $type ) : numfmt_format( $fmt, $number, $type );
148}
149function ut_nfmt_parse( $fmt, $string, $type = NumberFormatter::TYPE_DOUBLE, &$position = null )
150{
151    if(is_null($position)) {
152    return $GLOBALS['oo-mode'] ? $fmt->parse( $string, $type ) : numfmt_parse( $fmt, $string, $type );
153    } else {
154    return $GLOBALS['oo-mode'] ? $fmt->parse( $string, $type, $position ) : numfmt_parse( $fmt, $string, $type, $position );
155    }
156}
157function ut_nfmt_format_currency( $fmt, $number, $currency )
158{
159    return $GLOBALS['oo-mode'] ? $fmt->formatCurrency( $number, $currency ) : numfmt_format_currency( $fmt, $number, $currency );
160}
161function ut_nfmt_parse_currency( $fmt, $string, &$currency, &$position = null )
162{
163    if(is_null($position)) {
164    return $GLOBALS['oo-mode'] ? $fmt->parseCurrency( $string, $currency ) : numfmt_parse_currency( $fmt, $string, $currency );
165    } else {
166    return $GLOBALS['oo-mode'] ? $fmt->parseCurrency( $string, $currency, $position ) : numfmt_parse_currency( $fmt, $string, $currency, $position );
167   }
168}
169function ut_nfmt_set_attribute( $fmt, $attribute, $value )
170{
171    return $GLOBALS['oo-mode'] ? $fmt->setAttribute( $attribute, $value ) : numfmt_set_attribute( $fmt, $attribute, $value );
172}
173function ut_nfmt_set_text_attribute( $fmt, $attribute, $value )
174{
175    return $GLOBALS['oo-mode'] ? $fmt->setTextAttribute( $attribute, $value ) : numfmt_set_text_attribute( $fmt, $attribute, $value );
176}
177function ut_nfmt_set_symbol( $fmt, $attribute, $value )
178{
179    return $GLOBALS['oo-mode'] ? $fmt->setSymbol( $attribute, $value ) : numfmt_set_symbol( $fmt, $attribute, $value );
180}
181function ut_nfmt_set_pattern( $fmt, $pattern )
182{
183    return $GLOBALS['oo-mode'] ? $fmt->setPattern( $pattern ) : numfmt_set_pattern( $fmt, $pattern );
184}
185function ut_nfmt_get_attribute( $fmt, $attribute )
186{
187    return $GLOBALS['oo-mode'] ? $fmt->getAttribute( $attribute ) : numfmt_get_attribute( $fmt, $attribute );
188}
189function ut_nfmt_get_text_attribute( $fmt, $attribute )
190{
191    return $GLOBALS['oo-mode'] ? $fmt->getTextAttribute( $attribute ) : numfmt_get_text_attribute( $fmt, $attribute );
192}
193function ut_nfmt_get_symbol( $fmt, $attribute )
194{
195    return $GLOBALS['oo-mode'] ? $fmt->getSymbol( $attribute ) : numfmt_get_symbol( $fmt, $attribute );
196}
197function ut_nfmt_get_pattern( $fmt )
198{
199    return $GLOBALS['oo-mode'] ? $fmt->getPattern() : numfmt_get_pattern( $fmt );
200}
201function ut_nfmt_get_locale( $fmt, $type = 0 )
202{
203    return $GLOBALS['oo-mode'] ? $fmt->getLocale( $type ) : numfmt_get_locale( $fmt, $type );
204}
205function ut_nfmt_get_error_code( $fmt )
206{
207    return $GLOBALS['oo-mode'] ? $fmt->getErrorCode() : numfmt_get_error_code( $fmt );
208}
209function ut_nfmt_get_error_message( $fmt )
210{
211    return $GLOBALS['oo-mode'] ? $fmt->getErrorMessage() : numfmt_get_error_message( $fmt );
212}
213
214function ut_norm_normalize( $str, $form )
215{
216    return $GLOBALS['oo-mode'] ? Normalizer::normalize( $str, $form ) : normalizer_normalize( $str, $form );
217}
218function ut_norm_is_normalized( $str, $form )
219{
220    return $GLOBALS['oo-mode'] ? Normalizer::isNormalized( $str, $form ) : normalizer_is_normalized( $str, $form );
221}
222function ut_norm_get_raw_decomposition( $str, $form )
223{
224    return $GLOBALS['oo-mode'] ? Normalizer::getRawDecomposition( $str, $form ) : normalizer_get_raw_decomposition( $str, $form );
225}
226
227/*
228 * Wrappers around Collator methods to run them in either OO- or procedural mode.
229 */
230
231function ut_loc_get_default( )
232{
233    return $GLOBALS['oo-mode'] ? Locale::getDefault( ) : locale_get_default();
234}
235function ut_loc_set_default( $locale  )
236{
237   	return $GLOBALS['oo-mode'] ? Locale::setDefault( $locale  ) : locale_set_default( $locale );
238}
239function ut_loc_get_primary_language( $locale )
240{
241    return $GLOBALS['oo-mode'] ? Locale::getPrimaryLanguage( $locale ) : locale_get_primary_language( $locale );
242}
243function ut_loc_get_script( $locale )
244{
245    return $GLOBALS['oo-mode'] ? Locale::getScript( $locale ) : locale_get_script( $locale );
246}
247function ut_loc_get_region( $locale )
248{
249    return $GLOBALS['oo-mode'] ? Locale::getRegion( $locale ) : locale_get_region( $locale );
250}
251function ut_loc_get_keywords( $locale )
252{
253    return $GLOBALS['oo-mode'] ? Locale::getKeywords( $locale ) : locale_get_keywords( $locale );
254}
255function ut_loc_get_display_name( $locale , $dispLocale )
256{
257    return $GLOBALS['oo-mode'] ? Locale::getDisplayName( $locale , $dispLocale ) : locale_get_display_name( $locale , $dispLocale );
258}
259function ut_loc_get_display_language( $locale , $dispLocale )
260{
261    return $GLOBALS['oo-mode'] ? Locale::getDisplayLanguage( $locale , $dispLocale ) : locale_get_display_language( $locale , $dispLocale );
262}
263function ut_loc_get_display_script( $locale , $dispLocale )
264{
265    return $GLOBALS['oo-mode'] ? Locale::getDisplayScript( $locale , $dispLocale ) : locale_get_display_script( $locale , $dispLocale );
266}
267function ut_loc_get_display_region( $locale, $dispLocale  )
268{
269    return $GLOBALS['oo-mode'] ? Locale::getDisplayRegion( $locale, $dispLocale  ) : locale_get_display_region( $locale, $dispLocale  );
270}
271function ut_loc_get_display_variant( $locale , $dispLocale )
272{
273    return $GLOBALS['oo-mode'] ? Locale::getDisplayVariant( $locale , $dispLocale ) : locale_get_display_variant( $locale, $dispLocale  );
274}
275function ut_loc_locale_compose( $loc_parts_arr )
276{
277    return $GLOBALS['oo-mode'] ? Locale::composeLocale( $loc_parts_arr ) : locale_compose( $loc_parts_arr );
278}
279function ut_loc_locale_parse( $locale )
280{
281    return $GLOBALS['oo-mode'] ? Locale::parseLocale( $locale ) : locale_parse($locale );
282}
283function ut_loc_locale_get_all_variants( $locale )
284{
285    return $GLOBALS['oo-mode'] ? Locale::getAllVariants( $locale ) : locale_get_all_variants( $locale );
286}
287function ut_loc_locale_filter_matches( $lang_tag,$loc_range  ,$isCanonical)
288{
289    return $GLOBALS['oo-mode'] ? Locale::filterMatches( $lang_tag,$loc_range ,$isCanonical) : locale_filter_matches( $lang_tag,$loc_range ,$isCanonical);
290}
291function ut_loc_canonicalize( $locale )
292{
293    return $GLOBALS['oo-mode'] ? Locale::canonicalize( $locale ) : locale_canonicalize( $locale );
294}
295function ut_loc_locale_lookup( $lang_tag_arr,$loc_range,$isCanonical,$default_loc)
296{
297    return $GLOBALS['oo-mode'] ? Locale::lookup( $lang_tag_arr,$loc_range,$isCanonical,$default_loc ) : locale_lookup( $lang_tag_arr,$loc_range,$isCanonical,$default_loc );
298}
299function ut_loc_accept_http($http) {
300	return $GLOBALS['oo-mode'] ? Locale::acceptFromHttp($http):locale_accept_from_http($http);
301}
302/* MessageFormatter functions */
303function ut_msgfmt_create( $locale, $pattern)
304{
305    return $GLOBALS['oo-mode'] ? MessageFormatter::create( $locale, $pattern ) : msgfmt_create( $locale, $pattern );
306}
307function ut_msgfmt_format( $fmt, $args )
308{
309    return $GLOBALS['oo-mode'] ? $fmt->format( $args ) : msgfmt_format( $fmt, $args);
310}
311function ut_msgfmt_parse( $fmt, $string)
312{
313    return $GLOBALS['oo-mode'] ? $fmt->parse( $string) : msgfmt_parse( $fmt, $string);
314}
315function ut_msgfmt_format_message( $locale, $pattern, $args )
316{
317    return $GLOBALS['oo-mode'] ? MessageFormatter::formatMessage( $locale, $pattern, $args ) : msgfmt_format_message( $locale, $pattern, $args );
318}
319function ut_msgfmt_parse_message( $locale, $pattern, $string )
320{
321    return $GLOBALS['oo-mode'] ? MessageFormatter::parseMessage( $locale, $pattern, $string ) : msgfmt_parse_message( $locale, $pattern, $string );
322}
323function ut_msgfmt_set_pattern( $fmt, $pattern )
324{
325    return $GLOBALS['oo-mode'] ? $fmt->setPattern( $pattern ) : msgfmt_set_pattern( $fmt, $pattern );
326}
327function ut_msgfmt_get_pattern( $fmt )
328{
329    return $GLOBALS['oo-mode'] ? $fmt->getPattern() : msgfmt_get_pattern( $fmt );
330}
331function ut_msgfmt_get_locale( $fmt )
332{
333    return $GLOBALS['oo-mode'] ? $fmt->getLocale( ) : msgfmt_get_locale( $fmt );
334}
335function ut_msgfmt_get_error_code( $fmt )
336{
337    return $GLOBALS['oo-mode'] ? $fmt->getErrorCode() : msgfmt_get_error_code( $fmt );
338}
339function ut_msgfmt_get_error_message( $fmt )
340{
341    return $GLOBALS['oo-mode'] ? $fmt->getErrorMessage() : msgfmt_get_error_message( $fmt );
342}
343/* IntlDateFormatter functions */
344function ut_datefmt_create( $locale, $datetype, $timetype, $timezone = null, $calendar = null ,$pattern = null)
345{
346    return $GLOBALS['oo-mode'] ? datefmt_create( $locale, $datetype, $timetype, $timezone, $calendar ,$pattern ) : datefmt_create( $locale, $datetype, $timetype, $timezone, $calendar ,$pattern);
347}
348function ut_datefmt_get_datetype( $fmt )
349{
350    return $GLOBALS['oo-mode'] ? $fmt->getDateType( ) : datefmt_get_datetype( $fmt );
351}
352function ut_datefmt_get_timetype( $fmt )
353{
354    return $GLOBALS['oo-mode'] ? $fmt->getTimeType( ) : datefmt_get_timetype( $fmt );
355}
356function ut_datefmt_get_calendar( $fmt )
357{
358    return $GLOBALS['oo-mode'] ? $fmt->getCalendar( ) : datefmt_get_calendar( $fmt );
359}
360function ut_datefmt_set_calendar( $fmt ,$calendar )
361{
362    return $GLOBALS['oo-mode'] ? $fmt->setCalendar( $calendar ) : datefmt_set_calendar( $fmt , $calendar );
363}
364function ut_datefmt_get_timezone_id( $fmt )
365{
366    return $GLOBALS['oo-mode'] ? $fmt->getTimeZoneId( ) : datefmt_get_timezone_id( $fmt );
367}
368function ut_datefmt_set_timezone_id( $fmt ,$timezone_id )
369{
370    return $GLOBALS['oo-mode'] ? $fmt->setTimeZone( $timezone_id ) : datefmt_set_timezone( $fmt ,$timezone_id);
371}
372function ut_datefmt_get_pattern( $fmt )
373{
374    return $GLOBALS['oo-mode'] ? $fmt->getPattern() : datefmt_get_pattern( $fmt );
375}
376function ut_datefmt_set_pattern( $fmt , $pattern )
377{
378    return $GLOBALS['oo-mode'] ? $fmt->setPattern( $pattern ) : datefmt_set_pattern( $fmt ,  $pattern);
379}
380function ut_datefmt_get_locale( $fmt ,$type=ULOC_ACTUAL_LOCALE)
381{
382    return $GLOBALS['oo-mode'] ? $fmt->getLocale($type ) : datefmt_get_locale( $fmt ,$type);
383}
384function ut_datefmt_is_lenient( $fmt )
385{
386    return $GLOBALS['oo-mode'] ? $fmt->isLenient() : datefmt_is_lenient( $fmt );
387}
388function ut_datefmt_set_lenient( $fmt , $lenient )
389{
390    return $GLOBALS['oo-mode'] ? $fmt->setLenient(  $lenient ) : datefmt_set_lenient( $fmt ,  $lenient);
391}
392function ut_datefmt_format( $fmt , $value )
393{
394    return $GLOBALS['oo-mode'] ? $fmt->format(  $value ) : datefmt_format( $fmt ,  $value);
395}
396function ut_datefmt_parse( $fmt , $value , &$parse_pos=0 )
397{
398    return $GLOBALS['oo-mode'] ? $fmt->parse(  $value ,$parse_pos ) : datefmt_parse( $fmt ,  $value,$parse_pos);
399}
400function ut_datefmt_localtime( $fmt , $value , &$parse_pos=0 )
401{
402    return $GLOBALS['oo-mode'] ? $fmt->localtime(  $value , $parse_pos ) : datefmt_localtime( $fmt ,  $value , $parse_pos );
403}
404
405function ut_resourcebundle_create( $locale, $bundle, $fallback=true )
406{
407    if ($GLOBALS['oo-mode']) {
408        try {
409            return new ResourceBundle($locale, $bundle, $fallback);
410    } catch (Exception $e) {
411            return NULL;
412        }
413    } else {
414        return resourcebundle_create($locale, $bundle, $fallback);
415    }
416}
417function ut_resourcebundle_count($bundle )
418{
419    return $GLOBALS['oo-mode'] ? $bundle->count():resourcebundle_count($bundle);
420}
421function ut_resourcebundle_locales($bundle )
422{
423    return $GLOBALS['oo-mode'] ? ResourceBundle::getLocales($bundle):resourcebundle_locales($bundle);
424}
425function ut_resourcebundle_get($bundle, $idx )
426{
427    return $GLOBALS['oo-mode'] ? $bundle->get($idx):resourcebundle_get($bundle, $idx);
428}
429function ut_resourcebundle_get_error_code($bundle )
430{
431    return $GLOBALS['oo-mode'] ? $bundle->getErrorCode():resourcebundle_get_error_code($bundle);
432}
433function ut_resourcebundle_get_error_message($bundle )
434{
435    return $GLOBALS['oo-mode'] ? $bundle->getErrorMessage():resourcebundle_get_error_message($bundle);
436}
437