Skip to content

test: add unit tests for the Elasticsearch filters - #36

Merged
turegjorup merged 3 commits into
developfrom
test/filter-unit-tests
Jul 8, 2026
Merged

test: add unit tests for the Elasticsearch filters#36
turegjorup merged 3 commits into
developfrom
test/filter-unit-tests

Conversation

@turegjorup

Copy link
Copy Markdown
Contributor

Add fast unit coverage for the custom Elasticsearch filters (src/Api/Filter/ElasticSearch/*). They currently have no unit tests — only behavioural (ES-backed) coverage that asserts result counts, which can't pin the exact query DSL a filter emits.

Approach

Each filter extends API Platform's AbstractFilter, whose constructor needs three metadata factories. When a filter is configured with an explicit properties map (as every resource does), AbstractFilter::getProperties() yields array_keys($properties) and never touches the factories — so the tests construct filters with inert stubs and assert apply()/getDescription() directly. No kernel, no Elasticsearch: 37 tests run in ~20ms.

Coverage

  • Match / Boolean / Tag / Id — exact match/terms clause shape, boost placement, comma-splitting, unset/empty skipping, single-vs-list return. "0" is asserted to reach ES (guards the strict-guard behaviour from the earlier empty() removal).
  • DateRange — fallback and explicit operators → range DSL; betweenexclusive gt/lt (a consumer-visible semantic); every getDescription() [operator] variant.
  • Pinned current behaviour (not fixed here): malformed between → uncaught \InvalidArgumentException; unknown operator ([foo]) → native \Error — two distinct paths that leak as HTTP 500 today; and the throwOnInvalid config flag is demonstrably dead.

Verification

bin/phpunit tests/Unit37 tests, 49 assertions, no notices (stubs, not mocks, so PHPUnit 13's no-expectations notice stays clean). php-cs-fixer clean; full suite 201 tests / 492 assertions green. Every test method carries a short context/goal comment.

Tests only — no product code changed.

Cover the custom filters' apply() query DSL and getDescription() descriptors
directly, without Elasticsearch or the kernel — the filters are constructed
with inert stubs for the AbstractFilter metadata factories (never called once
`properties` is set).

- MatchFilter/BooleanFilter/TagFilter/IdFilter: exact term/match clauses, boost
  shape, comma-splitting, single-vs-list return; "0" reaches ES (guarding the
  strict-guard behaviour).
- DateRangeFilter: fallback/explicit operators, `between` → exclusive gt/lt, and
  the two current error leaks (malformed `between` → \InvalidArgumentException,
  unknown operator → native \Error) plus the dead `throwOnInvalid` flag, pinned
  as current behaviour.
@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown

No changes detected in API specification

@turegjorup turegjorup self-assigned this Jul 8, 2026
@turegjorup
turegjorup merged commit c756231 into develop Jul 8, 2026
12 checks passed
@turegjorup
turegjorup deleted the test/filter-unit-tests branch July 8, 2026 08:22
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