Skip to content

Commit

Permalink
Replace deprecated at() Mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonFrings committed Sep 3, 2020
1 parent 4b182a9 commit 7658f54
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 119 deletions.
119 changes: 37 additions & 82 deletions tests/Client/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,46 +36,21 @@ public function requestShouldBindToStreamEventsAndUseconnector()

$this->successfulConnectionMock();

$this->stream
->expects($this->at(0))
->method('on')
->with('drain', $this->identicalTo(array($request, 'handleDrain')));
$this->stream
->expects($this->at(1))
->method('on')
->with('data', $this->identicalTo(array($request, 'handleData')));
$this->stream
->expects($this->at(2))
->method('on')
->with('end', $this->identicalTo(array($request, 'handleEnd')));
$this->stream
->expects($this->at(3))
->method('on')
->with('error', $this->identicalTo(array($request, 'handleError')));
$this->stream
->expects($this->at(4))
->method('on')
->with('close', $this->identicalTo(array($request, 'handleClose')));
$this->stream
->expects($this->at(6))
->method('removeListener')
->with('drain', $this->identicalTo(array($request, 'handleDrain')));
$this->stream
->expects($this->at(7))
->method('removeListener')
->with('data', $this->identicalTo(array($request, 'handleData')));
$this->stream
->expects($this->at(8))
->method('removeListener')
->with('end', $this->identicalTo(array($request, 'handleEnd')));
$this->stream
->expects($this->at(9))
->method('removeListener')
->with('error', $this->identicalTo(array($request, 'handleError')));
$this->stream
->expects($this->at(10))
->method('removeListener')
->with('close', $this->identicalTo(array($request, 'handleClose')));
$this->stream->expects($this->exactly(6))->method('on')->withConsecutive(
array('drain', $this->identicalTo(array($request, 'handleDrain'))),
array('data', $this->identicalTo(array($request, 'handleData'))),
array('end', $this->identicalTo(array($request, 'handleEnd'))),
array('error', $this->identicalTo(array($request, 'handleError'))),
array('close', $this->identicalTo(array($request, 'handleClose')))
);

$this->stream->expects($this->exactly(5))->method('removeListener')->withConsecutive(
array('drain', $this->identicalTo(array($request, 'handleDrain'))),
array('data', $this->identicalTo(array($request, 'handleData'))),
array('end', $this->identicalTo(array($request, 'handleEnd'))),
array('error', $this->identicalTo(array($request, 'handleError'))),
array('close', $this->identicalTo(array($request, 'handleClose')))
);

$request->on('end', $this->expectCallableNever());

Expand Down Expand Up @@ -223,18 +198,11 @@ public function writeWithAPostRequestShouldSendToTheStream()

$this->successfulConnectionMock();

$this->stream
->expects($this->at(5))
->method('write')
->with($this->matchesRegularExpression("#^POST / HTTP/1\.0\r\nHost: www.example.com\r\nUser-Agent:.*\r\n\r\nsome$#"));
$this->stream
->expects($this->at(6))
->method('write')
->with($this->identicalTo("post"));
$this->stream
->expects($this->at(7))
->method('write')
->with($this->identicalTo("data"));
$this->stream->expects($this->exactly(3))->method('write')->withConsecutive(
array($this->matchesRegularExpression("#^POST / HTTP/1\.0\r\nHost: www.example.com\r\nUser-Agent:.*\r\n\r\nsome$#")),
array($this->identicalTo("post")),
array($this->identicalTo("data"))
);

$request->write("some");
$request->write("post");
Expand All @@ -253,15 +221,12 @@ public function writeWithAPostRequestShouldSendBodyAfterHeadersAndEmitDrainEvent

$resolveConnection = $this->successfulAsyncConnectionMock();

$this->stream
->expects($this->at(5))
->method('write')
->with($this->matchesRegularExpression("#^POST / HTTP/1\.0\r\nHost: www.example.com\r\nUser-Agent:.*\r\n\r\nsomepost$#"))
->willReturn(true);
$this->stream
->expects($this->at(6))
->method('write')
->with($this->identicalTo("data"));
$this->stream->expects($this->exactly(2))->method('write')->withConsecutive(
array($this->matchesRegularExpression("#^POST / HTTP/1\.0\r\nHost: www.example.com\r\nUser-Agent:.*\r\n\r\nsomepost$#")),
array($this->identicalTo("data"))
)->willReturn(
true
);

