Skip to content

Commit 3bfeff7

Browse files
author
Jacob Middag
committed
Better resize integration testing
1 parent e95301d commit 3bfeff7

7 files changed

Lines changed: 86 additions & 67 deletions

File tree

test/fixtures/input-large.jpg

105 KB
Loading

test/integration/CompressIntegrationTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ public function testIncorrectJsonButton() {
8282
}
8383

8484
public function testResizeFit() {
85-
$this->set_api_key('RESIZE123');
85+
$this->set_api_key('JPG123');
8686
$this->enable_resize(300, 200);
87-
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-example.png');
87+
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-large.jpg');
8888
$this->assertContains('Resized original to 300x200',
8989
self::$driver->findElement(WebDriverBy::cssSelector('td.tiny-compress-images'))->getText());
9090
$this->view_edit_image();
@@ -93,9 +93,9 @@ public function testResizeFit() {
9393
}
9494

9595
public function testResizeScale() {
96-
$this->set_api_key('RESIZE123');
96+
$this->set_api_key('JPG123');
9797
$this->enable_resize(0, 200);
98-
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-example.png');
98+
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-large.jpg');
9999
$this->assertContains('Resized original to 300x200',
100100
self::$driver->findElement(WebDriverBy::cssSelector('td.tiny-compress-images'))->getText());
101101
$this->view_edit_image();
@@ -105,26 +105,26 @@ public function testResizeScale() {
105105

106106
public function testResizeNotNeeded()
107107
{
108-
$this->set_api_key('RESIZE123');
108+
$this->set_api_key('JPG123');
109109
$this->enable_resize(30000, 20000);
110-
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-example.png');
110+
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-large.jpg');
111111
$this->assertNotContains('Resized original',
112112
self::$driver->findElement(WebDriverBy::cssSelector('td.tiny-compress-images'))->getText());
113113
$this->view_edit_image();
114-
$this->assertContains('Dimensions: 1080 × 720',
114+
$this->assertContains('Dimensions: 1080 × 330',
115115
self::$driver->findElement(WebDriverBy::cssSelector('div.misc-pub-dimensions'))->getText());
116116
}
117117

118118
public function testResizeDisabled()
119119
{
120-
$this->set_api_key('RESIZE123');
120+
$this->set_api_key('JPG123');
121121
$this->enable_resize(300, 200);
122122
$this->disable_resize();
123-
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-example.png');
123+
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-large.jpg');
124124
$this->assertNotContains('Resized original',
125125
self::$driver->findElement(WebDriverBy::cssSelector('td.tiny-compress-images'))->getText());
126126
$this->view_edit_image();
127-
$this->assertContains('Dimensions: 1080 × 720',
127+
$this->assertContains('Dimensions: 1080 × 330',
128128
self::$driver->findElement(WebDriverBy::cssSelector('div.misc-pub-dimensions'))->getText());
129129
}
130130
}

test/integration/IntegrationTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ protected function disable_resize() {
8989
self::$driver->findElement(WebDriverBy::tagName('form'))->submit();
9090
}
9191

92-
protected function view_edit_image($image_title = 'input-example') {
92+
protected function view_edit_image($image_title = 'input-large') {
9393
$url = wordpress('/wp-admin/upload.php');
9494
if (self::$driver->getCurrentUrl() != $url) {
9595
self::$driver->get($url);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
define('SESSION_FILE', '/tmp/session.dat');
4+
5+
if (file_exists(SESSION_FILE)) {
6+
$session = unserialize(file_get_contents(SESSION_FILE));
7+
} else {
8+
$session = array('Compression-Count' => 0);
9+
}
10+
11+
function save_session() {
12+
global $session;
13+
if ($session) {
14+
file_put_contents(SESSION_FILE, serialize($session));
15+
} elseif (file_exists(SESSION_FILE)) {
16+
unlink(SESSION_FILE);
17+
}
18+
}
19+
register_shutdown_function('save_session');
20+
21+
function get_api_key() {
22+
$request_headers = apache_request_headers();
23+
if (!isset($request_headers['Authorization'])) {
24+
return null;
25+
}
26+
$basic_auth = base64_decode(str_replace('Basic ', '', $request_headers['Authorization']));
27+
return next(explode(':', $basic_auth));
28+
}
29+
30+
function get_json_body() {
31+
return json_decode(file_get_contents("php://input"));
32+
}
33+
34+
function mock_invalid_response() {
35+
global $session;
36+
37+
header('HTTP/1.1 401 Unauthorized');
38+
header("Content-Type: application/json; charset=utf-8");
39+
40+
$response = array(
41+
"error" => "Unauthorized",
42+
"message" => "Credentials are invalid"
43+
);
44+
return json_encode($response);
45+
}
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
11
<?php
22
ob_start();
33

4+
require_once('common.php');
5+
46
if (preg_match('#output/.+[.](png|jpg)$#', $_SERVER['REQUEST_URI'], $match)) {
57
$file = str_replace('/', '-', $match[0]);
8+
$ext = $match[1];
69
$mime = $match[1] == 'jpg' ? 'image/jpeg' : "image/$ext";
710
} else {
811
$file = null;
912
}
1013

14+
$api_key = get_api_key();
15+
if (!is_null($api_key)) {
16+
$data = get_json_body();
17+
if (is_null($data) || $api_key != 'JPG123') {
18+
mock_invalid_response();
19+
ob_end_flush();
20+
exit();
21+
}
22+
23+
$resize = $data->resize;
24+
if ($resize->method) {
25+
$file = "output-resized.$ext";
26+
header("Image-Width: {$resize->width}");
27+
header("Image-Height: {$resize->height}");
28+
}
29+
}
30+
1131
if ($file && file_exists($file)) {
1232
header("Content-Type: $mime");
1333
header('Content-Disposition: attachment');
@@ -16,4 +36,4 @@
1636
header("HTTP/1.1 404 Not Found");
1737
}
1838

19-
ob_end_flush();
39+
ob_end_flush();
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
<?php
22

3-
define('SESSION_FILE', '/tmp/session.dat');
4-
5-
if (file_exists(SESSION_FILE)) {
6-
unlink(SESSION_FILE);
7-
}
3+
require('common.php');
4+
$session = null;

test/mock-tinypng-webservice/shrink.php

Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
<?php
22
ob_start();
33

4-
define('SESSION_FILE', '/tmp/session.dat');
5-
6-
if (file_exists(SESSION_FILE)) {
7-
$session = unserialize(file_get_contents(SESSION_FILE));
8-
} else {
9-
$session = array('Compression-Count' => 0);
10-
}
4+
require_once('common.php');
115

126
function mock_png_response() {
137
global $session;
148

159
$session['Compression-Count'] += 1;
1610
header('HTTP/1.1 201 Created');
1711
header("Location: http://webservice/output/example.png");
12+
header("Content-Type: application/json; charset=utf-8");
1813
header("Compression-Count: {$session['Compression-Count']}");
1914
$response = array(
2015
"input" => array("size" => 161885, "type" => "image/png"),
@@ -29,6 +24,7 @@ function mock_jpg_response() {
2924
$session['Compression-Count'] += 1;
3025
header('HTTP/1.1 201 Created');
3126
header("Location: http://webservice/output/example.jpg");
27+
header("Content-Type: application/json; charset=utf-8");
3228
header("Compression-Count: {$session['Compression-Count']}");
3329

3430
$response = array(
@@ -44,6 +40,7 @@ function mock_large_response() {
4440
$session['Compression-Count'] += 1;
4541
header('HTTP/1.1 201 Created');
4642
header("Location: http://webservice/output/large.png");
43+
header("Content-Type: application/json; charset=utf-8");
4744
header("Compression-Count: {$session['Compression-Count']}");
4845

4946
$response = array(
@@ -53,19 +50,6 @@ function mock_large_response() {
5350
return json_encode($response);
5451
}
5552

56-
function mock_invalid_response() {
57-
global $session;
58-
59-
header('HTTP/1.1 401 Unauthorized');
60-
header("Content-Type: application/json; charset=utf-8");
61-
62-
$response = array(
63-
"error" => "Unauthorized",
64-
"message" => "Credentials are invalid"
65-
);
66-
return json_encode($response);
67-
}
68-
6953
function mock_empty_response() {
7054
global $session;
7155

@@ -100,31 +84,12 @@ function mock_invalid_json_response() {
10084
$session['Compression-Count'] += 1;
10185
header('HTTP/1.1 201 Created');
10286
header("Location: http://webservice/output/example.png");
87+
header("Content-Type: application/json; charset=utf-8");
10388
header("Compression-Count: {$session['Compression-Count']}");
10489
return '{invalid: json}';
10590
}
10691

107-
function mock_resized_response() {
108-
global $session;
109-
110-
$session['Compression-Count'] += 1;
111-
header('HTTP/1.1 201 Created');
112-
header("Location: http://webservice/output/resized.jpg");
113-
header("Compression-Count: {$session['Compression-Count']}");
114-
$response = array(
115-
"input" => array("size" => 161885, "type" => "image/jpg"),
116-
"output" => array("size" => 14238, "type" => "image/jpg", "ratio" => 0.95, "width" => 1080, "height" => 720)
117-
);
118-
return json_encode($response);
119-
}
120-
121-
$request_headers = apache_request_headers();
122-
$basic_auth = base64_decode(str_replace('Basic ', '', $request_headers['Authorization']));
123-
$api_key_elements = explode(':', $basic_auth);
124-
$api_key = $api_key_elements[1];
125-
126-
header("Content-Type: application/json; charset=utf-8");
127-
92+
$api_key = get_api_key();
12893
if ($api_key == 'PNG123') {
12994
if (intval($_SERVER['CONTENT_LENGTH']) == 0) {
13095
echo mock_empty_response();
@@ -145,16 +110,8 @@ function mock_resized_response() {
145110
}
146111
} else if ($api_key == 'LIMIT123') {
147112
echo mock_limit_reached_response();
148-
} else if ($api_key == 'RESIZE123') {
149-
if (intval($_SERVER['CONTENT_LENGTH']) == 0) {
150-
echo mock_empty_response();
151-
} else {
152-
echo mock_resized_response();
153-
}
154113
} else {
155114
echo mock_invalid_response();
156115
}
157116

158-
file_put_contents(SESSION_FILE, serialize($session));
159-
160-
ob_end_flush();
117+
ob_end_flush();

0 commit comments

Comments
 (0)