Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit b855b48

Browse files
committed
Merge pull request #49 from kbond/guzzle4-5
Add support for guzzle 4/5
2 parents cf104c7 + 7a57e26 commit b855b48

File tree

3 files changed

+159
-28
lines changed

3 files changed

+159
-28
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ php:
1313

1414
before_install:
1515
- composer self-update
16+
- if [[ "$TRAVIS_PHP_VERSION" != "5.3" && "$TRAVIS_PHP_VERSION" != "5.3.3" ]]; then composer require --no-update guzzlehttp/guzzle:~4; fi
1617
- composer install --prefer-source
1718
- wget http://cs.sensiolabs.org/get/php-cs-fixer.phar
1819

src/ZendDiagnostics/Check/GuzzleHttpService.php

+105-16
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
namespace ZendDiagnostics\Check;
77

8-
use Guzzle\Http\Client;
9-
use Guzzle\Http\ClientInterface;
8+
use Guzzle\Http\Client as Guzzle3Client;
9+
use Guzzle\Http\ClientInterface as Guzzle3ClientInterface;
10+
use GuzzleHttp\Client as Guzzle4And5Client;
11+
use GuzzleHttp\ClientInterface as Guzzle4And5ClientInterface;
1012
use ZendDiagnostics\Result\Failure;
1113
use ZendDiagnostics\Result\Success;
1214

@@ -21,27 +23,33 @@ class GuzzleHttpService extends AbstractCheck
2123
protected $guzzle;
2224

