Skip to content

Add strictMode config for misconfigured SearchComponent#371

Merged
dereuromark merged 5 commits intoFriendsOfCake:masterfrom
dereuromark:feature/warn-on-misconfigured-model-class
Feb 9, 2026
Merged

Add strictMode config for misconfigured SearchComponent#371
dereuromark merged 5 commits intoFriendsOfCake:masterfrom
dereuromark:feature/warn-on-misconfigured-model-class

Conversation

@dereuromark
Copy link
Copy Markdown
Member

@dereuromark dereuromark commented Jan 28, 2026

Summary

  • Adds a strictMode config option to SearchComponent (default false) that controls behavior when the model cannot be fetched or the Search behavior is not loaded
  • With strictMode disabled (default): silently skips, preserving the existing behavior that allows loading the component in AppController where not all controllers have a searchable table
  • With strictMode enabled: throws RuntimeException with a clear message identifying the controller, action, and what went wrong — catching misconfiguration early

Changes

  • src/Controller/Component/SearchComponent.php:
    • Added strictMode config option (default false)
    • When strictMode is true and fetchTable() fails: throws RuntimeException with the original exception chained
    • When strictMode is true and Search behavior is missing: throws RuntimeException
    • When strictMode is false: silently returns (no change from current master behavior)
  • tests/TestCase/Controller/Component/SearchComponentTest.php:
    • Added testBeforeRenderWithoutModelSilentlySkips — verifies default silent behavior
    • Added testBeforeRenderWithoutBehaviorSilentlySkips — verifies default silent behavior
    • Added testStrictModeThrowsOnMissingModel — verifies exception with strictMode
    • Added testStrictModeThrowsOnMissingBehavior — verifies exception with strictMode

Migration path

In a future major version, strictMode could become true by default, with users opting out via 'strictMode' => false for the AppController use case.

@dereuromark dereuromark added this to the 7.x milestone Jan 28, 2026
@dereuromark
Copy link
Copy Markdown
Member Author

The MySQL 8.2 test failure is pre-existing and unrelated to this PR — it's caused by 3 deprecation warnings surfacing via --display-deprecations from newer CakePHP versions since master was last green (Dec 2025). None of the deprecation lines (111, ORM/Table.php) correspond to the changes in this PR (lines 171 and 186).

@ADmad
Copy link
Copy Markdown
Member

ADmad commented Jan 29, 2026

These 2 silents returns where to allow adding the SearchComponent to AppController itself instead of each controller (I do that for backends). So if a controller has $defaultTable disabled or the Search behavior wasn't attached to a table the component would silently move out of the way.

The issue you pointed out is also valid. To allow both types of usages possible, instead of adding the warnings we could add a "strictMode" flag. With strict mode enabled we could throw exception for model not being fetched or it not having the behavior attached. With strict mode disabled things remain as now.

@dereuromark
Copy link
Copy Markdown
Member Author

I agree, that might be then cleaner. In a future major we could make the strict mode default, and allowing to opt-in for such a AppController case instead of accidentally forgetting strict mode and wondering why nothing happens.

Replace deprecation warnings with a `strictMode` config option (default
`false`) that throws RuntimeException when enabled. This is BC-safe:

- strictMode=false (default): silently skips as before, allowing the
  component to be loaded in AppController where not all controllers
  have a searchable table
- strictMode=true: throws meaningful exceptions when the model cannot
  be fetched or the Search behavior is not loaded, catching
  misconfiguration early

In a future major version, strictMode can become the default.
@dereuromark dereuromark changed the title Add deprecation warnings for misconfigured SearchComponent Add strictMode config for misconfigured SearchComponent Jan 29, 2026
ArticlesTable::initialize() auto-loads the Search behavior, so use
a plain Table class instead when testing the missing behavior scenario.
@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 29, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.98%. Comparing base (7132608) to head (e758c87).
⚠️ Report is 11 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #371      +/-   ##
============================================
+ Coverage     91.35%   91.98%   +0.62%     
- Complexity      240      242       +2     
============================================
  Files            19       19              
  Lines           694      711      +17     
============================================
+ Hits            634      654      +20     
+ Misses           60       57       -3     

☔ View full report in Codecov by Sentry.
📢 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.

@dereuromark dereuromark requested a review from ADmad January 29, 2026 08:52
@dereuromark dereuromark merged commit e2e718b into FriendsOfCake:master Feb 9, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants