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 Mar 5, 2019
1 parent fb4711d commit 1651c9a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 131 deletions.
7 changes: 3 additions & 4 deletions Collector/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
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 @@ -27,15 +26,15 @@ final class Formatter implements MessageFormatter
private $formatter;

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

/**
* @param MessageFormatter $formatter
* @param CurlCommandFormatter $curlFormatter
* @param MessageFormatter $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 @@ -7,6 +7,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;
Expand All @@ -16,9 +17,9 @@ final class PluginClientFactoryListenerTest extends TestCase
{
public function testRegisterPluginClientFactory()
{
$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 @@ -8,6 +8,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 @@ -35,10 +36,10 @@ class ProfileClientFactoryTest extends TestCase

public function setUp()
{
$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()
Expand Down
38 changes: 9 additions & 29 deletions Tests/Unit/Collector/ProfileClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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 @@ -90,31 +91,27 @@ class ProfileClientTest extends TestCase

public function setUp()
{
$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')
;
$this->formatter
->method('formatException')
->with($this->exception)
->willReturn('FormattedException')
;

$this->stopwatch
->method('start')
Expand Down Expand Up @@ -154,11 +151,6 @@ public function testSendAsyncRequest()
->willReturn($this->fulfilledPromise)
;

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

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

$this->assertEquals($this->fulfilledPromise, $promise);
Expand All @@ -170,12 +162,6 @@ public function testSendAsyncRequest()

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

$this->stopwatchEvent
->expects($this->once())
->method('stop')
Expand All @@ -195,12 +181,6 @@ public function testOnFulfilled()

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

$this->stopwatchEvent
->expects($this->once())
->method('stop')
Expand All @@ -214,7 +194,7 @@ public function testOnRejected()
$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
34 changes: 12 additions & 22 deletions Tests/Unit/Collector/ProfilePluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,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 @@ -71,20 +73,17 @@ class ProfilePluginTest extends TestCase

public function setUp()
{
$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 @@ -93,29 +92,22 @@ public function setUp()
})
;

$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 @@ -168,9 +160,6 @@ public function testOnFulfilled()
$this->assertEquals('FormattedResponse', $profile->getResponse());
}

/**
* @expectedException \Http\Client\Exception\TransferException
*/
public function testOnRejected()
{
$promise = $this->subject->handleRequest($this->request, function () {
Expand All @@ -180,6 +169,7 @@ public function testOnRejected()

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

0 comments on commit 1651c9a

Please sign in to comment.