Skip to content

Commit 6abddf8

Browse files
authored
Merge pull request #175 from pepabo/catch-ApiException
Page クラスのショートコードの例外処理で、 \ColorMeShop\Swagger\ApiExceptionがcatchされるようにする
2 parents 5ff393b + df0190d commit 6abddf8

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/shortcodes/product/class-page.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public static function show( $container, $atts, $content, $tag ) {
4444

4545
try {
4646
$product = $container['api.product_api']->fetch( $filtered_atts['product_id'] )['product'];
47+
} catch ( \ColorMeShop\Swagger\ApiException $e ) {
48+
if ( $container['WP_DEBUG_LOG'] ) {
49+
error_log( '存在しない商品IDが指定された可能性があります。' . $e );
50+
}
51+
return '';
4752
} catch ( \RuntimeException $e ) {
4853
if ( $container['WP_DEBUG_LOG'] ) {
4954
error_log( $e );

tests/src/shortcodes/product/class-page-test.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@
22
namespace ColorMeShop\Shortcodes\Product;
33

44
class Page_Test extends \WP_UnitTestCase {
5+
6+
/** @var \Pimple\Container */
7+
private $container;
8+
9+
/** @var string */
10+
private $error_log;
11+
12+
/** @var string */
13+
private $original_error_log;
14+
15+
public function setUp() {
16+
parent::setUp();
17+
18+
$this->container = _get_container();
19+
// ログ出力先
20+
$this->error_log = tempnam( sys_get_temp_dir(), 'TEST' );
21+
$this->original_error_log = ini_set( 'error_log', $this->error_log );
22+
}
23+
24+
public function tearDown() {
25+
parent::tearDown();
26+
ini_set( 'error_log', $this->original_error_log );
27+
}
28+
529
/**
630
* @test
731
*/
@@ -25,4 +49,30 @@ public function show_テンプレート名が不正な場合は空文字を返
2549
)
2650
);
2751
}
52+
53+
/**
54+
* @test
55+
*/
56+
public function show_存在しない商品IDが指定された場合、デバッグが有効であればエラーメッセージを出力し、空文字を返す() {
57+
$this->container['WP_DEBUG_LOG'] = function ( $c ) {
58+
return true;
59+
};
60+
61+
$pageShow = Page::show(
62+
$this->container,
63+
[
64+
'template' => 'default',
65+
'product_id' => '00000000',
66+
],
67+
null,
68+
null
69+
);
70+
71+
$this->assertSame(
72+
'',
73+
$pageShow
74+
);
75+
76+
$this->assertStringContainsString( '存在しない商品IDが指定された可能性があります。', file_get_contents( $this->error_log ) );
77+
}
2878
}

0 commit comments

Comments
 (0)