Skip to content

Commit 8a896e7

Browse files
committed
adjust tests to not mock
1 parent 3ccd96c commit 8a896e7

File tree

6 files changed

+47
-122
lines changed

6 files changed

+47
-122
lines changed

src/Collector/Formatter.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Http\Client\Exception\HttpException;
99
use Http\Client\Exception\TransferException;
1010
use Http\Message\Formatter as MessageFormatter;
11-
use Http\Message\Formatter\CurlCommandFormatter;
1211
use Psr\Http\Client\NetworkExceptionInterface;
1312
use Psr\Http\Message\RequestInterface;
1413
use Psr\Http\Message\ResponseInterface;
@@ -29,11 +28,11 @@ final class Formatter implements MessageFormatter
2928
private $formatter;
3029

3130
/**
32-
* @var CurlCommandFormatter
31+
* @var MessageFormatter
3332
*/
3433
private $curlFormatter;
3534

36-
public function __construct(MessageFormatter $formatter, CurlCommandFormatter $curlFormatter)
35+
public function __construct(MessageFormatter $formatter, MessageFormatter $curlFormatter)
3736
{
3837
$this->formatter = $formatter;
3938
$this->curlFormatter = $curlFormatter;

tests/Unit/Collector/PluginClientFactoryListenerTest.php

Lines changed: 4 additions & 3 deletions
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

Lines changed: 5 additions & 4 deletions
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

Lines changed: 9 additions & 24 deletions
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;
@@ -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')
@@ -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

Lines changed: 13 additions & 19 deletions
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
}

tests/Unit/Collector/StackPluginTest.php

Lines changed: 14 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use Http\Client\Exception\HttpException;
1010
use Http\HttplugBundle\Collector\Collector;
1111
use Http\HttplugBundle\Collector\Formatter;
12-
use Http\HttplugBundle\Collector\Stack;
1312
use Http\HttplugBundle\Collector\StackPlugin;
13+
use Http\Message\Formatter as MessageFormatter;
1414
use Http\Promise\FulfilledPromise;
1515
use Http\Promise\RejectedPromise;
1616
use PHPUnit\Framework\Error\Warning;
@@ -52,75 +52,43 @@ class StackPluginTest extends TestCase
5252

5353
public function setUp(): void
5454
{
55-
$this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock();
56-
$this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
55+
$this->collector = new Collector();
56+
$messageFormatter = $this->createMock(MessageFormatter::class);
57+
$this->formatter = new Formatter($messageFormatter, $this->createMock(MessageFormatter::class));
5758
$this->request = new Request('GET', '/');
5859
$this->response = new Response();
5960
$this->exception = new HttpException('', $this->request, $this->response);
6061

61-
$this->formatter
62+
$messageFormatter
6263
->method('formatRequest')
6364
->with($this->request)
6465
->willReturn('FormattedRequest')
6566
;
6667

67-
$this->formatter
68+
$messageFormatter
6869
->method('formatResponse')
6970
->with($this->response)
7071
->willReturn('FormattedResponse')
7172
;
7273

73-
$this->formatter
74-
->method('formatException')
75-
->with($this->exception)
76-
->willReturn('FormattedException')
77-
;
78-
7974
$this->subject = new StackPlugin($this->collector, $this->formatter, 'default');
8075
}
8176

8277
public function testStackIsInitialized(): void
8378
{
84-
$this->collector
85-
->expects($this->once())
86-
->method('addStack')
87-
->with($this->callback(function (Stack $stack) {
88-
$this->assertEquals('default', $stack->getClient());
89-
$this->assertEquals('FormattedRequest', $stack->getRequest());
90-
91-
return true;
92-
}))
93-
;
94-
$this->collector
95-
->expects($this->once())
96-
->method('activateStack')
97-
;
98-
9979
$next = function () {
10080
return new FulfilledPromise($this->response);
10181
};
10282

10383
$this->subject->handleRequest($this->request, $next, function (): void {
10484
});
85+
$stack = $this->collector->getActiveStack();
86+
$this->assertEquals('default', $stack->getClient());
87+
$this->assertEquals('FormattedRequest', $stack->getRequest());
10588
}
10689

10790
public function testOnFulfilled(): void
10891
{
109-
//Capture the current stack
110-
$currentStack = null;
111-
$this->collector
112-
->method('addStack')
113-
->with($this->callback(function (Stack $stack) use (&$currentStack) {
114-
$currentStack = $stack;
115-
116-
return true;
117-
}))
118-
;
119-
$this->collector
120-
->expects($this->once())
121-
->method('deactivateStack')
122-
;
123-
12492
$next = function () {
12593
return new FulfilledPromise($this->response);
12694
};
@@ -129,45 +97,27 @@ public function testOnFulfilled(): void
12997
});
13098

13199
$this->assertEquals($this->response, $promise->wait());
132-
$this->assertInstanceOf(Stack::class, $currentStack);
100+
$currentStack = $this->collector->getActiveStack();
133101
$this->assertEquals('FormattedResponse', $currentStack->getResponse());
134102
}
135103

136104
public function testOnRejected(): void
137105
{
138-
//Capture the current stack
139-
$currentStack = null;
140-
$this->collector
141-
->method('addStack')
142-
->with($this->callback(function (Stack $stack) use (&$currentStack) {
143-
$currentStack = $stack;
144-
145-
return true;
146-
}))
147-
;
148-
$this->collector
149-
->expects($this->once())
150-
->method('deactivateStack')
151-
;
152-
153106
$next = function () {
154107
return new RejectedPromise($this->exception);
155108
};
156109

157110
$promise = $this->subject->handleRequest($this->request, $next, function (): void {
158111
});
159112

160-
$this->expectException(\Exception::class);
161-
$promise->wait();
113+
$this->assertEquals($this->exception, $promise->wait());
114+
$currentStack = $this->collector->getActiveStack();
115+
$this->assertEquals('FormattedResponse', $currentStack->getResponse());
116+
$this->assertTrue($currentStack->isFailed());
162117
}
163118

164119
public function testOnException(): void
165120
{
166-
$this->collector
167-
->expects($this->once())
168-
->method('deactivateStack')
169-
;
170-
171121
$next = function (): void {
172122
throw new \Exception();
173123
};
@@ -185,11 +135,6 @@ public function testOnError(): void
185135
$this->expectException(Warning::class);
186136
}
187137

188-
$this->collector
189-
->expects($this->once())
190-
->method('deactivateStack')
191-
;
192-
193138
$next = function () {
194139
return 2 / 0;
195140
};

0 commit comments

Comments
 (0)