Skip to content

Stop mocking immutable/value objects #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions Tests/Unit/Collector/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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())
Expand All @@ -50,7 +50,7 @@ public function testFormatRequest()

public function testFormatResponse()
{
$response = $this->getMockBuilder(ResponseInterface::class)->getMock();
$response = new Response();

$this->formatter
->expects($this->once())
Expand All @@ -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())
Expand All @@ -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())
Expand Down
24 changes: 9 additions & 15 deletions Tests/Unit/Collector/ProfileClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
53 changes: 11 additions & 42 deletions Tests/Unit/Collector/ProfilePluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -40,12 +42,7 @@ class ProfilePluginTest extends \PHPUnit_Framework_TestCase
private $currentStack;

/**
* @var Promise
*/
private $promise;

/**
* @var Exception
* @var TransferException
*/
private $exception;

Expand All @@ -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
Expand All @@ -80,7 +76,7 @@ public function setUp()
->willReturnCallback(function ($request, $next, $first) {
$next($request);

return $this->promise;
return new FulfilledPromise($this->response);
})
;

Expand Down Expand Up @@ -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 () {
});
Expand All @@ -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 () {
});

Expand Down
45 changes: 11 additions & 34 deletions Tests/Unit/Collector/StackPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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 () {
Expand All @@ -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 () {
Expand All @@ -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 () {
Expand Down