Completed
Push — develop ( fa822c...3d25fb )
by Remco
01:23
created

MoneyTest::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Money
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Money
9
 */
10
11
namespace Pronamic\WordPress\Money;
12
13
use WP_UnitTestCase;
14
15
/**
16
 * Money
17
 *
18
 * @author Remco Tolsma
19
 * @version 1.0
20
 */
21
class MoneyTest extends WP_UnitTestCase {
22
	/**
23
	 * Setup.
24
	 */
25
	public function setUp() {
0 ignored issues
show
Coding Style introduced by
The function name setUp is in camel caps, but expected set_up instead as per the coding standard.
Loading history...
26
		parent::setUp();
27
28
		if ( version_compare( PHP_VERSION, '5.4', '<' ) ) {
29
			add_filter( 'number_format_i18n', array( $this, 'maybe_fix_multibyte_number_format' ), 10, 3 );
30
		}
31
	}
32
33
	/**
34
	 * Maybe fix multibyte number format.
35
	 *
36
	 * @link https://github.com/WordPress/WordPress/blob/4.9.6/wp-includes/functions.php#L206-L237
37
	 *
38
	 * @global WP_Locale $wp_locale
39
	 *
40
	 * @param float $number   The number to convert based on locale.
41
	 * @param int   $decimals Optional. Precision of the number of decimal places. Default 0.
42
	 * @return string Converted number in string format.
43
	 */
44
	public function maybe_fix_multibyte_number_format( $formatted, $number, $decimals ) {
45
		global $wp_locale;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
46
47
		if ( empty( $wp_locale ) ) {
48
			return $formatted;
49
		}
50
51
		$dec_point     = $wp_locale->number_format['decimal_point'];
52
		$thousands_sep = $wp_locale->number_format['thousands_sep'];
53
54
		if ( 1 === strlen( $dec_point ) && 1 === strlen( $thousands_sep ) ) {
55
			return $formatted;
56
		}
57
58
		$formatted = number_format( $number, $decimals, 'd', 't' );
59
60
		$formatted = strtr( $formatted, array(
61
			'd' => $dec_point,
62
			't' => $thousands_sep,
63
		) );
64
65
		return $formatted;
66
	}
67
68
	/**
69
	 * Test default format.
70
	 *
71
	 * @see https://github.com/WordPress/WordPress/blob/4.9.5/wp-includes/l10n.php
72
	 *
73
	 * @dataProvider default_format_provider
74
	 */
75
	public function test_default_format( $locale, $expected ) {
76
		// Note: Switching from nl_NL to fr_FR back to nl_NL is not working correctly (bug?).
77
		switch_to_locale( $locale );
78
79
		$value = Money::get_default_format();
80
81
		$this->assertEquals( $locale, get_locale() );
82
		$this->assertEquals( $expected, $value );
83
	}
84
85
	public function default_format_provider() {
86
		// Note: Switching from nl_NL to fr_FR back to nl_NL is not working correctly (bug?).
87
		return array(
88
			array( 'en_US', '%1$s%2$s %3$s' ),
89
			array( 'fr_FR', '%1$s%2$s %3$s' ),
90
			array( 'nl_NL', '%1$s %2$s' ),
91
		);
92
	}
93
94
	/**
95
	 * Test format.
96
	 *
97
	 * @see https://github.com/WordPress/WordPress/blob/4.9.5/wp-includes/l10n.php
98
	 *
99
	 * @param float $amount
100
	 * @param string $currency
101
	 * @param string $locale
102
	 * @param string $expected
103
	 *
104
	 * @dataProvider format_provider
105
	 */
106
	public function test_format( $locale, $currency, $amount, $expected ) {
107
		// Note: Switching from nl_NL to fr_FR back to nl_NL is not working correctly (bug?).
108
		switch_to_locale( $locale );
109
110
		$money = new Money( $amount, $currency );
111
112
		$value = $money->format_i18n();
113
114
		$this->assertEquals( $locale, get_locale() );
115
		$this->assertEquals( $expected, $value, 'Locale: ' . get_locale() . ' Money format: ' . Money::get_default_format() . ' Test: ' . _x( '%1$s%2$s %3$s', 'money format', 'pronamic-money' ) );
116
	}
117
118
	public function format_provider() {
119
		// Note: Switching from nl_NL to fr_FR back to nl_NL is not working correctly (bug?).
120
		return array(
121
			// Dutch
122
			array( 'nl_NL', 'EUR', 49.7512, '€ 49,75' ),
123
			array( 'nl_NL', 'NLG', 49.7512, 'G 49,7512' ),
124
			array( 'nl_NL', 'USD', 49.7512, '$ 49,75' ),
125
			array( 'nl_NL', 'USD', 1234567890.1234, '$ 1.234.567.890,12' ),
126
			// English
127
			array( 'en_US', 'EUR', 49.7512, '€49.75 EUR' ),
128
			array( 'en_US', 'USD', 1234567890.1234, '$1,234,567,890.12 USD' ),
129
			// French
130
			array( 'fr_FR', 'USD', 1234567890.1234, '$1 234 567 890,12 USD' ),
131
		);
132
	}
133
}
134