Skip to content

test: add suite for sweeper tasks (+ fix flushSnapshots static-call crash) - #4

Open
erikfrerejean wants to merge 9 commits into
mainfrom
feature/src-tests
Open

test: add suite for sweeper tasks (+ fix flushSnapshots static-call crash)#4
erikfrerejean wants to merge 9 commits into
mainfrom
feature/src-tests

Conversation

@erikfrerejean

Copy link
Copy Markdown
Member

What

First test suite for the sweeper module's two loadable BuildTasks, plus a versioned-snapshots testbed profile so the optional snapshot path is covered. The module previously had zero tests — a real risk given its production use against live databases and _Versions history.

Coverage

  • SweeperReportTask — no-instance DataObject detection, the SilverStripe / namespace filters, and never-applied DataExtension detection (incl. a 5-case filter matrix).
  • SweeperClearArchiveTask — version-retention math across the draft / archived / orphaned / subclass-orphaned paths, dry-run-vs-real for every mutating method, getKeepVersions() config precedence, flushClass fast/non-fast, and getBaseVersionedClasses() filtering.
  • flushSnapshots() (snapshots profile) — dry/real retention and the per-object exception-swallowing path.

All integration tests (SapphireTest) with programmatic test-only DataObjects — no YAML fixtures, since the versioned/deleted/orphaned states aren't expressible statically. PHPUnit 9.6 (annotations, not attributes), matching the testbed.

Results: default suite 30 tests / 49 assertions; snapshots profile 4 tests / 10 assertions.

⚠️ Production bug fix (one line in src/)

The new tests surfaced that SweeperClearArchiveTask::flushSnapshots() never worked on SS5 / PHP 8: line 141 called the instance method Snapshot::hashObjectForSnapshot() statically, a fatal Error on the first object processed. Fixed minimally:

- $objectHash = (...::SNAPSHOT_CLASS)::hashObjectForSnapshot($object);
+ $objectHash = (...::SNAPSHOT_CLASS)::singleton()->hashObjectForSnapshot($object);

This is the only src/ change. The separate SweeperArtefacts namespace bug is deliberately left for a follow-up.

Testbed

  • New snapshots Docker compose profile (mirrors the grid module's Fluent profile): composer.snapshots.json, app-snapshots service with its own vendor-snapshots volume + silverstripe_snapshots DB, a snapshots PHPUnit suite, and task test-snapshots. The default integration suite excludes the snapshot-only tests so they never load SnapshotEvent where the module isn't installed.
  • The Taskfile now clears the SilverStripe manifest cache before each phpunit run — the entrypoint's dev/build caches a test-excluding manifest, so without this the first run on a fresh container fails to apply test-only classes' config (missing _Versions tables, unapplied extensions).

Notes

  • Commits are unsigned (built in an environment without the signing key) — re-sign on merge if required.
  • getDraftObjects()'s >100-record batching boundary is exercised single-batch only (provoking it needs 100+ records × multiple versions — a deliberate cost trade-off).

- Capture and assert output in testDeleteOrphanedVersionsDryReportsCountAndKeepsRows
- Add assertGreaterThanOrEqual(3) guard in testDeleteArchivedVersionsDryReportsCountAndKeepsRows
- Add clarifying comment in testFlushClassFastSkipsDeleteOldVersions about retention pass iterating all RecordIDs
The container entrypoint runs sake dev/build without test classes,
caching a manifest that excludes test-only class config (e.g.
ReportPopulatedObject::$extensions, VersionedRecord::$extensions).
PHPUnit reused that stale cache, causing testExcludesAppliedDataExtension
to fail and ~10 missing _Versions table errors.

Fix: rm -rf /tmp/silverstripe-cache-* before every phpunit invocation
(test-unit, test-integration, test-snapshots, coverage) so the test
kernel rebuilds the manifest with test classes included.
…uisite assertions

Add `assertSame(DataObject::class, get_parent_class(SnapshotEvent::class))` and
`assertTrue(DataObject::has_extension(SnapshotEvent::class, Versioned::class))` before
the `assertNotContains`, so the test proves the exclusion guard actually fired rather
than passing vacuously if SnapshotEvent were never a candidate.

Also add `deps: [docker-env]` to `ensure-up-snapshots` task in Taskfile.yml, matching
the pattern already used by `ensure-up`.
…or handling

fix: use Snapshot::singleton() for hashObjectForSnapshot() — the trait
method is non-static and calling it statically throws in PHP 8.3.
Add assertStringContainsString('Cleared', \$output) to
testFlushSnapshotsDryReportsCountsAndDeletesNothing so the test
proves the per-object retention loop executed — not just that the
opening (dry-run) prefix line was printed.
Add 'namespace excludes all' data-provider case to testExtensionFilterMatrix
so the str_contains exclusion arm is exercised (namespace-filter matching
nothing, excluding NeverAppliedExtension and all SilverStripe\ classes).
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