2325
/**
24-
* @param string $url The absolute url to check
25-
* @param array $headers An array of headers used to create the request
26-
* @param array $options An array of guzzle options used to create the request
27-
* @param int $statusCode The response status code to check
28-
* @param null $content The response content to check
29-
* @param ClientInterface $guzzle Instance of guzzle to use
30-
* @param string $method The method of the request
31-
* @param mixed $body The body of the request (used for POST, PUT and DELETE requests)
26+
* @param string $url The absolute url to check
27+
* @param array $headers An array of headers used to create the request
28+
* @param array $options An array of guzzle options used to create the request
29+
* @param int $statusCode The response status code to check
30+
* @param null $content The response content to check
31+
* @param \Guzzle\Http\Client|\GuzzleHttp\Client $guzzle Instance of guzzle to use
32+
* @param string $method The method of the request
33+
* @param mixed $body The body of the request (used for POST, PUT and DELETE requests)
34+
*
35+
* @throws \InvalidArgumentException
3236
*/
33-
public function __construct($url, array $headers = array(), array $options = array(), $statusCode = 200, $content = null, ClientInterface $guzzle = null, $method = 'GET', $body = null)
37+
public function __construct($url, array $headers = array(), array $options = array(), $statusCode = 200, $content = null, $guzzle = null, $method = 'GET', $body = null)
3438
{
3539
$this->url = $url;
3640
$this->headers = $headers;
3741
$this->options = $options;
38-
$this->statusCode = $statusCode;
42+
$this->statusCode = (int) $statusCode;
3943
$this->content = $content;
4044
$this->method = $method;
4145
$this->body = $body;
4246

4347
if (!$guzzle) {
44-
$guzzle = new Client();
48+
$guzzle = $this->createGuzzleClient();
49+
}
50+
51+
if ((!$guzzle instanceof Guzzle3ClientInterface) && (!$guzzle instanceof Guzzle4And5ClientInterface)) {
52+
throw new \InvalidArgumentException('Parameter "guzzle" must be an instance of "\Guzzle\Http\ClientInterface" or "\GuzzleHttp\ClientInterface"');
4553
}
4654

4755
$this->guzzle = $guzzle;
@@ -52,16 +60,97 @@ public function __construct($url, array $headers = array(), array $options = arr
5260
*/
5361
public function check()
5462
{
55-
$response = $this->guzzle->createRequest($this->method, $this->url, $this->headers, $this->body, $this->options)->send();
63+
if ($this->guzzle instanceof Guzzle3ClientInterface) {
64+
return $this->guzzle3Check();
65+
}
66+
67+
return $this->guzzle4And5Check();
68+
}
69+
70+
/**
71+
* @return Failure|Success
72+
*/
73+
private function guzzle3Check()
74+
{
75+
$response = $this->guzzle->createRequest(
76+
$this->method,
77+
$this->url,
78+
$this->headers,
79+
$this->body,
80+
array_merge(array('exceptions' => false), $this->options)
81+
)->send();
5682

5783
if ($this->statusCode !== $statusCode = $response->getStatusCode()) {
58-
return new Failure("Status code {$this->statusCode} does not match {$statusCode} in response from {$this->url}");
84+
return $this->createStatusCodeFailure($statusCode);
5985
}
6086

6187
if ($this->content && (false === strpos($response->getBody(true), $this->content))) {
62-
return new Failure("Content {$this->content} not found in response from {$this->url}");
88+
return $this->createContentFailure();
89+
}
90+
91+
return new Success();
92+
}
93+
94+
/**
95+
* @return Failure|Success
96+
*/
97+
private function guzzle4And5Check()
98+
{
99+
$request = $this->guzzle->createRequest(
100+
$this->method,
101+
$this->url,
102+
array_merge(
103+
array('headers' => $this->headers, 'body' => $this->body, 'exceptions' => false),
104+
$this->options
105+
)
106+
);
107+
108+
$response = $this->guzzle->send($request);
109+
110+
if ($this->statusCode !== $statusCode = (int) $response->getStatusCode()) {
111+
return $this->createStatusCodeFailure($statusCode);
112+
}
113+
114+
if ($this->content && (false === strpos((string) $response->getBody(), $this->content))) {
115+
return $this->createContentFailure();
63116
}
64117

65118
return new Success();
66119
}
120+
121+
/**
122+
* @param int $statusCode
123+
*
124+
* @return Failure
125+
*/
126+
private function createStatusCodeFailure($statusCode)
127+
{
128+
return new Failure("Status code {$this->statusCode} does not match {$statusCode} in response from {$this->url}");
129+
}
130+
131+
/**
132+
* @return Failure
133+
*/
134+
private function createContentFailure()
135+
{
136+
return new Failure("Content {$this->content} not found in response from {$this->url}");
137+
}
138+
139+
/**
140+
* @return \Guzzle\Http\Client|\GuzzleHttp\Client
141+
*
142+
* @throws \Exception
143+
*/
144+
private function createGuzzleClient()
145+
{
146+
if (class_exists('GuzzleHttp\Client')) {
147+
return new Guzzle4And5Client();
148+
}
149+
150+
if (!class_exists('Guzzle\Http\Client')) {
151+
throw new \Exception('Guzzle is required.');
152+
}
153+
154+
return new Guzzle3Client();
155+
}
67156
}

tests/ZendDiagnosticsTest/GuzzleHttpServiceTest.php

+53-12
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,29 @@
22

33
namespace ZendDiagnosticsTest;
44

5-
use Guzzle\Http\Client;
6-
use Guzzle\Http\Message\Response;
5+
use Guzzle\Http\Client as Guzzle3Client;
6+
use Guzzle\Http\Message\Response as Guzzle3Response;
7+
use GuzzleHttp\Client as Guzzle4And5Client;
8+
use GuzzleHttp\Message\Response as Guzzle4And5Response;
79
use Guzzle\Plugin\Mock\MockPlugin;
10+
use GuzzleHttp\Stream\Stream;
11+
use GuzzleHttp\Subscriber\Mock;
812
use ZendDiagnostics\Check\GuzzleHttpService;
913

1014
class GuzzleHttpServiceTest extends \PHPUnit_Framework_TestCase
1115
{
1216
/**
1317
* @dataProvider checkProvider
1418
*/
15-
public function testCheck($content, $actualContent, $actualStatusCode, $resultClass, $method = 'GET', $body = null)
19+
public function testGuzzle3Check($content, $actualContent, $actualStatusCode, $resultClass, $method = 'GET', $body = null)
1620
{
1721
$check = new GuzzleHttpService(
1822
'http://www.example.com/foobar',
1923
array(),
2024
array(),
21-
200,
25+
'200',
2226
$content,
23-
$this->getMockClient($actualStatusCode, $actualContent),
27+
$this->getMockGuzzle3Client($actualStatusCode, $actualContent),
2428
$method,
2529
$body
2630
);
@@ -29,6 +33,38 @@ public function testCheck($content, $actualContent, $actualStatusCode, $resultCl
2933
$this->assertInstanceOf($resultClass, $result);
3034
}
3135

36+
/**
37+
* @dataProvider checkProvider
38+
*/
39+
public function testGuzzle4And5Check($content, $actualContent, $actualStatusCode, $resultClass, $method = 'GET', $body = null)
40+
{
41+
if (!class_exists('GuzzleHttp\Client')) {
42+
$this->markTestSkipped('guzzlehttp/guzzle not installed.');
43+
}
44+
45+
$check = new GuzzleHttpService(
46+
'http://www.example.com/foobar',
47+
array(),
48+
array(),
49+
'200',
50+
$content,
51+
$this->getMockGuzzle4And5Client($actualStatusCode, $actualContent),
52+
$method,
53+
$body
54+
);
55+
$result = $check->check();
56+
57+
$this->assertInstanceOf($resultClass, $result);
58+
}
59+
60+
/**
61+
* @expectedException \InvalidArgumentException
62+
*/
63+
public function testInvalidClient()
64+
{
65+
$check = new GuzzleHttpService('http://example.com', array(), array(), 200, null, 'not guzzle');
66+
}
67+
3268
public function checkProvider()
3369
{
3470
return array(
@@ -44,21 +80,26 @@ public function checkProvider()
4480
array('baz', 'foobar', 200, 'ZendDiagnostics\Result\FailureInterface'),
4581
array('baz', 'foobar', 200, 'ZendDiagnostics\Result\FailureInterface', 'POST', array('key' => 'value')),
4682
array('baz', 'foobar', 200, 'ZendDiagnostics\Result\FailureInterface', 'PUT'),
83+
array('baz', 'foobar', 500, 'ZendDiagnostics\Result\FailureInterface'),
4784
);
4885
}
4986

50-
private function getMockClient($statusCode = 200, $content = null)
87+
private function getMockGuzzle3Client($statusCode = 200, $content = null)
5188
{
5289
$plugin = new MockPlugin();
53-
$plugin->addResponse(new Response($statusCode, null, $content));
90+
$plugin->addResponse(new Guzzle3Response($statusCode, null, $content));
5491

55-
$client = new Client(null, array(
56-
'request.options' => array(
57-
'exceptions' => false
58-
)
59-
));
92+
$client = new Guzzle3Client();
6093
$client->addSubscriber($plugin);
6194

6295
return $client;
6396
}
97+
98+
private function getMockGuzzle4And5Client($statusCode = 200, $content = null)
99+
{
100+
$client = new Guzzle4And5Client();
101+
$client->getEmitter()->attach(new Mock(array(new Guzzle4And5Response($statusCode, array(), Stream::factory((string) $content)))));
102+
103+
return $client;
104+
}
64105
}

0 commit comments

Comments
 (0)