Skip to content

Commit dd01464

Browse files
committed
adjust tests to not mock
1 parent 91a213e commit dd01464

File tree

4 files changed

+33
-46
lines changed

4 files changed

+33
-46
lines changed

src/Collector/Formatter.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
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;

tests/Unit/Collector/ProfileClientTest.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Http\HttplugBundle\Collector\ProfileClient;
1414
use Http\HttplugBundle\Collector\Stack;
1515
use Http\Message\Formatter as MessageFormatter;
16-
use Http\Message\Formatter\SimpleFormatter;
1716
use Http\Promise\FulfilledPromise;
1817
use Http\Promise\Promise;
1918
use Http\Promise\RejectedPromise;
@@ -45,26 +44,27 @@ class ProfileClientTest extends TestCase
4544

4645
public function setUp(): void
4746
{
48-
$messageFormatter = $this->createMock(SimpleFormatter::class);
47+
$messageFormatter = $this->createMock(MessageFormatter::class);
4948
$formatter = new Formatter($messageFormatter, $this->createMock(MessageFormatter::class));
5049
$this->collector = new Collector();
5150
$stopwatch = $this->createMock(Stopwatch::class);
5251

53-
$activeStack = new Stack('default', 'FormattedRequest');
54-
$this->collector->activateStack($activeStack);
52+
$this->activeStack = new Stack('default', 'FormattedRequest');
53+
$this->collector->activateStack($this->activeStack);
54+
5555
$this->client = $this->getMockBuilder(CombinedClientInterface::class)->getMock();
5656
$uri = new Uri('https://example.com/target');
5757
$this->request = new Request('GET', $uri);
5858
$this->stopwatchEvent = $this->createMock(StopwatchEvent::class);
5959
$this->subject = new ProfileClient($this->client, $this->collector, $formatter, $stopwatch);
6060
$this->response = new Response();
61-
$exception = new \Exception('test');
61+
$this->exception = new \Exception();
6262
$this->fulfilledPromise = new FulfilledPromise($this->response);
63-
$this->rejectedPromise = new RejectedPromise($exception);
63+
$this->rejectedPromise = new RejectedPromise($this->exception);
6464

6565
$messageFormatter
66-
->method('formatResponseForRequest')
67-
->with($this->response, $this->request)
66+
->method('formatResponse')
67+
->with($this->response)
6868
->willReturn('FormattedResponse')
6969
;
7070

@@ -168,10 +168,8 @@ public function testOnRejected(): void
168168

169169
$this->subject->sendAsyncRequest($this->request);
170170

171-
$activeStack = $this->collector->getActiveStack();
172-
$this->assertInstanceOf(Stack::class, $activeStack);
173-
$this->assertEquals(42, $activeStack->getDuration());
174-
$this->assertEquals('Unexpected exception of type "Exception": test', $activeStack->getClientException());
171+
$this->assertEquals(42, $this->activeStack->getDuration());
172+
$this->assertEquals('FormattedResponse', $this->activeStack->getClientException());
175173
}
176174
}
177175

tests/Unit/Collector/ProfilePluginTest.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313
use Http\HttplugBundle\Collector\ProfilePlugin;
1414
use Http\HttplugBundle\Collector\Stack;
1515
use Http\Message\Formatter as MessageFormatter;
16-
use Http\Message\Formatter\SimpleFormatter;
1716
use Http\Promise\FulfilledPromise;
1817
use Http\Promise\Promise;
1918
use Http\Promise\RejectedPromise;
19+
use PHPUnit\Framework\MockObject\MockObject;
2020
use PHPUnit\Framework\TestCase;
2121
use Psr\Http\Message\RequestInterface;
2222
use Psr\Http\Message\ResponseInterface;
2323

