Skip to content

Commit

Permalink
adjust tests to not mock
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Nov 18, 2023
1 parent cd42daa commit 3e38cc0
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 134 deletions.
6 changes: 2 additions & 4 deletions src/Collector/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

namespace Http\HttplugBundle\Collector;

use Exception;
use Http\Client\Exception\HttpException;
use Http\Client\Exception\TransferException;
use Http\Message\Formatter as MessageFormatter;
use Http\Message\Formatter\CurlCommandFormatter;
use Psr\Http\Client\NetworkExceptionInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -29,11 +27,11 @@ final class Formatter implements MessageFormatter
private $formatter;

/**
* @var CurlCommandFormatter
* @var MessageFormatter
*/
private $curlFormatter;

public function __construct(MessageFormatter $formatter, CurlCommandFormatter $curlFormatter)
public function __construct(MessageFormatter $formatter, MessageFormatter $curlFormatter)
{
$this->formatter = $formatter;
$this->curlFormatter = $curlFormatter;
Expand Down
7 changes: 4 additions & 3 deletions tests/Unit/Collector/PluginClientFactoryListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Http\HttplugBundle\Collector\Formatter;
use Http\HttplugBundle\Collector\PluginClientFactory;
use Http\HttplugBundle\Collector\PluginClientFactoryListener;
use Http\Message\Formatter as MessageFormatter;
use Nyholm\NSA;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\Event as LegacyEvent;
Expand All @@ -20,9 +21,9 @@ final class PluginClientFactoryListenerTest extends TestCase
{
public function testRegisterPluginClientFactory(): void
{
$collector = $this->getMockBuilder(Collector::class)->getMock();
$formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
$stopwatch = $this->getMockBuilder(Stopwatch::class)->getMock();
$collector = new Collector();
$formatter = new Formatter($this->createMock(MessageFormatter::class), $this->createMock(MessageFormatter::class));
$stopwatch = $this->createMock(Stopwatch::class);

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

Expand Down
9 changes: 5 additions & 4 deletions tests/Unit/Collector/ProfileClientFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Http\HttplugBundle\Collector\Formatter;
use Http\HttplugBundle\Collector\ProfileClient;
use Http\HttplugBundle\Collector\ProfileClientFactory;
use Http\Message\Formatter as MessageFormatter;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Stopwatch\Stopwatch;

Expand Down Expand Up @@ -37,10 +38,10 @@ class ProfileClientFactoryTest extends TestCase

public function setUp(): void
{
$this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock();
$this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
$this->stopwatch = $this->getMockBuilder(Stopwatch::class)->getMock();
$this->client = $this->getMockBuilder(HttpClient::class)->getMock();
$this->collector = new Collector();
$this->formatter = new Formatter($this->createMock(MessageFormatter::class), $this->createMock(MessageFormatter::class));
$this->stopwatch = $this->createMock(Stopwatch::class);
$this->client = $this->createMock(HttpClient::class);
}

public function testCreateClientFromClientFactory(): void
Expand Down
45 changes: 10 additions & 35 deletions tests/Unit/Collector/ProfileClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Http\HttplugBundle\Collector\Formatter;
use Http\HttplugBundle\Collector\ProfileClient;
use Http\HttplugBundle\Collector\Stack;
use Http\Message\Formatter as MessageFormatter;
use Http\Promise\FulfilledPromise;
use Http\Promise\Promise;
use Http\Promise\RejectedPromise;
Expand Down Expand Up @@ -47,7 +48,7 @@ class ProfileClientTest extends TestCase
private $request;

/**
* @var Formatter|MockObject
* @var Formatter
*/
private $formatter;

Expand Down Expand Up @@ -93,22 +94,23 @@ class ProfileClientTest extends TestCase

public function setUp(): void
{
$this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock();
$messageFormatter = $this->createMock(MessageFormatter::class);
$this->formatter = new Formatter($messageFormatter, $this->createMock(MessageFormatter::class));
$this->collector = new Collector();
$this->stopwatch = $this->createMock(Stopwatch::class);

$this->activeStack = new Stack('default', 'FormattedRequest');
$this->client = $this->getMockBuilder(ClientInterface::class)->getMock();
$this->uri = new Uri('https://example.com/target');
$this->request = new Request('GET', $this->uri);
$this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
$this->stopwatch = $this->getMockBuilder(Stopwatch::class)->disableOriginalConstructor()->getMock();
$this->stopwatchEvent = $this->getMockBuilder(StopwatchEvent::class)->disableOriginalConstructor()->getMock();
$this->stopwatchEvent = $this->createMock(StopwatchEvent::class);
$this->subject = new ProfileClient($this->client, $this->collector, $this->formatter, $this->stopwatch);
$this->response = new Response();
$this->exception = new \Exception();
$this->fulfilledPromise = new FulfilledPromise($this->response);
$this->rejectedPromise = new RejectedPromise($this->exception);

$this->collector->method('getActiveStack')->willReturn($this->activeStack);
$this->formatter
$messageFormatter
->method('formatResponse')
->with($this->response)
->willReturn('FormattedResponse')
Expand Down Expand Up @@ -151,10 +153,6 @@ public function testSendRequestTypeError()
->willReturnCallback(function () {
throw new \Error('You set string to int prop');
});
$this->formatter
->expects($this->once())
->method('formatException')
->with($this->isInstanceOf(\Error::class));

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

$this->collector
->expects($this->once())
->method('deactivateStack')
;

$promise = $this->subject->sendAsyncRequest($this->request);

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

public function testOnFulfilled(): void
{
$this->collector
->expects($this->once())
->method('activateStack')
->with($this->activeStack)
;

$this->stopwatchEvent
->expects($this->once())
->method('stop')
Expand All @@ -211,12 +198,6 @@ public function testOnFulfilled(): void

public function testOnRejected(): void
{
$this->collector
->expects($this->once())
->method('activateStack')
->with($this->activeStack)
;

$this->stopwatchEvent
->expects($this->once())
->method('stop')
Expand All @@ -227,16 +208,10 @@ public function testOnRejected(): void
->willReturn($this->rejectedPromise)
;

$this->formatter
->method('formatException')
->with($this->exception)
->willReturn('FormattedException')
;

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

$this->assertEquals(42, $this->activeStack->getDuration());
$this->assertEquals('FormattedException', $this->activeStack->getClientException());
$this->assertEquals('FormattedResponse', $this->activeStack->getClientException());
}
}

Expand Down
32 changes: 13 additions & 19 deletions tests/Unit/Collector/ProfilePluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@
use Http\HttplugBundle\Collector\Formatter;
use Http\HttplugBundle\Collector\ProfilePlugin;
use Http\HttplugBundle\Collector\Stack;
use Http\Message\Formatter as MessageFormatter;
use Http\Promise\FulfilledPromise;
use Http\Promise\Promise;
use Http\Promise\RejectedPromise;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class ProfilePluginTest extends TestCase
{
/**
* @var Plugin
* @var Plugin|MockObject
*/
private $plugin;

Expand Down Expand Up @@ -73,20 +75,17 @@ class ProfilePluginTest extends TestCase

public function setUp(): void
{
$this->collector = new Collector();
$messageFormatter = $this->createMock(MessageFormatter::class);
$this->formatter = new Formatter($messageFormatter, $this->createMock(MessageFormatter::class));

$this->plugin = $this->getMockBuilder(Plugin::class)->getMock();
$this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock();
$this->request = new Request('GET', '/');
$this->response = new Response();
$this->fulfilledPromise = new FulfilledPromise($this->response);
$this->currentStack = new Stack('default', 'FormattedRequest');
$this->exception = new TransferException();
$this->rejectedPromise = new RejectedPromise($this->exception);
$this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();

$this->collector
->method('getActiveStack')
->willReturn($this->currentStack)
;

$this->plugin
->method('handleRequest')
Expand All @@ -95,29 +94,22 @@ public function setUp(): void
})
;

$this->formatter
$messageFormatter
->method('formatRequest')
->with($this->identicalTo($this->request))
->willReturn('FormattedRequest')
;

$this->formatter
$messageFormatter
->method('formatResponse')
->with($this->identicalTo($this->response))
->willReturn('FormattedResponse')
;

$this->formatter
->method('formatException')
->with($this->identicalTo($this->exception))
->willReturn('FormattedException')
;

$this->subject = new ProfilePlugin(
$this->plugin,
$this->collector,
$this->formatter,
'http.plugin.mock'
$this->formatter
);
}

Expand Down Expand Up @@ -177,7 +169,9 @@ public function testOnRejected(): void
}, function (): void {
});

$this->assertEquals($this->exception, $promise->wait());
$profile = $this->currentStack->getProfiles()[0];
$this->expectException(TransferException::class);
$promise->wait();
$profile->getResponse();
}
}
Loading

0 comments on commit 3e38cc0

Please sign in to comment.