Skip to content

Commit bca693b

Browse files
Build/Test Tools: Replace assertInternalType() usage in unit tests.
The `assertInternalType()` and `assertNotInternalType()` methods are deprecated in PHPUnit 8 and removed in PHPUnit 9. While WordPress test suite currently only supports PHPUnit up to 7.5.x, this allows us to switch to newer assertions ahead of adding full support for PHPUnit 8+. These methods introduced in PHPUnit 7.5 should be used as an alternative: * `assertIsArray()` * `assertIsBool()` * `assertIsFloat()` * `assertIsInt()` * `assertIsNumeric()` * `assertIsObject()` * `assertIsResource()` * `assertIsString()` * `assertIsScalar()` * `assertIsCallable()` * `assertIsIterable()` * `assertIsNotArray()` * `assertIsNotBool()` * `assertIsNotFloat()` * `assertIsNotInt()` * `assertIsNotNumeric()` * `assertIsNotObject()` * `assertIsNotResource()` * `assertIsNotString()` * `assertIsNotScalar()` * `assertIsNotCallable()` * `assertIsNotIterable()` As WordPress currently uses PHPUnit 5.7.x to run tests on PHP 5.6, polyfills for these methods are now added to the `WP_UnitTestCase` class for PHPUnit < 7.5. Props pbearne, jrf, dd32, SergeyBiryukov. Fixes #53491. See #46149. git-svn-id: https://develop.svn.wordpress.org/trunk@51331 602fd350-edb4-49c9-b593-d223f7449a82
1 parent fee45f9 commit bca693b

104 files changed

Lines changed: 884 additions & 535 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tests/phpunit/includes/testcase.php

