Skip to content

Commit 5cff084

Browse files
committed
Allow URLs as well as Guzzle clients in the constructor
1 parent d48cf6f commit 5cff084

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace PHPCouchDB\Exception;
4+
5+
class ServerException {
6+
}
7+

src/PHPCouchDB/Server.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,32 @@ class Server
1616
{
1717
protected $client;
1818

19-
public function __construct(\GuzzleHttp\ClientInterface $client)
19+
/**
20+
* Create the object to represent the server
21+
*
22+
* See also: \PHPCouchDB\Server:createFromURL()
23+
*
24+
* @param array $options Supply either a string "url" parameter OR a
25+
* \GuzzleHttp\ClientInterface "client" parameter if more configuration
26+
* is required
27+
* @throws \PHPCouchDB\Exception\ServerException if there's a problem with parsing arguments or creating the client
28+
*/
29+
public function __construct(array $options)
2030
{
21-
if ($client) {
22-
$this->client = $client;
31+
if(empty($options) || !is_array($options)) {
32+
throw new \PHPCouchDB\Exception\ServerException('$options is a required parameter, array should contain either a url or a client');
33+
}
34+
35+
if(isset($options['client']) && $options['client'] instanceof \GuzzleHttp\ClientInterface) {
36+
$this->client = $options['client'];
37+
} elseif(isset($options['url'])) {
38+
try {
39+
$this->client = new \GuzzleHttp\Client(["base_uri" => $options['url']]);
40+
} catch(Exception $e) {
41+
throw new \PHPCouchDB\Exception\ServerException("Could not connect with URL. Error: " . $e->getMessage());
42+
}
2343
} else {
24-
throw new Exception('$client is required');
44+
throw new \PHPCouchDB\Exception\ServerException('Failed to parse $options, array should contain either a url or a client');
2545
}
2646
}
2747

tests/PHPCouchDB/ServerTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,25 @@
1111

1212
class ServerTest extends \PHPUnit\Framework\TestCase
1313
{
14+
public function testCreateWithURL() {
15+
$url = "http://localhost:5984";
16+
17+
$server = new \PHPCouchDB\Server(["url" => $url]);
18+
19+
$this->assertObjectHasAttribute('client', $server);
20+
$this->assertAttributeInstanceOf('\GuzzleHttp\ClientInterface', 'client', $server);
21+
}
22+
23+
public function testCreateWithClient() {
24+
$url = "http://localhost:5984";
25+
$client = new \GuzzleHttp\Client(["base_uri" => $url]);
26+
27+
$server = new \PHPCouchDB\Server(["client" => $client]);
28+
29+
$this->assertObjectHasAttribute('client', $server);
30+
$this->assertAttributeInstanceOf('\GuzzleHttp\ClientInterface', 'client', $server);
31+
}
32+
1433
public function testGetVersion() {
1534

1635
$couchdb1 = '{"couchdb":"Welcome","uuid":"fce3d5aabfe189c988273c0ffa8d375b","version":"1.6.0","vendor":{"name":"Ubuntu","version":"15.10"}}';
@@ -22,9 +41,8 @@ public function testGetVersion() {
2241
$handler = HandlerStack::create($mock);
2342
$client = new Client(['handler' => $handler]);
2443

25-
2644
// userland code starts
27-
$server = new \PHPCouchDB\Server($client);
45+
$server = new \PHPCouchDB\Server(["client" => $client]);
2846
$this->assertEquals("1.6.0", $server->getVersion());
2947

3048
}

0 commit comments

Comments
 (0)