Skip to content

Commit 27e9397

Browse files
committed
Merge pull request #14 from php-http/feature/feature-testing
Add feature testing
2 parents a358edd + 1b7ad3b commit 27e9397

File tree

2 files changed

+308
-0
lines changed

2 files changed

+308
-0
lines changed

src/FeatureTestListener.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Http\Client\Tests;
4+
5+
class FeatureTestListener extends \PHPUnit_TextUI_ResultPrinter
6+
{
7+
public function write($buffer)
8+
{
9+
}
10+
11+
public function startTest(\PHPUnit_Framework_Test $test)
12+
{
13+
$feature = $this->extractFeature($test);
14+
15+
if (empty($feature)) {
16+
$feature = $test->getName();
17+
} else {
18+
$feature = $feature[0];
19+
}
20+
21+
echo sprintf("%-40.s : ", $feature);
22+
}
23+
24+
public function endTest(\PHPUnit_Framework_Test $test, $time)
25+
{
26+
if (!$this->lastTestFailed) {
27+
echo $this->formatWithColor('fg-green', "Supported"). "\n";
28+
} else {
29+
echo $this->formatWithColor('fg-red', "Not supported"). "\n";
30+
}
31+
32+
$this->lastTestFailed = false;
33+
}
34+
35+
private function extractFeature(\PHPUnit_Framework_Test $test)
36+
{
37+
$class = get_class($test);
38+
$method = $test->getName();
39+
$reflection = new \ReflectionMethod($class, $method);
40+
41+
return $this->parseDocBlock($reflection->getDocComment(), '@feature');
42+
}
43+
44+
private function parseDocBlock($doc_block, $tag)
45+
{
46+
$matches = [];
47+
48+
if (empty($doc_block)) {
49+
return $matches;
50+
}
51+
52+
$regex = "/{$tag} (.*)(\\r\\n|\\r|\\n)/U";
53+
preg_match_all( $regex, $doc_block, $matches );
54+
55+
if (empty($matches[1])) {
56+
return [];
57+
}
58+
59+
$matches = $matches[1];
60+
61+
foreach ($matches as $ix => $match) {
62+
$matches[ $ix ] = trim( $match );
63+
}
64+
65+
return $matches;
66+
}
67+
}

