Bug 12844: New module to manage prices into Koha
[koha.git] / t / Number / Price.t
1 use Modern::Perl;
2
3 use Test::More tests => 19;
4
5 use Test::MockModule;
6 use t::lib::Mocks;
7
8 use C4::Budgets;
9 my $budget_module = Test::MockModule->new('C4::Budgets');
10 my $currency;
11 $budget_module->mock( 'GetCurrency', sub { return $currency; } );
12 use_ok('Koha::Number::Price');
13
14 t::lib::Mocks::mock_preference( 'CurrencyFormat', 'US' );
15 $currency = {
16     currency => 'USD',
17     symbol   => '$',
18     rate     => 1,
19     active   => 1,
20 };
21
22 is( Koha::Number::Price->new->format,    '0.00', 'US: format 0' );
23 is( Koha::Number::Price->new(3)->format, '3.00', 'US: format 3' );
24 is( Koha::Number::Price->new(1234567890)->format,
25     '1,234,567,890.00', 'US: format 1234567890' );
26
27 # FIXME This should be display symbol, but it was the case before the creation of this module
28 is( Koha::Number::Price->new->format( { with_symbol => 1 } ),
29     '0.00', 'US: format 0 with symbol' );
30 is( Koha::Number::Price->new(3)->format( { with_symbol => 1 } ),
31     '3.00', 'US: format 3 with symbol' );
32 is(
33     Koha::Number::Price->new(1234567890)
34       ->format( { with_symbol => 1 }, 'US: format 1234567890 with symbol' ),
35     '1,234,567,890.00'
36 );
37
38 is( Koha::Number::Price->new->unformat,    '0', 'US: unformat 0' );
39 is( Koha::Number::Price->new(3)->unformat, '3', 'US: unformat 3' );
40 is( Koha::Number::Price->new(1234567890)->unformat,
41     '1234567890', 'US: unformat 1234567890' );
42
43 t::lib::Mocks::mock_preference( 'CurrencyFormat', 'FR' );
44 $currency = {
45     currency => 'EUR',
46     symbol   => '€',
47     rate     => 1,
48     active   => 1,
49 };
50
51 # Actually,the price formating for France is 3,00€
52 # How put the symbol at the end with Number::Format?
53 is( Koha::Number::Price->new->format,    '0,00', 'FR: format 0' );
54 is( Koha::Number::Price->new(3)->format, '3,00', 'FR: format 3' );
55 is(
56     Koha::Number::Price->new(1234567890)->format,
57     '1 234 567 890,00',
58     'FR: format 1234567890'
59 );
60 is( Koha::Number::Price->new->format( { with_symbol => 1 } ),
61     '€0,00', 'FR: format 0 with symbol' );
62 is( Koha::Number::Price->new(3)->format( { with_symbol => 1 } ),
63     '€3,00', 'FR: format 3 with symbol' );
64 is(
65     Koha::Number::Price->new(1234567890)
66       ->format( { with_symbol => 1 }, 'FR: format 123567890 with symbol' ),
67     '€1 234 567 890,00'
68 );
69
70 is( Koha::Number::Price->new->unformat,    '0', 'FR: unformat 0' );
71 is( Koha::Number::Price->new(3)->unformat, '3', 'FR: unformat 3' );
72 is( Koha::Number::Price->new(1234567890)->unformat,
73     '1234567890', 'FR: unformat 1234567890' );