Skip to content

Commit 21727b4

Browse files
Store compression count.
1 parent e2acc02 commit 21727b4

4 files changed

Lines changed: 55 additions & 4 deletions

File tree

src/class-tiny-compress-client.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@
2929
}
3030

3131
class Tiny_Compress_Client extends Tiny_Compress {
32+
private $last_error_code = 0;
33+
private $last_message = '';
34+
private $proxy;
35+
3236
protected function __construct($api_key, $after_compress_callback) {
3337
parent::__construct( $after_compress_callback );
3438

35-
$this->last_error_code = 0;
36-
$this->last_message = '';
3739
$this->proxy = new WP_HTTP_Proxy();
3840

3941
\Tinify\setAppIdentifier( self::identifier() );

src/class-tiny-compress-fopen.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@
1919
*/
2020

2121
class Tiny_Compress_Fopen extends Tiny_Compress {
22+
private $last_error_code = 0;
23+
private $compression_count;
24+
private $api_key;
25+
2226
protected static function identifier() {
2327
parent::identifier() . ' fopen';
2428
}
2529

2630
protected function __construct($api_key, $after_compress_callback) {
2731
parent::__construct( $after_compress_callback );
2832

29-
$this->last_error_code = 0;
3033
$this->api_key = $api_key;
3134
}
3235

@@ -35,7 +38,7 @@ public function can_create_key() {
3538
}
3639

3740
public function get_compression_count() {
38-
return null;
41+
return $this->compression_count;
3942
}
4043

4144
public function get_key() {
@@ -152,6 +155,10 @@ private function request($params, $url = Tiny_Config::URL) {
152155
$status_code = $this->parse_status_code( $headers );
153156
$headers = $this->parse_headers( $headers );
154157

158+
if ( isset( $headers['compression-count'] ) ) {
159+
$this->compression_count = intval( $headers['compression-count'] );
160+
}
161+
155162
$this->last_error_code = $status_code;
156163

157164
$response = stream_get_contents( $request );

test/helpers/mock-tinify-client.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ public function request($method, $url, $body = null, $header = array() ) {
1616
$body = isset( $handler['body'] ) ? $handler['body'] : '';
1717
$headers = isset( $handler['headers'] ) ? $handler['headers'] : array();
1818

19+
if ( isset( $headers["compression-count"] ) ) {
20+
\Tinify\Tinify::setCompressionCount(
21+
intval( $headers["compression-count"] )
22+
);
23+
}
24+
1925
$isError = $status <= 199 || $status >= 300;
2026
$isJson = true;
2127

test/unit/TinyCompressSharedTestCase.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,40 @@ public function test_compress_file_should_return_unauthorized_status() {
265265
$this->setExpectedException( 'Tiny_Exception' );
266266
throw $exception;
267267
}
268+
269+
public function test_get_compression_count_should_return_null_before_compresion() {
270+
$this->assertSame( null, $this->compressor->get_compression_count() );
271+
}
272+
273+
public function test_get_compression_count_should_return_count() {
274+
$this->register( 'POST', '/shrink', array(
275+
'status' => 201,
276+
'headers' => array(
277+
'location' => 'https://api.tinify.com/output/compressed.png',
278+
'content-type' => 'application/json',
279+
'compression-count' => 12,
280+
),
281+
'body' => '{}',
282+
));
283+
284+
$handler = array(
285+
'status' => 200,
286+
'headers' => array(
287+
'content-type' => 'image/png',
288+
'content-length' => 9,
289+
'image-width' => 10,
290+
'image-height' => 15,
291+
'compression-count' => 12,
292+
),
293+
'body' => 'optimized',
294+
);
295+
296+
$this->register( 'GET', '/output/compressed.png', $handler );
297+
$this->register( 'POST', '/output/compressed.png', $handler );
298+
299+
file_put_contents( $this->vfs->url() . '/image.png', 'unoptimized' );
300+
$this->compressor->compress_file( $this->vfs->url() . '/image.png' );
301+
302+
$this->assertSame( 12, $this->compressor->get_compression_count() );
303+
}
268304
}

0 commit comments

Comments
 (0)