Skip to content

Commit e4a7dcc

Browse files
committed
Replace deprecated at() Mocks
1 parent 4b182a9 commit e4a7dcc

File tree

3 files changed

+123
-119
lines changed

3 files changed

+123
-119
lines changed

tests/Client/RequestTest.php

Lines changed: 37 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -36,46 +36,21 @@ public function requestShouldBindToStreamEventsAndUseconnector()
3636

3737
$this->successfulConnectionMock();
3838

39-
$this->stream
40-
->expects($this->at(0))
41-
->method('on')
42-
->with('drain', $this->identicalTo(array($request, 'handleDrain')));
43-
$this->stream
44-
->expects($this->at(1))
45-
->method('on')
46-
->with('data', $this->identicalTo(array($request, 'handleData')));
47-
$this->stream
48-
->expects($this->at(2))
49-
->method('on')
50-
->with('end', $this->identicalTo(array($request, 'handleEnd')));
51-
$this->stream
52-
->expects($this->at(3))
53-
->method('on')
54-
->with('error', $this->identicalTo(array($request, 'handleError')));
55-
$this->stream
56-
->expects($this->at(4))
57-
->method('on')
58-
->with('close', $this->identicalTo(array($request, 'handleClose')));
59-
$this->stream
60-
->expects($this->at(6))
61-
->method('removeListener')
62-
->with('drain', $this->identicalTo(array($request, 'handleDrain')));
63-
$this->stream
64-
->expects($this->at(7))
65-
->method('removeListener')
66-
->with('data', $this->identicalTo(array($request, 'handleData')));
67-
$this->stream
68-
->expects($this->at(8))
69-
->method('removeListener')
70-
->with('end', $this->identicalTo(array($request, 'handleEnd')));
71-
$this->stream
72-
->expects($this->at(9))
73-
->method('removeListener')
74-
->with('error', $this->identicalTo(array($request, 'handleError')));
75-
$this->stream
76-
->expects($this->at(10))
77-
->method('removeListener')
78-
->with('close', $this->identicalTo(array($request, 'handleClose')));
39+
$this->stream->expects($this->exactly(6))->method('on')->withConsecutive(
40+
array('drain', $this->identicalTo(array($request, 'handleDrain'))),
41+
array('data', $this->identicalTo(array($request, 'handleData'))),
42+
array('end', $this->identicalTo(array($request, 'handleEnd'))),
43+
array('error', $this->identicalTo(array($request, 'handleError'))),
44+
array('close', $this->identicalTo(array($request, 'handleClose')))
45+
);
46+
47+
$this->stream->expects($this->exactly(5))->method('removeListener')->withConsecutive(
48+
array('drain', $this->identicalTo(array($request, 'handleDrain'))),
49+
array('data', $this->identicalTo(array($request, 'handleData'))),
50+
array('end', $this->identicalTo(array($request, 'handleEnd'))),
51+
array('error', $this->identicalTo(array($request, 'handleError'))),
52+
array('close', $this->identicalTo(array($request, 'handleClose')))
53+
);
7954

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

@@ -223,18 +198,11 @@ public function writeWithAPostRequestShouldSendToTheStream()
223198

224199
$this->successfulConnectionMock();
225200

226-
$this->stream
227-
->expects($this->at(5))
228-
->method('write')
229-
->with($this->matchesRegularExpression("#^POST / HTTP/1\.0\r\nHost: www.example.com\r\nUser-Agent:.*\r\n\r\nsome$#"));
230-
$this->stream
231-
->expects($this->at(6))
232-
->method('write')
233-
->with($this->identicalTo("post"));
234-
$this->stream
235-
->expects($this->at(7))
236-
->method('write')
237-
->with($this->identicalTo("data"));
201+
$this->stream->expects($this->exactly(3))->method('write')->withConsecutive(
202+
array($this->matchesRegularExpression("#^POST / HTTP/1\.0\r\nHost: www.example.com\r\nUser-Agent:.*\r\n\r\nsome$#")),
203+
array($this->identicalTo("post")),
204+
array($this->identicalTo("data"))
205+
);
238206

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

254222
$resolveConnection = $this->successfulAsyncConnectionMock();
255223

256-
$this->stream
257-
->expects($this->at(5))
258-
->method('write')
259-
->with($this->matchesRegularExpression("#^POST / HTTP/1\.0\r\nHost: www.example.com\r\nUser-Agent:.*\r\n\r\nsomepost$#"))
260-
->willReturn(true);
261-
$this->stream
262-
->expects($this->at(6))
263-
->method('write')
264-
->with($this->identicalTo("data"));
224+
$this->stream->expects($this->exactly(2))->method('write')->withConsecutive(
225+
array($this->matchesRegularExpression("#^POST / HTTP/1\.0\r\nHost: www.example.com\r\nUser-Agent:.*\r\n\r\nsomepost$#")),
226+
array($this->identicalTo("data"))
227+
)->willReturn(
228+
true
229+
);
265230

266231
$this->assertFalse($request->write("some"));
267232
$this->assertFalse($request->write("post"));
@@ -292,15 +257,12 @@ public function writeWithAPostRequestShouldForwardDrainEventIfFirstChunkExceedsB
292257

293258
$resolveConnection = $this->successfulAsyncConnectionMock();
294259

295-
$this->stream
296-
->expects($this->at(0))
297-
->method('write')
298-
->with($this->matchesRegularExpression("#^POST / HTTP/1\.0\r\nHost: www.example.com\r\nUser-Agent:.*\r\n\r\nsomepost$#"))
299-
->willReturn(false);
300-
$this->stream
301-
->expects($this->at(1))
302-
->method('write')
303-
->with($this->identicalTo("data"));
260+
$this->stream->expects($this->exactly(2))->method('write')->withConsecutive(
261+
array($this->matchesRegularExpression("#^POST / HTTP/1\.0\r\nHost: www.example.com\r\nUser-Agent:.*\r\n\r\nsomepost$#")),
262+
array($this->identicalTo("data"))
263+
)->willReturn(
264+
false
265+
);
304266

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

328290
$this->successfulConnectionMock();
329291

330-
$this->stream
331-
->expects($this->at(5))
332-
->method('write')
333-
->with($this->matchesRegularExpression("#^POST / HTTP/1\.0\r\nHost: www.example.com\r\nUser-Agent:.*\r\n\r\nsome$#"));
334-
$this->stream
335-
->expects($this->at(6))
336-
->method('write')
337-
->with($this->identicalTo("post"));
338-
$this->stream
339-
->expects($this->at(7))
340-
->method('write')
341-
->with($this->identicalTo("data"));
292+
$this->stream->expects($this->exactly(3))->method('write')->withConsecutive(
293+
array($this->matchesRegularExpression("#^POST / HTTP/1\.0\r\nHost: www.example.com\r\nUser-Agent:.*\r\n\r\nsome$#")),
294+
array($this->identicalTo("post")),
295+
array($this->identicalTo("data"))
296+
);
342297

343298
$loop = $this
344299
->getMockBuilder('React\EventLoop\LoopInterface')

tests/Io/TransactionTest.php

Lines changed: 65 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,9 @@ public function testCustomRedirectResponseCode333WillFollowLocationHeaderAndSend
512512
$transaction->send($requestOriginal);
513513
}
514514

