Skip to content

test: CalendarControllerTest を実行日に依存しないよう修正する (#6390) - #7054

Open
ttokoro20240902 wants to merge 4 commits into
4.4from
fix/issue-6390-calendar-test-date-dependency
Open

test: CalendarControllerTest を実行日に依存しないよう修正する (#6390)#7054
ttokoro20240902 wants to merge 4 commits into
4.4from
fix/issue-6390-calendar-test-date-dependency

Conversation

@ttokoro20240902

@ttokoro20240902 ttokoro20240902 commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

概要(Overview・Refs Issue)

Refs #6390

CalendarControllerTest実行日に依存して失敗する 問題を修正します。

testWeekendHolidaysStyle は当月の第一土曜日・第一日曜日のセルを #this-month-holiday-X で探していますが、
その日が「今日」と重なると calendar.twig#today を優先するため要素が見つからず、
InvalidArgumentException: The current node list is empty. で落ちます。

Issue には「月初日が日曜日の場合の第一土曜日」とありますが、実測すると月初の曜日に関係なく
第一土曜日と第一日曜日の 2 日
で発生します(毎月 2 日)。修正前のコードに時刻固定だけを入れて確認した結果:

固定日 曜日 結果
2025-06-01 日(第一日曜) ERROR
2025-06-07 土(第一土曜) ERROR
2025-06-08 日(第二日曜) OK
2025-06-02 OK

方針(Policy)

Issue の「修正案」に沿いつつ、次の 4 点で構成しました。

  1. Carbon::setTestNow()#[DataProvider] で実行日を固定する — 月初の曜日 × 今日が土日と重なるか、の組み合わせを 7 パターン網羅します。CalendarControllerloopCount === 28 の専用分岐があるため、2 月がちょうど 28 日で日曜始まりのケースも含めています。
  2. 対象日が今日のときは #today を参照するcalendar.twig の ID 優先順位(#today-and-holiday#today#this-month-holiday-X)に合わせます。
  3. setTestNow() のリセットは tearDown() で行う — Issue の修正案にある「メソッド末尾でリセット」は、途中のアサーションが失敗すると到達せず、同一プロセスの後続テストを巻き込みます。実際に再現用パッチを当てた状態でクラス全体を流したところ、testWeekendHolidaysStyle ではなく testTodayStyle が失敗しました。
  4. 月初からの曜日計算を firstOfMonth(曜日) に置き換える — 元の if/elseif 7 分岐と「日曜の前日が今月かどうか」の判定が不要になります。

実装に関する補足(Appendix)

ガード用のアサーションを追加しています。 このテストは期待値と CSS セレクタを同じ変数から組み立てているため、
日付の取得自体が誤っていてもアサーションが通ってしまいます(元のコードからある性質です)。
計算方法を置き換えた以上そのままにはできないので、取得結果が本当に土曜/日曜で 7 日以内かを検証しています。

testTodayStyle も併せて修正しています。 こちらは Issue の範囲外ですが、期待値を new \DateTime()
組み立てており、描画側の Carbon::now() と不揃いでした。通常実行では一致するため表面化しませんが、
時刻を固定した瞬間に必ず壊れます。同クラスの他のテストは既に Carbon::now() を使っており、これだけが例外でした。

修正範囲は実測で決めました。 setUp() に一時的なハーネスを入れて外から時刻を与え、
2025-06〜2026-03 の各月について 1 日 / 月末 / 月末前日 の計 30 日でクラス全体を実行したところ、
全日で落ちるのは testTodayStyle だけでした。testHolidayStyle の土日回避ロジックは
見た目こそ複雑ですが月末・年境界・2 月末を含む全日で正しく動いていたため、手を付けていません

テスト(Test)

コードスタイル / 静的解析 / リファクタ規約 / ユニットテストの 4 ゲートをローカルで通しています。

ゲート 結果
php-cs-fixer 0 件
phpstan (level 6, src) No errors
rector Rector is done!(YieldDataProviderRector / AttributeArgumentsOrderRector に追従済み)
phpunit tests/Eccube/Tests/Web/Block/ OK (17 tests, 66 assertions)

修正が実際にゲートとして機能するかを、部分ごとに外して確認しました。

実験 結果
セレクタ切替(#today 対応)を外す 土日が今日と重なる 4 ケースだけ ERROR、平日 3 ケースは OK
tearDown() のリセットを外す testTodayStyle が失敗(setTestNow の漏れ)

時刻を固定した通し確認も行っています。

  • 2025-06〜2026-03 の各月 1 日 / 月末 / 月末前日 / 15 日 = 32 日で全テスト緑
  • ランダム実行順(seed 2 種)で緑 — tearDown() のリセットが効いていること
  • --filter 単体実行で緑 — 他テストへの依存がないこと
  • 連続 2 回実行で緑 — dtb_calendar は実行前後とも 0 件で汚染なし

相談(Discussion)

  • コミットは「Issue 本体の修正」「testTodayStyle の整合修正」「rector 追従」の 3 本に分けています。まとめたい場合は squash してください。
  • testHolidayStyle は上記のとおり実測では全日通るため今回は触っていません。日付固定に揃えるべきかはご判断ください。

マイナーバージョン互換性保持のための制限事項チェックリスト

テストコードのみの変更で、src/ には一切手を入れていません。

  • 既存機能の仕様変更はありません
  • フックポイントの呼び出しタイミングの変更はありません
  • フックポイントのパラメータの削除・データ型の変更はありません
  • twigファイルに渡しているパラメータの削除・データ型の変更はありません
  • Serviceクラスの公開関数の、引数の削除・データ型の変更はありません
  • 入出力ファイル(CSVなど)のフォーマット変更はありません

レビュワー確認項目

  • 動作確認
  • コードレビュー
  • E2E/Unit テスト確認(テストの追加・変更が必要かどうか)
  • 互換性が保持されているか
  • セキュリティ上の問題がないか
    • 権限を超えた操作が可能にならないか
    • 不要なファイルアップロードがないか
    • 外部へ公開されるファイルや機能の追加ではないか
    • テンプレートでのエスケープ漏れがないか

Summary by CodeRabbit

  • テスト
    • カレンダーの週末・休日表示について、月初を含む複数の日付条件で検証できるようになりました。
    • テスト後の日付設定を適切に解除し、テスト間の影響を防止しました。

ttokoro20240902 and others added 3 commits August 12, 2026 11:52
testWeekendHolidaysStyle は当月の第一土曜日・第一日曜日のセルを
#this-month-holiday-X で探していたが, その日が「今日」と重なると
テンプレートが #today を優先するため要素が見つからず失敗していた.
毎月 2 日 (第一土曜・第一日曜) に発生する.

- Carbon::setTestNow() と DataProvider で実行日を固定し, 月初の曜日と
  今日が土日に重なるケースを網羅する
- 対象日が今日の場合は #today を参照するようセレクタを切り替える
- setTestNow() は tearDown() で戻す. メソッド末尾で戻すとアサーション
  失敗時に到達せず, 後続のテストを巻き込むため
- 月初からの曜日計算を firstOfMonth(曜日) に置き換え, 取得結果が
  実際に土日かどうかをアサーションで担保する

Refs #6390

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
CalendarController は「今日」の判定を Carbon::now() で行うが, テスト側は
new \DateTime() で期待値を組み立てていた. 通常実行では一致するため表面化
しないが, Carbon::setTestNow() で時刻を固定すると描画側だけが固定日付に
なり, 実時刻との差で必ず失敗する.

同クラスの他のテストは既に Carbon::now() を使っており, これだけが不揃い
だった. 期待値を Carbon::now() に統一する.

2025-06 〜 2026-03 の各月について 1日 / 月末 / 月末前日 / 15日 の計 32 日で
時刻を固定して全テストを実行し, 全日で緑になることを確認した.

Refs #6390

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
rector.php の YieldDataProviderRector / AttributeArgumentsOrderRector に追従する.

- データプロバイダの return 配列を yield へ変更し戻り値を \Iterator に
- #[DataProvider] の引数を名前付き引数へ

Refs #6390

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 05a6fb72-c567-4cb6-89f7-508f7941a853

📥 Commits

Reviewing files that changed from the base of the PR and between 0d48b1f and fb0ac2f.

📒 Files selected for processing (1)
  • tests/Eccube/Tests/Web/Block/CalendarControllerTest.php
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/Eccube/Tests/Web/Block/CalendarControllerTest.php

📝 Walkthrough

Walkthrough

カレンダー表示テストに Carbon の時刻リセット処理を追加しました。週末休日テストを複数の日付ケースに対応させ、月初の土曜日・日曜日と当日表示を検証します。

Changes

カレンダー表示テストの安定化

Layer / File(s) Summary
テスト時刻の分離
tests/Eccube/Tests/Web/Block/CalendarControllerTest.php
各テスト後に Carbon::setTestNow() の設定を解除します。
週末休日の日付ケース検証
tests/Eccube/Tests/Web/Block/CalendarControllerTest.php
データプロバイダーで固定日付を指定します。月初の土曜日・日曜日を取得し、曜日と月初7日以内であることを検証します。weekendCellSelector() で当日と休日のセレクターを切り替えます。testTodayStyle()Carbon::now() を期待値に使用します。

Estimated code review effort: 3 (Moderate) | ~20 minutes

Suggested reviewers: nanasess, dotani1111

Poem

うさぎは日付を固定して
土曜と日曜を確認する
今日のセルは #today
テスト後に時刻を戻す
カレンダーは静かに安定する

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed タイトルは、CalendarControllerTestを実行日に依存しないよう修正する主な変更内容を明確かつ簡潔に示しています。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/issue-6390-calendar-test-date-dependency

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/Eccube/Tests/Web/Block/CalendarControllerTest.php`:
- Line 162: Update the testTodayStyle() method declaration to include the : void
return type, since the test does not return a value and must satisfy the
project’s PHP typing guidelines.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 5742b516-7b5e-4b5d-b563-d15866417deb

📥 Commits

Reviewing files that changed from the base of the PR and between 9faf936 and 0d48b1f.

📒 Files selected for processing (1)
  • tests/Eccube/Tests/Web/Block/CalendarControllerTest.php

Comment thread tests/Eccube/Tests/Web/Block/CalendarControllerTest.php Outdated
CodeRabbit の指摘に対応する. 本 PR で本文を変更したメソッドであり,
同じく書き換えた testWeekendHolidaysStyle には : void が付いているため
不揃いだった.

同クラスの他 5 メソッドは本 PR で触っていない既存コードのため対象外とする.

Refs #6390

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@codecov

codecov Bot commented Aug 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.76%. Comparing base (9faf936) to head (fb0ac2f).
⚠️ Report is 2 commits behind head on 4.4.

Additional details and impacted files
@@            Coverage Diff             @@
##              4.4    #7054      +/-   ##
==========================================
+ Coverage   77.66%   77.76%   +0.10%     
==========================================
  Files         597      597              
  Lines       29339    29339              
==========================================
+ Hits        22785    22816      +31     
+ Misses       6554     6523      -31     
Flag Coverage Δ
Unit 77.76% <ø> (+0.10%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@dotani1111 dotani1111 added this to the 4.4.0 milestone Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants