Skip to content

Commit 872c31d

Browse files
test: add comprehensive test for bulk optimization with wpdb mocking
- Add test for optimize command when no attachments are specified - Mock global wpdb object with get_results method for database operations - Define ARRAY_A constant required for WordPress database operations - Mock wp_get_attachment_metadata to simulate attachment metadata retrieval - Create virtual test images and database results for realistic test scenarios - Verify compress_file method is called when processing uncompressed attachments - Fix parameter expectations in existing compress_file test mock
1 parent f479c4c commit 872c31d

1 file changed

Lines changed: 72 additions & 5 deletions

File tree

test/unit/TinyCliTest.php

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ public function test_will_compress_attachments_given_in_params()
111111
$expected['resize'],
112112
$expected['preserve'],
113113
$expected['convert_opts']
114-
)
115-
->willReturn('foo');
114+
);
116115
$settings->set_compressor($mockCompressor);
117116

118117
$command = new Tiny_Command($settings);
@@ -122,8 +121,76 @@ public function test_will_compress_attachments_given_in_params()
122121
));
123122
}
124123

125-
public function test_will_compress_all_uncompressed_attachments_if_none_given() {
126-
124+
public function test_will_compress_all_uncompressed_attachments_if_none_given()
125+
{
126+
// Define WordPress constants needed for wpdb operations
127+
if (!defined('ARRAY_A')) {
128+
define('ARRAY_A', 'ARRAY_A');
129+
}
130+
131+
$this->wp->stub('get_post_mime_type', function ($i) {
132+
return 'image/png';
133+
});
134+
135+
// Mock the global wpdb object
136+
global $wpdb;
137+
138+
// Create a mock class that has the methods we need
139+
$wpdb = $this->getMockBuilder(stdClass::class)
140+
->addMethods(['get_results'])
141+
->getMock();
142+
143+
$wpdb->posts = 'wp_posts';
144+
$wpdb->postmeta = 'wp_postmeta';
145+
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+
155+
$mock_results = array(
156+
array(
157+
'ID' => 1,
158+
'post_title' => 'Test Image',
159+
'meta_value' => serialize(array(
160+
'width' => 1200,
161+
'height' => 800,
162+
'file' => '2025/07/test.png',
163+
'sizes' => array()
164+
)),
165+
'unique_attachment_name' => '2025/07/test.png',
166+
'tiny_meta_value' => ''
167+
)
168+
);
169+
$virtual_test_image = array(
170+
'path' => '2025/07',
171+
'images' => array(
172+
array(
173+
'size' => 137856,
174+
'file' => 'test.png',
175+
),
176+
)
177+
);
178+
$this->wp->createImagesFromJSON($virtual_test_image);
179+
180+
$wpdb->method('get_results')
181+
->willReturn($mock_results);
182+
183+
$settings = new Tiny_Settings();
184+
$mockCompressor = $this->createMock(Tiny_Compress::class);
185+
186+
$mockCompressor->expects($this->once())
187+
->method('compress_file')
188+
->willReturn(array('output' => array('size' => 1000)));
189+
190+
$settings->set_compressor($mockCompressor);
191+
192+
$command = new Tiny_Command($settings);
193+
194+
$command->optimize(array(), array());
127195
}
128-
129196
}

0 commit comments

Comments
 (0)