Skip to content

Commit 2324dc9

Browse files
committed
refactor tests
1 parent e909234 commit 2324dc9

File tree

5 files changed

+60
-64
lines changed

5 files changed

+60
-64
lines changed

tests/CurlTest.php

Lines changed: 47 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,27 @@
22

33
namespace Curl;
44

5-
class CurlTest extends \PHPUnit_Framework_TestCase {
5+
class CurlTest extends \PHPUnit_Framework_TestCase
6+
{
67

7-
const TEST_URL = 'http://php-curl-test.anezi.net/tests/server.php';
8-
9-
var $test_url = 'http://php-curl-test.anezi.net/tests/server/';
8+
const TEST_URL = 'http://php-curl-test.anezi.net';
109

1110
/**
1211
*
1312
* @var Curl
1413
*/
1514
protected $curl;
1615

17-
function setUp() {
18-
$this->curl = new Curl();
19-
$this->curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
20-
$this->curl->setOpt(CURLOPT_SSL_VERIFYHOST, FALSE);
16+
function setUp() {
17+
$this->curl = new Curl();
18+
$this->curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
19+
$this->curl->setOpt(CURLOPT_SSL_VERIFYHOST, FALSE);
2120
}
2221

23-
function server($request_method, $data='') {
24-
$request_method = strtolower($request_method);
25-
$this->curl->$request_method(self::TEST_URL, $data);
26-
return $this->curl->response;
22+
function server($request_method, $data='') {
23+
$request_method = strtolower($request_method);
24+
$this->curl->$request_method(self::TEST_URL . '/server.php', $data);
25+
return $this->curl->response;
2726
}
2827

2928
public function testExtensionLoaded() {
@@ -72,39 +71,36 @@ public function testPostMultidimensionalData() {
7271
'wobble',
7372
),
7473
);
75-
76-
$this->curl->post($this->test_url . 'post_multidimensional.php', $data);
77-
74+
75+
$this->curl->post(self::TEST_URL . '/post_multidimensional.php', $data);
76+
7877
$this->assertEquals(
7978
'key=file&file%5B0%5D=wibble&file%5B1%5D=wubble&file%5B2%5D=wobble',
8079
$this->curl->response);
8180

8281
}
8382

84-
public function testPostFilePathUpload() {
83+
public function testPostFilePathUpload()
84+
{
85+
8586
$file_path = $this->get_png();
8687

8788
$data = array(
8889
'key' => 'image',
89-
'image' => "@" . $file_path,
90+
'image' => '@' . $file_path,
9091
);
91-
92-
$image = file_get_contents($file_path);
9392

94-
$data = array(
95-
'key' => 'image',
96-
'image' => $image . ';filename=test.png;type=image/png',
97-
);
98-
99-
$this->curl->post($this->test_url . 'post_file_path_upload.php', $data);
100-
93+
$this->curl->setOpt(CURLOPT_RETURNTRANSFER, true);
94+
95+
$this->curl->post(self::TEST_URL . '/post_file_path_upload.php', $data);
96+
10197
$this->assertEquals(
10298
array(
10399
'request_method' => 'POST',
104100
'key' => 'image',
105101
'mime_content_type' => 'image/png'
106-
),
107-
json_decode($this->curl->response, true));
102+
),
103+
json_decode($this->curl->response, true));
108104

109105
unlink($file_path);
110106
}
@@ -130,7 +126,7 @@ public function testPutFileHandle() {
130126
$this->curl->setopt(CURLOPT_PUT, TRUE);
131127
$this->curl->setopt(CURLOPT_INFILE, $tmp_file);
132128
$this->curl->setopt(CURLOPT_INFILESIZE, strlen($png));
133-
$this->curl->put(self::TEST_URL, array(
129+
$this->curl->put(self::TEST_URL . '/server.php', array(
134130
'test' => 'put_file_handle',
135131
));
136132

@@ -155,7 +151,7 @@ public function testBasicHttpAuth() {
155151

156152
$data = array();
157153

158-
$this->curl->get($this->test_url . 'http_basic_auth.php', $data);
154+
$this->curl->get(self::TEST_URL . '/http_basic_auth.php', $data);
159155

160156
$this->assertEquals('canceled', $this->curl->response);
161157

@@ -164,7 +160,7 @@ public function testBasicHttpAuth() {
164160

165161
$this->curl->setBasicAuthentication($username, $password);
166162

167-
$this->curl->get($this->test_url . 'http_basic_auth.php', $data);
163+
$this->curl->get(self::TEST_URL . '/http_basic_auth.php', $data);
168164

169165
$this->assertEquals(
170166
'{"username":"myusername","password":"mypassword"}',
@@ -213,25 +209,25 @@ public function testHeaders() {
213209
)) === 'application/json');
214210
}
215211

216-
function create_png() {
217-
// PNG image data, 1 x 1, 1-bit colormap, non-interlaced
218-
ob_start();
219-
imagepng(imagecreatefromstring(base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7')));
220-
$raw_image = ob_get_contents();
221-
ob_end_clean();
222-
return $raw_image;
223-
}
224-
225-
function create_tmp_file($data) {
226-
$tmp_file = tmpfile();
227-
fwrite($tmp_file, $data);
228-
rewind($tmp_file);
229-
return $tmp_file;
230-
}
231-
232-
function get_png() {
233-
$tmp_filename = tempnam('/tmp', 'php-curl-class.');
234-
file_put_contents($tmp_filename, $this->create_png());
235-
return $tmp_filename;
212+
function create_png() {
213+
// PNG image data, 1 x 1, 1-bit colormap, non-interlaced
214+
ob_start();
215+
imagepng(imagecreatefromstring(base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7')));
216+
$raw_image = ob_get_contents();
217+
ob_end_clean();
218+
return $raw_image;
219+
}
220+
221+
function create_tmp_file($data) {
222+
$tmp_file = tmpfile();
223+
fwrite($tmp_file, $data);
224+
rewind($tmp_file);
225+
return $tmp_file;
226+
}
227+
228+
function get_png() {
229+
$tmp_filename = tempnam('/tmp', 'php-curl-class.');
230+
file_put_contents($tmp_filename, $this->create_png());
231+
return $tmp_filename;
236232
}
237233
}
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22

3-
if (!isset($_SERVER['PHP_AUTH_USER'])) {
4-
header('WWW-Authenticate: Basic realm="My Realm"');
5-
header('HTTP/1.0 401 Unauthorized');
6-
echo 'canceled';
7-
exit;
8-
}
9-
10-
header('Content-Type: application/json');
11-
echo json_encode(array(
12-
'username' => $_SERVER['PHP_AUTH_USER'],
13-
'password' => $_SERVER['PHP_AUTH_PW'],
3+
if (!isset($_SERVER['PHP_AUTH_USER'])) {
4+
header('WWW-Authenticate: Basic realm="My Realm"');
5+
header('HTTP/1.0 401 Unauthorized');
6+
echo 'canceled';
7+
exit;
8+
}
9+
10+
header('Content-Type: application/json');
11+
echo json_encode(array(
12+
'username' => $_SERVER['PHP_AUTH_USER'],
13+
'password' => $_SERVER['PHP_AUTH_PW'],
1414
));

tests/server/post_file_path_upload.php renamed to tests/server/php-curl-test/post_file_path_upload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
$request_method = isset($_SERVER['REQUEST_METHOD']) ?
44
$_SERVER['REQUEST_METHOD'] : '';
5-
5+
66
$data_values = $request_method === 'POST' ? $_POST : $_GET;
77

88
$key = isset($data_values['key']) ? $data_values['key'] : '';
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?php
22

3-
$http_raw_post_data = file_get_contents('php://input');
3+
$http_raw_post_data = file_get_contents('php://input');
44
echo $http_raw_post_data;
File renamed without changes.

0 commit comments

Comments
 (0)