Skip to content

Commit b89618e

Browse files
committed
adjust tests to not mock
1 parent e836461 commit b89618e

6 files changed

+45
-131
lines changed

src/Collector/Formatter.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Http\Client\Exception\HttpException;
77
use Http\Client\Exception\TransferException;
88
use Http\Message\Formatter as MessageFormatter;
9-
use Http\Message\Formatter\CurlCommandFormatter;
109
use Psr\Http\Client\NetworkExceptionInterface;
1110
use Psr\Http\Message\RequestInterface;
1211
use Psr\Http\Message\ResponseInterface;
@@ -27,15 +26,15 @@ final class Formatter implements MessageFormatter
2726
private $formatter;
2827

2928
/**
30-
* @var CurlCommandFormatter
29+
* @var MessageFormatter
3130
*/
3231
private $curlFormatter;
3332

3433
/**
3534
* @param MessageFormatter $formatter
36-
* @param CurlCommandFormatter $curlFormatter
35+
* @param MessageFormatter $curlFormatter
3736
*/
38-
public function __construct(MessageFormatter $formatter, CurlCommandFormatter $curlFormatter)
37+
public function __construct(MessageFormatter $formatter, MessageFormatter $curlFormatter)
3938
{
4039
$this->formatter = $formatter;
4140
$this->curlFormatter = $curlFormatter;

tests/Unit/Collector/PluginClientFactoryListenerTest.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Http\HttplugBundle\Collector\Formatter;
88
use Http\HttplugBundle\Collector\PluginClientFactory;
99
use Http\HttplugBundle\Collector\PluginClientFactoryListener;
10+
use Http\Message\Formatter as MessageFormatter;
1011
use Nyholm\NSA;
1112
use PHPUnit\Framework\TestCase;
1213
use Symfony\Component\EventDispatcher\Event;
@@ -16,9 +17,9 @@ final class PluginClientFactoryListenerTest extends TestCase
1617
{
1718
public function testRegisterPluginClientFactory()
1819
{
19-
$collector = $this->getMockBuilder(Collector::class)->getMock();
20-
$formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
21-
$stopwatch = $this->getMockBuilder(Stopwatch::class)->getMock();
20+
$collector = new Collector();
21+
$formatter = new Formatter($this->createMock(MessageFormatter::class), $this->createMock(MessageFormatter::class));
22+
$stopwatch = $this->createMock(Stopwatch::class);
2223

2324
$factory = new PluginClientFactory($collector, $formatter, $stopwatch);
2425

tests/Unit/Collector/ProfileClientFactoryTest.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Http\HttplugBundle\Collector\Formatter;
99
use Http\HttplugBundle\Collector\ProfileClient;
1010
use Http\HttplugBundle\Collector\ProfileClientFactory;
11+
use Http\Message\Formatter as MessageFormatter;
1112
use PHPUnit\Framework\TestCase;
1213
use Symfony\Component\Stopwatch\Stopwatch;
1314

@@ -35,10 +36,10 @@ class ProfileClientFactoryTest extends TestCase
3536

3637
public function setUp()
3738
{
38-
$this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock();
39-
$this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
40-
$this->stopwatch = $this->getMockBuilder(Stopwatch::class)->getMock();
41-
$this->client = $this->getMockBuilder(HttpClient::class)->getMock();
39+
$this->collector = new Collector();
40+
$this->formatter = new Formatter($this->createMock(MessageFormatter::class), $this->createMock(MessageFormatter::class));
41+
$this->stopwatch = $this->createMock(Stopwatch::class);
42+
$this->client = $this->createMock(HttpClient::class);
4243
}
4344

4445
public function testCreateClientFromClientFactory()

tests/Unit/Collector/ProfileClientTest.php

+9-29
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Http\HttplugBundle\Collector\Formatter;
1212
use Http\HttplugBundle\Collector\ProfileClient;
1313
use Http\HttplugBundle\Collector\Stack;
14+
use Http\Message\Formatter as MessageFormatter;
1415
use Http\Promise\FulfilledPromise;
1516
use Http\Promise\Promise;
1617
use Http\Promise\RejectedPromise;
@@ -90,31 +91,27 @@ class ProfileClientTest extends TestCase
9091

9192
public function setUp()
9293
{
93-
$this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock();
94+
$messageFormatter = $this->createMock(MessageFormatter::class);
95+
$this->formatter = new Formatter($messageFormatter, $this->createMock(MessageFormatter::class));
96+
$this->collector = new Collector();
97+
$this->stopwatch = $this->createMock(Stopwatch::class);
98+
9499
$this->activeStack = new Stack('default', 'FormattedRequest');
95100
$this->client = $this->getMockBuilder(ClientInterface::class)->getMock();
96101
$this->uri = new Uri('https://example.com/target');
97102
$this->request = new Request('GET', $this->uri);
98-
$this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
99-
$this->stopwatch = $this->getMockBuilder(Stopwatch::class)->disableOriginalConstructor()->getMock();
100-
$this->stopwatchEvent = $this->getMockBuilder(StopwatchEvent::class)->disableOriginalConstructor()->getMock();
103+
$this->stopwatchEvent = $this->createMock(StopwatchEvent::class);
101104
$this->subject = new ProfileClient($this->client, $this->collector, $this->formatter, $this->stopwatch);
102105
$this->response = new Response();
103106
$this->exception = new \Exception();
104107
$this->fulfilledPromise = new FulfilledPromise($this->response);
105108
$this->rejectedPromise = new RejectedPromise($this->exception);
106109

107-
$this->collector->method('getActiveStack')->willReturn($this->activeStack);
108-
$this->formatter
110+
$messageFormatter
109111
->method('formatResponse')
110112
->with($this->response)
111113
->willReturn('FormattedResponse')
112114
;
113-
$this->formatter
114-
->method('formatException')
115-
->with($this->exception)
116-
->willReturn('FormattedException')
117-
;
118115

119116
$this->stopwatch
120117
->method('start')
@@ -154,11 +151,6 @@ public function testSendAsyncRequest()
154151
->willReturn($this->fulfilledPromise)
155152
;
156153

157-
$this->collector
158-
->expects($this->once())
159-
->method('deactivateStack')
160-
;
161-
162154
$promise = $this->subject->sendAsyncRequest($this->request);
163155

164156
$this->assertEquals($this->fulfilledPromise, $promise);
@@ -170,12 +162,6 @@ public function testSendAsyncRequest()
170162

171163
public function testOnFulfilled()
172164
{
173-
$this->collector
174-
->expects($this->once())
175-
->method('activateStack')
176-
->with($this->activeStack)
177-
;
178-
179165
$this->stopwatchEvent
180166
->expects($this->once())
181167
->method('stop')
@@ -195,12 +181,6 @@ public function testOnFulfilled()
195181

196182
public function testOnRejected()
197183
{
198-
$this->collector
199-
->expects($this->once())
200-
->method('activateStack')
201-
->with($this->activeStack)
202-
;
203-
204184
$this->stopwatchEvent
205185
->expects($this->once())
206186
->method('stop')
@@ -214,7 +194,7 @@ public function testOnRejected()
214194
$this->subject->sendAsyncRequest($this->request);
215195

216196
$this->assertEquals(42, $this->activeStack->getDuration());
217-
$this->assertEquals('FormattedException', $this->activeStack->getClientException());
197+
$this->assertEquals('FormattedResponse', $this->activeStack->getClientException());
218198
}
219199
}
220200

tests/Unit/Collector/ProfilePluginTest.php

+12-22
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@
1010
use Http\HttplugBundle\Collector\Formatter;
1111
use Http\HttplugBundle\Collector\ProfilePlugin;
1212
use Http\HttplugBundle\Collector\Stack;
13+
use Http\Message\Formatter as MessageFormatter;
1314
use Http\Promise\FulfilledPromise;
1415
use Http\Promise\Promise;
1516
use Http\Promise\RejectedPromise;
17+
use PHPUnit\Framework\MockObject\MockObject;
1618
use PHPUnit\Framework\TestCase;
1719
use Psr\Http\Message\RequestInterface;
1820
use Psr\Http\Message\ResponseInterface;
1921

2022
class ProfilePluginTest extends TestCase
2123
{
2224
/**
23-
* @var Plugin
25+
* @var Plugin|MockObject
2426
*/
2527
private $plugin;
2628

@@ -71,20 +73,17 @@ class ProfilePluginTest extends TestCase
7173

7274
public function setUp()
7375
{
76+
$this->collector = new Collector();
77+
$messageFormatter = $this->createMock(MessageFormatter::class);
78+
$this->formatter = new Formatter($messageFormatter, $this->createMock(MessageFormatter::class));
79+
7480
$this->plugin = $this->getMockBuilder(Plugin::class)->getMock();
75-
$this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock();
7681
$this->request = new Request('GET', '/');
7782
$this->response = new Response();
7883
$this->fulfilledPromise = new FulfilledPromise($this->response);
7984
$this->currentStack = new Stack('default', 'FormattedRequest');
8085
$this->exception = new TransferException();
8186
$this->rejectedPromise = new RejectedPromise($this->exception);
82-
$this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
83-
84-
$this->collector
85-
->method('getActiveStack')
86-
->willReturn($this->currentStack)
87-
;
8887

8988
$this->plugin
9089
->method('handleRequest')
@@ -93,29 +92,22 @@ public function setUp()
9392
})
9493
;
9594

96-
$this->formatter
95+
$messageFormatter
9796
->method('formatRequest')
9897
->with($this->identicalTo($this->request))
9998
->willReturn('FormattedRequest')
10099
;
101100

102-
$this->formatter
101+
$messageFormatter
103102
->method('formatResponse')
104103
->with($this->identicalTo($this->response))
105104
->willReturn('FormattedResponse')
106105
;
107106

108-
$this->formatter
109-
->method('formatException')
110-
->with($this->identicalTo($this->exception))
111-
->willReturn('FormattedException')
112-
;
113-
114107
$this->subject = new ProfilePlugin(
115108
$this->plugin,
116109
$this->collector,
117-
$this->formatter,
118-
'http.plugin.mock'
110+
$this->formatter
119111
);
120112
}
121113

@@ -168,9 +160,6 @@ public function testOnFulfilled()
168160
$this->assertEquals('FormattedResponse', $profile->getResponse());
169161
}
170162

171-
/**
172-
* @expectedException \Http\Client\Exception\TransferException
173-
*/
174163
public function testOnRejected()
175164
{
176165
$promise = $this->subject->handleRequest($this->request, function () {
@@ -180,6 +169,7 @@ public function testOnRejected()
180169

181170
$this->assertEquals($this->exception, $promise->wait());
182171
$profile = $this->currentStack->getProfiles()[0];
183-
$this->assertEquals('FormattedException', $profile->getResponse());
172+
$this->expectException(TransferException::class);
173+
$profile->getResponse();
184174
}
185175
}

0 commit comments

Comments
 (0)