diff --git a/src/Collector/Formatter.php b/src/Collector/Formatter.php index 619d43bb..3ad1574c 100644 --- a/src/Collector/Formatter.php +++ b/src/Collector/Formatter.php @@ -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; @@ -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; diff --git a/tests/Unit/Collector/PluginClientFactoryListenerTest.php b/tests/Unit/Collector/PluginClientFactoryListenerTest.php index ef2be31a..376d317e 100644 --- a/tests/Unit/Collector/PluginClientFactoryListenerTest.php +++ b/tests/Unit/Collector/PluginClientFactoryListenerTest.php @@ -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; @@ -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); diff --git a/tests/Unit/Collector/ProfileClientFactoryTest.php b/tests/Unit/Collector/ProfileClientFactoryTest.php index 4aeae247..0b893423 100644 --- a/tests/Unit/Collector/ProfileClientFactoryTest.php +++ b/tests/Unit/Collector/ProfileClientFactoryTest.php @@ -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; @@ -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 diff --git a/tests/Unit/Collector/ProfileClientTest.php b/tests/Unit/Collector/ProfileClientTest.php index a6db587a..c8fb9501 100644 --- a/tests/Unit/Collector/ProfileClientTest.php +++ b/tests/Unit/Collector/ProfileClientTest.php @@ -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; @@ -47,7 +48,7 @@ class ProfileClientTest extends TestCase private $request; /** - * @var Formatter|MockObject + * @var Formatter */ private $formatter; @@ -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') @@ -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'); @@ -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); @@ -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') @@ -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') @@ -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()); } } diff --git a/tests/Unit/Collector/ProfilePluginTest.php b/tests/Unit/Collector/ProfilePluginTest.php index 115d8c60..12d74e4f 100644 --- a/tests/Unit/Collector/ProfilePluginTest.php +++ b/tests/Unit/Collector/ProfilePluginTest.php @@ -12,9 +12,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; @@ -22,7 +24,7 @@ class ProfilePluginTest extends TestCase { /** - * @var Plugin + * @var Plugin|MockObject */ private $plugin; @@ -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') @@ -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 ); } @@ -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(); } } diff --git a/tests/Unit/Collector/StackPluginTest.php b/tests/Unit/Collector/StackPluginTest.php index 8eb443cd..ddb5f2cb 100644 --- a/tests/Unit/Collector/StackPluginTest.php +++ b/tests/Unit/Collector/StackPluginTest.php @@ -9,8 +9,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\Error\Warning; @@ -52,75 +52,43 @@ class StackPluginTest extends TestCase public function setUp(): void { - $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(): void { - $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 (): void { }); + $stack = $this->collector->getActiveStack(); + $this->assertEquals('default', $stack->getClient()); + $this->assertEquals('FormattedRequest', $stack->getRequest()); } public function testOnFulfilled(): void { - //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); }; @@ -129,27 +97,12 @@ public function testOnFulfilled(): void }); $this->assertEquals($this->response, $promise->wait()); - $this->assertInstanceOf(Stack::class, $currentStack); + $currentStack = $this->collector->getActiveStack(); $this->assertEquals('FormattedResponse', $currentStack->getResponse()); } public function testOnRejected(): void { - //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); }; @@ -157,17 +110,14 @@ public function testOnRejected(): void $promise = $this->subject->handleRequest($this->request, $next, function (): void { }); - $this->expectException(\Exception::class); - $promise->wait(); + $this->assertEquals($this->exception, $promise->wait()); + $currentStack = $this->collector->getActiveStack(); + $this->assertEquals('FormattedResponse', $currentStack->getResponse()); + $this->assertTrue($currentStack->isFailed()); } public function testOnException(): void { - $this->collector - ->expects($this->once()) - ->method('deactivateStack') - ; - $next = function (): void { throw new \Exception(); }; @@ -185,11 +135,6 @@ public function testOnError(): void $this->expectException(Warning::class); } - $this->collector - ->expects($this->once()) - ->method('deactivateStack') - ; - $next = function () { return 2 / 0; };