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