Skip to content

Commit 5d263a2

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 eb6b517 commit 5d263a2

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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+
'stringable object' => array(
78+
'input' => new Fixture_Formatting_WpParseStr(),
79+
'expected' => array(
80+
'foobar' => '',
81+
),
82+
),
83+
);
84+
}
85+
86+
/**
87+
* Tests that the result array only contains the result of the string parsing
88+
* when provided with different types of "result" arrays.
89+
*
90+
* @dataProvider data_wp_parse_str_result_array_is_always_overwritten
91+
*
92+
* @param array|null $output Value for the $output parameter.
93+
* @param array $expected Expected function output.
94+
*/
95+
public function test_wp_parse_str_result_array_is_always_overwritten( $output, $expected ) {
96+
wp_parse_str( 'key=25&thing=text', $output );
97+
$this->assertSame( $expected, $output );
98+
}
99+
100+
/**
101+
* Data Provider.
102+
*
103+
* @return array
104+
*/
105+
public function data_wp_parse_str_result_array_is_always_overwritten() {
106+
// Standard value for expected output.
107+
$expected = array(
108+
'key' => '25',
109+
'thing' => 'text',
110+
);
111+
112+
return array(
113+
'output null' => array(
114+
'output' => null,
115+
'expected' => $expected,
116+
),
117+
'output empty array' => array(
118+
'output' => array(),
119+
'expected' => $expected,
120+
),
121+
'output non empty array, no conflicting keys' => array(
122+
'output' => array(
123+
'foo' => 'bar',
124+
),
125+
'expected' => $expected,
126+
),
127+
'output non empty array, conflicting keys' => array(
128+
'output' => array(
129+
'key' => 'value',
130+
),
131+
'expected' => $expected,
132+
),
133+
);
134+
}
135+
}
136+
137+
/**
138+
* Fixture for use in the tests.
139+
*/
140+
class Fixture_Formatting_WpParseStr {
141+
public function __toString() {
142+
return 'foobar';
143+
}
144+
}

0 commit comments

Comments
 (0)