2424
class ProfilePluginTest extends TestCase
2525
{
26-
private Plugin $plugin;
26+
private Plugin&MockObject $plugin;
2727

2828
private RequestInterface $request;
2929

@@ -42,15 +42,15 @@ class ProfilePluginTest extends TestCase
4242
public function setUp(): void
4343
{
4444
$this->collector = new Collector();
45-
$messageFormatter = $this->createMock(SimpleFormatter::class);
46-
$formatter = new Formatter($messageFormatter, $this->createMock(MessageFormatter::class));
45+
$messageFormatter = $this->createMock(MessageFormatter::class);
46+
$this->formatter = new Formatter($messageFormatter, $this->createMock(MessageFormatter::class));
4747

48-
$this->plugin = $this->createMock(Plugin::class);
48+
$this->plugin = $this->getMockBuilder(Plugin::class)->getMock();
4949
$this->request = new Request('GET', '/');
5050
$this->response = new Response();
5151
$this->fulfilledPromise = new FulfilledPromise($this->response);
52-
$currentStack = new Stack('default', 'FormattedRequest');
53-
$this->collector->activateStack($currentStack);
52+
$this->currentStack = new Stack('default', 'FormattedRequest');
53+
$this->collector->activateStack($this->currentStack);
5454
$this->exception = new TransferException();
5555
$this->rejectedPromise = new RejectedPromise($this->exception);
5656

@@ -74,7 +74,7 @@ public function setUp(): void
7474
$this->subject = new ProfilePlugin(
7575
$this->plugin,
7676
$this->collector,
77-
$formatter
77+
$this->formatter
7878
);
7979
}
8080

@@ -130,10 +130,9 @@ public function testOnRejected(): void
130130
$promise = $this->subject->handleRequest($this->request, fn () => $this->rejectedPromise, function (): void {
131131
});
132132

133-
$activeStack = $this->collector->getActiveStack();
134-
$this->assertCount(1, $activeStack->getProfiles());
135-
133+
$this->assertEquals($this->exception, $promise->wait());
134+
$profile = $this->currentStack->getProfiles()[0];
136135
$this->expectException(TransferException::class);
137-
$promise->wait();
136+
$profile->getResponse();
138137
}
139138
}

tests/Unit/Collector/StackPluginTest.php

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Http\HttplugBundle\Collector\Formatter;
1212
use Http\HttplugBundle\Collector\StackPlugin;
1313
use Http\Message\Formatter as MessageFormatter;
14-
use Http\Message\Formatter\SimpleFormatter;
1514
use Http\Promise\FulfilledPromise;
1615
use Http\Promise\RejectedPromise;
1716
use PHPUnit\Framework\Error\Warning;
@@ -49,8 +48,8 @@ class StackPluginTest extends TestCase
4948
public function setUp(): void
5049
{
5150
$this->collector = new Collector();
52-
$messageFormatter = $this->createMock(SimpleFormatter::class);
53-
$formatter = new Formatter($messageFormatter, $this->createMock(MessageFormatter::class));
51+
$messageFormatter = $this->createMock(MessageFormatter::class);
52+
$this->formatter = new Formatter($messageFormatter, $this->createMock(MessageFormatter::class));
5453
$this->request = new Request('GET', '/');
5554
$this->response = new Response();
5655
$this->exception = new HttpException('', $this->request, $this->response);
@@ -67,7 +66,7 @@ public function setUp(): void
6766
->willReturn('FormattedResponse')
6867
;
6968

70-
$this->subject = new StackPlugin($this->collector, $formatter, 'default');
69+
$this->subject = new StackPlugin($this->collector, $this->formatter, 'default');
7170
}
7271

7372
public function testStackIsInitialized(): void
@@ -77,11 +76,9 @@ public function testStackIsInitialized(): void
7776
$this->subject->handleRequest($this->request, $next, function (): void {
7877
});
7978

80-
$this->assertNull($this->collector->getActiveStack());
81-
$stacks = $this->collector->getStacks();
82-
$this->assertCount(1, $stacks);
83-
$this->assertEquals('default', $stacks[0]->getClient());
84-
$this->assertEquals('FormattedRequest', $stacks[0]->getRequest());
79+
$stack = $this->collector->getActiveStack();
80+
$this->assertEquals('default', $stack->getClient());
81+
$this->assertEquals('FormattedRequest', $stack->getRequest());
8582
}
8683

8784
public function testOnFulfilled(): void
@@ -92,10 +89,8 @@ public function testOnFulfilled(): void
9289
});
9390

9491
$this->assertEquals($this->response, $promise->wait());
95-
$this->assertNull($this->collector->getActiveStack());
96-
$stacks = $this->collector->getStacks();
97-
$this->assertCount(1, $stacks);
98-
$this->assertEquals('FormattedResponse', $stacks[0]->getResponse());
92+
$currentStack = $this->collector->getActiveStack();
93+
$this->assertEquals('FormattedResponse', $currentStack->getResponse());
9994
}
10095

10196
public function testOnRejected(): void
@@ -105,14 +100,10 @@ public function testOnRejected(): void
105100
$promise = $this->subject->handleRequest($this->request, $next, function (): void {
106101
});
107102

108-
$this->assertNull($this->collector->getActiveStack());
109-
$stacks = $this->collector->getStacks();
110-
$this->assertCount(1, $stacks);
111-
$this->assertEquals('FormattedResponse', $stacks[0]->getResponse());
112-
$this->assertTrue($stacks[0]->isFailed());
113-
114-
$this->expectException(\Exception::class);
115-
$promise->wait();
103+
$this->assertEquals($this->exception, $promise->wait());
104+
$currentStack = $this->collector->getActiveStack();
105+
$this->assertEquals('FormattedResponse', $currentStack->getResponse());
106+
$this->assertTrue($currentStack->isFailed());
116107
}
117108

118109
public function testOnException(): void

0 commit comments

Comments
 (0)