月初日が日曜日の場合、 CalendarControllerTest::testWeekendHolidaysStyle を第一土曜日に実行すると失敗する
- Dominant language
- PHP
- Stars
- 788
- Forks
- 719
- Avg merge
- 3d 20h
- Merged PRs (30d)
- 45
Description
### 概要(Overview)
CalendarControllerTest::testWeekendHolidaysStyle テストが土曜日に実行された場合に失敗します。これはテストコードが
現在日付に依存した設計になっているためで、特定の条件下で期待するIDの要素が見つからないことが原因です
### 期待される動作
テストの実行日に関わらず、テストが成功すること
### 原因分析
問題はテストコードにあります:
1. 日付依存の問題: テストが Carbon::now() を使用しているため、実行日によって結果が変わる
2. ID競合の問題:
- 2025年6月7日(土)にテストを実行した場合
- テストは土曜日である6月7日の要素を #this-month-holiday-7 で探す
- しかし、6月7日は「今日」でもあるため、Twigテンプレートでは以下の優先順位でIDが付与される:
- today == 1 and holiday == 1 → #today-and-holiday
- today == 1 and holiday == 0 → #today
- today == 0 and holiday == 1 → #this-month-holiday-X
- 土日 → #this-month-holiday-X
- 結果として、テスト実行日が土曜日の場合、#today または #today-and-holiday
のIDが優先され、#this-month-holiday-7 が見つからない
3. テストロジックの問題:
- 114-118行目で土曜日を計算する際、月初日が日曜日の場合に6日後を土曜日としているが、これがテスト実行日と重なる可能性がある
### 修正案
テストコードを日付に依存しない設計に変更する:
1. Carbon::setTestNow() を使用して固定日付でテスト
public function testWeekendHolidaysStyle()
{
// テストを平日の固定日付で実行
Carbon::setTestNow('2025-06-02'); // 月曜日
// 既存のテストロジック
// ...
Carbon::setTestNow(); // リセット
}
2. テスト実行日を考慮したアサーション
- テスト実行日が土日の場合は、適切なIDを探すようにロジックを改善
3. データプロバイダーを使用した複数日付でのテスト
- 様々な日付パターンでテストを実行し、日付依存の問題を回避
### 関連ファイル
- tests/Eccube/Tests/Web/Block/CalendarControllerTest.php (問題のテストコード)
- src/Eccube/Resource/template/default/Block/calendar.twig (ID付与の優先順位)
- src/Eccube/Controller/Block/CalendarController.php (正常に動作)
### 再現手順(Procedure)
以下のパッチを当て、 vendor/bin/phpunit --fliter=testWeekendHolidaysStyle を実行する
```diff
diff --git a/tests/Eccube/Tests/Web/Block/CalendarControllerTest.php b/tests/Eccube/Tests/Web/Block/CalendarControllerTest.php
index 852dc8ae83..c90a2c43cb 100644
--- a/tests/Eccube/Tests/Web/Block/CalendarControllerTest.php
+++ b/tests/Eccube/Tests/Web/Block/CalendarControllerTest.php
@@ -88,6 +88,7 @@ class CalendarControllerTest extends AbstractWebTestCase
public function testWeekendHolidaysStyle()
{
+ Carbon::setTestNow('2025-06-07'); // 月初日が日曜日の第一土曜日
// 月初日を取得
$firstDayOfThisMonth = Carbon::now()->firstOfMonth();
```
エラー内容
InvalidArgumentException: The current node list is empty.
/home/runner/work/ec-cube/ec-cube/vendor/symfony/dom-crawler/Crawler.php:575
/home/runner/work/ec-cube/ec-cube/tests/Eccube/Tests/Web/Block/CalendarControllerTest.php:124
### 環境(Environment)
- EC-CUBE バージョン: 4.3
- PHP バージョン: 8.1~8.3
- テスト実行日: 土曜日
以下の GitHub Actions で再現
https://github.com/EC-CUBE/ec-cube/actions/runs/15505935096/job/43660791407?pr=6378
Contributor guide
Research direction
Start with tests/Eccube/Tests/Web/Block/CalendarControllerTest.php, especially testWeekendHolidaysStyle and the assertions around line 124. Run vendor/bin/phpunit --filter=testWeekendHolidaysStyle and review the ID precedence in src/Eccube/Resource/template/default/Block/calendar.twig. Done means the test passes regardless of its execution date.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100