Skip to content

Commit 7658f54

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

File tree

3 files changed

+120
-119
lines changed

3 files changed

+120
-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: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -523,18 +523,21 @@ public function testFollowingRedirectWithSpecifiedHeaders()
523523
// mock sender to resolve promise with the given $redirectResponse in
524524
// response to the given $requestWithUserAgent
525525
$redirectResponse = new Response(301, array('Location' => 'http://redirect.com'));
526-
$sender->expects($this->at(0))->method('send')->willReturn(Promise\resolve($redirectResponse));
527526

528527
// mock sender to resolve promise with the given $okResponse in
529528
// response to the given $requestWithUserAgent
530529
$okResponse = new Response(200);
531530
$that = $this;
532-
$sender->expects($this->at(1))
533-
->method('send')
534-
->with($this->callback(function (RequestInterface $request) use ($that) {
531+
$sender->expects($this->exactly(2))->method('send')->withConsecutive(
532+
array($this->anything()),
533+
array($this->callback(function (RequestInterface $request) use ($that) {
535534
$that->assertEquals(array('Chrome'), $request->getHeader('User-Agent'));
536535
return true;
537-
}))->willReturn(Promise\resolve($okResponse));
536+
}))
537+
)->willReturnOnConsecutiveCalls(
538+
Promise\resolve($redirectResponse),
539+
Promise\resolve($okResponse)
540+
);
538541

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

556558
// mock sender to resolve promise with the given $okResponse in
557559
// response to the given $requestWithAuthorization
558560
$okResponse = new Response(200);
559561
$that = $this;
560-
$sender->expects($this->at(1))
561-
->method('send')
562-
->with($this->callback(function (RequestInterface $request) use ($that) {
562+
$sender->expects($this->exactly(2))->method('send')->withConsecutive(
563+
array($this->anything()),
564+
array($this->callback(function (RequestInterface $request) use ($that) {
563565
$that->assertFalse($request->hasHeader('Authorization'));
564566
return true;
565-
}))->willReturn(Promise\resolve($okResponse));
567+
}))
568+
)->willReturnOnConsecutiveCalls(
569+
Promise\resolve($redirectResponse),
570+
Promise\resolve($okResponse)
571+
);
566572

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

584589
// mock sender to resolve promise with the given $okResponse in
585590
// response to the given $requestWithAuthorization
586591
$okResponse = new Response(200);
587592
$that = $this;
588-
$sender->expects($this->at(1))
589-
->method('send')
590-
->with($this->callback(function (RequestInterface $request) use ($that) {
593+
$sender->expects($this->exactly(2))->method('send')->withConsecutive(
594+
array($this->anything()),
595+
array($this->callback(function (RequestInterface $request) use ($that) {
591596
$that->assertEquals(array('secret'), $request->getHeader('Authorization'));
592597
return true;
593-
}))->willReturn(Promise\resolve($okResponse));
598+
}))
599+
)->willReturnOnConsecutiveCalls(
600+
Promise\resolve($redirectResponse),
601+
Promise\resolve($okResponse)
602+
);
594603

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

611619
// mock sender to resolve promise with the given $okResponse in
612620
// response to the given $requestWithAuthorization
613621
$okResponse = new Response(200);
614622
$that = $this;
615-
$sender->expects($this->at(1))
616-
->method('send')
617-
->with($this->callback(function (RequestInterface $request) use ($that) {
623+
$sender->expects($this->exactly(2))->method('send')->withConsecutive(
624+
array($this->anything()),
625+
array($this->callback(function (RequestInterface $request) use ($that) {
618626
$that->assertEquals('user:pass', $request->getUri()->getUserInfo());
619627
$that->assertFalse($request->hasHeader('Authorization'));
620628
return true;
621-
}))->willReturn(Promise\resolve($okResponse));
629+
}))
630+
)->willReturnOnConsecutiveCalls(
631+
Promise\resolve($redirectResponse),
632+
Promise\resolve($okResponse)
633+
);
622634

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

644655
// mock sender to resolve promise with the given $okResponse in
645656
// response to the given $requestWithCustomHeaders
646657
$okResponse = new Response(200);
647658
$that = $this;
648-
$sender->expects($this->at(1))
649-
->method('send')
650-
->with($this->callback(function (RequestInterface $request) use ($that) {
659+
$sender->expects($this->exactly(2))->method('send')->withConsecutive(
660+
array($this->anything()),
661+
array($this->callback(function (RequestInterface $request) use ($that) {
651662
$that->assertFalse($request->hasHeader('Content-Type'));
652663
$that->assertFalse($request->hasHeader('Content-Length'));
653-
return true;
654-
}))->willReturn(Promise\resolve($okResponse));
664+
return true;;
665+
}))
666+
)->willReturnOnConsecutiveCalls(
667+
Promise\resolve($redirectResponse),
668+
Promise\resolve($okResponse)
669+
);
655670

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

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

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

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

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

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

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

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

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

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

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

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

804834
$transaction = new Transaction($sender, $loop);
805835
$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)