From ac6a2cc5a3cc32fb48a984ae1b73692a3b72be11 Mon Sep 17 00:00:00 2001 From: shiorin <32048522+ShioriPeace@users.noreply.github.com> Date: Tue, 13 Aug 2024 19:03:51 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=20\ColorMeShop\Swagger\ApiException?= =?UTF-8?q?=E3=82=92catch=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/shortcodes/product/class-page.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/shortcodes/product/class-page.php b/src/shortcodes/product/class-page.php index 270081b..86781c8 100644 --- a/src/shortcodes/product/class-page.php +++ b/src/shortcodes/product/class-page.php @@ -44,6 +44,11 @@ public static function show( $container, $atts, $content, $tag ) { try { $product = $container['api.product_api']->fetch( $filtered_atts['product_id'] )['product']; + } catch ( \ColorMeShop\Swagger\ApiException $e ) { + if ( $container['WP_DEBUG_LOG'] ) { + error_log( $e ); + } + return ''; } catch ( \RuntimeException $e ) { if ( $container['WP_DEBUG_LOG'] ) { error_log( $e ); From 5bd93d1cc3f9c480434dd7dde506e70ff9b06b0a Mon Sep 17 00:00:00 2001 From: shiorin <32048522+ShioriPeace@users.noreply.github.com> Date: Tue, 13 Aug 2024 19:04:09 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=E8=BF=BD=E5=8A=A0=E3=81=97=E3=81=9F?= =?UTF-8?q?=E9=83=A8=E5=88=86=E3=81=8C=E5=8B=95=E3=81=8F=E3=81=93=E3=81=A8?= =?UTF-8?q?=E3=82=92=E6=8B=85=E4=BF=9D=E3=81=99=E3=82=8B=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shortcodes/product/class-page-test.php | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/src/shortcodes/product/class-page-test.php b/tests/src/shortcodes/product/class-page-test.php index 9ad3568..de1a9ef 100644 --- a/tests/src/shortcodes/product/class-page-test.php +++ b/tests/src/shortcodes/product/class-page-test.php @@ -25,4 +25,34 @@ public function show_テンプレート名が不正な場合は空文字を返 ) ); } + + /** + * @test + */ + public function show_存在しない商品IDが指定された場合は、ApiExceptionをcatchし空文字を返す() { + $container = $this->createMock(\Pimple\Container::class); + $product_api = $this->createMock(\ColorMeShop\Swagger\Api\ProductApi::class); + + $product_api->method('fetch') + ->willThrowException(new \ColorMeShop\Swagger\ApiException()); + + $container->method('offsetGet') + ->willReturnMap([ + ['api.product_api', $product_api], + ['WP_DEBUG_LOG', false], + ]); + + $this->assertSame( + '', + Page::show( + $container, + [ + 'template' => 'default', + 'product_id' => '999999', // 存在しない商品ID + ], + null, + null + ) + ); + } } From 01afa53cda56ec8abdfce05dc5d6c820db8a5b1c Mon Sep 17 00:00:00 2001 From: shiorin <32048522+ShioriPeace@users.noreply.github.com> Date: Fri, 16 Aug 2024 17:34:19 +0900 Subject: [PATCH 3/4] =?UTF-8?q?ApiException=E3=81=8C=E7=99=BA=E7=94=9F?= =?UTF-8?q?=E3=81=97=E3=81=9D=E3=82=8C=E3=82=92catch=E3=81=A7=E3=81=8D?= =?UTF-8?q?=E3=81=9F=E3=81=93=E3=81=A8=E3=81=8C=E3=83=AD=E3=82=B0=E3=81=8B?= =?UTF-8?q?=E3=82=89=E7=A2=BA=E8=AA=8D=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88?= =?UTF-8?q?=E3=81=86=E3=81=AA=E3=83=86=E3=82=B9=E3=83=88=E3=82=92=E4=BD=9C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/shortcodes/product/class-page.php | 2 +- .../shortcodes/product/class-page-test.php | 66 +++++++++++++------ 2 files changed, 46 insertions(+), 22 deletions(-) diff --git a/src/shortcodes/product/class-page.php b/src/shortcodes/product/class-page.php index 86781c8..9c1c7dd 100644 --- a/src/shortcodes/product/class-page.php +++ b/src/shortcodes/product/class-page.php @@ -46,7 +46,7 @@ public static function show( $container, $atts, $content, $tag ) { $product = $container['api.product_api']->fetch( $filtered_atts['product_id'] )['product']; } catch ( \ColorMeShop\Swagger\ApiException $e ) { if ( $container['WP_DEBUG_LOG'] ) { - error_log( $e ); + error_log( '存在しない商品IDが指定された可能性があります。' . $e ); } return ''; } catch ( \RuntimeException $e ) { diff --git a/tests/src/shortcodes/product/class-page-test.php b/tests/src/shortcodes/product/class-page-test.php index de1a9ef..86049ae 100644 --- a/tests/src/shortcodes/product/class-page-test.php +++ b/tests/src/shortcodes/product/class-page-test.php @@ -2,6 +2,34 @@ namespace ColorMeShop\Shortcodes\Product; class Page_Test extends \WP_UnitTestCase { + + /** @var \Pimple\Container */ + private $container; + + /** @var string */ + private $error_log; + + /** @var string */ + private $original_error_log; + + public function setUp() { + parent::setUp(); + + $this->container = _get_container(); + $this->container['token'] = function ( $c ) { + return 'dummy'; + }; + + // ログ出力先 + $this->error_log = tempnam( sys_get_temp_dir(), 'TEST' ); + $this->original_error_log = ini_set( 'error_log', $this->error_log ); + } + + public function tearDown() { + parent::tearDown(); + ini_set( 'error_log', $this->original_error_log ); + } + /** * @test */ @@ -29,30 +57,26 @@ public function show_テンプレート名が不正な場合は空文字を返 /** * @test */ - public function show_存在しない商品IDが指定された場合は、ApiExceptionをcatchし空文字を返す() { - $container = $this->createMock(\Pimple\Container::class); - $product_api = $this->createMock(\ColorMeShop\Swagger\Api\ProductApi::class); - - $product_api->method('fetch') - ->willThrowException(new \ColorMeShop\Swagger\ApiException()); - - $container->method('offsetGet') - ->willReturnMap([ - ['api.product_api', $product_api], - ['WP_DEBUG_LOG', false], - ]); + public function show_存在しない商品IDが指定された場合、デバッグが有効であればエラーメッセージを出力し、空文字を返す() { + $this->container['WP_DEBUG_LOG'] = function ( $c ) { + return true; + }; + $pageShow = Page::show( + $this->container, + [ + 'template' => 'default', + 'product_id' => '00000000', + ], + null, + null + ); + $this->assertSame( '', - Page::show( - $container, - [ - 'template' => 'default', - 'product_id' => '999999', // 存在しない商品ID - ], - null, - null - ) + $pageShow ); + + $this->assertStringContainsString( '存在しない商品IDが指定された可能性があります。', file_get_contents( $this->error_log ) ); } } From df0190d8166bd06c502ebe5c5e9703641025d274 Mon Sep 17 00:00:00 2001 From: shiorin <32048522+ShioriPeace@users.noreply.github.com> Date: Fri, 16 Aug 2024 17:40:41 +0900 Subject: [PATCH 4/4] =?UTF-8?q?token=E3=82=92=E5=91=BC=E3=81=B6=E3=83=86?= =?UTF-8?q?=E3=82=B9=E3=83=88=E3=81=AF=E8=BF=BD=E5=8A=A0=E3=81=97=E3=81=A6?= =?UTF-8?q?=E3=81=8A=E3=82=89=E3=81=9A=E4=B8=8D=E8=A6=81=E3=81=A0=E3=81=A3?= =?UTF-8?q?=E3=81=9F=E3=81=AE=E3=81=A7=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/src/shortcodes/product/class-page-test.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/src/shortcodes/product/class-page-test.php b/tests/src/shortcodes/product/class-page-test.php index 86049ae..d603be6 100644 --- a/tests/src/shortcodes/product/class-page-test.php +++ b/tests/src/shortcodes/product/class-page-test.php @@ -16,10 +16,6 @@ public function setUp() { parent::setUp(); $this->container = _get_container(); - $this->container['token'] = function ( $c ) { - return 'dummy'; - }; - // ログ出力先 $this->error_log = tempnam( sys_get_temp_dir(), 'TEST' ); $this->original_error_log = ini_set( 'error_log', $this->error_log );