Skip to content

Commit 0840edc

Browse files
Make a few tests more readable.
1 parent d2da131 commit 0840edc

4 files changed

Lines changed: 124 additions & 50 deletions

File tree

src/class-tiny-settings.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,12 @@ public function get_active_tinify_sizes() {
279279
}
280280

281281
public function get_resize_enabled() {
282+
/* This only applies if the original is being resized. */
283+
$sizes = $this->get_sizes();
284+
if ( ! $sizes[ Tiny_Image::ORIGINAL ]['tinify'] ) {
285+
return false;
286+
}
287+
282288
$setting = get_option( self::get_prefixed_name( 'resize_original' ) );
283289
return isset( $setting['enabled'] ) && $setting['enabled'] === 'on';
284290
}

test/unit/TinyPluginTest.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -207,25 +207,4 @@ public function test_wrong_metadata_should_save_tiny_metadata() {
207207
$this->subject->compress_on_upload( $testmeta, 1 );
208208
$this->assertEquals( 2, count( $this->wp->getCalls( 'update_post_meta' ) ) );
209209
}
210-
211-
// skip TODO
212-
public function test_analyze_media_library() {
213-
// $this->assertEquals("analyze all JPEG/PNG and store file size for each image size in the tiny_compress_images metadata", "todo");
214-
}
215-
216-
public function test_create_api_key_new_user_should_return_api_key() {
217-
//Test if we get a api key back
218-
}
219-
220-
public function test_create_api_key_existing_user_should_not_return_api_key() {
221-
//Test if we get a api key back. We should not
222-
}
223-
224-
public function test_save_api_key_should_set_key_if_valid() {
225-
//Set the key if it is valid
226-
}
227-
228-
public function test_save_api_key_should_not_set_key_if_invalid() {
229-
//Do not set key if invalid
230-
}
231210
}

test/unit/TinySettingsTest.php

Lines changed: 109 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,45 @@ public function test_admin_init_should_register_keys() {
2020
}
2121

2222
public function test_admin_init_should_add_settings_section() {
23-
$this->assertEquals(array(
24-
array( 'tinypng_settings', 'JPEG and PNG optimization', array($this->subject, 'render_section'), 'media' ),
25-
array( 'section_end', '', array($this->subject, 'render_section_end'), 'media'),), $this->wp->getCalls( 'add_settings_section' ) );
23+
$this->assertEquals( array(
24+
array(
25+
'tinypng_settings',
26+
'JPEG and PNG optimization',
27+
array( $this->subject, 'render_section' ),
28+
'media',
29+
),
30+
array(
31+
'section_end',
32+
'',
33+
array( $this->subject, 'render_section_end' ),
34+
'media',
35+
),
36+
), $this->wp->getCalls( 'add_settings_section' ) );
2637
}
2738

2839
public function test_admin_init_should_add_settings_field() {
29-
$this->assertEquals(array(
30-
array( 'tinypng_api_key', 'TinyPNG account', array( $this->subject, 'render_pending_status' ), 'media', 'tinypng_settings' ),
31-
array( 'tinypng_sizes', 'File compression', array( $this->subject, 'render_sizes' ), 'media', 'tinypng_settings' ),
32-
array( 'tinypng_resize_original', 'Original image', array( $this->subject, 'render_resize' ), 'media', 'tinypng_settings' ),
33-
), $this->wp->getCalls( 'add_settings_field' ));
40+
$this->assertEquals( array(
41+
array(
42+
'tinypng_api_key',
43+
'TinyPNG account',
44+
array( $this->subject, 'render_pending_status' ),
45+
'media',
46+
'tinypng_settings',
47+
),
48+
array(
49+
'tinypng_sizes',
50+
'File compression', array( $this->subject, 'render_sizes' ),
51+
'media',
52+
'tinypng_settings',
53+
),
54+
array(
55+
'tinypng_resize_original',
56+
'Original image',
57+
array( $this->subject, 'render_resize' ),
58+
'media',
59+
'tinypng_settings',
60+
),
61+
), $this->wp->getCalls( 'add_settings_field' ) );
3462
}
3563