Lines changed: 340 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,344 @@ class WP_UnitTestCase extends WP_UnitTestCase_Base {
3232
public static function assertEqualsWithDelta( $expected, $actual, $delta, $message = '' ) {
3333
static::assertEquals( $expected, $actual, $message, $delta );
3434
}
35+
36+
/**
37+
* Asserts that a variable is of type array.
38+
*
39+
* This method has been backported from a more recent PHPUnit version,
40+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
41+
*
42+
* @since 5.9.0
43+
*
44+
* @param mixed $actual The value to check.
45+
* @param string $message Optional. Message to display when the assertion fails.
46+
*/
47+
public static function assertIsArray( $actual, $message = '' ) {
48+
static::assertInternalType( 'array', $actual, $message );
49+
}
50+
51+
/**
52+
* Asserts that a variable is of type bool.
53+
*
54+
* This method has been backported from a more recent PHPUnit version,
55+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
56+
*
57+
* @since 5.9.0
58+
*
59+
* @param mixed $actual The value to check.
60+
* @param string $message Optional. Message to display when the assertion fails.
61+
*/
62+
public static function assertIsBool( $actual, $message = '' ) {
63+
static::assertInternalType( 'bool', $actual, $message );
64+
}
65+
66+
/**
67+
* Asserts that a variable is of type float.
68+
*
69+
* This method has been backported from a more recent PHPUnit version,
70+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
71+
*
72+
* @since 5.9.0
73+
*
74+
* @param mixed $actual The value to check.
75+
* @param string $message Optional. Message to display when the assertion fails.
76+
*/
77+
public static function assertIsFloat( $actual, $message = '' ) {
78+
static::assertInternalType( 'float', $actual, $message );
79+
}
80+
81+
/**
82+
* Asserts that a variable is of type int.
83+
*
84+
* This method has been backported from a more recent PHPUnit version,
85+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
86+
*
87+
* @since 5.9.0
88+
*
89+
* @param mixed $actual The value to check.
90+
* @param string $message Optional. Message to display when the assertion fails.
91+
*/
92+
public static function assertIsInt( $actual, $message = '' ) {
93+
static::assertInternalType( 'int', $actual, $message );
94+
}
95+
96+
/**
97+
* Asserts that a variable is of type numeric.
98+
*
99+
* This method has been backported from a more recent PHPUnit version,
100+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
101+
*
102+
* @since 5.9.0
103+
*
104+
* @param mixed $actual The value to check.
105+
* @param string $message Optional. Message to display when the assertion fails.
106+
*/
107+
public static function assertIsNumeric( $actual, $message = '' ) {
108+
static::assertInternalType( 'numeric', $actual, $message );
109+
}
110+
111+
/**
112+
* Asserts that a variable is of type object.
113+
*
114+
* This method has been backported from a more recent PHPUnit version,
115+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
116+
*
117+
* @since 5.9.0
118+
*
119+
* @param mixed $actual The value to check.
120+
* @param string $message Optional. Message to display when the assertion fails.
121+
*/
122+
public static function assertIsObject( $actual, $message = '' ) {
123+
static::assertInternalType( 'object', $actual, $message );
124+
}
125+
126+
/**
127+
* Asserts that a variable is of type resource.
128+
*
129+
* This method has been backported from a more recent PHPUnit version,
130+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
131+
*
132+
* @since 5.9.0
133+
*
134+
* @param mixed $actual The value to check.
135+
* @param string $message Optional. Message to display when the assertion fails.
136+
*/
137+
public static function assertIsResource( $actual, $message = '' ) {
138+
static::assertInternalType( 'resource', $actual, $message );
139+
}
140+
141+
/**
142+
* Asserts that a variable is of type string.
143+
*
144+
* This method has been backported from a more recent PHPUnit version,
145+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
146+
*
147+
* @since 5.9.0
148+
*
149+
* @param mixed $actual The value to check.
150+
* @param string $message Optional. Message to display when the assertion fails.
151+
*/
152+
public static function assertIsString( $actual, $message = '' ) {
153+
static::assertInternalType( 'string', $actual, $message );
154+
}
155+
156+
/**
157+
* Asserts that a variable is of type scalar.
158+
*
159+
* This method has been backported from a more recent PHPUnit version,
160+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
161+
*
162+
* @since 5.9.0
163+
*
164+
* @param mixed $actual The value to check.
165+
* @param string $message Optional. Message to display when the assertion fails.
166+
*/
167+
public static function assertIsScalar( $actual, $message = '' ) {
168+
static::assertInternalType( 'scalar', $actual, $message );
169+
}
170+
171+
/**
172+
* Asserts that a variable is of type callable.
173+
*
174+
* This method has been backported from a more recent PHPUnit version,
175+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
176+
*
177+
* @since 5.9.0
178+
*
179+
* @param mixed $actual The value to check.
180+
* @param string $message Optional. Message to display when the assertion fails.
181+
*/
182+
public static function assertIsCallable( $actual, $message = '' ) {
183+
static::assertInternalType( 'callable', $actual, $message );
184+
}
185+
186+
/**
187+
* Asserts that a variable is of type iterable.
188+
*
189+
* This method has been backported from a more recent PHPUnit version,
190+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
191+
*
192+
* Support for `iterable` was only added to `Assert::assertNotInternalType()`
193+
* in PHPUnit 7.1.0, so this polyfill cannot use a direct fall-through
194+
* to that functionality until WordPress test suite requires PHPUnit 7.1.0
195+
* as the minimum version.
196+
*
197+
* @since 5.9.0
198+
*
199+
* @param mixed $actual The value to check.
200+
* @param string $message Optional. Message to display when the assertion fails.
201+
*/
202+
public static function assertIsIterable( $actual, $message = '' ) {
203+
static::assertTrue( is_iterable( $actual ), $message );
204+
}
205+
206+
/**
207+
* Asserts that a variable is not of type array.
208+
*
209+
* This method has been backported from a more recent PHPUnit version,
210+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
211+
*
212+
* @since 5.9.0
213+
*
214+
* @param mixed $actual The value to check.
215+
* @param string $message Optional. Message to display when the assertion fails.
216+
*/
217+
public static function assertIsNotArray( $actual, $message = '' ) {
218+
static::assertNotInternalType( 'array', $actual, $message );
219+
}
220+
221+
/**
222+
* Asserts that a variable is not of type bool.
223+
*
224+
* This method has been backported from a more recent PHPUnit version,
225+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
226+
*
227+
* @since 5.9.0
228+
*
229+
* @param mixed $actual The value to check.
230+
* @param string $message Optional. Message to display when the assertion fails.
231+
*/
232+
public static function assertIsNotBool( $actual, $message = '' ) {
233+
static::assertNotInternalType( 'bool', $actual, $message );
234+
}
235+
236+
/**
237+
* Asserts that a variable is not of type float.
238+
*
239+
* This method has been backported from a more recent PHPUnit version,
240+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
241+
*
242+
* @since 5.9.0
243+
*
244+
* @param mixed $actual The value to check.
245+
* @param string $message Optional. Message to display when the assertion fails.
246+
*/
247+
public static function assertIsNotFloat( $actual, $message = '' ) {
248+
static::assertNotInternalType( 'float', $actual, $message );
249+
}
250+
251+
/**
252+
* Asserts that a variable is not of type int.
253+
*
254+
* This method has been backported from a more recent PHPUnit version,
255+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
256+
*
257+
* @since 5.9.0
258+
*
259+
* @param mixed $actual The value to check.
260+
* @param string $message Optional. Message to display when the assertion fails.
261+
*/
262+
public static function assertIsNotInt( $actual, $message = '' ) {
263+
static::assertNotInternalType( 'int', $actual, $message );
264+
}
265+
266+
/**
267+
* Asserts that a variable is not of type numeric.
268+
*
269+
* This method has been backported from a more recent PHPUnit version,
270+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
271+
*
272+
* @since 5.9.0
273+
*
274+
* @param mixed $actual The value to check.
275+
* @param string $message Optional. Message to display when the assertion fails.
276+
*/
277+
public static function assertIsNotNumeric( $actual, $message = '' ) {
278+
static::assertNotInternalType( 'numeric', $actual, $message );
279+
}
280+
281+
/**
282+
* Asserts that a variable is not of type object.
283+
*
284+
* This method has been backported from a more recent PHPUnit version,
285+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
286+
*
287+
* @since 5.9.0
288+
*
289+
* @param mixed $actual The value to check.
290+
* @param string $message Optional. Message to display when the assertion fails.
291+
*/
292+
public static function assertIsNotObject( $actual, $message = '' ) {
293+
static::assertNotInternalType( 'object', $actual, $message );
294+
}
295+
296+
/**
297+
* Asserts that a variable is not of type resource.
298+
*
299+
* This method has been backported from a more recent PHPUnit version,
300+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
301+
*
302+
* @since 5.9.0
303+
*
304+
* @param mixed $actual The value to check.
305+
* @param string $message Optional. Message to display when the assertion fails.
306+
*/
307+
public static function assertIsNotResource( $actual, $message = '' ) {
308+
static::assertNotInternalType( 'resource', $actual, $message );
309+
}
310+
311+
/**
312+
* Asserts that a variable is not of type string.
313+
*
314+
* This method has been backported from a more recent PHPUnit version,
315+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
316+
*
317+
* @since 5.9.0
318+
*
319+
* @param mixed $actual The value to check.
320+
* @param string $message Optional. Message to display when the assertion fails.
321+
*/
322+
public static function assertIsNotString( $actual, $message = '' ) {
323+
static::assertNotInternalType( 'string', $actual, $message );
324+
}
325+
326+
/**
327+
* Asserts that a variable is not of type scalar.
328+
*
329+
* This method has been backported from a more recent PHPUnit version,
330+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
331+
*
332+
* @since 5.9.0
333+
*
334+
* @param mixed $actual The value to check.
335+
* @param string $message Optional. Message to display when the assertion fails.
336+
*/
337+
public static function assertIsNotScalar( $actual, $message = '' ) {
338+
static::assertNotInternalType( 'scalar', $actual, $message );
339+
}
340+
341+
/**
342+
* Asserts that a variable is not of type callable.
343+
*
344+
* This method has been backported from a more recent PHPUnit version,
345+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
346+
*
347+
* @since 5.9.0
348+
*
349+
* @param mixed $actual The value to check.
350+
* @param string $message Optional. Message to display when the assertion fails.
351+
*/
352+
public static function assertIsNotCallable( $actual, $message = '' ) {
353+
static::assertNotInternalType( 'callable', $actual, $message );
354+
}
355+
356+
/**
357+
* Asserts that a variable is not of type iterable.
358+
*
359+
* This method has been backported from a more recent PHPUnit version,
360+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
361+
*
362+
* Support for `iterable` was only added to `Assert::assertNotInternalType()`
363+
* in PHPUnit 7.1.0, so this polyfill cannot use a direct fall-through
364+
* to that functionality until WordPress test suite requires PHPUnit 7.1.0
365+
* as the minimum version.
366+
*
367+
* @since 5.9.0
368+
*
369+
* @param mixed $actual The value to check.
370+
* @param string $message Optional. Message to display when the assertion fails.
371+
*/
372+
public static function assertIsNotIterable( $actual, $message = '' ) {
373+
static::assertFalse( is_iterable( $actual ), $message );
374+
}
35375
}

