Skip to content

Commit 8ee98d4

Browse files
committed
refactoring.
1 parent 1a56d15 commit 8ee98d4

File tree

3 files changed

+88
-54
lines changed

3 files changed

+88
-54
lines changed

src/PearHttpRequest.php

+67-32
Original file line numberDiff line numberDiff line change
@@ -89,28 +89,57 @@ public function setBody($body)
8989
$this->body = $body;
9090
}
9191

92+
9293
public function sendRequest($saveBody = true)
94+
{
95+
$this->prepareOptions();
96+
97+
try {
98+
$this->client = new Client();
99+
$this->response = $this->client->request($this->method, $this->url, $this->options);
100+
return true;
101+
} catch (RequestException $e) {
102+
$this->response = $e->getResponse();
103+
return false;
104+
}
105+
}
106+
107+
private function prepareOptions()
93108
{
94109
$this->options = array_merge([
95110
'headers' => $this->headers,
96111
'query' => $this->queryParams,
97112
], $this->options);
98113

114+
$this->prepareBody();
115+
$this->prepareAuth();
116+
}
117+
118+
private function prepareBody()
119+
{
99120
if ($this->method === 'POST' || $this->method === 'PUT') {
121+
$this->setDefaultContentType();
122+
100123
if (!empty($this->postData)) {
101124
$this->options['form_params'] = $this->postData;
102125
} elseif (isset($this->body)) {
103-
if (!isset($this->headers['Content-Type'])) {
104-
$this->options['headers'] = array_merge(
105-
// POSTでContent-Type未指定の場合はapplication/x-www-form-urlencodedにFallbackする
106-
['Content-Type' => 'application/x-www-form-urlencoded'],
107-
$this->options['headers']
108-
);
109-
}
110126
$this->options['body'] = $this->body;
111127
}
112128
}
129+
}
130+
131+
private function setDefaultContentType()
132+
{
133+
if (!isset($this->headers['Content-Type'])) {
134+
$this->options['headers'] = array_merge(
135+
['Content-Type' => 'application/x-www-form-urlencoded'],
136+
$this->options['headers']
137+
);
138+
}
139+
}
113140

141+
private function prepareAuth()
142+
{
114143
if (isset($this->basicAuthUsername)) {
115144
$this->options = array_merge([
116145
'auth' => [
@@ -120,15 +149,6 @@ public function sendRequest($saveBody = true)
120149
]
121150
], $this->options);
122151
}
123-
124-
try {
125-
$this->client = new Client();
126-
$this->response = $this->client->request($this->method, $this->url, $this->options);
127-
return true;
128-
} catch (RequestException $e) {
129-
$this->response = $e->getResponse();
130-
return false;
131-
}
132152
}
133153

134154
public function getResponseCode()
@@ -165,6 +185,30 @@ public function setBasicAuth($user, $pass)
165185
$this->basicAuthPassword = $pass;
166186
}
167187

188+
public function getResponseReason()
189+
{
190+
$response = $this->client->getResponse();
191+
return $response->getReasonPhrase();
192+
}
193+
194+
public function getResponseCookies()
195+
{
196+
$response = $this->client->getResponse();
197+
$cookieJar = $response->getCookies();
198+
$cookies = [];
199+
200+
foreach ($cookieJar as $cookie) {
201+
$cookies[$cookie->getName()] = $cookie->getValue();
202+
}
203+
204+
return $cookies;
205+
}
206+
207+
public function clearPostData()
208+
{
209+
$this->postData = [];
210+
}
211+
168212
public function setHttpVer($http)
169213
{
170214
throw new LogicException('Not implemented yet');
@@ -185,36 +229,27 @@ public function addCookie($name, $value)
185229
throw new LogicException('Not implemented yet');
186230
}
187231

188-
public function getResponseReason()
232+
public function disconnect()
189233
{
190-
$response = $this->client->getResponse();
191-
return $response->getReasonPhrase();
234+
throw new LogicException('Not implemented yet');
192235
}
193236

194-
public function getResponseCookies()
237+
public function attach(&$listener)
195238
{
196-
$response = $this->client->getResponse();
197-
$cookieJar = $response->getCookies();
198-
$cookies = [];
199-
200-
foreach ($cookieJar as $cookie) {
201-
$cookies[$cookie->getName()] = $cookie->getValue();
202-
}
203-
204-
return $cookies;
239+
throw new LogicException('Not implemented yet');
205240
}
206241

207-
public function disconnect()
242+
public function detach(&$listener)
208243
{
209244
throw new LogicException('Not implemented yet');
210245
}
211246

212-
public function attach(&$listener)
247+
public function reset($url, $params = array())
213248
{
214249
throw new LogicException('Not implemented yet');
215250
}
216251

217-
public function detach(&$listener)
252+
public function clearCookies()
218253
{
219254
throw new LogicException('Not implemented yet');
220255
}

