Skip to content

Commit

Permalink
test: use narrower exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Mar 9, 2024
1 parent cbbda20 commit 68d0ffc
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions tests/system/Cache/Handlers/MemcachedHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
namespace CodeIgniter\Cache\Handlers;

use CodeIgniter\CLI\CLI;
use CodeIgniter\Exceptions\BadMethodCallException;
use CodeIgniter\I18n\Time;
use Config\Cache;
use Exception;

/**
* @group CacheLive
Expand Down Expand Up @@ -125,7 +125,7 @@ public function testDelete(): void
public function testDeleteMatching(): void
{
// Not implemented for Memcached, should throw an exception
$this->expectException(Exception::class);
$this->expectException(BadMethodCallException::class);

$this->handler->deleteMatching('key*');
}
Expand Down
4 changes: 2 additions & 2 deletions tests/system/Cache/ResponseCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\Cache;

use CodeIgniter\Exceptions\RuntimeException;
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Response;
Expand All @@ -23,7 +24,6 @@
use Config\App as AppConfig;
use Config\Cache as CacheConfig;
use ErrorException;
use Exception;

/**
* @backupGlobals enabled
Expand Down Expand Up @@ -245,7 +245,7 @@ public function testUnserializeError()

public function testInvalidCacheError()
{
$this->expectException(Exception::class);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Error unserializing page cache');

$cache = mock(CacheFactory::class);
Expand Down
6 changes: 3 additions & 3 deletions tests/system/Log/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
namespace CodeIgniter\Log;

use CodeIgniter\Exceptions\FrameworkException;
use CodeIgniter\Exceptions\RuntimeException;
use CodeIgniter\I18n\Time;
use CodeIgniter\Log\Exceptions\LogException;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockLogger as LoggerConfig;
use Exception;
use Tests\Support\Log\Handlers\TestHandler;
use TypeError;

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

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

Expand Down
13 changes: 8 additions & 5 deletions tests/system/Test/ControllerTestTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
use App\Controllers\Home;
use App\Controllers\NeverHeardOfIt;
use CodeIgniter\Controller;
use CodeIgniter\Exceptions\InvalidArgumentException;
use CodeIgniter\Exceptions\RuntimeException;
use CodeIgniter\Log\Logger;
use CodeIgniter\Test\Mock\MockLogger as LoggerConfig;
use Config\App;
use Config\Services;
use Exception;
use Tests\Support\Controllers\Newautorouting;
use Tests\Support\Controllers\Popcorn;

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

public function testBadController(): void
{
$this->expectException('InvalidArgumentException');
$this->expectException(InvalidArgumentException::class);

$logger = new Logger(new LoggerConfig());
$this->withURI('http://example.com')
->withLogger($logger)
Expand All @@ -51,7 +53,8 @@ public function testBadController(): void

public function testBadControllerMethod(): void
{
$this->expectException('InvalidArgumentException');
$this->expectException(InvalidArgumentException::class);

$logger = new Logger(new LoggerConfig());
$this->withURI('http://example.com')
->withLogger($logger)
Expand Down Expand Up @@ -252,12 +255,12 @@ public function testUsesRequestBody(): void
$this->controller = new class () extends Controller {
public function throwsBody(): void
{
throw new Exception($this->request->getBody());
throw new RuntimeException($this->request->getBody());
}
};
$this->controller->initController($this->request, $this->response, $this->logger);

$this->expectException(Exception::class);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('banana');

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

0 comments on commit 68d0ffc

Please sign in to comment.