3
3
namespace Omnipay \Tests ;
4
4
5
5
use Mockery as m ;
6
- use PHPUnit_Framework_TestCase ;
6
+ use Omnipay \Common \Http \Client ;
7
+ use PHPUnit \Framework \TestCase as PHPUnit_Framework_TestCase ;
8
+ use Psr \Http \Message \ResponseInterface ;
7
9
use ReflectionObject ;
8
- use Guzzle \Common \Event ;
9
- use Guzzle \Http \Client as HttpClient ;
10
- use Guzzle \Http \Message \Response ;
11
- use Guzzle \Http \Message \RequestInterface as GuzzleRequestInterface ;
12
- use Guzzle \Plugin \Mock \MockPlugin ;
10
+ use Http \Mock \Client as MockClient ;
13
11
use Symfony \Component \HttpFoundation \Request as HttpRequest ;
14
12
15
13
/**
19
17
*/
20
18
abstract class TestCase extends PHPUnit_Framework_TestCase
21
19
{
22
- private $ mockHttpRequests = array ();
20
+ /** @var \Omnipay\Common\Message\RequestInterface */
23
21
private $ mockRequest ;
22
+ /** @var MockClient */
23
+ private $ mockClient ;
24
24
private $ httpClient ;
25
25
private $ httpRequest ;
26
26
@@ -41,19 +41,6 @@ function ($match) {
41
41
);
42
42
}
43
43
44
- /**
45
- * Mark a request as being mocked
46
- *
47
- * @param GuzzleRequestInterface $request
48
- *
49
- * @return self
50
- */
51
- public function addMockedHttpRequest (GuzzleRequestInterface $ request )
52
- {
53
- $ this ->mockHttpRequests [] = $ request ;
54
-
55
- return $ this ;
56
- }
57
44
58
45
/**
59
46
* Get all of the mocked requests
@@ -62,19 +49,19 @@ public function addMockedHttpRequest(GuzzleRequestInterface $request)
62
49
*/
63
50
public function getMockedRequests ()
64
51
{
65
- return $ this ->mockHttpRequests ;
52
+ return $ this ->mockClient -> getRequests () ;
66
53
}
67
54
68
55
/**
69
56
* Get a mock response for a client by mock file name
70
57
*
71
58
* @param string $path Relative path to the mock response file
72
59
*
73
- * @return Response
60
+ * @return ResponseInterface
74
61
*/
75
62
public function getMockHttpResponse ($ path )
76
63
{
77
- if ($ path instanceof Response ) {
64
+ if ($ path instanceof ResponseInterface ) {
78
65
return $ path ;
79
66
}
80
67
@@ -83,10 +70,10 @@ public function getMockHttpResponse($path)
83
70
84
71
// if mock file doesn't exist, check parent directory
85
72
if (!file_exists ($ dir .'/Mock/ ' .$ path ) && file_exists ($ dir .'/../Mock/ ' .$ path )) {
86
- return MockPlugin:: getMockFile ( $ dir .'/../Mock/ ' .$ path );
73
+ return \ GuzzleHttp \ Psr7 \parse_response ( file_get_contents ( $ dir .'/../Mock/ ' .$ path) );
87
74
}
88
75
89
- return MockPlugin:: getMockFile ( $ dir .'/Mock/ ' .$ path );
76
+ return \ GuzzleHttp \ Psr7 \parse_response ( file_get_contents ( $ dir .'/Mock/ ' .$ path) );
90
77
}
91
78
92
79
/**
@@ -95,31 +82,19 @@ public function getMockHttpResponse($path)
95
82
* This method assumes that mock response files are located under the
96
83
* Mock/ subdirectory of the current class. A mock response is added to the next
97
84
* request sent by the client.
98
- *
99
- * An array of path can be provided and the next x number of client requests are
85
+ *
86
+ * An array of path can be provided and the next x number of client requests are
100
87
* mocked in the order of the array where x = the array length.
101
88
*
102
89
* @param array|string $paths Path to files within the Mock folder of the service
103
90
*
104
- * @return MockPlugin returns the created mock plugin
91
+ * @return void returns the created mock plugin
105
92
*/
106
93
public function setMockHttpResponse ($ paths )
107
94
{
108
- $ this ->mockHttpRequests = array ();
109
- $ that = $ this ;
110
- $ mock = new MockPlugin (null , true );
111
- $ this ->getHttpClient ()->getEventDispatcher ()->removeSubscriber ($ mock );
112
- $ mock ->getEventDispatcher ()->addListener ('mock.request ' , function (Event $ event ) use ($ that ) {
113
- $ that ->addMockedHttpRequest ($ event ['request ' ]);
114
- });
115
-
116
95
foreach ((array ) $ paths as $ path ) {
117
- $ mock ->addResponse ($ this ->getMockHttpResponse ($ path ));
96
+ $ this -> mockClient ->addResponse ($ this ->getMockHttpResponse ($ path ));
118
97
}
119
-
120
- $ this ->getHttpClient ()->getEventDispatcher ()->addSubscriber ($ mock );
121
-
122
- return $ mock ;
123
98
}
124
99
125
100
/**
@@ -160,10 +135,21 @@ public function getMockRequest()
160
135
return $ this ->mockRequest ;
161
136
}
162
137
138
+ public function getMockClient ()
139
+ {
140
+ if (null === $ this ->mockClient ) {
141
+ $ this ->mockClient = new MockClient ();
142
+ }
143
+
144
+ return $ this ->mockClient ;
145
+ }
146
+
163
147
public function getHttpClient ()
164
148
{
165
149
if (null === $ this ->httpClient ) {
166
- $ this ->httpClient = new HttpClient ;
150
+ $ this ->httpClient = new Client (
151
+ $ this ->getMockClient ()
152
+ );
167
153
}
168
154
169
155
return $ this ->httpClient ;
0 commit comments