Skip to content

Commit 4f42d06

Browse files
committed
Refactor tests for phpunit expected exceptions
1 parent 1aab0e9 commit 4f42d06

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

tests/PagerDutyChannelTest.php

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Illuminate\Notifications\Notification;
1010
use NotificationChannels\PagerDuty\PagerDutyChannel;
1111
use NotificationChannels\PagerDuty\PagerDutyMessage;
12-
use NotificationChannels\PagerDuty\Exceptions\ApiError;
1312
use NotificationChannels\PagerDuty\Exceptions\CouldNotSendNotification;
1413

1514
class ChannelTest extends TestCase
@@ -44,7 +43,11 @@ public function it_skips_when_not_routable()
4443
$channel->send(new TestNotifiableUnRoutable(), new TestNotification());
4544
}
4645

47-
/** @test */
46+
/**
47+
* @test
48+
* @expectedException \NotificationChannels\PagerDuty\Exceptions\ApiError
49+
* @expectedExceptionMessage PagerDuty returned 400 Bad Request: Event object is invalid - Length of 'routing_key' is incorrect (should be 32 characters)
50+
*/
4851
public function it_throws_an_exception_when_400_bad_request()
4952
{
5053
$responseBody = '{
@@ -55,11 +58,6 @@ public function it_throws_an_exception_when_400_bad_request()
5558
]
5659
}';
5760

58-
$this->setExpectedException(
59-
ApiError::class,
60-
'PagerDuty returned 400 Bad Request: Event object is invalid - Length of \'routing_key\' is incorrect (should be 32 characters)'
61-
);
62-
6361
$response = new Response(400, [], $responseBody, '1.1', 'Bad Request');
6462
$client = Mockery::mock(Client::class);
6563
$client->shouldReceive('post')
@@ -69,14 +67,13 @@ public function it_throws_an_exception_when_400_bad_request()
6967
$channel->send(new TestNotifiable(), new TestNotification());
7068
}
7169

72-
/** @test */
70+
/**
71+
* @test
72+
* @expectedException \NotificationChannels\PagerDuty\Exceptions\ApiError
73+
* @expectedExceptionMessage PagerDuty returned 400 Bad Request: -
74+
*/
7375
public function it_throws_the_expected_exception_if_error_response_doesnt_contain_expected_body_on_error()
7476
{
75-
$this->setExpectedException(
76-
ApiError::class,
77-
'PagerDuty returned 400 Bad Request: -'
78-
);
79-
8077
$response = new Response(400, [], '', '1.1', 'Bad Request');
8178
$client = Mockery::mock(Client::class);
8279
$client->shouldReceive('post')
@@ -86,14 +83,13 @@ public function it_throws_the_expected_exception_if_error_response_doesnt_contai
8683
$channel->send(new TestNotifiable(), new TestNotification());
8784
}
8885

89-
/** @test */
86+
/**
87+
* @test
88+
* @expectedException \NotificationChannels\PagerDuty\Exceptions\ApiError
89+
* @expectedExceptionMessage PagerDuty returned 429 Too Many Requests
90+
*/
9091
public function it_throws_exception_on_rate_limit()
9192
{
92-
$this->setExpectedException(
93-
ApiError::class,
94-
'PagerDuty returned 429 Too Many Requests'
95-
);
96-
9793
$response = new Response(429, [], '', '1.1', 'Too Many Requests');
9894
$client = Mockery::mock(Client::class);
9995
$client->shouldReceive('post')
@@ -103,14 +99,13 @@ public function it_throws_exception_on_rate_limit()
10399
$channel->send(new TestNotifiable(), new TestNotification());
104100
}
105101

106-
/** @test */
102+
/**
103+
* @test
104+
* @expectedException \NotificationChannels\PagerDuty\Exceptions\ApiError
105+
* @expectedExceptionMessage PagerDuty responded with an unexpected HTTP Status: 503
106+
*/
107107
public function it_throws_exception_on_unexpected_code()
108108
{
109-
$this->setExpectedException(
110-
ApiError::class,
111-
'PagerDuty responded with an unexpected HTTP Status: 503'
112-
);
113-
114109
$response = new Response(503, [], '', '1.1', 'Service Unavailable');
115110
$client = Mockery::mock(Client::class);
116111
$client->shouldReceive('post')
@@ -120,16 +115,15 @@ public function it_throws_exception_on_unexpected_code()
120115
$channel->send(new TestNotifiable(), new TestNotification());
121116
}
122117

123-
/** @test */
118+
/**
119+
* @test
120+
* @expectedException \NotificationChannels\PagerDuty\Exceptions\CouldNotSendNotification
121+
* @expectedExceptionMessage Cannot send message to PagerDuty: Test Exception
122+
*/
124123
public function it_rethrows_exception_on_client_exception()
125124
{
126125
$e = new \Exception('Test Exception');
127126

128-
$this->setExpectedException(
129-
CouldNotSendNotification::class,
130-
'Cannot send message to PagerDuty: Test Exception'
131-
);
132-
133127
$client = Mockery::mock(Client::class);
134128
$client->shouldReceive('post')
135129
->once()

0 commit comments

Comments
 (0)