Skip to content

Commit f479c4c

Browse files
refactor: simplify is_valid_attachment and add comprehensive CLI tests
- Simplify is_valid_attachment method to use get_post_mime_type() directly - Remove redundant post type and attachment validation checks - Add comprehensive test for optimize command with attachment parameters - Mock WordPress functions for proper test isolation - Create virtual test images for file system simulation - Add mock for Tiny_Compress with expected parameters validation
1 parent d59f5f3 commit f479c4c

2 files changed

Lines changed: 90 additions & 30 deletions

File tree

src/class-tiny-cli.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,22 +146,11 @@ private function optimize_attachment( $attachment_id ) {
146146
}
147147

148148
private function is_valid_attachment( $attachment_id ) {
149-
$attachment = get_post( $attachment_id );
150-
if ( ! $attachment ) {
151-
return false;
152-
}
153-
154-
if ( 'attachment' !== $attachment->post_type ) {
155-
return false;
156-
}
157-
158-
// Check if it's an image
159-
$mime_type = $attachment->post_mime_type;
149+
$mime_type = get_post_mime_type( $attachment_id );
160150
if ( ! $mime_type || strpos( $mime_type, 'image/' ) !== 0 ) {
161151
return false;
162152
}
163153

164-
// Check if it's a supported format
165154
$supported_types = array( 'image/jpeg', 'image/png', 'image/webp' );
166155
if ( ! in_array( $mime_type, $supported_types, true ) ) {
167156
return false;

test/unit/TinyCliTest.php

Lines changed: 89 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,129 @@
11
<?php
22

3-
require_once dirname( __FILE__ ) . '/TinyTestCase.php';
3+
require_once dirname(__FILE__) . '/TinyTestCase.php';
4+
require_once dirname(__FILE__) . '/../../src/class-tiny-cli.php';
45

5-
class Tiny_Cli_Test extends Tiny_TestCase {
6+
class Tiny_Cli_Test extends Tiny_TestCase
7+
{
8+
protected $subject;
9+
protected $compressor;
610

7-
8-
public function set_up() {
11+
public function set_up()
12+
{
913
parent::set_up();
1014
}
1115

12-
public function test_will_register_command_on_cli_init_hook() {
16+
public function test_will_register_command_on_cli_init_hook()
17+
{
1318
$this->wp->defaults();
14-
19+
1520
if (!defined('WP_CLI')) {
1621
define('WP_CLI', true);
1722
}
18-
23+
1924
$tiny_cli = new Tiny_Cli(null);
20-
25+
2126
$add_action_calls = $this->wp->getCalls('add_action');
2227
$cli_init_found = false;
23-
28+
2429
foreach ($add_action_calls as $call) {
2530
if ($call[0] === 'cli_init' && $call[1] === array($tiny_cli, 'register_command')) {
2631
$cli_init_found = true;
2732
break;
2833
}
2934
}
30-
35+
3136
$this->assertTrue($cli_init_found, 'register_command should be hooked to cli_init');
3237
}
3338

34-
public function test_will_not_hook_if_cli_is_unavailable() {
39+
public function test_will_not_hook_if_cli_is_unavailable()
40+
{
3541
$this->wp->defaults();
36-
42+
3743
$add_action_calls = $this->wp->getCalls('add_action');
3844
$cli_init_found = false;
39-
45+
4046
foreach ($add_action_calls as $call) {
4147
if ($call[0] === 'cli_init') {
4248
$cli_init_found = true;
4349
break;
4450
}
4551
}
46-
52+
4753
$this->assertFalse($cli_init_found, 'No cli_init hooks should be registered when WP_CLI is not available');
4854
}
4955

50-
public function test_will_compress_attachments_given_in_params() {
51-
$command = new Tiny_Command();
56+
public function test_will_compress_attachments_given_in_params()
57+
{
58+
$this->wp->stub('get_post_mime_type', function ($i) {
59+
return 'image/png';
60+
});
61+
$this->wp->stub('wp_get_attachment_metadata', function ($i) {
62+
return array(
63+
'width' => 1256,
64+
'height' => 1256,
65+
'file' => '2025/07/test.png',
66+
'sizes' => array(
67+
'thumbnail' => array(
68+
'file' => 'test-150x150.png',
69+
'width' => 150,
70+
'height' => 150,
71+
'mime-type' => 'image/png'
72+
)
73+
),
74+
);
75+
});
76+
77+
$virtual_test_image = array(
78+
'path' => '2025/07',
79+
'images' => array(
80+
array(
81+
'size' => 137856,
82+
'file' => 'test.png',
83+
),
84+
array(
85+
'size' => 37856,
86+
'file' => 'test-150x150.jpg',
87+
),
88+
)
89+
);
90+
$this->wp->createImagesFromJSON($virtual_test_image);
91+
92+
$settings = new Tiny_Settings();
93+
$mockCompressor = $this->createMock(Tiny_Compress::class);
94+
95+
$expected = array(
96+
'file' => "vfs://root/wp-content/uploads/2025/07/test.png",
97+
'resize' => false,
98+
'preserve' => array(),
99+
'convert_opts' => array(
100+
'convert' => false,
101+
'convert_to' => array(
102+
'image/avif',
103+
'image/webp'
104+
)
105+
),
106+
);
107+
$mockCompressor->expects($this->once())
108+
->method('compress_file')
109+
->with(
110+
$expected['file'],
111+
$expected['resize'],
112+
$expected['preserve'],
113+
$expected['convert_opts']
114+
)
115+
->willReturn('foo');
116+
$settings->set_compressor($mockCompressor);
117+
118+
$command = new Tiny_Command($settings);
119+
52120
$command->optimize(array(), array(
53-
"attachments" => array(4030),
121+
"attachments" => '4030',
54122
));
123+
}
55124

125+
public function test_will_compress_all_uncompressed_attachments_if_none_given() {
56126

57127
}
58-
}
128+
129+
}

0 commit comments

Comments
 (0)