Add strictMode config for misconfigured SearchComponent#371
Conversation
|
The MySQL 8.2 test failure is pre-existing and unrelated to this PR — it's caused by 3 deprecation warnings surfacing via |
|
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 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. |
|
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.
ArticlesTable::initialize() auto-loads the Search behavior, so use a plain Table class instead when testing the missing behavior scenario.
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
Summary
strictModeconfig option toSearchComponent(defaultfalse) that controls behavior when the model cannot be fetched or the Search behavior is not loadedstrictModedisabled (default): silently skips, preserving the existing behavior that allows loading the component inAppControllerwhere not all controllers have a searchable tablestrictModeenabled: throwsRuntimeExceptionwith a clear message identifying the controller, action, and what went wrong — catching misconfiguration earlyChanges
src/Controller/Component/SearchComponent.php:strictModeconfig option (defaultfalse)strictModeis true andfetchTable()fails: throwsRuntimeExceptionwith the original exception chainedstrictModeis true and Search behavior is missing: throwsRuntimeExceptionstrictModeis false: silently returns (no change from current master behavior)tests/TestCase/Controller/Component/SearchComponentTest.php:testBeforeRenderWithoutModelSilentlySkips— verifies default silent behaviortestBeforeRenderWithoutBehaviorSilentlySkips— verifies default silent behaviortestStrictModeThrowsOnMissingModel— verifies exception with strictModetestStrictModeThrowsOnMissingBehavior— verifies exception with strictModeMigration path
In a future major version,
strictModecould becometrueby default, with users opting out via'strictMode' => falsefor the AppController use case.