Skip to content

Commit 1ecefe0

Browse files
refactor: reorganize bulk optimization test with clearer structure
- Reorganize test sections with descriptive comments for better readability - Group related mocking operations together (db, images, WordPress functions) - Move wpdb mock setup before result data for logical flow - Add clear section headers: mock db, create mock image, mock WordPress functions - Simplify compressor mock expectations without return value - Improve test maintainability and understanding
1 parent 872c31d commit 1ecefe0

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

test/unit/TinyCliTest.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -123,35 +123,18 @@ public function test_will_compress_attachments_given_in_params()
123123

124124
public function test_will_compress_all_uncompressed_attachments_if_none_given()
125125
{
126-
// Define WordPress constants needed for wpdb operations
126+
// mock db
127127
if (!defined('ARRAY_A')) {
128128
define('ARRAY_A', 'ARRAY_A');
129129
}
130-
131-
$this->wp->stub('get_post_mime_type', function ($i) {
132-
return 'image/png';
133-
});
134-
135-
// Mock the global wpdb object
136130
global $wpdb;
137-
138-
// Create a mock class that has the methods we need
139131
$wpdb = $this->getMockBuilder(stdClass::class)
140132
->addMethods(['get_results'])
141133
->getMock();
142134

143135
$wpdb->posts = 'wp_posts';
144136
$wpdb->postmeta = 'wp_postmeta';
145137

146-
$this->wp->stub('wp_get_attachment_metadata', function ($i) {
147-
return array(
148-
'width' => 1256,
149-
'height' => 1256,
150-
'file' => '2025/07/test.png',
151-
'sizes' => array(),
152-
);
153-
});
154-
155138
$mock_results = array(
156139
array(
157140
'ID' => 1,
@@ -166,6 +149,11 @@ public function test_will_compress_all_uncompressed_attachments_if_none_given()
166149
'tiny_meta_value' => ''
167150
)
168151
);
152+
153+
$wpdb->method('get_results')
154+
->willReturn($mock_results);
155+
156+
// create mock image
169157
$virtual_test_image = array(
170158
'path' => '2025/07',
171159
'images' => array(
@@ -177,20 +165,32 @@ public function test_will_compress_all_uncompressed_attachments_if_none_given()
177165
);
178166
$this->wp->createImagesFromJSON($virtual_test_image);
179167

180-
$wpdb->method('get_results')
181-
->willReturn($mock_results);
168+
// mock wp_get_attachment_metadata
169+
$this->wp->stub('wp_get_attachment_metadata', function ($i) {
170+
return array(
171+
'width' => 1256,
172+
'height' => 1256,
173+
'file' => '2025/07/test.png',
174+
'sizes' => array(),
175+
);
176+
});
182177

178+
// mock get_post_mime_type
179+
$this->wp->stub('get_post_mime_type', function ($i) {
180+
return 'image/png';
181+
});
182+
183+
// mock compressor
183184
$settings = new Tiny_Settings();
184185
$mockCompressor = $this->createMock(Tiny_Compress::class);
186+
$settings->set_compressor($mockCompressor);
185187

188+
// create assertion
186189
$mockCompressor->expects($this->once())
187-
->method('compress_file')
188-
->willReturn(array('output' => array('size' => 1000)));
189-
190-
$settings->set_compressor($mockCompressor);
190+
->method('compress_file');
191191

192+
// invoke test
192193
$command = new Tiny_Command($settings);
193-
194194
$command->optimize(array(), array());
195195
}
196196
}

0 commit comments

Comments
 (0)