File tree Expand file tree Collapse file tree 1 file changed +14
-11
lines changed Expand file tree Collapse file tree 1 file changed +14
-11
lines changed Original file line number Diff line number Diff line change 7
7
use GuzzleHttp \Psr7 \Response ;
8
8
use Psr \Http \Message \RequestInterface ;
9
9
10
+ /**
11
+ * HTTP adapter mock
12
+ *
13
+ * This mock is most useful in tests. It does not send requests but stores them
14
+ * for later retrieval. Additionally, you can set an exception to test
15
+ * exception handling.
16
+ */
10
17
class MockHttpAdapter implements HttpAdapter
11
18
{
12
- private $ requests = array () ;
19
+ private $ requests = [] ;
13
20
private $ exception ;
14
21
15
22
/**
@@ -18,11 +25,7 @@ class MockHttpAdapter implements HttpAdapter
18
25
public function sendRequest (RequestInterface $ request , array $ options = [])
19
26
{
20
27
$ this ->requests [] = $ request ;
21
-
22
- if ($ this ->exception ) {
23
- throw $ this ->exception ;
24
- }
25
-
28
+
26
29
return new Response ();
27
30
}
28
31
@@ -33,19 +36,19 @@ public function sendRequests(array $requests, array $options = [])
33
36
{
34
37
$ responses = [];
35
38
$ exceptions = new MultiHttpAdapterException ();
36
-
39
+
37
40
foreach ($ requests as $ request ) {
38
41
try {
39
42
$ responses [] = $ this ->sendRequest ($ request );
40
43
} catch (\Exception $ e ) {
41
44
$ exceptions ->addException ($ e );
42
45
}
43
46
}
44
-
47
+
45
48
if ($ exceptions ->hasExceptions ()) {
46
49
throw $ exceptions ;
47
50
}
48
-
51
+
49
52
return $ responses ;
50
53
}
51
54
@@ -63,12 +66,12 @@ public function getName()
63
66
{
64
67
return 'mock ' ;
65
68
}
66
-
69
+
67
70
public function getRequests ()
68
71
{
69
72
return $ this ->requests ;
70
73
}
71
-
74
+
72
75
public function clear ()
73
76
{
74
77
$ this ->exception = null ;
You can’t perform that action at this time.
0 commit comments