Back to index

php5  5.3.10
ut_common.inc
Go to the documentation of this file.
00001 <?php
00002 /*
00003  * Run unit test in OO- and in procedural mode.
00004  * Then compare the outputs.
00005  * It they're equal then show one of them.
00006  * Otherwise indicate an error.
00007  */
00008 function ut_run()
00009 {
00010     // Run unit test in OO mode.
00011     $GLOBALS['oo-mode'] = true;
00012     $oo_result = ut_main();
00013 
00014     // Run unit test in procedural mode.
00015     $GLOBALS['oo-mode'] = false;
00016     $proc_result = ut_main();
00017 
00018     // Show error if the APIs produce different results.
00019     if( $proc_result !== $oo_result )
00020     {
00021           echo "ERROR: OO- and procedural APIs produce different results!\n";
00022           echo "OO API output:\n";
00023           echo str_repeat( '=', 78 ) . "\n";
00024           echo $oo_result;
00025           echo str_repeat( '=', 78 ) . "\n";
00026           echo "procedural API output:\n";
00027           echo str_repeat( '=', 78 ) . "\n";
00028           echo $proc_result;
00029           echo str_repeat( '=', 78 ) . "\n";
00030           return;
00031     }
00032 
00033     // Else, if the results are equal, show one of them.
00034     echo $proc_result;
00035 }
00036 
00037 function dump( $val )
00038 {
00039     return var_export( $val, true );
00040 }
00041 
00042 /*
00043  * Wrappers around Collator methods to run them in either OO- or procedural mode.
00044  */
00045 
00046 function ut_coll_create( $locale )
00047 {
00048     return $GLOBALS['oo-mode'] ? Collator::create( $locale ) : collator_create( $locale );
00049 }
00050 function ut_coll_compare( $coll, $str1, $str2 )
00051 {
00052     return $GLOBALS['oo-mode'] ? $coll->compare( $str1, $str2 ) : collator_compare( $coll, $str1, $str2 );
00053 }
00054 function ut_coll_sort( $coll, &$arr, $sort_flag = Collator::SORT_REGULAR )
00055 {
00056     return $GLOBALS['oo-mode'] ? $coll->sort( $arr, $sort_flag ) : collator_sort( $coll, $arr, $sort_flag );
00057 }
00058 function ut_coll_sort_with_sort_keys( $coll, &$arr )
00059 {
00060     return $GLOBALS['oo-mode'] ? $coll->sortWithSortKeys( $arr ) : collator_sort_with_sort_keys( $coll, $arr );
00061 }
00062 function ut_coll_get_sort_key( $coll, $str )
00063 {
00064     return $GLOBALS['oo-mode'] ? $coll->getSortKey( $str ) : collator_get_sort_key( $coll, $str );
00065 }
00066 function ut_coll_asort( $coll, &$arr, $sort_flag = Collator::SORT_REGULAR )
00067 {
00068     return $GLOBALS['oo-mode'] ? $coll->asort( $arr, $sort_flag ) : collator_asort( $coll, $arr, $sort_flag );
00069 }
00070 function ut_coll_get_locale( $coll, $type )
00071 {
00072     return $GLOBALS['oo-mode'] ? $coll->getLocale( $type ) : collator_get_locale( $coll, $type );
00073 }
00074 function ut_coll_get_display_name( $obj_loc, $disp_loc )
00075 {
00076     return $GLOBALS['oo-mode'] ? Collator::getDisplayName( $obj_loc, $disp_loc ) : collator_get_display_name( $obj_loc, $disp_loc );
00077 }
00078 function ut_coll_get_available_locales()
00079 {
00080     return $GLOBALS['oo-mode'] ? Collator::getAvailableLocales() : collator_get_available_locales();
00081 }
00082 function ut_coll_get_attribute( $coll, $attr )
00083 {
00084     return $GLOBALS['oo-mode'] ? $coll->getAttribute( $attr ) : collator_get_attribute( $coll, $attr );
00085 }
00086 function ut_coll_get_strength( $coll )
00087 {
00088     return $GLOBALS['oo-mode'] ? $coll->getStrength() : collator_get_strength( $coll );
00089 }
00090 function ut_coll_set_strength( $coll, $strength )
00091 {
00092     return $GLOBALS['oo-mode'] ? $coll->setStrength( $strength ) : collator_set_strength( $coll, $strength );
00093 }
00094 function ut_coll_set_attribute( $coll, $attr, $val )
00095 {
00096     return $GLOBALS['oo-mode'] ? $coll->setAttribute( $attr, $val ) : collator_set_attribute( $coll, $attr, $val );
00097 }
00098 function ut_coll_get_variable_top( $coll )
00099 {
00100     return $GLOBALS['oo-mode'] ? $coll->getVariableTop() : collator_get_variable_top( $coll );
00101 }
00102 function ut_coll_set_variable_top( $coll, $var_top )
00103 {
00104     return $GLOBALS['oo-mode'] ? $coll->setVariableTop( $var_top ) : collator_set_variable_top( $coll, $var_top );
00105 }
00106 function ut_coll_restore_variable_top( $coll, $var_top )
00107 {
00108     return $GLOBALS['oo-mode'] ? $coll->restoreVariableTop( $var_top ) : collator_restore_variable_top( $coll, $var_top );
00109 }
00110 function ut_coll_get_error_code( $coll )
00111 {
00112     return $GLOBALS['oo-mode'] ? $coll->getErrorCode() : collator_get_error_code( $coll );
00113 }
00114 function ut_coll_get_error_message( $coll )
00115 {
00116     return $GLOBALS['oo-mode'] ? $coll->getErrorMessage() : collator_get_error_message( $coll );
00117 }
00118 function ut_coll_get_default()
00119 {
00120     return $GLOBALS['oo-mode'] ? Collator::getDefault() : collator_get_default();
00121 }
00122 function ut_coll_set_default( $coll )
00123 {
00124     return $GLOBALS['oo-mode'] ? Collator::setDefault( $coll ) : collator_set_default( $coll );
00125 }
00126 
00127 /*
00128  * Wrappers around NumberFormatter methods to run them in either OO- or procedural mode.
00129  */
00130 
00131 // FIXME: incomplete list
00132 
00133 function ut_nfmt_create( $locale, $style, $pattern = null )
00134 {
00135     return $GLOBALS['oo-mode'] ? new NumberFormatter( $locale, $style, $pattern ) : numfmt_create( $locale, $style, $pattern );
00136 }
00137 function ut_nfmt_format( $fmt, $number, $type = null )
00138 {
00139     return $GLOBALS['oo-mode'] ? $fmt->format( $number, $type ) : numfmt_format( $fmt, $number, $type );
00140 }
00141 function ut_nfmt_parse( $fmt, $string, $type = NumberFormatter::TYPE_DOUBLE, &$position = null )
00142 {
00143     if(is_null($position)) {
00144     return $GLOBALS['oo-mode'] ? $fmt->parse( $string, $type ) : numfmt_parse( $fmt, $string, $type );
00145     } else {
00146     return $GLOBALS['oo-mode'] ? $fmt->parse( $string, $type, $position ) : numfmt_parse( $fmt, $string, $type, $position );
00147     }
00148 }
00149 function ut_nfmt_format_currency( $fmt, $number, $currency )
00150 {
00151     return $GLOBALS['oo-mode'] ? $fmt->formatCurrency( $number, $currency ) : numfmt_format_currency( $fmt, $number, $currency );
00152 }
00153 function ut_nfmt_parse_currency( $fmt, $string, &$currency, &$position = null )
00154 {
00155     if(is_null($position)) {
00156     return $GLOBALS['oo-mode'] ? $fmt->parseCurrency( $string, $currency ) : numfmt_parse_currency( $fmt, $string, $currency );
00157     } else {
00158     return $GLOBALS['oo-mode'] ? $fmt->parseCurrency( $string, $currency, $position ) : numfmt_parse_currency( $fmt, $string, $currency, $position );
00159    }
00160 }
00161 function ut_nfmt_set_attribute( $fmt, $attribute, $value )
00162 {
00163     return $GLOBALS['oo-mode'] ? $fmt->setAttribute( $attribute, $value ) : numfmt_set_attribute( $fmt, $attribute, $value );
00164 }
00165 function ut_nfmt_set_text_attribute( $fmt, $attribute, $value )
00166 {
00167     return $GLOBALS['oo-mode'] ? $fmt->setTextAttribute( $attribute, $value ) : numfmt_set_text_attribute( $fmt, $attribute, $value );
00168 }
00169 function ut_nfmt_set_symbol( $fmt, $attribute, $value )
00170 {
00171     return $GLOBALS['oo-mode'] ? $fmt->setSymbol( $attribute, $value ) : numfmt_set_symbol( $fmt, $attribute, $value );
00172 }
00173 function ut_nfmt_set_pattern( $fmt, $pattern )
00174 {
00175     return $GLOBALS['oo-mode'] ? $fmt->setPattern( $pattern ) : numfmt_set_pattern( $fmt, $pattern );
00176 }
00177 function ut_nfmt_get_attribute( $fmt, $attribute )
00178 {
00179     return $GLOBALS['oo-mode'] ? $fmt->getAttribute( $attribute ) : numfmt_get_attribute( $fmt, $attribute );
00180 }
00181 function ut_nfmt_get_text_attribute( $fmt, $attribute )
00182 {
00183     return $GLOBALS['oo-mode'] ? $fmt->getTextAttribute( $attribute ) : numfmt_get_text_attribute( $fmt, $attribute );
00184 }
00185 function ut_nfmt_get_symbol( $fmt, $attribute )
00186 {
00187     return $GLOBALS['oo-mode'] ? $fmt->getSymbol( $attribute ) : numfmt_get_symbol( $fmt, $attribute );
00188 }
00189 function ut_nfmt_get_pattern( $fmt )
00190 {
00191     return $GLOBALS['oo-mode'] ? $fmt->getPattern() : numfmt_get_pattern( $fmt );
00192 }
00193 function ut_nfmt_get_locale( $fmt, $type = 0 )
00194 {
00195     return $GLOBALS['oo-mode'] ? $fmt->getLocale( $type ) : numfmt_get_locale( $fmt, $type );
00196 }
00197 function ut_nfmt_get_error_code( $fmt )
00198 {
00199     return $GLOBALS['oo-mode'] ? $fmt->getErrorCode() : numfmt_get_error_code( $fmt );
00200 }
00201 function ut_nfmt_get_error_message( $fmt )
00202 {
00203     return $GLOBALS['oo-mode'] ? $fmt->getErrorMessage() : numfmt_get_error_message( $fmt );
00204 }
00205 
00206 function ut_norm_normalize( $str, $form )
00207 {
00208     return $GLOBALS['oo-mode'] ? Normalizer::normalize( $str, $form ) : normalizer_normalize( $str, $form );
00209 }
00210 function ut_norm_is_normalized( $str, $form )
00211 {
00212     return $GLOBALS['oo-mode'] ? Normalizer::isNormalized( $str, $form ) : normalizer_is_normalized( $str, $form );
00213 }
00214 
00215 /*
00216  * Wrappers around Collator methods to run them in either OO- or procedural mode.
00217  */
00218 
00219 function ut_loc_get_default( )
00220 {
00221     return $GLOBALS['oo-mode'] ? Locale::getDefault( ) : locale_get_default();
00222 }
00223 function ut_loc_set_default( $locale  )
00224 {
00225        return $GLOBALS['oo-mode'] ? Locale::setDefault( $locale  ) : locale_set_default( $locale );
00226 }
00227 function ut_loc_get_primary_language( $locale )
00228 {
00229     return $GLOBALS['oo-mode'] ? Locale::getPrimaryLanguage( $locale ) : locale_get_primary_language( $locale );
00230 }
00231 function ut_loc_get_script( $locale )
00232 {
00233     return $GLOBALS['oo-mode'] ? Locale::getScript( $locale ) : locale_get_script( $locale );
00234 }
00235 function ut_loc_get_region( $locale )
00236 {
00237     return $GLOBALS['oo-mode'] ? Locale::getRegion( $locale ) : locale_get_region( $locale );
00238 }
00239 function ut_loc_get_keywords( $locale )
00240 {
00241     return $GLOBALS['oo-mode'] ? Locale::getKeywords( $locale ) : locale_get_keywords( $locale );
00242 }
00243 function ut_loc_get_display_name( $locale , $dispLocale )
00244 {
00245     return $GLOBALS['oo-mode'] ? Locale::getDisplayName( $locale , $dispLocale ) : locale_get_display_name( $locale , $dispLocale );
00246 }
00247 function ut_loc_get_display_language( $locale , $dispLocale )
00248 {
00249     return $GLOBALS['oo-mode'] ? Locale::getDisplayLanguage( $locale , $dispLocale ) : locale_get_display_language( $locale , $dispLocale );
00250 }
00251 function ut_loc_get_display_script( $locale , $dispLocale )
00252 {
00253     return $GLOBALS['oo-mode'] ? Locale::getDisplayScript( $locale , $dispLocale ) : locale_get_display_script( $locale , $dispLocale );
00254 }
00255 function ut_loc_get_display_region( $locale, $dispLocale  )
00256 {
00257     return $GLOBALS['oo-mode'] ? Locale::getDisplayRegion( $locale, $dispLocale  ) : locale_get_display_region( $locale, $dispLocale  );
00258 }
00259 function ut_loc_get_display_variant( $locale , $dispLocale )
00260 {
00261     return $GLOBALS['oo-mode'] ? Locale::getDisplayVariant( $locale , $dispLocale ) : locale_get_display_variant( $locale, $dispLocale  );
00262 }
00263 function ut_loc_locale_compose( $loc_parts_arr )
00264 {
00265     return $GLOBALS['oo-mode'] ? Locale::composeLocale( $loc_parts_arr ) : locale_compose( $loc_parts_arr );
00266 }
00267 function ut_loc_locale_parse( $locale )
00268 {
00269     return $GLOBALS['oo-mode'] ? Locale::parseLocale( $locale ) : locale_parse($locale );
00270 }
00271 function ut_loc_locale_get_all_variants( $locale )
00272 {
00273     return $GLOBALS['oo-mode'] ? Locale::getAllVariants( $locale ) : locale_get_all_variants( $locale );
00274 }
00275 function ut_loc_locale_filter_matches( $lang_tag,$loc_range  ,$isCanonical)
00276 {
00277     return $GLOBALS['oo-mode'] ? Locale::filterMatches( $lang_tag,$loc_range ,$isCanonical) : locale_filter_matches( $lang_tag,$loc_range ,$isCanonical);
00278 }
00279 function ut_loc_canonicalize( $locale )
00280 {
00281     return $GLOBALS['oo-mode'] ? Locale::canonicalize( $locale ) : locale_canonicalize( $locale );
00282 }
00283 function ut_loc_locale_lookup( $lang_tag_arr,$loc_range,$isCanonical,$default_loc)
00284 {
00285     return $GLOBALS['oo-mode'] ? Locale::lookup( $lang_tag_arr,$loc_range,$isCanonical,$default_loc ) : locale_lookup( $lang_tag_arr,$loc_range,$isCanonical,$default_loc );
00286 }
00287 function ut_loc_accept_http($http) {
00288        return $GLOBALS['oo-mode'] ? Locale::acceptFromHttp($http):locale_accept_from_http($http);
00289 }
00290 /* MessageFormatter functions */
00291 function ut_msgfmt_create( $locale, $pattern)
00292 {
00293     return $GLOBALS['oo-mode'] ? MessageFormatter::create( $locale, $pattern ) : msgfmt_create( $locale, $pattern );
00294 }
00295 function ut_msgfmt_format( $fmt, $args )
00296 {
00297     return $GLOBALS['oo-mode'] ? $fmt->format( $args ) : msgfmt_format( $fmt, $args);
00298 }
00299 function ut_msgfmt_parse( $fmt, $string)
00300 {
00301     return $GLOBALS['oo-mode'] ? $fmt->parse( $string) : msgfmt_parse( $fmt, $string);
00302 }
00303 function ut_msgfmt_format_message( $locale, $pattern, $args )
00304 {
00305     return $GLOBALS['oo-mode'] ? MessageFormatter::formatMessage( $locale, $pattern, $args ) : msgfmt_format_message( $locale, $pattern, $args );
00306 }
00307 function ut_msgfmt_parse_message( $locale, $pattern, $string )
00308 {
00309     return $GLOBALS['oo-mode'] ? MessageFormatter::parseMessage( $locale, $pattern, $string ) : msgfmt_parse_message( $locale, $pattern, $string );
00310 }
00311 function ut_msgfmt_set_pattern( $fmt, $pattern )
00312 {
00313     return $GLOBALS['oo-mode'] ? $fmt->setPattern( $pattern ) : msgfmt_set_pattern( $fmt, $pattern );
00314 }
00315 function ut_msgfmt_get_pattern( $fmt )
00316 {
00317     return $GLOBALS['oo-mode'] ? $fmt->getPattern() : msgfmt_get_pattern( $fmt );
00318 }
00319 function ut_msgfmt_get_locale( $fmt )
00320 {
00321     return $GLOBALS['oo-mode'] ? $fmt->getLocale( ) : msgfmt_get_locale( $fmt );
00322 }
00323 function ut_msgfmt_get_error_code( $fmt )
00324 {
00325     return $GLOBALS['oo-mode'] ? $fmt->getErrorCode() : msgfmt_get_error_code( $fmt );
00326 }
00327 function ut_msgfmt_get_error_message( $fmt )
00328 {
00329     return $GLOBALS['oo-mode'] ? $fmt->getErrorMessage() : msgfmt_get_error_message( $fmt );
00330 }
00331 /* IntlDateFormatter functions */
00332 function ut_datefmt_create( $locale, $datetype, $timetype, $timezone = null, $calendar = null ,$pattern = null)
00333 {
00334     return $GLOBALS['oo-mode'] ? datefmt_create( $locale, $datetype, $timetype, $timezone, $calendar ,$pattern ) : datefmt_create( $locale, $datetype, $timetype, $timezone, $calendar ,$pattern);
00335 }
00336 function ut_datefmt_get_datetype( $fmt )
00337 {
00338     return $GLOBALS['oo-mode'] ? $fmt->getDateType( ) : datefmt_get_datetype( $fmt );
00339 }
00340 function ut_datefmt_get_timetype( $fmt )
00341 {
00342     return $GLOBALS['oo-mode'] ? $fmt->getTimeType( ) : datefmt_get_timetype( $fmt );
00343 }
00344 function ut_datefmt_get_calendar( $fmt )
00345 {
00346     return $GLOBALS['oo-mode'] ? $fmt->getCalendar( ) : datefmt_get_calendar( $fmt );
00347 }
00348 function ut_datefmt_set_calendar( $fmt ,$calendar )
00349 {
00350     return $GLOBALS['oo-mode'] ? $fmt->setCalendar( $calendar ) : datefmt_set_calendar( $fmt , $calendar );
00351 }
00352 function ut_datefmt_get_timezone_id( $fmt )
00353 {
00354     return $GLOBALS['oo-mode'] ? $fmt->getTimeZoneId( ) : datefmt_get_timezone_id( $fmt );
00355 }
00356 function ut_datefmt_set_timezone_id( $fmt ,$timezone_id )
00357 {
00358     return $GLOBALS['oo-mode'] ? $fmt->setTimeZoneId( $timezone_id ) : datefmt_set_timezone_id( $fmt ,$timezone_id);
00359 }
00360 function ut_datefmt_get_pattern( $fmt )
00361 {
00362     return $GLOBALS['oo-mode'] ? $fmt->getPattern() : datefmt_get_pattern( $fmt );
00363 }
00364 function ut_datefmt_set_pattern( $fmt , $pattern )
00365 {
00366     return $GLOBALS['oo-mode'] ? $fmt->setPattern( $pattern ) : datefmt_set_pattern( $fmt ,  $pattern);
00367 }
00368 function ut_datefmt_get_locale( $fmt ,$type=ULOC_ACTUAL_LOCALE)
00369 {
00370     return $GLOBALS['oo-mode'] ? $fmt->getLocale($type ) : datefmt_get_locale( $fmt ,$type);
00371 }
00372 function ut_datefmt_is_lenient( $fmt )
00373 {
00374     return $GLOBALS['oo-mode'] ? $fmt->isLenient() : datefmt_is_lenient( $fmt );
00375 }
00376 function ut_datefmt_set_lenient( $fmt , $lenient )
00377 {
00378     return $GLOBALS['oo-mode'] ? $fmt->setLenient(  $lenient ) : datefmt_set_lenient( $fmt ,  $lenient);
00379 }
00380 function ut_datefmt_format( $fmt , $value )
00381 {
00382     return $GLOBALS['oo-mode'] ? $fmt->format(  $value ) : datefmt_format( $fmt ,  $value);
00383 }
00384 function ut_datefmt_parse( $fmt , $value , &$parse_pos=0 )
00385 {
00386     return $GLOBALS['oo-mode'] ? $fmt->parse(  $value ,$parse_pos ) : datefmt_parse( $fmt ,  $value,$parse_pos);
00387 }
00388 function ut_datefmt_localtime( $fmt , $value , &$parse_pos=0 )
00389 {
00390     return $GLOBALS['oo-mode'] ? $fmt->localtime(  $value , $parse_pos ) : datefmt_localtime( $fmt ,  $value , $parse_pos );
00391 }
00392 
00393 function ut_resourcebundle_create( $locale, $bundle, $fallback=true )
00394 {
00395     return $GLOBALS['oo-mode'] ? new ResourceBundle($locale, $bundle, $fallback): resourcebundle_create($locale, $bundle, $fallback);
00396 }
00397 function ut_resourcebundle_count($bundle )
00398 {
00399     return $GLOBALS['oo-mode'] ? $bundle->count():resourcebundle_count($bundle);
00400 }
00401 function ut_resourcebundle_locales($bundle )
00402 {
00403     return $GLOBALS['oo-mode'] ? ResourceBundle::getLocales($bundle):resourcebundle_locales($bundle);
00404 }
00405 function ut_resourcebundle_get($bundle, $idx )
00406 {
00407     return $GLOBALS['oo-mode'] ? $bundle->get($idx):resourcebundle_get($bundle, $idx);
00408 }
00409 function ut_resourcebundle_get_error_code($bundle )
00410 {
00411     return $GLOBALS['oo-mode'] ? $bundle->getErrorCode():resourcebundle_get_error_code($bundle);
00412 }
00413 function ut_resourcebundle_get_error_message($bundle )
00414 {
00415     return $GLOBALS['oo-mode'] ? $bundle->getErrorMessage():resourcebundle_get_error_message($bundle);
00416 }