515+
/**
516+
* @group a
517+
*/
515518
public function testFollowingRedirectWithSpecifiedHeaders()
516519
{
517520
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
@@ -523,18 +526,21 @@ public function testFollowingRedirectWithSpecifiedHeaders()
523526
// mock sender to resolve promise with the given $redirectResponse in
524527
// response to the given $requestWithUserAgent
525528
$redirectResponse = new Response(301, array('Location' => 'http://redirect.com'));
526-
$sender->expects($this->at(0))->method('send')->willReturn(Promise\resolve($redirectResponse));
527529

528530
// mock sender to resolve promise with the given $okResponse in
529531
// response to the given $requestWithUserAgent
530532
$okResponse = new Response(200);
531533
$that = $this;
532-
$sender->expects($this->at(1))
533-
->method('send')
534-
->with($this->callback(function (RequestInterface $request) use ($that) {
534+
$sender->expects($this->exactly(2))->method('send')->withConsecutive(
535+
array($this->anything()),
536+
array($this->callback(function (RequestInterface $request) use ($that) {
535537
$that->assertEquals(array('Chrome'), $request->getHeader('User-Agent'));
536538
return true;
537-
}))->willReturn(Promise\resolve($okResponse));
539+
}))
540+
)->willReturnOnConsecutiveCalls(
541+
Promise\resolve($redirectResponse),
542+
Promise\resolve($okResponse)
543+
);
538544

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

556561
// mock sender to resolve promise with the given $okResponse in
557562
// response to the given $requestWithAuthorization
558563
$okResponse = new Response(200);
559564
$that = $this;
560-
$sender->expects($this->at(1))
561-
->method('send')
562-
->with($this->callback(function (RequestInterface $request) use ($that) {
565+
$sender->expects($this->exactly(2))->method('send')->withConsecutive(
566+
array($this->anything()),
567+
array($this->callback(function (RequestInterface $request) use ($that) {
563568
$that->assertFalse($request->hasHeader('Authorization'));
564569
return true;
565-
}))->willReturn(Promise\resolve($okResponse));
570+
}))
571+
)->willReturnOnConsecutiveCalls(
572+
Promise\resolve($redirectResponse),
573+
Promise\resolve($okResponse)
574+
);
566575

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

584592
// mock sender to resolve promise with the given $okResponse in
585593
// response to the given $requestWithAuthorization
586594
$okResponse = new Response(200);
587595
$that = $this;
588-
$sender->expects($this->at(1))
589-
->method('send')
590-
->with($this->callback(function (RequestInterface $request) use ($that) {
596+
$sender->expects($this->exactly(2))->method('send')->withConsecutive(
597+
array($this->anything()),
598+
array($this->callback(function (RequestInterface $request) use ($that) {
591599
$that->assertEquals(array('secret'), $request->getHeader('Authorization'));
592600
return true;
593-
}))->willReturn(Promise\resolve($okResponse));
601+
}))
602+
)->willReturnOnConsecutiveCalls(
603+
Promise\resolve($redirectResponse),
604+
Promise\resolve($okResponse)
605+
);
594606

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

611622
// mock sender to resolve promise with the given $okResponse in
612623
// response to the given $requestWithAuthorization
613624
$okResponse = new Response(200);
614625
$that = $this;
615-
$sender->expects($this->at(1))
616-
->method('send')
617-
->with($this->callback(function (RequestInterface $request) use ($that) {
626+
$sender->expects($this->exactly(2))->method('send')->withConsecutive(
627+
array($this->anything()),
628+
array($this->callback(function (RequestInterface $request) use ($that) {
618629
$that->assertEquals('user:pass', $request->getUri()->getUserInfo());
619630
$that->assertFalse($request->hasHeader('Authorization'));
620631
return true;
621-
}))->willReturn(Promise\resolve($okResponse));
632+
}))
633+
)->willReturnOnConsecutiveCalls(
634+
Promise\resolve($redirectResponse),
635+
Promise\resolve($okResponse)
636+
);
622637

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

644658
// mock sender to resolve promise with the given $okResponse in
645659
// response to the given $requestWithCustomHeaders
646660
$okResponse = new Response(200);
647661
$that = $this;
648-
$sender->expects($this->at(1))
649-
->method('send')
650-
->with($this->callback(function (RequestInterface $request) use ($that) {
662+
$sender->expects($this->exactly(2))->method('send')->withConsecutive(
663+
array($this->anything()),
664+
array($this->callback(function (RequestInterface $request) use ($that) {
651665
$that->assertFalse($request->hasHeader('Content-Type'));
652666
$that->assertFalse($request->hasHeader('Content-Length'));
653-
return true;
654-
}))->willReturn(Promise\resolve($okResponse));
667+
return true;;
668+
}))
669+
)->willReturnOnConsecutiveCalls(
670+
Promise\resolve($redirectResponse),
671+
Promise\resolve($okResponse)
672+
);
655673

656674
$transaction = new Transaction($sender, $loop);
657675
$transaction->send($requestWithCustomHeaders);
@@ -706,12 +724,17 @@ public function testCancelTransactionWillCancelRedirectedRequest()
706724

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

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

713730
// mock sender to return pending promise which should be cancelled when cancelling result
714-
$sender->expects($this->at(1))->method('send')->willReturn($pending);
731+
$sender->expects($this->exactly(2))->method('send')->withConsecutive(
732+
array($this->anything()),
733+
array($this->anything())
734+
)->willReturnOnConsecutiveCalls(
735+
Promise\resolve($redirectResponse),
736+
$pending
737+
);
715738

716739
$transaction = new Transaction($sender, $loop);
717740
$promise = $transaction->send($request);
@@ -728,12 +751,17 @@ public function testCancelTransactionWillCancelRedirectedRequestAgain()
728751

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

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

735757
// mock sender to return pending promise which should be cancelled when cancelling result
736-
$sender->expects($this->at(1))->method('send')->willReturn($second);
758+
$sender->expects($this->exactly(2))->method('send')->withConsecutive(
759+
array($this->anything()),
760+
array($this->anything())
761+
)->willReturnOnConsecutiveCalls(
762+
$first->promise(),
763+
$second
764+
);
737765

738766
$transaction = new Transaction($sender, $loop);
739767
$promise = $transaction->send($request);
@@ -794,12 +822,17 @@ public function testCancelTransactionShouldCancelSendingPromise()
794822

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

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

801828
// mock sender to return pending promise which should be cancelled when cancelling result
802-
$sender->expects($this->at(1))->method('send')->willReturn($pending);
829+
$sender->expects($this->exactly(2))->method('send')->withConsecutive(
830+
array($this->anything()),
831+
array($this->anything())
832+
)->willReturnOnConsecutiveCalls(
833+
Promise\resolve($redirectResponse),
834+
$pending
835+
);
803836

804837
$transaction = new Transaction($sender, $loop);
805838
$promise = $transaction->send($request);

tests/TestCase.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,29 @@ protected function expectCallableConsecutive($numberOfCalls, array $with)
4141
{
4242
$mock = $this->createCallableMock();
4343

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

51+
if($numberOfCalls == 3){
52+
$mock->expects($this->exactly($numberOfCalls))->method('__invoke')->withConsecutive(
53+
array($this->equalTo($with[0])),
54+
array($this->equalTo($with[1])),
55+
array($this->equalTo($with[2]))
56+
);
57+
}
58+
59+
if($numberOfCalls == 4){
60+
$mock->expects($this->exactly($numberOfCalls))->method('__invoke')->withConsecutive(
61+
array($this->equalTo($with[0])),
62+
array($this->equalTo($with[1])),
63+
array($this->equalTo($with[2])),
64+
array($this->equalTo($with[3]))
65+
);
66+
}
5167
return $mock;
5268
}
5369

0 commit comments

Comments
 (0)