Skip to content

Commit b0b76d6

Browse files
committed
Added setExpectedException method for compatibility
1 parent 58db19a commit b0b76d6

2 files changed

Lines changed: 27 additions & 25 deletions

File tree

tests/Query/ExecutorTest.php

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,17 @@ public function queryShouldCreateUdpRequest()
4646
$this->executor->query('8.8.8.8:53', $query);
4747
}
4848

49-
/**
50-
* @test
51-
* @expectedException RuntimeException
52-
*/
49+
/** @test */
5350
public function resolveShouldRejectIfRequestIsLargerThan512Bytes()
5451
{
5552
$query = new Query(str_repeat('a', 512).'.igor.io', Message::TYPE_A, Message::CLASS_IN, 1345656451);
5653
$promise = $this->executor->query('8.8.8.8:53', $query);
5754

58-
$this->expectExceptionMessage('DNS query for ' . $query->name . ' failed: Requested transport "tcp" not available, only UDP is supported in this version');
55+
$this->setExpectedException('RuntimeException', 'DNS query for ' . $query->name . ' failed: Requested transport "tcp" not available, only UDP is supported in this version');
5956
Block\await($promise, $this->loop);
6057
}
6158

62-
/**
63-
* @test
64-
* @expectedException React\Dns\Query\CancellationException
65-
* @expectedExceptionMessage DNS query for igor.io has been cancelled
66-
*/
59+
/** @test */
6760
public function resolveShouldCloseConnectionWhenCancelled()
6861
{
6962
$conn = $this->createConnectionMock(false);
@@ -87,14 +80,11 @@ public function resolveShouldCloseConnectionWhenCancelled()
8780

8881
$promise->cancel();
8982

83+
$this->setExpectedException('React\Dns\Query\CancellationException', 'DNS query for igor.io has been cancelled');
9084
Block\await($promise, $this->loop);
9185
}
9286

93-
/**
94-
* @test
95-
* @expectedException React\Dns\Query\CancellationException
96-
* @expectedExceptionMessage DNS query for igor.io has been cancelled
97-
*/
87+
/** @test */
9888
public function resolveShouldNotStartOrCancelTimerWhenCancelledWithTimeoutIsNull()
9989
{
10090
$this->loop
@@ -108,6 +98,7 @@ public function resolveShouldNotStartOrCancelTimerWhenCancelledWithTimeoutIsNull
10898

10999
$promise->cancel();
110100

101+
$this->setExpectedException('React\Dns\Query\CancellationException', 'DNS query for igor.io has been cancelled');
111102
Block\await($promise, $this->loop);
112103
}
113104

@@ -137,11 +128,7 @@ public function resolveShouldRejectIfResponseIsTruncated()
137128
$this->executor->query('8.8.8.8:53', $query);
138129
}
139130

140-
/**
141-
* @test
142-
* @expectedException RuntimeException
143-
* @expectedExceptionMessage DNS query for igor.io failed: Nope
144-
*/
131+
/** @test */
145132
public function resolveShouldFailIfUdpThrow()
146133
{
147134
$this->loop
@@ -162,6 +149,7 @@ public function resolveShouldFailIfUdpThrow()
162149
$query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN, 1345656451);
163150
$promise = $this->executor->query('8.8.8.8:53', $query);
164151

152+
$this->setExpectedException('RuntimeException', 'DNS query for igor.io failed: Nope');
165153
Block\await($promise, $this->loop);
166154
}
167155

@@ -200,11 +188,7 @@ public function resolveShouldCancelTimerWhenFullResponseIsReceived()
200188
$this->executor->query('8.8.8.8:53', $query);
201189
}
202190

203-
/**
204-
* @test
205-
* @expectedException React\Dns\Query\TimeoutException
206-
* @expectedExceptionMessage DNS query for igor.io timed out
207-
*/
191+
/** @test */
208192
public function resolveShouldCloseConnectionOnTimeout()
209193
{
210194
$this->executor = $this->createExecutorMock();
@@ -234,6 +218,7 @@ public function resolveShouldCloseConnectionOnTimeout()
234218
$this->assertNotNull($timerCallback);
235219
$timerCallback();
236220

221+
$this->setExpectedException('React\Dns\Query\TimeoutException', 'DNS query for igor.io timed out');
237222
Block\await($promise, $this->loop);
238223
}
239224

tests/TestCase.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,21 @@ protected function createCallableMock()
4141
{
4242
return $this->getMockBuilder('React\Tests\Dns\CallableStub')->getMock();
4343
}
44+
45+
public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null)
46+
{
47+
if (method_exists($this, 'expectException')) {
48+
// PHPUnit 5
49+
$this->expectException($exception);
50+
if ($exceptionMessage !== '') {
51+
$this->expectExceptionMessage($exceptionMessage);
52+
}
53+
if ($exceptionCode !== null) {
54+
$this->expectExceptionCode($exceptionCode);
55+
}
56+
} else {
57+
// legacy PHPUnit 4
58+
parent::setExpectedException($exception, $exceptionMessage, $exceptionCode);
59+
}
60+
}
4461
}

0 commit comments

Comments
 (0)