$this->assertFalse($request->write("some"));
$this->assertFalse($request->write("post"));
Expand Down Expand Up @@ -292,15 +257,12 @@ public function writeWithAPostRequestShouldForwardDrainEventIfFirstChunkExceedsB

$resolveConnection = $this->successfulAsyncConnectionMock();

$this->stream
->expects($this->at(0))
->method('write')
->with($this->matchesRegularExpression("#^POST / HTTP/1\.0\r\nHost: www.example.com\r\nUser-Agent:.*\r\n\r\nsomepost$#"))
->willReturn(false);
$this->stream
->expects($this->at(1))
->method('write')
->with($this->identicalTo("data"));
$this->stream->expects($this->exactly(2))->method('write')->withConsecutive(
array($this->matchesRegularExpression("#^POST / HTTP/1\.0\r\nHost: www.example.com\r\nUser-Agent:.*\r\n\r\nsomepost$#")),
array($this->identicalTo("data"))
)->willReturn(
false
);

$this->assertFalse($request->write("some"));
$this->assertFalse($request->write("post"));
Expand All @@ -327,18 +289,11 @@ public function pipeShouldPipeDataIntoTheRequestBody()

$this->successfulConnectionMock();

$this->stream
->expects($this->at(5))
->method('write')
->with($this->matchesRegularExpression("#^POST / HTTP/1\.0\r\nHost: www.example.com\r\nUser-Agent:.*\r\n\r\nsome$#"));
$this->stream
->expects($this->at(6))
->method('write')
->with($this->identicalTo("post"));
$this->stream
->expects($this->at(7))
->method('write')
->with($this->identicalTo("data"));
$this->stream->expects($this->exactly(3))->method('write')->withConsecutive(
array($this->matchesRegularExpression("#^POST / HTTP/1\.0\r\nHost: www.example.com\r\nUser-Agent:.*\r\n\r\nsome$#")),
array($this->identicalTo("post")),
array($this->identicalTo("data"))
);

$loop = $this
->getMockBuilder('React\EventLoop\LoopInterface')
Expand Down
94 changes: 62 additions & 32 deletions tests/Io/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,18 +523,21 @@ public function testFollowingRedirectWithSpecifiedHeaders()
// mock sender to resolve promise with the given $redirectResponse in
// response to the given $requestWithUserAgent
$redirectResponse = new Response(301, array('Location' => 'http://redirect.com'));
$sender->expects($this->at(0))->method('send')->willReturn(Promise\resolve($redirectResponse));

// mock sender to resolve promise with the given $okResponse in
// response to the given $requestWithUserAgent
$okResponse = new Response(200);
$that = $this;
$sender->expects($this->at(1))
->method('send')
->with($this->callback(function (RequestInterface $request) use ($that) {
$sender->expects($this->exactly(2))->method('send')->withConsecutive(
array($this->anything()),
array($this->callback(function (RequestInterface $request) use ($that) {
$that->assertEquals(array('Chrome'), $request->getHeader('User-Agent'));
return true;
}))->willReturn(Promise\resolve($okResponse));
}))
)->willReturnOnConsecutiveCalls(
Promise\resolve($redirectResponse),
Promise\resolve($okResponse)
);

$transaction = new Transaction($sender, $loop);
$transaction->send($requestWithUserAgent);
Expand All @@ -551,18 +554,21 @@ public function testRemovingAuthorizationHeaderWhenChangingHostnamesDuringRedire
// mock sender to resolve promise with the given $redirectResponse in
// response to the given $requestWithAuthorization
$redirectResponse = new Response(301, array('Location' => 'http://redirect.com'));
$sender->expects($this->at(0))->method('send')->willReturn(Promise\resolve($redirectResponse));

