@@ -, +, @@ --- Koha/DateUtils.pm | 18 ++++++++++------ Koha/Exceptions/DateUtils.pm | 21 +++++++++++++++++++ t/DateUtils.t | 49 +++++++++++++++++++++++++++++++++++--------- 3 files changed, 72 insertions(+), 16 deletions(-) create mode 100644 Koha/Exceptions/DateUtils.pm --- a/Koha/DateUtils.pm +++ a/Koha/DateUtils.pm @@ -19,7 +19,7 @@ package Koha::DateUtils; use Modern::Perl; use DateTime; use C4::Context; -use Carp; +use Koha::Exceptions::DateUtils; use base 'Exporter'; @@ -209,17 +209,23 @@ sub output_pref { $dt = $params; } - carp "output_pref should not be called with both dt and str parameters" - and return - if $dt and $str; + Koha::Exceptions::DateUtils::output_pref::ConflictingParameters->throw + if $dt and $str; if ( $str ) { local $@; $dt = eval { dt_from_string( $str ) }; - carp "Invalid date '$str' passed to output_pref\n" if $@; + Koha::Exceptions::DateUtils::output_pref::InvalidDateString->throw( + "Invalid date '$str' passed to output_pref" + ) if $@; } - return unless defined $dt && ref($dt) eq 'DateTime'; + if( !defined $dt || ref($dt) ne 'DateTime' ) { + Koha::Exceptions::DateUtils::output_pref::NoDateTime->throw( + "Object should have been a DateTime; check the call from ". + join ',', (caller), + ); + } # FIXME: see bug 13242 => no TZ for dates 'infinite' if ( $dt->ymd !~ /^9999/ ) { --- a/Koha/Exceptions/DateUtils.pm +++ a/Koha/Exceptions/DateUtils.pm @@ -0,0 +1,21 @@ +package Koha::Exceptions::DateUtils; + +use Modern::Perl; + +use Exception::Class ( + + 'Koha::Exceptions::DateUtils' => { + description => 'Something went wrong in DateUtils', + }, + 'Koha::Exceptions::DateUtils::output_pref::ConflictingParameters' => { + description => 'output_pref should not be called with both dt and str parameters', + }, + 'Koha::Exceptions::DateUtils::output_pref::InvalidDateString' => { + description => 'Invalid date string passed to output_pref', + }, + 'Koha::Exceptions::DateUtils::output_pref::NoDateTime' => { + description => 'Object should have been a DateTime', + }, +); + +1; --- a/t/DateUtils.t +++ a/t/DateUtils.t @@ -9,6 +9,8 @@ use Test::More tests => 63; use Test::MockModule; use Test::Warn; use Time::HiRes qw/ gettimeofday /; +use Try::Tiny; + use t::lib::Mocks; BEGIN { use_ok('Koha::DateUtils'); } @@ -223,19 +225,46 @@ $dt = dt_from_string('2015-01-31 01:02:03'); is( output_pref( {dt => $dt} ), '31/01/2015 01:02', 'dt_from_string should fallback to sql format' ); # output_pref with no parameters, single parameter (no hash) -is( output_pref(), undef, 'output_pref without parameters' ); -is( output_pref( 'no_dt' ), undef, 'Passed single invalid dt to output_pref' ); +try { + output_pref(); + ok( 0, 'output_pref without parameters' ); +} catch { + is( ref($_), 'Koha::Exceptions::DateUtils::output_pref::NoDateTime', + 'output_pref without parameters' ); +}; +try { + output_pref( 'no_dt' ); + ok( 0, 'Passed single invalid dt to output_pref' ); +} catch { + is( ref($_), 'Koha::Exceptions::DateUtils::output_pref::NoDateTime', + 'Passed single invalid dt to output_pref' ); +}; # pass invalid dt via hash -is( output_pref({ dt => 'no_dt' }), undef, 'Passed invalid dt in hash to output_pref' ); +try { + output_pref({ dt => 'no_dt' }); + ok( 0, 'Passed invalid dt in hash to output_pref' ); +} catch { + is( ref($_), 'Koha::Exceptions::DateUtils::output_pref::NoDateTime', + 'Passed invalid dt in hash to output_pref' ); +}; # output_pref with str parameter is( output_pref( { 'str' => $testdate_iso, dateformat => 'iso', dateonly => 1 } ), $testdate_iso, 'output_pref should handle correctly the iso parameter' ); my $output_for_invalid_date; -warning_like { $output_for_invalid_date = output_pref( { str => 'invalid_date' } ) } - { carped => qr[^Invalid date 'invalid_date' passed to output_pref] }, - 'output_pref should carp if an invalid date is passed for the str parameter'; -is( $output_for_invalid_date, undef, 'output_pref should return undef if an invalid date is passed' ); -warning_is { output_pref( { 'str' => $testdate_iso, dt => $dt, dateformat => 'iso', dateonly => 1 } ) } - { carped => 'output_pref should not be called with both dt and str parameters' }, - 'output_pref should carp if str and dt parameters are passed together'; +try { + $output_for_invalid_date = output_pref({ str => 'invalid_date' }); + ok( 0, 'Invalid date string passed to output_pref' ); +} catch { + is( ref($_), 'Koha::Exceptions::DateUtils::output_pref::InvalidDateString', + 'Invalid date string passed to output_pref' ); +}; +is( $output_for_invalid_date, undef, 'No return value from output_pref' ); +try { + output_pref({ 'str' => $testdate_iso, dt => $dt, dateformat => 'iso', dateonly => 1 }); + ok( 0, 'output_pref should carp if str and dt parameters are passed together' ); +} catch { + is( ref($_), 'Koha::Exceptions::DateUtils::output_pref::ConflictingParameters', 'output_pref should throw an exception if str and dt parameters are passed together' ); +}; + +# End of tests --