src/HttpFeatureTest.php

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
<?php
2+
3+
namespace Http\Client\Tests;
4+
5+
use Http\Client\HttpClient;
6+
use Http\Discovery\MessageFactoryDiscovery;
7+
use Http\Message\MessageFactory;
8+
9+
abstract class HttpFeatureTest extends \PHPUnit_Framework_TestCase
10+
{
11+
/**
12+
* @var MessageFactory
13+
*/
14+
protected static $messageFactory;
15+
16+
/**
17+
* {@inheritdoc}
18+
*/
19+
public static function setUpBeforeClass()
20+
{
21+
self::$messageFactory = MessageFactoryDiscovery::find();
22+
}
23+
24+
/**
25+
* @return HttpClient
26+
*/
27+
abstract protected function createClient();
28+
29+
/**
30+
* @feature Send a GET Request
31+
*/
32+
public function testGet()
33+
{
34+
$request = self::$messageFactory->createRequest(
35+
'GET',
36+
'http://httpbin.org/get'
37+
);
38+
39+
$response = $this->createClient()->sendRequest($request);
40+
41+
$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response);
42+
$this->assertSame(200, $response->getStatusCode());
43+
}
44+
45+
/**
46+
* @feature Send a POST Request
47+
*/
48+
public function testPost()
49+
{
50+
$testData = 'Test data';
51+
$request = self::$messageFactory->createRequest(
52+
'POST',
53+
'http://httpbin.org/post',
54+
['Content-Length' => strlen($testData)],
55+
$testData
56+
);
57+
58+
$response = $this->createClient()->sendRequest($request);
59+
60+
$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response);
61+
$this->assertSame(200, $response->getStatusCode());
62+
63+
$contents = json_decode($response->getBody()->getContents());
64+
65+
$this->assertEquals($testData, $contents->data);
66+
}
67+
68+
/**
69+
* @feature Send a PATCH Request
70+
*/
71+
public function testPatch()
72+
{
73+
$request = self::$messageFactory->createRequest(
74+
'PATCH',
75+
'http://httpbin.org/patch'
76+
);
77+
78+
$response = $this->createClient()->sendRequest($request);
79+
80+
$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response);
81+
$this->assertSame(200, $response->getStatusCode());
82+
}
83+
84+
/**
85+
* @feature Send a PUT Request
86+
*/
87+
public function testPut()
88+
{
89+
$request = self::$messageFactory->createRequest(
90+
'PUT',
91+
'http://httpbin.org/put'
92+
);
93+
94+
$response = $this->createClient()->sendRequest($request);
95+
96+
$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response);
97+
$this->assertSame(200, $response->getStatusCode());
98+
}
99+
100+
/**
101+
* @feature Send a DELETE Request
102+
*/
103+
public function testDelete()
104+
{
105+
$request = self::$messageFactory->createRequest(
106+
'DELETE',
107+
'http://httpbin.org/delete'
108+
);
109+
110+
$response = $this->createClient()->sendRequest($request);
111+
112+
$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response);
113+
$this->assertSame(200, $response->getStatusCode());
114+
}
115+
116+
/**
117+
* @feature Auto fixing content length header
118+
*/
119+
public function testAutoSetContentLength()
120+
{
121+
$testData = 'Test data';
122+
$request = self::$messageFactory->createRequest(
123+
'POST',
124+
'http://httpbin.org/post',
125+
[],
126+
$testData
127+
);
128+
129+
$response = $this->createClient()->sendRequest($request);
130+
131+
$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response);
132+
$this->assertSame(200, $response->getStatusCode());
133+
134+
$contents = json_decode($response->getBody()->getContents());
135+
136+
$this->assertEquals($testData, $contents->data);
137+
}
138+
139+
/**
140+
* @feature Encoding in UTF8
141+
*/
142+
public function testEncoding()
143+
{
144+
$request = self::$messageFactory->createRequest(
145+
'GET',
146+
'http://httpbin.org/encoding/utf8'
147+
);
148+
149+
$response = $this->createClient()->sendRequest($request);
150+
151+
$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response);
152+
$this->assertSame(200, $response->getStatusCode());
153+
$this->assertContains('', $response->getBody()->getContents());
154+
}
155+
156+
/**
157+
* @feature Gzip content decoding
158+
*/
159+
public function testGzip()
160+
{
161+
$request = self::$messageFactory->createRequest(
162+
'GET',
163+
'http://httpbin.org/gzip'
164+
);
165+
166+
$response = $this->createClient()->sendRequest($request);
167+
168+
$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response);
169+
$this->assertSame(200, $response->getStatusCode());
170+
$this->assertContains('gzip', $response->getBody()->getContents());
171+
}
172+
173+
/**
174+
* @feature Deflate content decoding
175+
*/
176+
public function testDeflate()
177+
{
178+
$request = self::$messageFactory->createRequest(
179+
'GET',
180+
'http://httpbin.org/deflate'
181+
);
182+
183+
$response = $this->createClient()->sendRequest($request);
184+
185+
$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response);
186+
$this->assertSame(200, $response->getStatusCode());
187+
$this->assertContains('deflate', $response->getBody()->getContents());
188+
}
189+
190+
/**
191+
* @feature Follow redirection
192+
*/
193+
public function testRedirect()
194+
{
195+
$request = self::$messageFactory->createRequest(
196+
'GET',
197+
'http://httpbin.org/redirect/1'
198+
);
199+
200+
$response = $this->createClient()->sendRequest($request);
201+
202+
$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response);
203+
$this->assertSame(200, $response->getStatusCode());
204+
}
205+
206+
/**
207+
* @feature Dechunk stream body
208+
*/
209+
public function testChunked()
210+
{
211+
$request = self::$messageFactory->createRequest(
212+
'GET',
213+
'http://httpbin.org/stream/1'
214+
);
215+
216+
$response = $this->createClient()->sendRequest($request);
217+
218+
$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response);
219+
$this->assertSame(200, $response->getStatusCode());
220+
221+
$content = @json_decode($response->getBody()->getContents());
222+
223+
$this->assertNotNull($content);
224+
}
225+
226+
/**
227+
* @feature Ssl connection
228+
*/
229+
public function testSsl()
230+
{
231+
$request = self::$messageFactory->createRequest(
232+
'GET',
233+
'https://httpbin.org/get'
234+
);
235+
236+
$response = $this->createClient()->sendRequest($request);
237+
238+
$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response);
239+
$this->assertSame(200, $response->getStatusCode());
240+
}
241+
}

0 commit comments

Comments
 (0)