3664
public function test_should_retrieve_sizes_with_settings() {
@@ -39,9 +67,6 @@ public function test_should_retrieve_sizes_with_settings() {
3967
$this->wp->addOption( 'tinypng_sizes[post-thumbnail]', 'on' );
4068
$this->wp->addImageSize( 'post-thumbnail', array( 'width' => 825, 'height' => 510 ) );
4169

42-
global $_wp_additional_image_sizes;
43-
$_wp_additional_image_sizes = array( 'post-thumbnail' => array( 'width' => 825, 'height' => 510 ) );
44-
4570
$this->subject->get_sizes();
4671
$this->assertEquals(array(
4772
0 => array( 'width' => null, 'height' => null, 'tinify' => true ),
@@ -104,48 +129,97 @@ public function test_should_show_additional_size_without_width() {
104129
);
105130
}
106131

107-
public function test_should_return_resize_enabled() {
132+
public function test_get_resize_enabled_should_return_true_if_enabled() {
108133
$this->wp->addOption( 'tinypng_resize_original', array( 'enabled' => 'on' ) );
109134
$this->assertEquals( true, $this->subject->get_resize_enabled() );
110135
}
111136

112-
public function test_should_return_resize_not_enabled_without_configuration() {
137+
public function test_get_resize_enabled_should_return_false_without_configuration() {
113138
$this->wp->addOption( 'tinypng_resize_original', array() );
114139
$this->assertEquals( false, $this->subject->get_resize_enabled() );
115140
}
116141

142+
public function test_get_resize_enabled_should_return_false_if_original_is_not_compressed() {
143+
$this->wp->addOption( 'tinypng_sizes[0]', 'off' );
144+
$this->wp->addOption( 'tinypng_resize_original', array( 'enabled' => 'on' ) );
145+
$this->assertEquals( false, $this->subject->get_resize_enabled() );
146+
}
147+
117148
public function test_should_return_resize_options_with_width_and_height() {
118-
$this->wp->addOption( 'tinypng_resize_original', array( 'enabled' => 'on', 'width' => '800', 'height' => '600' ) );
119-
$this->assertEquals( array( 'method' => 'fit', 'width' => 800, 'height' => 600), $this->subject->get_resize_options( Tiny_Image::ORIGINAL ) );
149+
$this->wp->addOption(
150+
'tinypng_resize_original',
151+
array( 'enabled' => 'on', 'width' => '800', 'height' => '600' )
152+
);
153+
154+
$this->assertEquals(
155+
array( 'method' => 'fit', 'width' => 800, 'height' => 600),
156+
$this->subject->get_resize_options( Tiny_Image::ORIGINAL )
157+
);
120158
}
121159

122160
public function test_should_return_resize_options_without_width() {
123-
$this->wp->addOption( 'tinypng_resize_original', array( 'enabled' => 'on', 'width' => '', 'height' => '600' ) );
124-
$this->assertEquals( array( 'method' => 'scale', 'height' => 600), $this->subject->get_resize_options( Tiny_Image::ORIGINAL ) );
161+
$this->wp->addOption(
162+
'tinypng_resize_original',
163+
array( 'enabled' => 'on', 'width' => '', 'height' => '600' )
164+
);
165+
166+
$this->assertEquals(
167+
array( 'method' => 'scale', 'height' => 600),
168+
$this->subject->get_resize_options( Tiny_Image::ORIGINAL )
169+
);
125170
}
126171

127172
public function test_should_return_resize_options_without_height() {
128-
$this->wp->addOption( 'tinypng_resize_original', array( 'enabled' => 'on', 'width' => '800', 'height' => '' ) );
129-
$this->assertEquals( array( 'method' => 'scale', 'width' => 800), $this->subject->get_resize_options( Tiny_Image::ORIGINAL ) );
173+
$this->wp->addOption(
174+
'tinypng_resize_original',
175+
array( 'enabled' => 'on', 'width' => '800', 'height' => '' )
176+
);
177+
178+
$this->assertEquals(
179+
array( 'method' => 'scale', 'width' => 800),
180+
$this->subject->get_resize_options( Tiny_Image::ORIGINAL )
181+
);
130182
}
131183

132184
public function test_should_return_resize_options_with_invaled_width() {
133-
$this->wp->addOption( 'tinypng_resize_original', array( 'enabled' => 'on', 'width' => '-1', 'height' => '600' ) );
134-
$this->assertEquals( array( 'method' => 'scale', 'height' => 600), $this->subject->get_resize_options( Tiny_Image::ORIGINAL ) );
185+
$this->wp->addOption(
186+
'tinypng_resize_original',
187+
array( 'enabled' => 'on', 'width' => '-1', 'height' => '600' )
188+
);
189+
190+
$this->assertEquals(
191+
array( 'method' => 'scale', 'height' => 600),
192+
$this->subject->get_resize_options( Tiny_Image::ORIGINAL )
193+
);
135194
}
136195

137196
public function test_should_return_resize_options_with_invaled_height() {
138-
$this->wp->addOption( 'tinypng_resize_original', array( 'enabled' => 'on', 'width' => '800', 'height' => '-1' ) );
139-
$this->assertEquals( array( 'method' => 'scale', 'width' => 800), $this->subject->get_resize_options( Tiny_Image::ORIGINAL ) );
197+
$this->wp->addOption(
198+
'tinypng_resize_original',
199+
array( 'enabled' => 'on', 'width' => '800', 'height' => '-1' )
200+
);
201+
202+
$this->assertEquals(
203+
array( 'method' => 'scale', 'width' => 800),
204+
$this->subject->get_resize_options( Tiny_Image::ORIGINAL )
205+
);
140206
}
141207

142208
public function test_should_not_return_resize_options_without_with_and_height() {
143-
$this->wp->addOption( 'tinypng_resize_original', array( 'enabled' => 'on', 'width' => '', 'height' => '' ) );
209+
$this->wp->addOption(
210+
'tinypng_resize_original',
211+
array( 'enabled' => 'on', 'width' => '', 'height' => '' )
212+
);
213+
144214
$this->assertEquals( false, $this->subject->get_resize_options( Tiny_Image::ORIGINAL ) );
145215
}
146216

147217
public function test_should_not_return_resize_options_when_not_enabled() {
148-
$this->wp->addOption( 'tinypng_resize_original', array( 'width' => '800', 'height' => '600') );
218+
$this->wp->addOption(
219+
'tinypng_resize_original',
220+
array( 'width' => '800', 'height' => '600')
221+
);
222+
149223
$this->assertEquals( false, $this->subject->get_resize_options( Tiny_Image::ORIGINAL ) );
150224
}
151225

@@ -161,11 +235,19 @@ public function test_should_return_include_metadata_not_enabled_without_configur
161235

162236
public function test_should_return_preserve_options_when_enabled() {
163237
$this->wp->addOption( 'tinypng_preserve_data', array( 'copyright' => 'on') );
164-
$this->assertEquals( array( '0' => 'copyright'), $this->subject->get_preserve_options( Tiny_Image::ORIGINAL ) );
238+
239+
$this->assertEquals(
240+
array( '0' => 'copyright'),
241+
$this->subject->get_preserve_options( Tiny_Image::ORIGINAL )
242+
);
165243
}
166244

167245
public function test_should_not_return_preserve_options_when_disabled() {
168246
$this->wp->addOption( 'tinypng_include_metadata', array() );
169-
$this->assertEquals( array(), $this->subject->get_preserve_options( Tiny_Image::ORIGINAL ) );
247+
248+
$this->assertEquals(
249+
array(),
250+
$this->subject->get_preserve_options( Tiny_Image::ORIGINAL )
251+
);
170252
}
171253
}

test/unit/TinyTestCase.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
use org\bovigo\vfs\vfsStream;
1010

1111
function plugin_autoloader($class) {
12-
$file = dirname( __FILE__ ) . '/../../src/class-' . str_replace( '_', '-', strtolower( $class ) ) . '.php';
12+
$file = dirname( __FILE__ ) . '/../../src/class-' .
13+
str_replace( '_', '-', strtolower( $class ) ) . '.php';
14+
1315
if ( file_exists( $file ) ) {
1416
include $file;
1517
} else {
@@ -65,7 +67,12 @@ protected function assertEqualWithinDelta($expected, $actual, $delta, $message =
6567
// @codingStandardsIgnoreEnd
6668

6769
protected function json($file_name) {
68-
return json_decode( file_get_contents( dirname( __FILE__ ) . '/../fixtures/json/' . $file_name . '.json' ), true );
70+
return json_decode(
71+
file_get_contents(
72+
dirname( __FILE__ ) . '/../fixtures/json/' . $file_name . '.json'
73+
),
74+
true
75+
);
6976
}
7077

7178
public static function set_up_before_class() {

0 commit comments

Comments
 (0)