tests/phpunit/tests/adminbar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public function test_admin_bar_contains_correct_links_for_users_with_no_role_on_
166166

167167
// Get primary blog.
168168
$primary = get_active_blog_for_user( self::$editor_id );
169-
$this->assertInternalType( 'object', $primary );
169+
$this->assertIsObject( $primary );
170170

171171
// No Site menu as the user isn't a member of this blog.
172172
$this->assertNull( $node_site_name );

tests/phpunit/tests/ajax/CustomizeManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ function test_save_success_publish_create() {
286286
);
287287
$this->make_ajax_call( 'customize_save' );
288288
$this->assertTrue( $this->_last_response_parsed['success'] );
289-
$this->assertInternalType( 'array', $this->_last_response_parsed['data'] );
289+
$this->assertIsArray( $this->_last_response_parsed['data'] );
290290

291291
$this->assertSame( 'publish', $this->_last_response_parsed['data']['changeset_status'] );
292292
$this->assertArrayHasKey( 'next_changeset_uuid', $this->_last_response_parsed['data'] );
@@ -325,7 +325,7 @@ function test_save_success_publish_edit() {
325325
$_POST['customize_changeset_title'] = 'Published';
326326
$this->make_ajax_call( 'customize_save' );
327327
$this->assertTrue( $this->_last_response_parsed['success'] );
328-
$this->assertInternalType( 'array', $this->_last_response_parsed['data'] );
328+
$this->assertIsArray( $this->_last_response_parsed['data'] );
329329

330330
$this->assertSame( 'publish', $this->_last_response_parsed['data']['changeset_status'] );
331331
$this->assertArrayHasKey( 'next_changeset_uuid', $this->_last_response_parsed['data'] );

tests/phpunit/tests/blocks/block-editor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ function test_get_default_block_editor_settings() {
172172

173173
$this->assertCount( 16, $settings );
174174
$this->assertFalse( $settings['alignWide'] );
175-
$this->assertInternalType( 'array', $settings['allowedMimeTypes'] );
175+
$this->assertIsArray( $settings['allowedMimeTypes'] );
176176
$this->assertTrue( $settings['allowedBlockTypes'] );
177177
$this->assertSameSets(
178178
array(
@@ -264,7 +264,7 @@ function test_get_default_block_editor_settings() {
264264
),
265265
$settings['imageSizes']
266266
);
267-
$this->assertInternalType( 'int', $settings['maxUploadFileSize'] );
267+
$this->assertIsInt( $settings['maxUploadFileSize'] );
268268
}
269269

270270
/**

tests/phpunit/tests/blocks/render.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ public function test_dynamic_block_renders_string() {
324324
$rendered = $block_type->render();
325325

326326
$this->assertSame( '10', $rendered );
327-
$this->assertInternalType( 'string', $rendered );
327+
$this->assertIsString( $rendered );
328328
}
329329

330330
public function test_dynamic_block_gets_inner_html() {

0 commit comments

Comments
 (0)