Skip to content

Commit 3e38cc0

Browse files
committed
adjust tests to not mock
1 parent cd42daa commit 3e38cc0

6 files changed

+48
-134
lines changed

src/Collector/Formatter.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44

55
namespace Http\HttplugBundle\Collector;
66

7-
use Exception;
87
use Http\Client\Exception\HttpException;
98
use Http\Client\Exception\TransferException;
109
use Http\Message\Formatter as MessageFormatter;
11-
use Http\Message\Formatter\CurlCommandFormatter;
1210
use Psr\Http\Client\NetworkExceptionInterface;
1311
use Psr\Http\Message\RequestInterface;
1412
use Psr\Http\Message\ResponseInterface;
@@ -29,11 +27,11 @@ final class Formatter implements MessageFormatter
2927
private $formatter;
3028

3129
/**
32-
* @var CurlCommandFormatter
30+
* @var MessageFormatter
3331
*/
3432
private $curlFormatter;
3533

36-
public function __construct(MessageFormatter $formatter, CurlCommandFormatter $curlFormatter)
34+
public function __construct(MessageFormatter $formatter, MessageFormatter $curlFormatter)
3735
{
3836
$this->formatter = $formatter;
3937
$this->curlFormatter = $curlFormatter;

tests/Unit/Collector/PluginClientFactoryListenerTest.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Http\HttplugBundle\Collector\Formatter;
1010
use Http\HttplugBundle\Collector\PluginClientFactory;
1111
use Http\HttplugBundle\Collector\PluginClientFactoryListener;
12+
use Http\Message\Formatter as MessageFormatter;
1213
use Nyholm\NSA;
1314
use PHPUnit\Framework\TestCase;
1415
use Symfony\Component\EventDispatcher\Event as LegacyEvent;
@@ -20,9 +21,9 @@ final class PluginClientFactoryListenerTest extends TestCase
2021
{
2122
public function testRegisterPluginClientFactory(): void
2223
{
23-
$collector = $this->getMockBuilder(Collector::class)->getMock();
24-
$formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
25-
$stopwatch = $this->getMockBuilder(Stopwatch::class)->getMock();
24+
$collector = new Collector();
25+
$formatter = new Formatter($this->createMock(MessageFormatter::class), $this->createMock(MessageFormatter::class));
26+
$stopwatch = $this->createMock(Stopwatch::class);
2627

2728
$factory = new PluginClientFactory($collector, $formatter, $stopwatch);
2829

tests/Unit/Collector/ProfileClientFactoryTest.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Http\HttplugBundle\Collector\Formatter;
1111
use Http\HttplugBundle\Collector\ProfileClient;
1212
use Http\HttplugBundle\Collector\ProfileClientFactory;
13+
use Http\Message\Formatter as MessageFormatter;
1314
use PHPUnit\Framework\TestCase;
1415
use Symfony\Component\Stopwatch\Stopwatch;
1516

@@ -37,10 +38,10 @@ class ProfileClientFactoryTest extends TestCase
3738

3839
public function setUp(): void
3940
{
40-
$this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock();
41-
$this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
42-
$this->stopwatch = $this->getMockBuilder(Stopwatch::class)->getMock();
43-
$this->client = $this->getMockBuilder(HttpClient::class)->getMock();
41+
$this->collector = new Collector();
42+
$this->formatter = new Formatter($this->createMock(MessageFormatter::class), $this->createMock(MessageFormatter::class));
43+
$this->stopwatch = $this->createMock(Stopwatch::class);
44+
$this->client = $this->createMock(HttpClient::class);
4445
}
4546

4647
public function testCreateClientFromClientFactory(): void

tests/Unit/Collector/ProfileClientTest.php

+10-35
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Http\HttplugBundle\Collector\Formatter;
1414
use Http\HttplugBundle\Collector\ProfileClient;
1515
use Http\HttplugBundle\Collector\Stack;
16+
use Http\Message\Formatter as MessageFormatter;
1617
use Http\Promise\FulfilledPromise;
1718
use Http\Promise\Promise;
1819
use Http\Promise\RejectedPromise;
@@ -47,7 +48,7 @@ class ProfileClientTest extends TestCase
4748
private $request;
4849

4950
/**
50-
* @var Formatter|MockObject
51+
* @var Formatter
5152
*/
5253
private $formatter;
5354

@@ -93,22 +94,23 @@ class ProfileClientTest extends TestCase
9394

9495
public function setUp(): void
9596
{
96-
$this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock();
97+
$messageFormatter = $this->createMock(MessageFormatter::class);
98+
$this->formatter = new Formatter($messageFormatter, $this->createMock(MessageFormatter::class));
99+
$this->collector = new Collector();
100+
$this->stopwatch = $this->createMock(Stopwatch::class);
101+
97102
$this->activeStack = new Stack('default', 'FormattedRequest');
98103
$this->client = $this->getMockBuilder(ClientInterface::class)->getMock();
99104
$this->uri = new Uri('https://example.com/target');
100105
$this->request = new Request('GET', $this->uri);
101-
$this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
102-
$this->stopwatch = $this->getMockBuilder(Stopwatch::class)->disableOriginalConstructor()->getMock();
103-
$this->stopwatchEvent = $this->getMockBuilder(StopwatchEvent::class)->disableOriginalConstructor()->getMock();
106+
$this->stopwatchEvent = $this->createMock(StopwatchEvent::class);
104107
$this->subject = new ProfileClient($this->client, $this->collector, $this->formatter, $this->stopwatch);
105108
$this->response = new Response();
106109
$this->exception = new \Exception();
107110
$this->fulfilledPromise = new FulfilledPromise($this->response);
108111
$this->rejectedPromise = new RejectedPromise($this->exception);
109112

110-
$this->collector->method('getActiveStack')->willReturn($this->activeStack);
111-
$this->formatter
113+
$messageFormatter
112114
->method('formatResponse')
113115
->with($this->response)
114116
->willReturn('FormattedResponse')
@@ -151,10 +153,6 @@ public function testSendRequestTypeError()
151153
->willReturnCallback(function () {
152154
throw new \Error('You set string to int prop');
153155
});
154-
$this->formatter
155-
->expects($this->once())
156-
->method('formatException')
157-
->with($this->isInstanceOf(\Error::class));
158156

159157
$this->expectException(\Error::class);
160158
$this->expectExceptionMessage('You set string to int prop');
@@ -170,11 +168,6 @@ public function testSendAsyncRequest(): void
170168
->willReturn($this->fulfilledPromise)
171169
;
172170

173-
$this->collector
174-
->expects($this->once())
175-
->method('deactivateStack')
176-
;
177-
178171
$promise = $this->subject->sendAsyncRequest($this->request);
179172

180173
$this->assertEquals($this->fulfilledPromise, $promise);
@@ -186,12 +179,6 @@ public function testSendAsyncRequest(): void
186179

187180
public function testOnFulfilled(): void
188181
{
189-
$this->collector
190-
->expects($this->once())
191-
->method('activateStack')
192-
->with($this->activeStack)
193-
;
194-
195182
$this->stopwatchEvent
196183
->expects($this->once())
197184
->method('stop')
@@ -211,12 +198,6 @@ public function testOnFulfilled(): void
211198

212199
public function testOnRejected(): void
213200
{
214-
$this->collector
215-
->expects($this->once())
216-
->method('activateStack')
217-
->with($this->activeStack)
218-
;
219-
220201
$this->stopwatchEvent
221202
->expects($this->once())
222203
->method('stop')
@@ -227,16 +208,10 @@ public function testOnRejected(): void
227208
->willReturn($this->rejectedPromise)
228209
;
229210

230-
$this->formatter
231-
->method('formatException')
232-
->with($this->exception)
233-
->willReturn('FormattedException')
234-
;
235-
236211
$this->subject->sendAsyncRequest($this->request);
237212

238213
$this->assertEquals(42, $this->activeStack->getDuration());
239-
$this->assertEquals('FormattedException', $this->activeStack->getClientException());
214+
$this->assertEquals('FormattedResponse', $this->activeStack->getClientException());
240215
}
241216
}
242217

tests/Unit/Collector/ProfilePluginTest.php

+13-19
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@
1212
use Http\HttplugBundle\Collector\Formatter;
1313
use Http\HttplugBundle\Collector\ProfilePlugin;
1414
use Http\HttplugBundle\Collector\Stack;
15+
use Http\Message\Formatter as MessageFormatter;
1516
use Http\Promise\FulfilledPromise;
1617
use Http\Promise\Promise;
1718
use Http\Promise\RejectedPromise;
19+
use PHPUnit\Framework\MockObject\MockObject;
1820
use PHPUnit\Framework\TestCase;
1921
use Psr\Http\Message\RequestInterface;
2022
use Psr\Http\Message\ResponseInterface;
2123

2224
class ProfilePluginTest extends TestCase
2325
{
2426
/**
25-
* @var Plugin
27+
* @var Plugin|MockObject
2628
*/
2729
private $plugin;
2830

@@ -73,20 +75,17 @@ class ProfilePluginTest extends TestCase
7375

7476
public function setUp(): void
7577
{
78+
$this->collector = new Collector();
79+
$messageFormatter = $this->createMock(MessageFormatter::class);
80+
$this->formatter = new Formatter($messageFormatter, $this->createMock(MessageFormatter::class));
81+
7682
$this->plugin = $this->getMockBuilder(Plugin::class)->getMock();
77-
$this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock();
7883
$this->request = new Request('GET', '/');
7984
$this->response = new Response();
8085
$this->fulfilledPromise = new FulfilledPromise($this->response);
8186
$this->currentStack = new Stack('default', 'FormattedRequest');
8287
$this->exception = new TransferException();
8388
$this->rejectedPromise = new RejectedPromise($this->exception);
84-
$this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
85-
86-
$this->collector
87-
->method('getActiveStack')
88-
->willReturn($this->currentStack)
89-
;
9089

9190
$this->plugin
9291
->method('handleRequest')
@@ -95,29 +94,22 @@ public function setUp(): void
9594
})
9695
;
9796

98-
$this->formatter
97+
$messageFormatter
9998
->method('formatRequest')
10099
->with($this->identicalTo($this->request))
101100
->willReturn('FormattedRequest')
102101
;
103102

104-
$this->formatter
103+
$messageFormatter
105104
->method('formatResponse')
106105
->with($this->identicalTo($this->response))
107106
->willReturn('FormattedResponse')
108107
;
109108

110-
$this->formatter
111-
->method('formatException')
112-
->with($this->identicalTo($this->exception))
113-
->willReturn('FormattedException')
114-
;
115-
116109
$this->subject = new ProfilePlugin(
117110
$this->plugin,
118111
$this->collector,
119-
$this->formatter,
120-
'http.plugin.mock'
112+
$this->formatter
121113
);
122114
}
123115

@@ -177,7 +169,9 @@ public function testOnRejected(): void
177169
}, function (): void {
178170
});
179171

172+
$this->assertEquals($this->exception, $promise->wait());
173+
$profile = $this->currentStack->getProfiles()[0];
180174
$this->expectException(TransferException::class);
181-
$promise->wait();
175+
$profile->getResponse();
182176
}
183177
}

0 commit comments

Comments
 (0)