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

Commit 6fd896a

Browse files
committed
Enhancement: allow setting the URL and also parameters
* added a little documentation about what to expect * added a small test case to confirm the check initializes
1 parent d73a0c8 commit 6fd896a

File tree

2 files changed

+72
-19
lines changed

2 files changed

+72
-19
lines changed

src/ZendDiagnostics/Check/CouchDBCheck.php

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,12 @@ class CouchDBCheck extends GuzzleHttpService
1919
*/
2020
public function __construct(array $couchDbSettings)
2121
{
22-
$couchDbUrl = '';
23-
if ($couchDbSettings['port'] === '5984' || $couchDbSettings['port'] === '80') {
24-
$couchDbUrl .= 'http://';
22+
if (false === array_key_exists('url', $couchDbSettings)) {
23+
$couchDbUrl = $this->createUrlFromParameters($couchDbSettings);
2524
} else {
26-
$couchDbUrl .= 'https://';
27-
}
28-
29-
if ($couchDbSettings['username'] && $couchDbSettings['password']) {
30-
$couchDbUrl .= sprintf(
31-
'%s:%s@',
32-
$couchDbSettings['username'],
33-
$couchDbSettings['password']
34-
);
25+
$couchDbUrl = $couchDbSettings['url'];
3526
}
3627

37-
$couchDbUrl .= sprintf(
38-
'%s:%s/%s',
39-
$couchDbSettings['host'],
40-
$couchDbSettings['port'],
41-
$couchDbSettings['dbname']
42-
);
43-
4428
parent::__construct($couchDbUrl);
4529
}
4630

@@ -61,4 +45,53 @@ public function check()
6145

6246
return $failure;
6347
}
48+
49+
/**
50+
* Assumes CouchDB defaults. Port 80 or 5984 is non-SSL, SSL otherwise.
51+
* Override with 'protocol' if you run something else.
52+
*
53+
* Requires/Supports the following keys in the array:
54+
*
55+
* - dbname
56+
* - host
57+
* - port
58+
* - protocol (optional)
59+
* - username (optional)
60+
* - password (optional)
61+
*
62+
* @param array $couchDbSettings
63+
*
64+
* @return string
65+
*/
66+
private function createUrlFromParameters(array $couchDbSettings)
67+
{
68+
$couchDbUrl = '';
69+
70+
if (array_key_exists('protocol', $couchDbSettings)) {
71+
$couchDbUrl .= $couchDbSettings['protocol'] . '://';
72+
} else {
73+
if ($couchDbSettings['port'] === '5984' || $couchDbSettings['port'] === '80') {
74+
$couchDbUrl .= 'http://';
75+
} else {
76+
$couchDbUrl .= 'https://';
77+
}
78+
}
79+
80+
if ($couchDbSettings['username'] && $couchDbSettings['password']) {
81+
$couchDbUrl .= sprintf(
82+
'%s:%s@',
83+
$couchDbSettings['username'],
84+
$couchDbSettings['password']
85+
);
86+
}
87+
88+
$couchDbUrl .= sprintf(
89+
'%s:%s/%s',
90+
$couchDbSettings['host'],
91+
$couchDbSettings['port'],
92+
$couchDbSettings['dbname']
93+
);
94+
95+
return $couchDbUrl;
96+
}
6497
}

tests/ZendDiagnosticsTest/GuzzleHttpServiceTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,22 @@
99
use Guzzle\Plugin\Mock\MockPlugin;
1010
use GuzzleHttp\Stream\Stream;
1111
use GuzzleHttp\Subscriber\Mock;
12+
use ZendDiagnostics\Check\CouchDBCheck;
1213
use ZendDiagnostics\Check\GuzzleHttpService;
1314

1415
class GuzzleHttpServiceTest extends \PHPUnit_Framework_TestCase
1516
{
17+
/**
18+
* @param array $params
19+
*
20+
* @dataProvider couchDbProvider
21+
*/
22+
public function testCouchDbCheck(array $params)
23+
{
24+
$check = new CouchDBCheck($params);
25+
$this->assertInstanceOf('ZendDiagnostics\Check\CouchDbCheck', $check);
26+
}
27+
1628
/**
1729
* @dataProvider checkProvider
1830
*/
@@ -84,6 +96,14 @@ public function checkProvider()
8496
);
8597
}
8698

99+
public function couchDbProvider()
100+
{
101+
return array(
102+
array(array('url' => 'http://root:party@localhost/hello')),
103+
array(array('host' => '127.0.0.1', 'port' => '443', 'username' => 'test', 'password' => 'test', 'dbname' => 'database')),
104+
);
105+
}
106+
87107
private function getMockGuzzle3Client($statusCode = 200, $content = null)
88108
{
89109
$plugin = new MockPlugin();

0 commit comments

Comments
 (0)