Skip to content

Commit e05e986

Browse files
committed
adjust tests to not mock
1 parent cbca805 commit e05e986

6 files changed

+48
-123
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(): void
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(): void
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(): void

tests/Unit/Collector/ProfileClientTest.php

+9-24
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;
@@ -91,22 +92,23 @@ class ProfileClientTest extends TestCase
9192

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

108-
$this->collector->method('getActiveStack')->willReturn($this->activeStack);
109-
$this->formatter
111+
$messageFormatter
110112
->method('formatResponse')
111113
->with($this->response)
112114
->willReturn('FormattedResponse')
@@ -170,11 +172,6 @@ public function testSendAsyncRequest(): void
170172
->willReturn($this->fulfilledPromise)
171173
;
172174

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

180177
$this->assertEquals($this->fulfilledPromise, $promise);
@@ -186,12 +183,6 @@ public function testSendAsyncRequest(): void
186183

187184
public function testOnFulfilled(): void
188185
{
189-
$this->collector
190-
->expects($this->once())
191-
->method('activateStack')
192-
->with($this->activeStack)
193-
;
194-
195186
$this->stopwatchEvent
196187
->expects($this->once())
197188
->method('stop')
@@ -211,12 +202,6 @@ public function testOnFulfilled(): void
211202

212203
public function testOnRejected(): void
213204
{
214-
$this->collector
215-
->expects($this->once())
216-
->method('activateStack')
217-
->with($this->activeStack)
218-
;
219-
220205
$this->stopwatchEvent
221206
->expects($this->once())
222207
->method('stop')
@@ -236,7 +221,7 @@ public function testOnRejected(): void
236221
$this->subject->sendAsyncRequest($this->request);
237222

238223
$this->assertEquals(42, $this->activeStack->getDuration());
239-
$this->assertEquals('FormattedException', $this->activeStack->getClientException());
224+
$this->assertEquals('FormattedResponse', $this->activeStack->getClientException());
240225
}
241226
}
242227

tests/Unit/Collector/ProfilePluginTest.php

+13-19
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(): void
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(): void
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

@@ -175,7 +167,9 @@ public function testOnRejected(): void
175167
}, function (): void {
176168
});
177169

170+
$this->assertEquals($this->exception, $promise->wait());
171+
$profile = $this->currentStack->getProfiles()[0];
178172
$this->expectException(TransferException::class);
179-
$promise->wait();
173+
$profile->getResponse();
180174
}
181175
}

0 commit comments

Comments
 (0)