From 624291d475e54947bf440bfe2a2f9be56a15e6da Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 17 Feb 2024 07:24:50 +0900 Subject: [PATCH 1/3] test: add test for network error --- tests/feature/BasicPagesTest.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/feature/BasicPagesTest.php b/tests/feature/BasicPagesTest.php index 4a52da30..556e7e87 100644 --- a/tests/feature/BasicPagesTest.php +++ b/tests/feature/BasicPagesTest.php @@ -1,7 +1,9 @@ assertSee('The small framework with powerful features'); } + public function testCanViewHomeWhenConnectException() + { + $github = $this->getMockBuilder(GitHub::class) + ->disableOriginalConstructor() + ->disableOriginalClone() + ->disableArgumentCloning() + ->disallowMockingUnknownTypes() + ->onlyMethods(['getRepos']) + ->getMock(); + $github->method('getRepos')->willThrowException( + new GuzzleHttp\Exception\ConnectException( + 'cURL error 6: Could not resolve host: api.github.com (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://api.github.com/repos/bcit-ci/CodeIgniter', + new GuzzleHttp\Psr7\Request( + 'GET', + 'https://api.github.com/repos/bcit-ci/CodeIgniter' + ) + ) + ); + Services::injectMock('github', $github); + + $result = $this->get('/'); + + $result->assertStatus(200); + $result->assertSee('The small framework with powerful features'); + } + public function testCanViewDiscuss() { $result = $this->get('/discuss'); From e4c31075a8d00f09b0289fbb72ee3523871e9eb1 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 17 Feb 2024 07:47:17 +0900 Subject: [PATCH 2/3] fix: catch exception when cannot reach GitHub --- app/Controllers/Contribute.php | 6 ++++-- app/Controllers/Download.php | 6 ++++-- app/Controllers/Home.php | 6 ++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/app/Controllers/Contribute.php b/app/Controllers/Contribute.php index cd976775..8fdff2e9 100644 --- a/app/Controllers/Contribute.php +++ b/app/Controllers/Contribute.php @@ -2,7 +2,7 @@ namespace App\Controllers; -use Github\Exception\ExceptionInterface; +use Psr\Http\Client\ClientExceptionInterface; class Contribute extends BaseController { @@ -16,7 +16,9 @@ public function index() // Contributors are already sorted, so grab the first 12 $data['contributors'][$id] = array_slice($contributors, 0, 12); } - } catch (ExceptionInterface $e) { + } catch (ClientExceptionInterface $e) { + log_message('error', '[' . __METHOD__ . '] ' . get_class($e) . ': ' . $e->getMessage()); + $data['contributors'] = null; } diff --git a/app/Controllers/Download.php b/app/Controllers/Download.php index 0bcfe437..c1e01de7 100644 --- a/app/Controllers/Download.php +++ b/app/Controllers/Download.php @@ -2,7 +2,7 @@ namespace App\Controllers; -use Github\Exception\ExceptionInterface; +use Psr\Http\Client\ClientExceptionInterface; class Download extends BaseController { @@ -18,7 +18,9 @@ public function index() 'v3link' => end($releases['framework3'])->download_url, 'v4link' => end($releases['framework4'])->download_url, ]; - } catch (ExceptionInterface $e) { + } catch (ClientExceptionInterface $e) { + log_message('error', '[' . __METHOD__ . '] ' . get_class($e) . ': ' . $e->getMessage()); + $data = [ 'v3name' => 'unknown', 'v4name' => 'unknown', diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php index 610f063e..cd4f63be 100644 --- a/app/Controllers/Home.php +++ b/app/Controllers/Home.php @@ -2,7 +2,7 @@ namespace App\Controllers; -use Github\Exception\ExceptionInterface; +use Psr\Http\Client\ClientExceptionInterface; class Home extends BaseController { @@ -17,7 +17,9 @@ public function index() 'stargazers_count' => number_format($repos['codeigniter4']->stargazers_count), 'forks_count' => number_format($repos['codeigniter4']->forks_count), ]; - } catch (ExceptionInterface $e) { + } catch (ClientExceptionInterface $e) { + log_message('error', '[' . __METHOD__ . '] ' . get_class($e) . ': ' . $e->getMessage()); + $data = [ 'html_url' => 'https://github.com/codeigniter4/CodeIgniter4', 'stargazers_count' => '', From 76024a7b824b02c9d4ab9654168ba6a1e9bf3a4f Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 17 Feb 2024 07:56:20 +0900 Subject: [PATCH 3/3] test: refactor by rector --- tests/feature/BasicPagesTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/feature/BasicPagesTest.php b/tests/feature/BasicPagesTest.php index 556e7e87..df13be5f 100644 --- a/tests/feature/BasicPagesTest.php +++ b/tests/feature/BasicPagesTest.php @@ -4,6 +4,8 @@ use CodeIgniter\Test\DatabaseTestTrait; use CodeIgniter\Test\FeatureTestTrait; use Config\Services; +use GuzzleHttp\Exception\ConnectException; +use GuzzleHttp\Psr7\Request; use Tests\Support\ProjectTestCase; /** @@ -32,9 +34,9 @@ public function testCanViewHomeWhenConnectException() ->onlyMethods(['getRepos']) ->getMock(); $github->method('getRepos')->willThrowException( - new GuzzleHttp\Exception\ConnectException( + new ConnectException( 'cURL error 6: Could not resolve host: api.github.com (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://api.github.com/repos/bcit-ci/CodeIgniter', - new GuzzleHttp\Psr7\Request( + new Request( 'GET', 'https://api.github.com/repos/bcit-ci/CodeIgniter' )