From 20eb12d0ee95c2e66dcbdb872778e622786ff8aa Mon Sep 17 00:00:00 2001 From: Fabien Bourigault Date: Mon, 26 Jun 2017 12:18:48 +0200 Subject: [PATCH] stop mocking immutable/value objects Psr\Http\Message\MessageInterface and Psr\Http\Message\ResponseInterface mocks are replaced with GuzzleHttp\Psr7\Request and GuzzleHttp\Psr7\Response respectively. Http\Promise\Promise mocks are replaced either with Http\Client\Promise\HttpFulfilledPromise or Http\Client\Promise\HttpRejectedPromise depending on the test requirements. --- Tests/Unit/Collector/FormatterTest.php | 19 ++++---- Tests/Unit/Collector/ProfileClientTest.php | 24 ++++------ Tests/Unit/Collector/ProfilePluginTest.php | 53 +++++----------------- Tests/Unit/Collector/StackPluginTest.php | 45 +++++------------- 4 files changed, 39 insertions(+), 102 deletions(-) diff --git a/Tests/Unit/Collector/FormatterTest.php b/Tests/Unit/Collector/FormatterTest.php index ecc31582..270ea3f5 100644 --- a/Tests/Unit/Collector/FormatterTest.php +++ b/Tests/Unit/Collector/FormatterTest.php @@ -2,13 +2,13 @@ namespace Http\HttplugBundle\Tests\Unit\Collector; +use GuzzleHttp\Psr7\Request; +use GuzzleHttp\Psr7\Response; use Http\Client\Exception\HttpException; use Http\Client\Exception\TransferException; use Http\HttplugBundle\Collector\Formatter; use Http\Message\Formatter as MessageFormatter; use Http\Message\Formatter\CurlCommandFormatter; -use Psr\Http\Message\RequestInterface; -use Psr\Http\Message\ResponseInterface; class FormatterTest extends \PHPUnit_Framework_TestCase { @@ -37,7 +37,7 @@ public function setUp() public function testFormatRequest() { - $request = $this->getMockBuilder(RequestInterface::class)->getMock(); + $request = new Request('GET', '/'); $this->formatter ->expects($this->once()) @@ -50,7 +50,7 @@ public function testFormatRequest() public function testFormatResponse() { - $response = $this->getMockBuilder(ResponseInterface::class)->getMock(); + $response = new Response(); $this->formatter ->expects($this->once()) @@ -63,12 +63,9 @@ public function testFormatResponse() public function testFormatHttpException() { - $response = $this->getMockBuilder(ResponseInterface::class)->getMock(); - $exception = $this->getMockBuilder(HttpException::class)->disableOriginalConstructor()->getMock(); - $exception - ->method('getResponse') - ->willReturn($response) - ; + $request = new Request('GET', '/'); + $response = new Response(); + $exception = new HttpException('', $request, $response); $this->formatter ->expects($this->once()) @@ -95,7 +92,7 @@ public function testFormatException() public function testFormatAsCurlCommand() { - $request = $this->getMockBuilder(RequestInterface::class)->getMock(); + $request = new Request('GET', '/'); $this->curlFormatter ->expects($this->once()) diff --git a/Tests/Unit/Collector/ProfileClientTest.php b/Tests/Unit/Collector/ProfileClientTest.php index e1a1a620..095cf912 100644 --- a/Tests/Unit/Collector/ProfileClientTest.php +++ b/Tests/Unit/Collector/ProfileClientTest.php @@ -2,12 +2,16 @@ namespace Http\HttplugBundle\Tests\Unit\Collector; +use GuzzleHttp\Psr7\Request; +use GuzzleHttp\Psr7\Response; +use GuzzleHttp\Psr7\Uri; use Http\Client\HttpAsyncClient; use Http\Client\HttpClient; use Http\HttplugBundle\Collector\Collector; use Http\HttplugBundle\Collector\Formatter; use Http\HttplugBundle\Collector\ProfileClient; use Http\HttplugBundle\Collector\Stack; +use Http\Promise\FulfilledPromise; use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -71,29 +75,19 @@ public function setUp() $this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock(); $this->currentStack = new Stack('default', 'FormattedRequest'); $this->client = $this->getMockBuilder(ClientInterface::class)->getMock(); - $this->request = $this->getMockBuilder(RequestInterface::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 = new Stopwatch(); $this->subject = new ProfileClient($this->client, $this->collector, $this->formatter, $this->stopwatch); - $this->response = $this->getMockBuilder(ResponseInterface::class)->getMock(); - $this->promise = $this->getMockBuilder(Promise::class)->getMock(); - $this->uri = $this->getMockBuilder(UriInterface::class)->getMock(); + $this->response = new Response(); + $this->promise = new FulfilledPromise($this->response); $this->client->method('sendRequest')->willReturn($this->response); $this->client->method('sendAsyncRequest')->will($this->returnCallback(function () { - $promise = $this->getMockBuilder(Promise::class)->getMock(); - $promise->method('then')->willReturn($this->promise); - - return $promise; + return $this->promise; })); - $this->request->method('getMethod')->willReturn('GET'); - $this->request->method('getRequestTarget')->willReturn('/target'); - $this->request->method('getUri')->willReturn($this->uri); - - $this->uri->method('getScheme')->willReturn('https'); - $this->uri->method('getHost')->willReturn('example.com'); - $this->collector->method('getCurrentStack')->willReturn($this->currentStack); } diff --git a/Tests/Unit/Collector/ProfilePluginTest.php b/Tests/Unit/Collector/ProfilePluginTest.php index e14daf48..2e971a28 100644 --- a/Tests/Unit/Collector/ProfilePluginTest.php +++ b/Tests/Unit/Collector/ProfilePluginTest.php @@ -2,13 +2,15 @@ namespace Http\HttplugBundle\Tests\Unit\Collector; -use Exception; +use GuzzleHttp\Psr7\Request; +use GuzzleHttp\Psr7\Response; use Http\Client\Common\Plugin; +use Http\Client\Exception\TransferException; use Http\HttplugBundle\Collector\Collector; use Http\HttplugBundle\Collector\Formatter; use Http\HttplugBundle\Collector\ProfilePlugin; use Http\HttplugBundle\Collector\Stack; -use Http\Promise\Promise; +use Http\Promise\FulfilledPromise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -40,12 +42,7 @@ class ProfilePluginTest extends \PHPUnit_Framework_TestCase private $currentStack; /** - * @var Promise - */ - private $promise; - - /** - * @var Exception + * @var TransferException */ private $exception; @@ -63,11 +60,10 @@ public function setUp() { $this->plugin = $this->getMockBuilder(Plugin::class)->getMock(); $this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock(); - $this->request = $this->getMockBuilder(RequestInterface::class)->getMock(); - $this->response = $this->getMockBuilder(ResponseInterface::class)->getMock(); + $this->request = new Request('GET', '/'); + $this->response = new Response(); $this->currentStack = new Stack('default', 'FormattedRequest'); - $this->promise = $this->getMockBuilder(Promise::class)->getMock(); - $this->exception = $this->getMockBuilder(Exception::class)->disableOriginalConstructor()->getMock(); + $this->exception = new TransferException(); $this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock(); $this->collector @@ -80,7 +76,7 @@ public function setUp() ->willReturnCallback(function ($request, $next, $first) { $next($request); - return $this->promise; + return new FulfilledPromise($this->response); }) ; @@ -146,20 +142,6 @@ public function testCollectRequestInformations() public function testOnFulfilled() { - $this->promise - ->method('then') - ->will($this->returnCallback(function (callable $onFulfilled) { - $fulfilled = $this->getMockBuilder(Promise::class)->getMock(); - $fulfilled - ->method('wait') - ->with(true) - ->willReturn($onFulfilled($this->response)) - ; - - return $fulfilled; - })) - ; - $promise = $this->subject->handleRequest($this->request, function () { }, function () { }); @@ -171,23 +153,10 @@ public function testOnFulfilled() public function testOnRejected() { - $this->setExpectedException(Exception::class); - - $this->promise - ->method('then') - ->will($this->returnCallback(function (callable $onFulfilled, callable $onRejected) { - $rejected = $this->getMockBuilder(Promise::class)->getMock(); - $rejected - ->method('wait') - ->with(true) - ->willReturn($onRejected($this->exception)) - ; - - return $rejected; - })) - ; + $this->setExpectedException(TransferException::class); $promise = $this->subject->handleRequest($this->request, function () { + throw new TransferException(); }, function () { }); diff --git a/Tests/Unit/Collector/StackPluginTest.php b/Tests/Unit/Collector/StackPluginTest.php index 448ff061..21f10640 100644 --- a/Tests/Unit/Collector/StackPluginTest.php +++ b/Tests/Unit/Collector/StackPluginTest.php @@ -3,11 +3,15 @@ namespace Http\HttplugBundle\Tests\Unit\Collector; use Exception; +use GuzzleHttp\Psr7\Request; +use GuzzleHttp\Psr7\Response; +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\Promise\Promise; +use Http\Promise\FulfilledPromise; +use Http\Promise\RejectedPromise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -47,9 +51,9 @@ public function setUp() { $this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock(); $this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock(); - $this->request = $this->getMockBuilder(RequestInterface::class)->getMock(); - $this->response = $this->getMockBuilder(ResponseInterface::class)->getMock(); - $this->exception = $this->getMockBuilder(Exception::class)->disableOriginalConstructor()->getMock(); + $this->request = new Request('GET', '/'); + $this->response = new Response(); + $this->exception = new HttpException('', $this->request, $this->response); $this->formatter ->method('formatRequest') @@ -86,7 +90,7 @@ public function testStackIsInitialized() ; $next = function () { - return $this->getMockBuilder(Promise::class)->getMock(); + return new FulfilledPromise($this->response); }; $this->subject->handleRequest($this->request, $next, function () { @@ -107,21 +111,7 @@ public function testOnFulfilled() ; $next = function () { - $promise = $this->getMockBuilder(Promise::class)->getMock(); - $promise->method('then') - ->will($this->returnCallback(function (callable $onFulfilled) { - $fulfilled = $this->getMockBuilder(Promise::class)->getMock(); - $fulfilled - ->method('wait') - ->with(true) - ->willReturn($onFulfilled($this->response)) - ; - - return $fulfilled; - })) - ; - - return $promise; + return new FulfilledPromise($this->response); }; $promise = $this->subject->handleRequest($this->request, $next, function () { @@ -148,20 +138,7 @@ public function testOnRejected() $this->setExpectedException(Exception::class); $next = function () { - $promise = $this->getMockBuilder(Promise::class)->getMock(); - $promise - ->method('then') - ->will($this->returnCallback(function (callable $onFulfilled, callable $onRejected) { - $rejected = $this->getMockBuilder(Promise::class)->getMock(); - $rejected - ->method('wait') - ->with(true) - ->willReturn($onRejected($this->exception)); - - return $rejected; - })); - - return $promise; + return new RejectedPromise($this->exception); }; $promise = $this->subject->handleRequest($this->request, $next, function () {