src/PearHttpRequestInterface.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
interface PearHttpRequestInterface
1111
{
12+
public function __construct($url = '', $params = array());
13+
public function reset($url, $params = array());
1214
public function setURL($url);
1315
public function getUrl();
1416
public function setProxy($host, $port = 8080, $user = null, $pass = null);
@@ -20,16 +22,19 @@ public function removeHeader($name);
2022
public function addQueryString($name, $value, $preencoded = false);
2123
public function addRawQueryString($querystring, $preencoded = true);
2224
public function addPostData($name, $value, $preencoded = false);
23-
public function addRawPostData($postdata, $preencoded = true);
2425
public function addFile($inputName, $fileName, $contentType = 'application/octet-stream');
26+
public function addRawPostData($postdata, $preencoded = true);
27+
public function setBody($body);
28+
public function clearPostData();
2529
public function addCookie($name, $value);
30+
public function clearCookies();
2631
public function sendRequest($saveBody = true);
32+
public function disconnect();
2733
public function getResponseCode();
2834
public function getResponseReason();
2935
public function getResponseHeader($headername = null);
3036
public function getResponseBody();
3137
public function getResponseCookies();
32-
public function disconnect();
3338
public function attach(&$listener);
3439
public function detach(&$listener);
3540
}

tests/PearHttpRequestTest.php

+14-20
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,20 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Uzulla\ForLegacy\HttpRequest\Tests;
46

57
use PHPUnit\Framework\TestCase;
68
use Uzulla\ForLegacy\HttpRequest\PearHttpRequest;
79

810
class PearHttpRequestTest extends TestCase
911
{
12+
const TEST_SERVER_URL = 'http://127.0.0.1:8080/';
1013
/** @var PearHttpRequest */
1114
protected $request;
12-
1315
/** @var TestHttpServer */
1416
protected $testServer;
1517

16-
const TEST_SERVER_URL = 'http://127.0.0.1:8080/';
17-
18-
protected function setUp(): void
19-
{
20-
$this->testServer = new TestHttpServer();
21-
$this->testServer->start();
22-
}
23-
24-
protected function tearDown(): void
25-
{
26-
$this->testServer = null;
27-
}
28-
2918
public function testSendRequest()
3019
{
3120
$this->request = new PearHttpRequest();
@@ -58,7 +47,6 @@ public function testAddQueryString()
5847

5948
$this->request->setURL(self::TEST_SERVER_URL);
6049
$this->request->setMethod(PearHttpRequest::HTTP_REQUEST_METHOD_GET);
61-
$this->request->addHeader('User-Agent', 'TestAgent');
6250
$this->request->addQueryString('foo', 'bar');
6351
$this->request->addQueryString('baz', 'qux', true);
6452

@@ -75,7 +63,6 @@ public function testAddPostData()
7563

7664
$this->request->setURL(self::TEST_SERVER_URL);
7765
$this->request->setMethod(PearHttpRequest::HTTP_REQUEST_METHOD_POST);
78-
$this->request->addHeader('User-Agent', 'TestAgent');
7966
$this->request->addPostData('foo', 'bar');
8067
$this->request->addPostData('baz', 'qux', true);
8168

@@ -92,7 +79,6 @@ public function testSetBasicAuth()
9279

9380
$this->request->setURL(self::TEST_SERVER_URL);
9481
$this->request->setMethod(PearHttpRequest::HTTP_REQUEST_METHOD_GET);
95-
$this->request->addHeader('User-Agent', 'TestAgent');
9682
$this->request->setBasicAuth('test_basic_auth_user', 'test_basic_auth_pass');
9783

9884
$this->request->sendRequest();
@@ -107,8 +93,6 @@ public function testSetBody()
10793

10894
$this->request->setURL(self::TEST_SERVER_URL);
10995
$this->request->setMethod(PearHttpRequest::HTTP_REQUEST_METHOD_POST);
110-
$this->request->addHeader('User-Agent', 'TestAgent');
111-
$this->request->addHeader('Content-Type', 'application/x-www-form-urlencoded');
11296
$this->request->setBody('this_is=raw_body');
11397

11498
$this->request->sendRequest();
@@ -122,11 +106,21 @@ public function testAddRawPostData()
122106

123107
$this->request->setURL(self::TEST_SERVER_URL);
124108
$this->request->setMethod(PearHttpRequest::HTTP_REQUEST_METHOD_POST);
125-
$this->request->addHeader('User-Agent', 'TestAgent');
126109
$this->request->addRawPostData('this_is=raw_body');
127110

128111
$this->request->sendRequest();
129112

130113
$this->assertStringContainsString('<tr><td class="e">$_POST[\'this_is\']</td><td class="v">raw_body</td></tr>', $this->request->getResponseBody());
131114
}
115+
116+
protected function setUp(): void
117+
{
118+
$this->testServer = new TestHttpServer();
119+
$this->testServer->start();
120+
}
121+
122+
protected function tearDown(): void
123+
{
124+
$this->testServer = null;
125+
}
132126
}

0 commit comments

Comments
 (0)