Skip to content

[#665] Fixed 'XmlTrait' XML-format assertions ignoring fixture content.#667

Merged
AlexSkrypnyk merged 1 commit into
mainfrom
feature/665-xml-format-fixture
Jul 6, 2026
Merged

[#665] Fixed 'XmlTrait' XML-format assertions ignoring fixture content.#667
AlexSkrypnyk merged 1 commit into
mainfrom
feature/665-xml-format-fixture

Conversation

@AlexSkrypnyk

@AlexSkrypnyk AlexSkrypnyk commented Jul 5, 2026

Copy link
Copy Markdown
Member

Closes #665

Summary

XmlTrait provides two fixture steps, Given the response content from the file :filename and Given the response content is the following:, that store content in $xmlTestContent so assertions can run without an HTTP request. Every other assertion in the trait resolves content through xmlEnsureDocument(), which prefers $xmlTestContent and falls back to the live page - except the two format-validity assertions, Then the response should be in XML format and Then the response should not be in XML format, which read $this->getSession()->getPage()->getContent() directly and therefore ignored fixture-set content, validating only the live page (and throwing a Mink DriverException when no page had been visited yet).

Changes

  • xmlAssertResponseIsXml() now delegates to $this->xmlEnsureDocument(), reusing the same fixture-or-page resolution and document cache as every other assertion, while still throwing the existing "Failed to load XML" error on invalid content.
  • xmlAssertResponseIsNotXml() now resolves content as $this->xmlTestContent ?? $this->getSession()->getPage()->getContent(), keeping its "expect invalid" behaviour and the "The response is valid XML, but it should not be." exception.
  • Added 3 scenarios to tests/behat/features/xml.feature that visit a page with one validity, then override it with a fixture holding the other, asserting the fixture wins - covering both assertions via the file fixture plus one via a PyString.
  • Enhanced the @code docblock examples on both methods to show fixture usage, and regenerated STEPS.md via the docs generator.

Screenshots

N/A - internal Behat trait change with no rendered UI.

Before / After

Before: the two format-validity assertions bypass xmlEnsureDocument()
and read the live page directly, so fixture content is invisible to them.

  Given the response content from the file "xml_valid.xml"
  Given the response content is the following: """<xml>...</xml>"""
                          |
                          v
                  $xmlTestContent  ─ ─ ─ ─ ─ ┐
                                              ┊ (ignored by these two)
  ┌────────────────────────────────────┐     ┊
  │ Then the response should be         │     ┊
  │ in / not in XML format              │<╌╌╌╌┘  reads page content directly
  └────────────────────────────────────┘
                          ^
                          |
              getSession()->getPage()->getContent()

  ┌────────────────────────────────────┐
  │ Then the XML element "..." should   │
  │ exist / equal / contain / ...       │──> xmlEnsureDocument()
  └────────────────────────────────────┘        │
                                                 ├─ $xmlTestContent set? use it
                                                 └─ else use page content


After: every assertion, including the two format-validity ones, resolves
content the same way - fixture content wins when a fixture step set it.

  Given the response content from the file "xml_valid.xml"
  Given the response content is the following: """<xml>...</xml>"""
                          |
                          v
                  $xmlTestContent
                          |
      ┌───────────────────┼───────────────────────────┐
      v                   v                            v
┌───────────────┐  ┌───────────────────┐   ┌────────────────────────┐
│ Then the       │  │ Then the response │   │ Then the XML element   │
│ response       │  │ should not be in  │   │ "..." should exist /   │
│ should be in   │  │ XML format        │   │ equal / contain / ...  │
│ XML format     │  │                   │   │                        │
└───────┬────────┘  └─────────┬─────────┘   └───────────┬────────────┘
        v                     v                          v
  xmlEnsureDocument()   $xmlTestContent ??        xmlEnsureDocument()
                        getSession()->getPage()->getContent()

Updated XmlTrait so XML format assertions now honor fixture-provided content first, then fall back to the live page when no fixture content is set. Added Behat coverage for fixture-driven valid/invalid XML checks, clarified the step examples in docblocks, and regenerated STEPS.md.

No step-definition rule violations were identified in the reviewed changes.

@coderabbitai

coderabbitai Bot commented Jul 5, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

XmlTrait's XML format validity assertions now resolve content from fixture-set data ($xmlTestContent) when available, falling back to page content, instead of always reading live page content. New Behat scenarios verify this behavior, and STEPS.md documentation is updated with corresponding examples.

Changes

XmlTrait Fixture Content Resolution

Layer / File(s) Summary
Resolve fixture or page content in validity assertions
src/XmlTrait.php
xmlAssertResponseIsXml() now delegates to xmlEnsureDocument(); xmlAssertResponseIsNotXml() resolves content from $xmlTestContent when set, otherwise falls back to page content.
Scenarios and docs for fixture-based validation
tests/behat/features/xml.feature, STEPS.md
New Behat scenarios verify fixture/PyString content overrides page content for valid and invalid XML checks; STEPS.md documents both examples.

Estimated code review effort: 2 (Simple) | ~10 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Scenario
  participant XmlTrait
  participant Session

  Scenario->>XmlTrait: the response should be in XML format
  XmlTrait->>XmlTrait: xmlEnsureDocument()
  alt xmlTestContent set
    XmlTrait->>XmlTrait: use fixture content
  else
    XmlTrait->>Session: getPage().getContent()
    Session-->>XmlTrait: page content
  end
  XmlTrait-->>Scenario: assertion result

  Scenario->>XmlTrait: the response should not be in XML format
  alt xmlTestContent set
    XmlTrait->>XmlTrait: use fixture content
  else
    XmlTrait->>Session: getPage().getContent()
    Session-->>XmlTrait: page content
  end
  XmlTrait-->>Scenario: assertion result
Loading

Poem

A rabbit hopped through fixture and file,
Found XML content lost in denial —
Now valid or not, the source is the same,
Fixture or page, we honor the name! 🐇📄
Hop, hop, hooray for the fix!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly states the XmlTrait XML-format assertion fix and matches the main change set.
Linked Issues check ✅ Passed The code and Behat updates make XML-format assertions honor fixture-loaded content as requested in #665.
Out of Scope Changes check ✅ Passed All changes support the XmlTrait XML assertion fix; the docs and tests are directly related.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/665-xml-format-fixture

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 markdownlint-cli2 (0.22.1)
STEPS.md

markdownlint-cli2 wrapper config was not available before execution


Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

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

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

149-164: 📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

Duplicated content-resolution and XML-load logic.

Line 153 re-implements the "fixture content first, else page content" fallback that xmlEnsureDocument() (lines 614-631) already encodes, and lines 155-160 duplicate the load/error-check logic from xmlLoadDocument() (584-604). Two independent implementations of the same rule increase the risk of drift if the fallback semantics change later.

Consider extracting a shared helper (e.g. xmlResolveContent(): string) for the fixture-vs-page fallback, and/or a helper that attempts to load XML and returns the resulting \LibXMLError[] without throwing, reused by both xmlLoadDocument() and xmlAssertResponseIsNotXml().

♻️ Suggested refactor
+  /**
+   * Resolve the XML content to validate.
+   *
+   * Prefers content set by fixture steps, falling back to the page content.
+   */
+  protected function xmlResolveContent(): string {
+    return $this->xmlTestContent ?? (string) $this->getSession()->getPage()->getContent();
+  }
+
   #[Then('the response should not be in XML format')]
   public function xmlAssertResponseIsNotXml(): void {
-    // Resolve content the same way as xmlEnsureDocument(): prefer content set
-    // by the fixture steps, falling back to the live page content.
-    $content = $this->xmlTestContent ?? $this->getSession()->getPage()->getContent();
+    $content = $this->xmlResolveContent();
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/XmlTrait.php` around lines 149 - 164, The XML response check duplicates
both the fixture-vs-page content fallback and the XML parsing/error collection
already handled by xmlEnsureDocument() and xmlLoadDocument(), so refactor
xmlAssertResponseIsNotXml() to reuse shared helpers instead of re-implementing
that logic. Extract a common content resolver such as xmlResolveContent() for
the xmlTestContent/getPage()->getContent() fallback, and reuse the existing XML
load path or a non-throwing helper that returns libxml errors so the behavior
stays consistent in XmlTrait.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/XmlTrait.php`:
- Around line 149-164: The XML response check duplicates both the
fixture-vs-page content fallback and the XML parsing/error collection already
handled by xmlEnsureDocument() and xmlLoadDocument(), so refactor
xmlAssertResponseIsNotXml() to reuse shared helpers instead of re-implementing
that logic. Extract a common content resolver such as xmlResolveContent() for
the xmlTestContent/getPage()->getContent() fallback, and reuse the existing XML
load path or a non-throwing helper that returns libxml errors so the behavior
stays consistent in XmlTrait.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 7f1a7ae4-2c92-4467-b664-6f74a862f179

📥 Commits

Reviewing files that changed from the base of the PR and between 296c300 and 02d382d.

📒 Files selected for processing (3)
  • STEPS.md
  • src/XmlTrait.php
  • tests/behat/features/xml.feature

@codecov

codecov Bot commented Jul 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.85%. Comparing base (296c300) to head (02d382d).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #667      +/-   ##
==========================================
- Coverage   96.85%   96.85%   -0.01%     
==========================================
  Files          46       46              
  Lines        3819     3818       -1     
==========================================
- Hits         3699     3698       -1     
  Misses        120      120              

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

@AlexSkrypnyk AlexSkrypnyk added the Needs review Pull request needs a review from assigned developers label Jul 5, 2026
@AlexSkrypnyk AlexSkrypnyk merged commit 5802aec into main Jul 6, 2026
15 checks passed
@AlexSkrypnyk AlexSkrypnyk deleted the feature/665-xml-format-fixture branch July 6, 2026 02:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs review Pull request needs a review from assigned developers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XmlTrait 'in XML format' assertions ignore content set via fixture steps

1 participant