Skip to content

Commit 79a3d17

Browse files
authored
Update for 3.x
Update for 3.x
1 parent 137fdea commit 79a3d17

File tree

2 files changed

+37
-47
lines changed

2 files changed

+37
-47
lines changed

composer.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,18 @@
2626
"psr-0": { "Omnipay\\Tests\\" : "src/" }
2727
},
2828
"require": {
29-
"guzzle/plugin-mock": "~3.1",
30-
"mockery/mockery": "~0.8",
31-
"phpunit/phpunit": "~3.7.0",
32-
"squizlabs/php_codesniffer": "~1.5"
29+
"php-http/mock-client": "^1",
30+
"php-http/guzzle6-adapter": "^1.1",
31+
"mockery/mockery": "^0.9",
32+
"phpunit/phpunit": "^5.7.0"
33+
},
34+
"require-dev": {
35+
"omnipay/common": "^3",
36+
"symfony/http-foundation": "^3"
3337
},
3438
"extra": {
3539
"branch-alias": {
36-
"dev-master": "2.0.x-dev"
40+
"dev-master": "3.0.x-dev"
3741
}
3842
}
3943
}

src/Omnipay/Tests/TestCase.php

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
namespace Omnipay\Tests;
44

55
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;
79
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;
1311
use Symfony\Component\HttpFoundation\Request as HttpRequest;
1412

1513
/**
@@ -19,8 +17,10 @@
1917
*/
2018
abstract class TestCase extends PHPUnit_Framework_TestCase
2119
{
22-
private $mockHttpRequests = array();
20+
/** @var \Omnipay\Common\Message\RequestInterface */
2321
private $mockRequest;
22+
/** @var MockClient */
23+
private $mockClient;
2424
private $httpClient;
2525
private $httpRequest;
2626

@@ -41,19 +41,6 @@ function ($match) {
4141
);
4242
}
4343

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-
}
5744

5845
/**
5946
* Get all of the mocked requests
@@ -62,19 +49,19 @@ public function addMockedHttpRequest(GuzzleRequestInterface $request)
6249
*/
6350
public function getMockedRequests()
6451
{
65-
return $this->mockHttpRequests;
52+
return $this->mockClient->getRequests();
6653
}
6754

6855
/**
6956
* Get a mock response for a client by mock file name
7057
*
7158
* @param string $path Relative path to the mock response file
7259
*
73-
* @return Response
60+
* @return ResponseInterface
7461
*/
7562
public function getMockHttpResponse($path)
7663
{
77-
if ($path instanceof Response) {
64+
if ($path instanceof ResponseInterface) {
7865
return $path;
7966
}
8067

@@ -83,10 +70,10 @@ public function getMockHttpResponse($path)
8370

8471
// if mock file doesn't exist, check parent directory
8572
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));
8774
}
8875

89-
return MockPlugin::getMockFile($dir.'/Mock/'.$path);
76+
return \GuzzleHttp\Psr7\parse_response(file_get_contents($dir.'/Mock/'.$path));
9077
}
9178

9279
/**
@@ -95,31 +82,19 @@ public function getMockHttpResponse($path)
9582
* This method assumes that mock response files are located under the
9683
* Mock/ subdirectory of the current class. A mock response is added to the next
9784
* 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
10087
* mocked in the order of the array where x = the array length.
10188
*
10289
* @param array|string $paths Path to files within the Mock folder of the service
10390
*
104-
* @return MockPlugin returns the created mock plugin
91+
* @return void returns the created mock plugin
10592
*/
10693
public function setMockHttpResponse($paths)
10794
{
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-
11695
foreach ((array) $paths as $path) {
117-
$mock->addResponse($this->getMockHttpResponse($path));
96+
$this->mockClient->addResponse($this->getMockHttpResponse($path));
11897
}
119-
120-
$this->getHttpClient()->getEventDispatcher()->addSubscriber($mock);
121-
122-
return $mock;
12398
}
12499

125100
/**
@@ -160,10 +135,21 @@ public function getMockRequest()
160135
return $this->mockRequest;
161136
}
162137

138+
public function getMockClient()
139+
{
140+
if (null === $this->mockClient) {
141+
$this->mockClient = new MockClient();
142+
}
143+
144+
return $this->mockClient;
145+
}
146+
163147
public function getHttpClient()
164148
{
165149
if (null === $this->httpClient) {
166-
$this->httpClient = new HttpClient;
150+
$this->httpClient = new Client(
151+
$this->getMockClient()
152+
);
167153
}
168154

169155
return $this->httpClient;

0 commit comments

Comments
 (0)