// mock sender to resolve promise with the given $okResponse in
// response to the given $requestWithAuthorization
$okResponse = new Response(200);
$that = $this;
$sender->expects($this->at(1))
->method('send')
->with($this->callback(function (RequestInterface $request) use ($that) {
$sender->expects($this->exactly(2))->method('send')->withConsecutive(
array($this->anything()),
array($this->callback(function (RequestInterface $request) use ($that) {
$that->assertFalse($request->hasHeader('Authorization'));
return true;
}))->willReturn(Promise\resolve($okResponse));
}))
)->willReturnOnConsecutiveCalls(
Promise\resolve($redirectResponse),
Promise\resolve($okResponse)
);

$transaction = new Transaction($sender, $loop);
$transaction->send($requestWithAuthorization);
Expand All @@ -579,18 +585,21 @@ public function testAuthorizationHeaderIsForwardedWhenRedirectingToSameDomain()
// mock sender to resolve promise with the given $redirectResponse in
// response to the given $requestWithAuthorization
$redirectResponse = new Response(301, array('Location' => 'http://example.com/new'));
$sender->expects($this->at(0))->method('send')->willReturn(Promise\resolve($redirectResponse));

// mock sender to resolve promise with the given $okResponse in
// response to the given $requestWithAuthorization
$okResponse = new Response(200);
$that = $this;
$sender->expects($this->at(1))
->method('send')
->with($this->callback(function (RequestInterface $request) use ($that) {
$sender->expects($this->exactly(2))->method('send')->withConsecutive(
array($this->anything()),
array($this->callback(function (RequestInterface $request) use ($that) {
$that->assertEquals(array('secret'), $request->getHeader('Authorization'));
return true;
}))->willReturn(Promise\resolve($okResponse));
}))
)->willReturnOnConsecutiveCalls(
Promise\resolve($redirectResponse),
Promise\resolve($okResponse)
);

$transaction = new Transaction($sender, $loop);
$transaction->send($requestWithAuthorization);
Expand All @@ -606,19 +615,22 @@ public function testAuthorizationHeaderIsForwardedWhenLocationContainsAuthentica
// mock sender to resolve promise with the given $redirectResponse in
// response to the given $requestWithAuthorization
$redirectResponse = new Response(301, array('Location' => 'http://user:[email protected]/new'));
$sender->expects($this->at(0))->method('send')->willReturn(Promise\resolve($redirectResponse));

// mock sender to resolve promise with the given $okResponse in
// response to the given $requestWithAuthorization
$okResponse = new Response(200);
$that = $this;
$sender->expects($this->at(1))
->method('send')
->with($this->callback(function (RequestInterface $request) use ($that) {
$sender->expects($this->exactly(2))->method('send')->withConsecutive(
array($this->anything()),
array($this->callback(function (RequestInterface $request) use ($that) {
$that->assertEquals('user:pass', $request->getUri()->getUserInfo());
$that->assertFalse($request->hasHeader('Authorization'));
return true;
}))->willReturn(Promise\resolve($okResponse));
}))
)->willReturnOnConsecutiveCalls(
Promise\resolve($redirectResponse),
Promise\resolve($okResponse)
);

$transaction = new Transaction($sender, $loop);
$transaction->send($request);
Expand All @@ -639,19 +651,22 @@ public function testSomeRequestHeadersShouldBeRemovedWhenRedirecting()
// mock sender to resolve promise with the given $redirectResponse in
// response to the given $requestWithCustomHeaders
$redirectResponse = new Response(301, array('Location' => 'http://example.com/new'));
$sender->expects($this->at(0))->method('send')->willReturn(Promise\resolve($redirectResponse));

// mock sender to resolve promise with the given $okResponse in
// response to the given $requestWithCustomHeaders
$okResponse = new Response(200);
$that = $this;
$sender->expects($this->at(1))
->method('send')
->with($this->callback(function (RequestInterface $request) use ($that) {
$sender->expects($this->exactly(2))->method('send')->withConsecutive(
array($this->anything()),
array($this->callback(function (RequestInterface $request) use ($that) {
$that->assertFalse($request->hasHeader('Content-Type'));
$that->assertFalse($request->hasHeader('Content-Length'));
return true;
}))->willReturn(Promise\resolve($okResponse));
return true;;
}))
)->willReturnOnConsecutiveCalls(
Promise\resolve($redirectResponse),
Promise\resolve($okResponse)
);

