forked from alexandresalome/mailcatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.php
203 lines (173 loc) · 5.07 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
namespace Alex\MailCatcher;
/**
* Client to manipulate a MailCatcher server.
*
* @author Alexandre Salomé <[email protected]>
*/
class Client
{
/**
* @var string
*/
protected $url;
/**
* @var array
*/
protected $messages = array();
/**
* Creates a new client.
*
* @param string $url url of the server
*/
public function __construct($url = 'http://localhost:1080')
{
$this->url = $url;
}
/**
* Deletes all messages on server.
*
* @return Client
*/
public function purge()
{
$this->request('DELETE');
$this->messages = array();
return $this;
}
/**
* @return string URL of server used by the client
*/
public function getUrl()
{
return $this->url;
}
/**
* Returns the number of messages on the server.
*
* @return int
*/
public function getMessageCount()
{
return count($this->request('GET'));
}
/**
* Searches for one messages on the server.
*
* See method `Message::match` method for more informations on criterias.
*
* @param array $criterias
*
* @return Message|null
*/
public function searchOne(array $criterias = array())
{
$results = $this->search($criterias, 1);
if (count($results) !== 1) {
return null;
}
return $results[0];
}
/**
* Searches for messages on the server.
*
* See method `Method::match` for more informations on criterias.
*
* @param array $criterias an array of criterias
* @param int $limit maximum number of elements to fetch. Null means no limit
*
* @return array a list of messages
*/
public function search(array $criterias = array(), $limit = null)
{
$messages = array();
foreach ($this->request('GET') as $message) {
if (isset($this->messages[$message['id']])) {
$messages[] = $this->messages[$message['id']];
} else {
$messages[] = $this->messages[$message['id']] = new Message($this, $message);
}
}
$result = array();
foreach ($messages as $message) {
if (null !== $limit && count($result) >= $limit) {
break;
}
if ($message->match($criterias)) {
$result[] = $message;
}
}
return $result;
}
/**
* Request the API of MailCatcher.
*
* @param string $method HTTP method to use (POST, PUT, GET, DELETE)
* @param string $path relative path from '/messages' (ex: null, '132.json')
* @param array $parameters parameters to POST
*
* @return string response body
*/
public function request($method, $path = null, $parameters = array())
{
if (null === $path) {
$url = '/messages';
} else {
$url = '/messages/'.$path;
}
return json_decode($this->requestRaw($method, $url, $parameters), true);
}
/**
* Raw method to request the API of MailCatcher.
*
* @param string $method HTTP method
* @param string $url absolute URL on server (`/messages/132.json`)
* @param array $parameters parameters to POST
*
* @return string response body
* @throws \RuntimeException
*/
public function requestRaw($method, $url, $parameters = array())
{
$url = $this->url.$url;
if (false === $curl = curl_init()) {
throw new \RuntimeException('Unable to create a new cURL handle');
}
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_URL => $url,
CURLOPT_TIMEOUT_MS => 3000,
CURLOPT_TIMEOUT => 3,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_MAXREDIRS => 5,
CURLOPT_FAILONERROR => true,
CURLOPT_SSL_VERIFYPEER => false,
);
switch ($method) {
case 'HEAD':
$options[CURLOPT_NOBODY] = true;
break;
case 'GET':
$options[CURLOPT_HTTPGET] = true;
break;
case 'POST':
case 'PUT':
case 'DELETE':
case 'PATCH':
$options[CURLOPT_POSTFIELDS] = http_build_query($parameters);
break;
}
curl_setopt_array($curl, $options);
$result = curl_exec($curl);
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($statusCode === 200 || $statusCode === 204 || ($statusCode >= 300 && $statusCode <= 303)) {
return $result;
}
if (0 === $statusCode) {
throw new \RuntimeException(sprintf('Unable to connect to "%s".', $this->url));
}
throw new \RuntimeException(sprintf('Unexpected status code. Expected valid code, got %s.', $statusCode));
}
}