-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathHelperTest.php
More file actions
131 lines (101 loc) · 3.14 KB
/
HelperTest.php
File metadata and controls
131 lines (101 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
use SkyVerge\WooCommerce\PluginFramework\v6_0_2 as Framework;
/**
* Tests for the helper class.
*
* @see \SkyVerge\WooCommerce\PluginFramework\v6_0_2\SV_WC_Plugin_Compatibility
*/
class HelperTest extends \Codeception\TestCase\WPTestCase {
/**
* Tests {@see Framework\SV_WC_Helper::is_rest_api_request()}.
*
* @param string $route endpoint
* @param bool $expected result
*
* @dataProvider provider_is_rest_api_request
*/
public function test_is_rest_api_request( $route, $expected ) {
$_SERVER['REQUEST_URI'] = $route;
$is_api_request = Framework\SV_WC_Helper::is_rest_api_request();
$this->assertSame( $is_api_request, $expected );
}
/**
* Data provider for {@see HelperTest::test_is_rest_api_request()}.
*
* @return array[]
*/
public function provider_is_rest_api_request() {
return [
[ '/wp-json/', true ],
[ '/', false ],
];
}
/**
* Tests that can convert an array of IDs to a comma separated list.
*
* @dataProvider provider_get_escaped_string_list
*
* @param array $strings
* @param string $expected
*/
public function can_get_escaped_string_list( array $strings, string $expected ) {
$this->assertSame( $expected, Framework\SV_WC_Helper::get_escaped_string_list( $strings ) );
}
/** @see can_get_escaped_string_list **/
public function provider_get_escaped_string_list() : array
{
return [
'Strings' => [['foo', 'bar', 'baz'], "'foo', 'bar', 'baz'"],
'Mixed content' => [['foo', 0, 1, '2', -3, '-4.5'], "'foo', '0', '1', '2', '-3', '-4.5'"],
];
}
/**
* Tests that can convert an array of IDs to a comma separated list.
*
* @dataProvider can_get_escaped_id_list
*
* @param array $ids
* @param string $expected
*/
public function can_get_escaped_id_list( array $ids, string $expected ) {
$this->assertSame( $expected, Framework\SV_WC_Helper::get_escaped_id_list( $ids ) );
}
/** @see can_get_escaped_id_list **/
public function provider_get_escaped_id_list() : array {
return [
'Non-numbers' => [[null, false, true, 'test'], '0,1'],
'Integers' => [[1, 2, 3], '1,2,3'],
'Negative integers' => [[-1, -2, -3], '-1,-2,-3'],
'Numerical strings' => [['1', '2', '3'], '1,2,3'],
'Negative numerical strings' => [['-1', '-2', '-3'], '-1,-2,-3'],
];
}
/**
* Tests {@see Framework\SV_WC_Helper::format_percentage()}.
*
* @dataProvider provider_format_percentage
*
* @param float|int|string $number the number to format as percentage
* @param string $expected result
* @param int|bool|null $decimal_points optional, the number of decimal points to use
*/
public function test_format_percentage( $number, string $expected, $decimal_points = false ) {
$this->assertSame( $expected, Framework\SV_WC_Helper::format_percentage( $number, $decimal_points ) );
}
/**
* Data provider for {@see HelperTest::test_format_percentage()}.
*
* @return array[]
*/
public function provider_format_percentage() {
return [
[ 0.5, '50%' ],
[ '0.5', '50%' ],
[ 0, '0%' ],
[ 1, '100%' ],
[ 1.333333, '133.3333%' ],
[ 1.333333, '133.33%', 2 ],
[ 1.333333, '133%', null ],
];
}
}