$transaction = new Transaction($sender, $loop);
$transaction->send($requestWithCustomHeaders);
Expand Down Expand Up @@ -706,12 +721,17 @@ public function testCancelTransactionWillCancelRedirectedRequest()

// mock sender to resolve promise with the given $redirectResponse in
$redirectResponse = new Response(301, array('Location' => 'http://example.com/new'));
$sender->expects($this->at(0))->method('send')->willReturn(Promise\resolve($redirectResponse));

$pending = new \React\Promise\Promise(function () { }, $this->expectCallableOnce());

// mock sender to return pending promise which should be cancelled when cancelling result
$sender->expects($this->at(1))->method('send')->willReturn($pending);
$sender->expects($this->exactly(2))->method('send')->withConsecutive(
array($this->anything()),
array($this->anything())
)->willReturnOnConsecutiveCalls(
Promise\resolve($redirectResponse),
$pending
);

$transaction = new Transaction($sender, $loop);
$promise = $transaction->send($request);
Expand All @@ -728,12 +748,17 @@ public function testCancelTransactionWillCancelRedirectedRequestAgain()

// mock sender to resolve promise with the given $redirectResponse in
$first = new Deferred();
$sender->expects($this->at(0))->method('send')->willReturn($first->promise());

$second = new \React\Promise\Promise(function () { }, $this->expectCallableOnce());

// mock sender to return pending promise which should be cancelled when cancelling result
$sender->expects($this->at(1))->method('send')->willReturn($second);
$sender->expects($this->exactly(2))->method('send')->withConsecutive(
array($this->anything()),
array($this->anything())
)->willReturnOnConsecutiveCalls(
$first->promise(),
$second
);

$transaction = new Transaction($sender, $loop);
$promise = $transaction->send($request);
Expand Down Expand Up @@ -794,12 +819,17 @@ public function testCancelTransactionShouldCancelSendingPromise()

// mock sender to resolve promise with the given $redirectResponse in
$redirectResponse = new Response(301, array('Location' => 'http://example.com/new'));
$sender->expects($this->at(0))->method('send')->willReturn(Promise\resolve($redirectResponse));

$pending = new \React\Promise\Promise(function () { }, $this->expectCallableOnce());

// mock sender to return pending promise which should be cancelled when cancelling result
$sender->expects($this->at(1))->method('send')->willReturn($pending);
$sender->expects($this->exactly(2))->method('send')->withConsecutive(
array($this->anything()),
array($this->anything())
)->willReturnOnConsecutiveCalls(
Promise\resolve($redirectResponse),
$pending
);

$transaction = new Transaction($sender, $loop);
$promise = $transaction->send($request);
Expand Down
26 changes: 21 additions & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,29 @@ protected function expectCallableConsecutive($numberOfCalls, array $with)
{
$mock = $this->createCallableMock();

for ($i = 0; $i < $numberOfCalls; $i++) {
$mock
->expects($this->at($i))
->method('__invoke')
->with($this->equalTo($with[$i]));
if($numberOfCalls == 2){
$mock->expects($this->exactly($numberOfCalls))->method('__invoke')->withConsecutive(
array($this->equalTo($with[0])),
array($this->equalTo($with[1]))
);
}

if($numberOfCalls == 3){
$mock->expects($this->exactly($numberOfCalls))->method('__invoke')->withConsecutive(
array($this->equalTo($with[0])),
array($this->equalTo($with[1])),
array($this->equalTo($with[2]))
);
}

if($numberOfCalls == 4){
$mock->expects($this->exactly($numberOfCalls))->method('__invoke')->withConsecutive(
array($this->equalTo($with[0])),
array($this->equalTo($with[1])),
array($this->equalTo($with[2])),
array($this->equalTo($with[3]))
);
}
return $mock;
}

Expand Down

0 comments on commit 7658f54

Please sign in to comment.