Skip to content

Commit 20eb12d

Browse files
committed
stop mocking immutable/value objects
Psr\Http\Message\MessageInterface and Psr\Http\Message\ResponseInterface mocks are replaced with GuzzleHttp\Psr7\Request and GuzzleHttp\Psr7\Response respectively. Http\Promise\Promise mocks are replaced either with Http\Client\Promise\HttpFulfilledPromise or Http\Client\Promise\HttpRejectedPromise depending on the test requirements.
1 parent a131e38 commit 20eb12d

File tree

4 files changed

+39
-102
lines changed

4 files changed

+39
-102
lines changed

Tests/Unit/Collector/FormatterTest.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
namespace Http\HttplugBundle\Tests\Unit\Collector;
44

5+
use GuzzleHttp\Psr7\Request;
6+
use GuzzleHttp\Psr7\Response;
57
use Http\Client\Exception\HttpException;
68
use Http\Client\Exception\TransferException;
79
use Http\HttplugBundle\Collector\Formatter;
810
use Http\Message\Formatter as MessageFormatter;
911
use Http\Message\Formatter\CurlCommandFormatter;
10-
use Psr\Http\Message\RequestInterface;
11-
use Psr\Http\Message\ResponseInterface;
1212

1313
class FormatterTest extends \PHPUnit_Framework_TestCase
1414
{
@@ -37,7 +37,7 @@ public function setUp()
3737

3838
public function testFormatRequest()
3939
{
40-
$request = $this->getMockBuilder(RequestInterface::class)->getMock();
40+
$request = new Request('GET', '/');
4141

4242
$this->formatter
4343
->expects($this->once())
@@ -50,7 +50,7 @@ public function testFormatRequest()
5050

5151
public function testFormatResponse()
5252
{
53-
$response = $this->getMockBuilder(ResponseInterface::class)->getMock();
53+
$response = new Response();
5454

5555
$this->formatter
5656
->expects($this->once())
@@ -63,12 +63,9 @@ public function testFormatResponse()
6363

6464
public function testFormatHttpException()
6565
{
66-
$response = $this->getMockBuilder(ResponseInterface::class)->getMock();
67-
$exception = $this->getMockBuilder(HttpException::class)->disableOriginalConstructor()->getMock();
68-
$exception
69-
->method('getResponse')
70-
->willReturn($response)
71-
;
66+
$request = new Request('GET', '/');
67+
$response = new Response();
68+
$exception = new HttpException('', $request, $response);
7269

7370
$this->formatter
7471
->expects($this->once())
@@ -95,7 +92,7 @@ public function testFormatException()
9592

9693
public function testFormatAsCurlCommand()
9794
{
98-
$request = $this->getMockBuilder(RequestInterface::class)->getMock();
95+
$request = new Request('GET', '/');
9996

10097
$this->curlFormatter
10198
->expects($this->once())

Tests/Unit/Collector/ProfileClientTest.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
namespace Http\HttplugBundle\Tests\Unit\Collector;
44

5+
use GuzzleHttp\Psr7\Request;
6+
use GuzzleHttp\Psr7\Response;
7+
use GuzzleHttp\Psr7\Uri;
58
use Http\Client\HttpAsyncClient;
69
use Http\Client\HttpClient;
710
use Http\HttplugBundle\Collector\Collector;
811
use Http\HttplugBundle\Collector\Formatter;
912
use Http\HttplugBundle\Collector\ProfileClient;
1013
use Http\HttplugBundle\Collector\Stack;
14+
use Http\Promise\FulfilledPromise;
1115
use Http\Promise\Promise;
1216
use Psr\Http\Message\RequestInterface;
1317
use Psr\Http\Message\ResponseInterface;
@@ -71,29 +75,19 @@ public function setUp()
7175
$this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock();
7276
$this->currentStack = new Stack('default', 'FormattedRequest');
7377
$this->client = $this->getMockBuilder(ClientInterface::class)->getMock();
74-
$this->request = $this->getMockBuilder(RequestInterface::class)->getMock();
78+
$this->uri = new Uri('https://example.com/target');
79+
$this->request = new Request('GET', $this->uri);
7580
$this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
7681
$this->stopwatch = new Stopwatch();
7782
$this->subject = new ProfileClient($this->client, $this->collector, $this->formatter, $this->stopwatch);
78-
$this->response = $this->getMockBuilder(ResponseInterface::class)->getMock();
79-
$this->promise = $this->getMockBuilder(Promise::class)->getMock();
80-
$this->uri = $this->getMockBuilder(UriInterface::class)->getMock();
83+
$this->response = new Response();
84+
$this->promise = new FulfilledPromise($this->response);
8185

8286
$this->client->method('sendRequest')->willReturn($this->response);
8387
$this->client->method('sendAsyncRequest')->will($this->returnCallback(function () {
84-
$promise = $this->getMockBuilder(Promise::class)->getMock();
85-
$promise->method('then')->willReturn($this->promise);
86-
87-
return $promise;
88+
return $this->promise;
8889
}));
8990

90-
$this->request->method('getMethod')->willReturn('GET');
91-
$this->request->method('getRequestTarget')->willReturn('/target');
92-
$this->request->method('getUri')->willReturn($this->uri);
93-
94-
$this->uri->method('getScheme')->willReturn('https');
95-
$this->uri->method('getHost')->willReturn('example.com');
96-
9791
$this->collector->method('getCurrentStack')->willReturn($this->currentStack);
9892
}
9993

Tests/Unit/Collector/ProfilePluginTest.php

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace Http\HttplugBundle\Tests\Unit\Collector;
44

5-
use Exception;
5+
use GuzzleHttp\Psr7\Request;
6+
use GuzzleHttp\Psr7\Response;
67
use Http\Client\Common\Plugin;
8+
use Http\Client\Exception\TransferException;
79
use Http\HttplugBundle\Collector\Collector;
810
use Http\HttplugBundle\Collector\Formatter;
911
use Http\HttplugBundle\Collector\ProfilePlugin;
1012
use Http\HttplugBundle\Collector\Stack;
11-
use Http\Promise\Promise;
13+
use Http\Promise\FulfilledPromise;
1214
use Psr\Http\Message\RequestInterface;
1315
use Psr\Http\Message\ResponseInterface;
1416

@@ -40,12 +42,7 @@ class ProfilePluginTest extends \PHPUnit_Framework_TestCase
4042
private $currentStack;
4143

4244
/**
43-
* @var Promise
44-
*/
45-
private $promise;
46-
47-
/**
48-
* @var Exception
45+
* @var TransferException
4946
*/
5047
private $exception;
5148

@@ -63,11 +60,10 @@ public function setUp()
6360
{
6461
$this->plugin = $this->getMockBuilder(Plugin::class)->getMock();
6562
$this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock();
66-
$this->request = $this->getMockBuilder(RequestInterface::class)->getMock();
67-
$this->response = $this->getMockBuilder(ResponseInterface::class)->getMock();
63+
$this->request = new Request('GET', '/');
64+
$this->response = new Response();
6865
$this->currentStack = new Stack('default', 'FormattedRequest');
69-
$this->promise = $this->getMockBuilder(Promise::class)->getMock();
70-
$this->exception = $this->getMockBuilder(Exception::class)->disableOriginalConstructor()->getMock();
66+
$this->exception = new TransferException();
7167
$this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
7268

7369
$this->collector
@@ -80,7 +76,7 @@ public function setUp()
8076
->willReturnCallback(function ($request, $next, $first) {
8177
$next($request);
8278

83-
return $this->promise;
79+
return new FulfilledPromise($this->response);
8480
})
8581
;
8682

@@ -146,20 +142,6 @@ public function testCollectRequestInformations()
146142

147143
public function testOnFulfilled()
148144
{
149-
$this->promise
150-
->method('then')
151-
->will($this->returnCallback(function (callable $onFulfilled) {
152-
$fulfilled = $this->getMockBuilder(Promise::class)->getMock();
153-
$fulfilled
154-
->method('wait')
155-
->with(true)
156-
->willReturn($onFulfilled($this->response))
157-
;
158-
159-
return $fulfilled;
160-
}))
161-
;
162-
163145
$promise = $this->subject->handleRequest($this->request, function () {
164146
}, function () {
165147
});
@@ -171,23 +153,10 @@ public function testOnFulfilled()
171153

172154
public function testOnRejected()
173155
{
174-
$this->setExpectedException(Exception::class);
175-
176-
$this->promise
177-
->method('then')
178-
->will($this->returnCallback(function (callable $onFulfilled, callable $onRejected) {
179-
$rejected = $this->getMockBuilder(Promise::class)->getMock();
180-
$rejected
181-
->method('wait')
182-
->with(true)
183-
->willReturn($onRejected($this->exception))
184-
;
185-
186-
return $rejected;
187-
}))
188-
;
156+
$this->setExpectedException(TransferException::class);
189157

190158
$promise = $this->subject->handleRequest($this->request, function () {
159+
throw new TransferException();
191160
}, function () {
192161
});
193162

Tests/Unit/Collector/StackPluginTest.php

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
namespace Http\HttplugBundle\Tests\Unit\Collector;
44

55
use Exception;
6+
use GuzzleHttp\Psr7\Request;
7+
use GuzzleHttp\Psr7\Response;
8+
use Http\Client\Exception\HttpException;
69
use Http\HttplugBundle\Collector\Collector;
710
use Http\HttplugBundle\Collector\Formatter;
811
use Http\HttplugBundle\Collector\Stack;
912
use Http\HttplugBundle\Collector\StackPlugin;
10-
use Http\Promise\Promise;
13+
use Http\Promise\FulfilledPromise;
14+
use Http\Promise\RejectedPromise;
1115
use Psr\Http\Message\RequestInterface;
1216
use Psr\Http\Message\ResponseInterface;
1317

@@ -47,9 +51,9 @@ public function setUp()
4751
{
4852
$this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock();
4953
$this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
50-
$this->request = $this->getMockBuilder(RequestInterface::class)->getMock();
51-
$this->response = $this->getMockBuilder(ResponseInterface::class)->getMock();
52-
$this->exception = $this->getMockBuilder(Exception::class)->disableOriginalConstructor()->getMock();
54+
$this->request = new Request('GET', '/');
55+
$this->response = new Response();
56+
$this->exception = new HttpException('', $this->request, $this->response);
5357

5458
$this->formatter
5559
->method('formatRequest')
@@ -86,7 +90,7 @@ public function testStackIsInitialized()
8690
;
8791

8892
$next = function () {
89-
return $this->getMockBuilder(Promise::class)->getMock();
93+
return new FulfilledPromise($this->response);
9094
};
9195

9296
$this->subject->handleRequest($this->request, $next, function () {
@@ -107,21 +111,7 @@ public function testOnFulfilled()
107111
;
108112

109113
$next = function () {
110-
$promise = $this->getMockBuilder(Promise::class)->getMock();
111-
$promise->method('then')
112-
->will($this->returnCallback(function (callable $onFulfilled) {
113-
$fulfilled = $this->getMockBuilder(Promise::class)->getMock();
114-
$fulfilled
115-
->method('wait')
116-
->with(true)
117-
->willReturn($onFulfilled($this->response))
118-
;
119-
120-
return $fulfilled;
121-
}))
122-
;
123-
124-
return $promise;
114+
return new FulfilledPromise($this->response);
125115
};
126116

127117
$promise = $this->subject->handleRequest($this->request, $next, function () {
@@ -148,20 +138,7 @@ public function testOnRejected()
148138
$this->setExpectedException(Exception::class);
149139

150140
$next = function () {
151-
$promise = $this->getMockBuilder(Promise::class)->getMock();
152-
$promise
153-
->method('then')
154-
->will($this->returnCallback(function (callable $onFulfilled, callable $onRejected) {
155-
$rejected = $this->getMockBuilder(Promise::class)->getMock();
156-
$rejected
157-
->method('wait')
158-
->with(true)
159-
->willReturn($onRejected($this->exception));
160-
161-
return $rejected;
162-
}));
163-
164-
return $promise;
141+
return new RejectedPromise($this->exception);
165142
};
166143

167144
$promise = $this->subject->handleRequest($this->request, $next, function () {

0 commit comments

Comments
 (0)