Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Page クラスのショートコードの例外処理で、 \ColorMeShop\Swagger\ApiExceptionがcatchされるようにする #175

Merged
merged 4 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/shortcodes/product/class-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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( '存在しない商品IDが指定された可能性があります。' . $e );
}
return '';
} catch ( \RuntimeException $e ) {
if ( $container['WP_DEBUG_LOG'] ) {
error_log( $e );
Expand Down
50 changes: 50 additions & 0 deletions tests/src/shortcodes/product/class-page-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@
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->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
*/
Expand All @@ -25,4 +49,30 @@ public function show_テンプレート名が不正な場合は空文字を返
)
);
}

/**
* @test
*/
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(
'',
$pageShow
);

$this->assertStringContainsString( '存在しない商品IDが指定された可能性があります。', file_get_contents( $this->error_log ) );
}
}
Loading