Skip to content

Add cache key tests#828

Merged
abnegate merged 2 commits intomainfrom
fix-cache
Mar 6, 2026
Merged

Add cache key tests#828
abnegate merged 2 commits intomainfrom
fix-cache

Conversation

@abnegate
Copy link
Member

@abnegate abnegate commented Mar 6, 2026

Summary by CodeRabbit

  • Tests

    • Added comprehensive test suite for cache-key generation, validating consistency and variation across field selections, filter overrides, ordering, and feature toggles.
  • Chores

    • Extended filter metadata to include a signature field, improving validation and cache behavior.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 6, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 0299b40b-e546-4bbe-a962-ec5d66a20dfc

📥 Commits

Reviewing files that changed from the base of the PR and between 68ce6c4 and 19ef2d4.

📒 Files selected for processing (1)
  • tests/unit/CacheKeyTest.php
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/unit/CacheKeyTest.php

📝 Walkthrough

Walkthrough

Updated Database::getInstanceFilters() to include a signature string per filter entry; added unit tests validating cache key generation across field selections, instance-filter overrides, and filter enable/disable scenarios.

Changes

Cohort / File(s) Summary
Database Filter Signature
src/Database/Database.php
Changed getInstanceFilters() return shape to array<string, array{encode: callable, decode: callable, signature: string}>, adding a signature field to each filter entry.
Cache Key Test Suite
tests/unit/CacheKeyTest.php
Added tests covering cache key derivation: identical configs produce identical keys, field-selection sensitivity and order-insensitivity, instance filter overrides and differences, and behavior when filters are disabled/enabled. Uses mocked Adapter and real Database+Cache instances.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐇 I nibble on code and find a new thread,
Each filter gets a signature tucked to bed,
Cache keys hop in orderly rows,
Tests pat their paws where logic grows,
A rabbit's cheer for tidy threads and stead.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the primary change: adding a new test file (CacheKeyTest.php) with 133 lines of new tests for cache key generation.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-cache

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 and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/Database/Database.php (1)

1144-1148: ⚠️ Potential issue | 🟠 Major

Instance filters with same-line closures can produce identical cache signatures despite different captures.

The computeCallableSignature() method reduces closures to filename:startLine, ignoring captured variables and bound state. Since instance filter signatures are included directly in cache keys (via getCacheKeys() line computation), two instance filters defined on the same source line but capturing different variables will generate identical signatures and collide in the cache, causing the wrong filter behavior to be applied.

The test suite covers different-line closures but does not cover the case of same-line closures with different captures. Please either accept an explicit caller-provided signature for instance filters or fold closure captures into the computed signature, and add a test case for same-line closure collisions.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/Database/Database.php` around lines 1144 - 1148, The instance filter
signatures can collide because computeCallableSignature reduces closures to
file:startLine; update the instance filter registration to accept an optional
explicit signature and use it when storing into the instanceFilters map
(reference: instanceFilters property and getInstanceFilters()), and update
getCacheKeys() to prefer that provided signature over
computeCallableSignature(); if no explicit signature is passed, fall back to
computeCallableSignature() as before (computeCallableSignature() and
getCacheKeys() are the functions to change). Add a unit test that registers two
same-line closures with different captures but different explicit signatures and
assert their cache keys differ to prevent collisions.
🧹 Nitpick comments (1)
tests/unit/CacheKeyTest.php (1)

77-103: Test may not behave as expected with identical closures.

Both $noopA and $noopB have identical code bodies. If signature generation is based on the closure's serialized form or code representation, these could produce the same signature, causing the assertion to fail unexpectedly.

Consider using meaningfully different closures to ensure distinct signatures:

Proposed fix
         $noopA = function (mixed $value) {
             return $value;
         };
-        $noopB = function (mixed $value) {
-            return $value;
-        };
+        $noopB = function (mixed $value) {
+            return \strval($value); // Different implementation
+        };

Alternatively, if the Database allows explicit signatures, provide different signature strings directly.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/unit/CacheKeyTest.php` around lines 77 - 103, The test
testDifferentInstanceFilterCallablesProduceDifferentCacheKeys uses two closures
($noopA and $noopB) with identical bodies which can yield identical signatures;
update the test so the two filter callables are distinguishable—either make
$noopA and $noopB have different behavior or metadata (e.g., different parameter
handling or returned values) or pass explicit distinct signature strings into
createDatabase for the 'myFilter' encode/decode entries—then keep asserting that
getHashKey($dbA) !== getHashKey($dbB).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@tests/unit/CacheKeyTest.php`:
- Around line 86-97: The instance filters for $dbA and $dbB (created via
createDatabase) are missing the required "signature" field for the "myFilter"
entries; update the filter definitions used in createDatabase so each "myFilter"
includes a signature property (or adjust/createDatabase to
auto-generate/validate signatures) to match the other tests that expect an
explicit signature for encode/decode filters.
- Around line 13-21: Add a proper PHPDoc type for the $instanceFilters parameter
in the createDatabase method to satisfy PHPStan: document it as an array of
associative arrays with keys 'encode', 'decode', and 'signature' (e.g.
array<int, array{encode:callable, decode:callable, signature:string}>), so tools
know the shape expected by Database::getCacheKeys(); update the PHPDoc above
createDatabase to use that type and keep the method signature unchanged.

---

Outside diff comments:
In `@src/Database/Database.php`:
- Around line 1144-1148: The instance filter signatures can collide because
computeCallableSignature reduces closures to file:startLine; update the instance
filter registration to accept an optional explicit signature and use it when
storing into the instanceFilters map (reference: instanceFilters property and
getInstanceFilters()), and update getCacheKeys() to prefer that provided
signature over computeCallableSignature(); if no explicit signature is passed,
fall back to computeCallableSignature() as before (computeCallableSignature()
and getCacheKeys() are the functions to change). Add a unit test that registers
two same-line closures with different captures but different explicit signatures
and assert their cache keys differ to prevent collisions.

---

Nitpick comments:
In `@tests/unit/CacheKeyTest.php`:
- Around line 77-103: The test
testDifferentInstanceFilterCallablesProduceDifferentCacheKeys uses two closures
($noopA and $noopB) with identical bodies which can yield identical signatures;
update the test so the two filter callables are distinguishable—either make
$noopA and $noopB have different behavior or metadata (e.g., different parameter
handling or returned values) or pass explicit distinct signature strings into
createDatabase for the 'myFilter' encode/decode entries—then keep asserting that
getHashKey($dbA) !== getHashKey($dbB).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 141684cf-8d25-492c-b67a-5d860a3b16ac

📥 Commits

Reviewing files that changed from the base of the PR and between 489e3ce and 68ce6c4.

📒 Files selected for processing (2)
  • src/Database/Database.php
  • tests/unit/CacheKeyTest.php

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@abnegate abnegate merged commit 91c9965 into main Mar 6, 2026
18 checks passed
@abnegate abnegate deleted the fix-cache branch March 6, 2026 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant