Skip to content

Commit 68d0ffc

Browse files
committed
test: use narrower exceptions
1 parent cbbda20 commit 68d0ffc

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

tests/system/Cache/Handlers/MemcachedHandlerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
namespace CodeIgniter\Cache\Handlers;
1515

1616
use CodeIgniter\CLI\CLI;
17+
use CodeIgniter\Exceptions\BadMethodCallException;
1718
use CodeIgniter\I18n\Time;
1819
use Config\Cache;
19-
use Exception;
2020

2121
/**
2222
* @group CacheLive
@@ -125,7 +125,7 @@ public function testDelete(): void
125125
public function testDeleteMatching(): void
126126
{
127127
// Not implemented for Memcached, should throw an exception
128-
$this->expectException(Exception::class);
128+
$this->expectException(BadMethodCallException::class);
129129

130130
$this->handler->deleteMatching('key*');
131131
}

tests/system/Cache/ResponseCacheTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace CodeIgniter\Cache;
1515

16+
use CodeIgniter\Exceptions\RuntimeException;
1617
use CodeIgniter\HTTP\CLIRequest;
1718
use CodeIgniter\HTTP\IncomingRequest;
1819
use CodeIgniter\HTTP\Response;
@@ -23,7 +24,6 @@
2324
use Config\App as AppConfig;
2425
use Config\Cache as CacheConfig;
2526
use ErrorException;
26-
use Exception;
2727

2828
/**
2929
* @backupGlobals enabled
@@ -245,7 +245,7 @@ public function testUnserializeError()
245245

246246
public function testInvalidCacheError()
247247
{
248-
$this->expectException(Exception::class);
248+
$this->expectException(RuntimeException::class);
249249
$this->expectExceptionMessage('Error unserializing page cache');
250250

251251
$cache = mock(CacheFactory::class);

tests/system/Log/LoggerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
namespace CodeIgniter\Log;
1515

1616
use CodeIgniter\Exceptions\FrameworkException;
17+
use CodeIgniter\Exceptions\RuntimeException;
1718
use CodeIgniter\I18n\Time;
1819
use CodeIgniter\Log\Exceptions\LogException;
1920
use CodeIgniter\Test\CIUnitTestCase;
2021
use CodeIgniter\Test\Mock\MockLogger as LoggerConfig;
21-
use Exception;
2222
use Tests\Support\Log\Handlers\TestHandler;
2323
use TypeError;
2424

@@ -241,8 +241,8 @@ public function testLogInterpolatesExceptions(): void
241241
$expected = 'ERROR - ' . Time::now()->format('Y-m-d') . ' --> [ERROR] These are not the droids you are looking for';
242242

243243
try {
244-
throw new Exception('These are not the droids you are looking for');
245-
} catch (Exception $e) {
244+
throw new RuntimeException('These are not the droids you are looking for');
245+
} catch (RuntimeException $e) {
246246
$logger->log('error', '[ERROR] {exception}', ['exception' => $e]);
247247
}
248248

tests/system/Test/ControllerTestTraitTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
use App\Controllers\Home;
1717
use App\Controllers\NeverHeardOfIt;
1818
use CodeIgniter\Controller;
19+
use CodeIgniter\Exceptions\InvalidArgumentException;
20+
use CodeIgniter\Exceptions\RuntimeException;
1921
use CodeIgniter\Log\Logger;
2022
use CodeIgniter\Test\Mock\MockLogger as LoggerConfig;
2123
use Config\App;
2224
use Config\Services;
23-
use Exception;
2425
use Tests\Support\Controllers\Newautorouting;
2526
use Tests\Support\Controllers\Popcorn;
2627

@@ -41,7 +42,8 @@ final class ControllerTestTraitTest extends CIUnitTestCase
4142

4243
public function testBadController(): void
4344
{
44-
$this->expectException('InvalidArgumentException');
45+
$this->expectException(InvalidArgumentException::class);
46+
4547
$logger = new Logger(new LoggerConfig());
4648
$this->withURI('http://example.com')
4749
->withLogger($logger)
@@ -51,7 +53,8 @@ public function testBadController(): void
5153

5254
public function testBadControllerMethod(): void
5355
{
54-
$this->expectException('InvalidArgumentException');
56+
$this->expectException(InvalidArgumentException::class);
57+
5558
$logger = new Logger(new LoggerConfig());
5659
$this->withURI('http://example.com')
5760
->withLogger($logger)
@@ -252,12 +255,12 @@ public function testUsesRequestBody(): void
252255
$this->controller = new class () extends Controller {
253256
public function throwsBody(): void
254257
{
255-
throw new Exception($this->request->getBody());
258+
throw new RuntimeException($this->request->getBody());
256259
}
257260
};
258261
$this->controller->initController($this->request, $this->response, $this->logger);
259262

260-
$this->expectException(Exception::class);
263+
$this->expectException(RuntimeException::class);
261264
$this->expectExceptionMessage('banana');
262265

263266
$this->withBody('banana')->execute('throwsBody');

0 commit comments

Comments
 (0)