Skip to content

Commit beb9ffe

Browse files
committed
Add types and type assertions to tests
1 parent d6f69d6 commit beb9ffe

2 files changed

Lines changed: 31 additions & 23 deletions

File tree

tests/phpunit/tests/media/wpDeleteAttachmentHeicCompanionFile.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@ public function test_deletes_heic_file_recorded_in_metadata_original(): void {
2222
$this->assertIsInt( $attachment_id );
2323

2424
$attached_file = get_attached_file( $attachment_id, true );
25-
$dir = dirname( $attached_file );
26-
$heic_name = 'companion-' . wp_generate_password( 6, false ) . '.heic';
27-
$heic_path = $dir . '/' . $heic_name;
25+
$this->assertIsString( $attached_file );
26+
$dir = dirname( $attached_file );
27+
$heic_name = 'companion-' . wp_generate_password( 6, false ) . '.heic';
28+
$heic_path = $dir . '/' . $heic_name;
2829

2930
// Create a dummy companion file on disk.
3031
file_put_contents( $heic_path, 'test' );
3132
$this->assertFileExists( $heic_path, 'Test fixture should be on disk.' );
3233

3334
// Record the companion under metadata['original'] as the sideload route does.
34-
$metadata = wp_get_attachment_metadata( $attachment_id, true );
35+
$metadata = wp_get_attachment_metadata( $attachment_id, true );
36+
$this->assertIsArray( $metadata );
3537
$metadata['original'] = $heic_name;
3638
wp_update_attachment_metadata( $attachment_id, $metadata );
3739

@@ -49,6 +51,7 @@ public function test_noop_when_metadata_original_is_missing(): void {
4951

5052
// Sanity: no 'original' key on freshly-created metadata.
5153
$metadata = wp_get_attachment_metadata( $attachment_id, true );
54+
$this->assertIsArray( $metadata );
5255
$this->assertArrayNotHasKey( 'original', $metadata );
5356

5457
// Should not raise even though the hook fires.
@@ -67,8 +70,10 @@ public function test_noop_when_metadata_original_is_not_a_string(): void {
6770
$attachment_id = self::factory()->attachment->create_upload_object( DIR_TESTDATA . '/images/canola.jpg' );
6871
$this->assertIsInt( $attachment_id );
6972
$attached_file = get_attached_file( $attachment_id, true );
73+
$this->assertIsString( $attached_file );
7074

71-
$metadata = wp_get_attachment_metadata( $attachment_id, true );
75+
$metadata = wp_get_attachment_metadata( $attachment_id, true );
76+
$this->assertIsArray( $metadata );
7277
$metadata['original'] = array( 'file' => 'should-not-delete.heic' );
7378
wp_update_attachment_metadata( $attachment_id, $metadata );
7479

tests/phpunit/tests/rest-api/rest-attachments-controller.php

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,53 @@
99
*/
1010
class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Controller_Testcase {
1111

12-
protected static $superadmin_id;
13-
protected static $editor_id;
14-
protected static $author_id;
15-
protected static $contributor_id;
16-
protected static $uploader_id;
17-
protected static $rest_after_insert_attachment_count;
18-
protected static $rest_insert_attachment_count;
12+
protected static int $superadmin_id;
13+
protected static int $editor_id;
14+
protected static int $author_id;
15+
protected static int $contributor_id;
16+
protected static int $uploader_id;
17+
protected static int $rest_after_insert_attachment_count;
18+
protected static int $rest_insert_attachment_count;
1919

2020
/**
2121
* @var string The path to a test file.
2222
*/
23-
private static $test_file;
23+
private static string $test_file;
2424

2525
/**
2626
* @var string The path to a second test file.
2727
*/
28-
private static $test_file2;
28+
private static string $test_file2;
2929

3030
/**
3131
* @var string The path to the AVIF test image.
3232
*/
33-
private static $test_avif_file;
33+
private static string $test_avif_file;
3434

3535
/**
3636
* @var string The path to the SVG test image.
3737
*/
38-
private static $test_svg_file;
38+
private static string $test_svg_file;
3939

4040
/**
4141
* @var string The path to the test video.
4242
*/
43-
private static $test_video_file;
43+
private static string $test_video_file;
4444

4545
/**
4646
* @var string The path to the test audio.
4747
*/
48-
private static $test_audio_file;
48+
private static string $test_audio_file;
4949

5050
/**
5151
* @var string The path to the test RTF file.
5252
*/
53-
private static $test_rtf_file;
53+
private static string $test_rtf_file;
5454

5555
/**
56-
* @var array The recorded posts query clauses.
56+
* @var string[] The recorded posts query clauses.
5757
*/
58-
protected $posts_clauses;
58+
protected array $posts_clauses;
5959

6060
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
6161
self::$superadmin_id = $factory->user->create(
@@ -3378,6 +3378,7 @@ public function test_sideload_route_includes_generate_sub_sizes_arg(): void {
33783378
$routes = rest_get_server()->get_routes();
33793379
$endpoint = $routes['/wp/v2/media/(?P<id>[\d]+)/sideload'][0];
33803380
$args = $endpoint['args'];
3381+
$this->assertIsArray( $args );
33813382

33823383
$this->assertArrayHasKey( 'generate_sub_sizes', $args, 'Route should have generate_sub_sizes arg.' );
33833384
$this->assertSame( 'boolean', $args['generate_sub_sizes']['type'], 'generate_sub_sizes should be a boolean.' );
@@ -3402,9 +3403,10 @@ public function test_sideload_original_heic_writes_metadata_original(): void {
34023403
$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
34033404
$request->set_header( 'Content-Type', 'image/jpeg' );
34043405
$request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
3405-
$request->set_body( file_get_contents( self::$test_file ) );
3406+
$request->set_body( (string) file_get_contents( self::$test_file ) );
34063407
$response = rest_get_server()->dispatch( $request );
34073408
$attachment_id = $response->get_data()['id'];
3409+
$this->assertIsInt( $attachment_id );
34083410

34093411
$this->assertSame( 201, $response->get_status() );
34103412

@@ -3416,12 +3418,13 @@ public function test_sideload_original_heic_writes_metadata_original(): void {
34163418
$request->set_header( 'Content-Disposition', 'attachment; filename=canola.heic' );
34173419
$request->set_param( 'image_size', 'original-heic' );
34183420
$request->set_param( 'convert_format', false );
3419-
$request->set_body( file_get_contents( DIR_TESTDATA . '/images/test-image.heic' ) );
3421+
$request->set_body( (string) file_get_contents( DIR_TESTDATA . '/images/test-image.heic' ) );
34203422
$response = rest_get_server()->dispatch( $request );
34213423

34223424
$this->assertSame( 200, $response->get_status(), 'Sideloading original-heic should succeed.' );
34233425

34243426
$metadata = wp_get_attachment_metadata( $attachment_id );
3427+
$this->assertIsArray( $metadata );
34253428
$this->assertArrayHasKey( 'original', $metadata, "Metadata should contain 'original' for the HEIC companion." );
34263429
$this->assertMatchesRegularExpression( '/canola.*\.heic$/', $metadata['original'], "Metadata 'original' should reference the HEIC filename." );
34273430
$this->assertArrayNotHasKey( 'original_image', $metadata, "Metadata 'original_image' should be untouched by the HEIC sideload." );

0 commit comments

Comments
 (0)