Skip to content

Commit bb44496

Browse files
committed
Tests fixes and some refactoring
1 parent 9af6074 commit bb44496

File tree

4 files changed

+46
-52
lines changed

4 files changed

+46
-52
lines changed

lib/Client.php

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
/**
44
* HTTP Client library
55
*
6-
* PHP version 5.4
7-
*
86
* @author Matt Bernier <[email protected]>
97
* @author Elmer Thomas <[email protected]>
108
* @copyright 2016 SendGrid
@@ -16,8 +14,16 @@
1614
namespace SendGrid;
1715

1816
/**
19-
* Quickly and easily access any REST or REST-like API.
20-
*/
17+
* Quickly and easily access any REST or REST-like API.
18+
*
19+
* @method Response get($body = null, $query = null, $headers = null)
20+
* @method Response post($body = null, $query = null, $headers = null)
21+
* @method Response patch($body = null, $query = null, $headers = null)
22+
* @method Response put($body = null, $query = null, $headers = null)
23+
* @method Response delete($body = null, $query = null, $headers = null)
24+
*
25+
* @method Client version($value)
26+
*/
2127
class Client
2228
{
2329
/** @var string */
@@ -38,7 +44,7 @@ class Client
3844
*
3945
* @var array
4046
*/
41-
private $methods = ['delete', 'get', 'patch', 'post', 'put'];
47+
private $methods = ['get', 'post', 'patch', 'put', 'delete'];
4248

4349
/**
4450
* Initialize the client
@@ -48,7 +54,7 @@ class Client
4854
* @param string $version api version (configurable)
4955
* @param array $path holds the segments of the url path
5056
*/
51-
public function __construct($host, $headers = [], $version = null, $path = [])
57+
public function __construct($host, $headers = [], $version = '/v3', $path = [])
5258
{
5359
$this->host = $host;
5460
$this->headers = $headers;
@@ -127,37 +133,6 @@ public function getCurlOptions()
127133
return $this->curlOptions;
128134
}
129135

130-
/**
131-
* Make a new Client object
132-
*
133-
* @param string $name name of the url segment
134-
*
135-
* @return Client object
136-
*/
137-
private function buildClient($name = null)
138-
{
139-
if (isset($name)) {
140-
$this->path[] = $name;
141-
}
142-
$client = $this->cloneClient();
143-
$this->path = [];
144-
return $client;
145-
}
146-
147-
/**
148-
* Clone existing Client object with all settings
149-
*
150-
* @return Client
151-
*/
152-
private function cloneClient()
153-
{
154-
$client = new static($this->host, $this->headers, $this->version, $this->path);
155-
$client->setCurlOptions($this->curlOptions);
156-
$client->setRetryOnLimit($this->retryOnLimit);
157-
158-
return $client;
159-
}
160-
161136
/**
162137
* Build the final URL to be passed
163138
*
@@ -249,7 +224,15 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry
249224
*/
250225
public function _($name = null)
251226
{
252-
return $this->buildClient($name);
227+
if (isset($name)) {
228+
$this->path[] = $name;
229+
}
230+
$client = new static($this->host, $this->headers, $this->version, $this->path);
231+
$client->setCurlOptions($this->curlOptions);
232+
$client->setRetryOnLimit($this->retryOnLimit);
233+
$this->path = [];
234+
235+
return $client;
253236
}
254237

255238
/**

lib/Response.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
/**
44
* HTTP Client library
55
*
6-
* PHP version 5.4
7-
*
86
* @author Matt Bernier <[email protected]>
97
* @author Elmer Thomas <[email protected]>
108
* @copyright 2016 SendGrid
@@ -89,7 +87,7 @@ public function headers($assoc = false)
8987
private function prettifyHeaders($headers)
9088
{
9189
if (!is_array($headers)) {
92-
throw new \InvalidArgumentException('$headers should be array');
90+
throw new \InvalidArgumentException('Headers should be an array');
9391
}
9492

9593
return array_reduce(

test/unit/ClientTest.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected function setUp()
2020
'Content-Type: application/json',
2121
'Authorization: Bearer SG.XXXX'
2222
];
23-
$this->client = new MockClient($this->host, $this->headers, '/v3', null, null);
23+
$this->client = new MockClient($this->host, $this->headers);
2424
}
2525

2626
public function testConstructor()
@@ -30,12 +30,14 @@ public function testConstructor()
3030
$this->assertAttributeEquals('/v3', 'version', $this->client);
3131
$this->assertAttributeEquals([], 'path', $this->client);
3232
$this->assertAttributeEquals([], 'curlOptions', $this->client);
33-
$this->assertAttributeEquals(['delete', 'get', 'patch', 'post', 'put'], 'methods', $this->client);
33+
$this->assertAttributeEquals(false, 'retryOnLimit', $this->client);
34+
$this->assertAttributeEquals(['get', 'post', 'patch', 'put', 'delete'], 'methods', $this->client);
3435
}
3536

3637
public function test_()
3738
{
38-
$client = new MockClient($this->host, $this->headers, '/v3', null, ['foo' => 'bar']);
39+
$client = new MockClient($this->host, $this->headers, '/v3');
40+
$client->setCurlOptions(['foo' => 'bar']);
3941
$client = $client->_('test');
4042

4143
$this->assertAttributeEquals(['test'], 'path', $client);
@@ -79,34 +81,35 @@ public function testGetHeaders()
7981
$client = new Client('https://localhost:4010', ['Content-Type: application/json', 'Authorization: Bearer SG.XXXX']);
8082
$this->assertSame(['Content-Type: application/json', 'Authorization: Bearer SG.XXXX'], $client->getHeaders());
8183

82-
$client2 = new Client('https://localhost:4010', null);
84+
$client2 = new Client('https://localhost:4010');
8385
$this->assertSame([], $client2->getHeaders());
8486
}
8587

8688
public function testGetVersion()
8789
{
88-
$client = new Client('https://localhost:4010', null, '/v3');
90+
$client = new Client('https://localhost:4010', [], '/v3');
8991
$this->assertSame('/v3', $client->getVersion());
9092

91-
$client = new Client('https://localhost:4010', null, null);
92-
$this->assertSame(null, $client->getVersion());
93+
$client = new Client('https://localhost:4010');
94+
$this->assertSame('/v3', $client->getVersion());
9395
}
9496

9597
public function testGetPath()
9698
{
97-
$client = new Client('https://localhost:4010', null, null, ['/foo/bar']);
99+
$client = new Client('https://localhost:4010', [], null, ['/foo/bar']);
98100
$this->assertSame(['/foo/bar'], $client->getPath());
99101

100-
$client = new Client('https://localhost:4010', null, null, null);
102+
$client = new Client('https://localhost:4010');
101103
$this->assertSame([], $client->getPath());
102104
}
103105

104106
public function testGetCurlOptions()
105107
{
106-
$client = new Client('https://localhost:4010', null, null, null, [CURLOPT_PROXY => '127.0.0.1:8080']);
108+
$client = new Client('https://localhost:4010');
109+
$client->setCurlOptions([CURLOPT_PROXY => '127.0.0.1:8080']);
107110
$this->assertSame([CURLOPT_PROXY => '127.0.0.1:8080'], $client->getCurlOptions());
108111

109-
$client = new Client('https://localhost:4010', null, null, null, null);
112+
$client = new Client('https://localhost:4010');
110113
$this->assertSame([], $client->getCurlOptions());
111114
}
112115
}

test/unit/ResponseTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,14 @@ public function testAssociativeHeaders()
4848

4949
$this->assertEquals(['Content-Type' => 'text/html', 'Status' => 'HTTP/1.1 200 OK'], $response->headers(true));
5050
}
51+
52+
/**
53+
* @expectedException \InvalidArgumentException
54+
* @expectedExceptionMessage Headers should be an array
55+
*/
56+
public function testHeadersWithInvalidValue()
57+
{
58+
$response = new Response(null, null, false);
59+
$response->headers(true);
60+
}
5161
}

0 commit comments

Comments
 (0)