From 1651c9a0f68877b85c87423341a243074d141389 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Tue, 5 Mar 2019 11:24:22 +0100 Subject: [PATCH] adjust tests to not mock --- Collector/Formatter.php | 7 +- .../PluginClientFactoryListenerTest.php | 7 +- .../Collector/ProfileClientFactoryTest.php | 9 ++- Tests/Unit/Collector/ProfileClientTest.php | 38 +++------ Tests/Unit/Collector/ProfilePluginTest.php | 34 +++----- Tests/Unit/Collector/StackPluginTest.php | 81 +++---------------- 6 files changed, 45 insertions(+), 131 deletions(-) diff --git a/Collector/Formatter.php b/Collector/Formatter.php index 16dc8124..be831815 100644 --- a/Collector/Formatter.php +++ b/Collector/Formatter.php @@ -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; @@ -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; diff --git a/Tests/Unit/Collector/PluginClientFactoryListenerTest.php b/Tests/Unit/Collector/PluginClientFactoryListenerTest.php index 7db38697..d6734ebc 100644 --- a/Tests/Unit/Collector/PluginClientFactoryListenerTest.php +++ b/Tests/Unit/Collector/PluginClientFactoryListenerTest.php @@ -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; @@ -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); diff --git a/Tests/Unit/Collector/ProfileClientFactoryTest.php b/Tests/Unit/Collector/ProfileClientFactoryTest.php index 507704e9..699b7683 100644 --- a/Tests/Unit/Collector/ProfileClientFactoryTest.php +++ b/Tests/Unit/Collector/ProfileClientFactoryTest.php @@ -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; @@ -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() diff --git a/Tests/Unit/Collector/ProfileClientTest.php b/Tests/Unit/Collector/ProfileClientTest.php index 121015bb..18558cd7 100644 --- a/Tests/Unit/Collector/ProfileClientTest.php +++ b/Tests/Unit/Collector/ProfileClientTest.php @@ -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; @@ -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') @@ -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); @@ -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') @@ -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') @@ -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()); } } diff --git a/Tests/Unit/Collector/ProfilePluginTest.php b/Tests/Unit/Collector/ProfilePluginTest.php index 7a06a527..c9eaf199 100644 --- a/Tests/Unit/Collector/ProfilePluginTest.php +++ b/Tests/Unit/Collector/ProfilePluginTest.php @@ -10,9 +10,11 @@ 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; @@ -20,7 +22,7 @@ class ProfilePluginTest extends TestCase { /** - * @var Plugin + * @var Plugin|MockObject */ private $plugin; @@ -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') @@ -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 ); } @@ -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 () { @@ -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(); } } diff --git a/Tests/Unit/Collector/StackPluginTest.php b/Tests/Unit/Collector/StackPluginTest.php index c7416008..3213e6b2 100644 --- a/Tests/Unit/Collector/StackPluginTest.php +++ b/Tests/Unit/Collector/StackPluginTest.php @@ -7,8 +7,8 @@ use Http\Client\Exception\HttpException; use Http\HttplugBundle\Collector\Collector; use Http\HttplugBundle\Collector\Formatter; -use Http\HttplugBundle\Collector\Stack; use Http\HttplugBundle\Collector\StackPlugin; +use Http\Message\Formatter as MessageFormatter; use Http\Promise\FulfilledPromise; use Http\Promise\RejectedPromise; use PHPUnit\Framework\TestCase; @@ -49,75 +49,43 @@ class StackPluginTest extends TestCase public function setUp() { - $this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock(); - $this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock(); + $this->collector = new Collector(); + $messageFormatter = $this->createMock(MessageFormatter::class); + $this->formatter = new Formatter($messageFormatter, $this->createMock(MessageFormatter::class)); $this->request = new Request('GET', '/'); $this->response = new Response(); $this->exception = new HttpException('', $this->request, $this->response); - $this->formatter + $messageFormatter ->method('formatRequest') ->with($this->request) ->willReturn('FormattedRequest') ; - $this->formatter + $messageFormatter ->method('formatResponse') ->with($this->response) ->willReturn('FormattedResponse') ; - $this->formatter - ->method('formatException') - ->with($this->exception) - ->willReturn('FormattedException') - ; - $this->subject = new StackPlugin($this->collector, $this->formatter, 'default'); } public function testStackIsInitialized() { - $this->collector - ->expects($this->once()) - ->method('addStack') - ->with($this->callback(function (Stack $stack) { - $this->assertEquals('default', $stack->getClient()); - $this->assertEquals('FormattedRequest', $stack->getRequest()); - - return true; - })) - ; - $this->collector - ->expects($this->once()) - ->method('activateStack') - ; - $next = function () { return new FulfilledPromise($this->response); }; $this->subject->handleRequest($this->request, $next, function () { }); + $stack = $this->collector->getActiveStack(); + $this->assertEquals('default', $stack->getClient()); + $this->assertEquals('FormattedRequest', $stack->getRequest()); } public function testOnFulfilled() { - //Capture the current stack - $currentStack = null; - $this->collector - ->method('addStack') - ->with($this->callback(function (Stack $stack) use (&$currentStack) { - $currentStack = $stack; - - return true; - })) - ; - $this->collector - ->expects($this->once()) - ->method('deactivateStack') - ; - $next = function () { return new FulfilledPromise($this->response); }; @@ -126,7 +94,7 @@ public function testOnFulfilled() }); $this->assertEquals($this->response, $promise->wait()); - $this->assertInstanceOf(Stack::class, $currentStack); + $currentStack = $this->collector->getActiveStack(); $this->assertEquals('FormattedResponse', $currentStack->getResponse()); } @@ -135,21 +103,6 @@ public function testOnFulfilled() */ public function testOnRejected() { - //Capture the current stack - $currentStack = null; - $this->collector - ->method('addStack') - ->with($this->callback(function (Stack $stack) use (&$currentStack) { - $currentStack = $stack; - - return true; - })) - ; - $this->collector - ->expects($this->once()) - ->method('deactivateStack') - ; - $next = function () { return new RejectedPromise($this->exception); }; @@ -158,8 +111,8 @@ public function testOnRejected() }); $this->assertEquals($this->exception, $promise->wait()); - $this->assertInstanceOf(Stack::class, $currentStack); - $this->assertEquals('FormattedException', $currentStack->getResponse()); + $currentStack = $this->collector->getActiveStack(); + $this->assertEquals('FormattedResponse', $currentStack->getResponse()); $this->assertTrue($currentStack->isFailed()); } @@ -168,11 +121,6 @@ public function testOnRejected() */ public function testOnException() { - $this->collector - ->expects($this->once()) - ->method('deactivateStack') - ; - $next = function () { throw new \Exception(); }; @@ -199,11 +147,6 @@ public function testOnError() $this->expectException(\PHPUnit\Framework\Error\Warning::class); } - $this->collector - ->expects($this->once()) - ->method('deactivateStack') - ; - $next = function () { return 2 / 0; };