Skip to content

Commit 7912ce3

Browse files
committed
Tests: add dedicated tests for the wp_parse_str() function
Even though the function doesn't really contain any logic, these tests document the function behaviour when different input values and output values are passed and safeguard PHP cross-version compatibility.
1 parent 25bc89f commit 7912ce3

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
/**
4+
* @group formatting.php
5+
* @covers ::wp_parse_str
6+
*/
7+
class Tests_Formatting_WpParseStr extends WP_UnitTestCase {
8+
9+
/**
10+
* Tests parsing of a string into variables.
11+
*
12+
* Note: while the function under test does not contain any significant logic,
13+
* these tests document the behaviour and safeguard PHP cross-version compatibility.
14+
*
15+
* @dataProvider data_wp_parse_str
16+
*
17+
* @param mixed $input Value to parse.
18+
* @param array $expected Expected function output.
19+
*/
20+
public function test_wp_parse_str( $input, $expected ) {
21+
wp_parse_str( $input, $output );
22+
$this->assertSame( $expected, $output );
23+
}
24+
25+
/**
26+
* Data Provider.
27+
*
28+
* @return array
29+
*/
30+
public function data_wp_parse_str() {
31+
return array(
32+
'null' => array(
33+
'input' => null,
34+
'expected' => array(),
35+
),
36+
'boolean false' => array(
37+
'input' => false,
38+
'expected' => array(),
39+
),
40+
'boolean true' => array(
41+
'input' => true,
42+
'expected' => array(
43+
1 => '',
44+
),
45+
),
46+
'integer 0' => array(
47+
'input' => 0,
48+
'expected' => array(
49+
0 => '',
50+
),
51+
),
52+
'integer 456' => array(
53+
'input' => 456,
54+
'expected' => array(
55+
456 => '',
56+
),
57+
),
58+
'float 12.53' => array(
59+
'input' => 12.53,
60+
'expected' => array(
61+
'12_53' => '',
62+
),
63+
),
64+
'plain string' => array(
65+
'input' => 'foobar',
66+
'expected' => array(
67+
'foobar' => '',
68+
),
69+
),
70+
'query string' => array(
71+
'input' => 'x=5&_baba=dudu&',
72+
'expected' => array(
73+
'x' => '5',
74+
'_baba' => 'dudu',
75+
),
76+
),
77+
);
78+
}
79+
80+
/**
81+
* Tests that the result array only contains the result of the string parsing
82+
* when provided with different types of "result" arrays.
83+
*
84+
* @dataProvider data_wp_parse_str_result_array_is_always_overwritten
85+
*
86+
* @param array|null $output Value for the $output parameter.
87+
* @param array $expected Expected function output.
88+
*/
89+
public function test_wp_parse_str_result_array_is_always_overwritten( $output, $expected ) {
90+
wp_parse_str( 'key=25&thing=text', $output );
91+
$this->assertSame( $expected, $output );
92+
}
93+
94+
/**
95+
* Data Provider.
96+
*
97+
* @return array
98+
*/
99+
public function data_wp_parse_str_result_array_is_always_overwritten() {
100+
// Standard value for expected output.
101+
$expected = array(
102+
'key' => '25',
103+
'thing' => 'text',
104+
);
105+
106+
return array(
107+
'output null' => array(
108+
'output' => null,
109+
'expected' => $expected,
110+
),
111+
'output empty array' => array(
112+
'output' => array(),
113+
'expected' => $expected,
114+
),
115+
'output non empty array, no conflicting keys' => array(
116+
'output' => array(
117+
'foo' => 'bar',
118+
),
119+
'expected' => $expected,
120+
),
121+
'output non empty array, conflicting keys' => array(
122+
'output' => array(
123+
'key' => 'value',
124+
),
125+
'expected' => $expected,
126+
),
127+
);
128+
}
129+
}

0 commit comments

Comments
 (0)