Skip to content

Test the encoding error of too deeply nested XML - #595

Merged
Art4 merged 2 commits into
v3.xfrom
591-coverage-xml-serializer
Nov 26, 2026
Merged

Art4 merged 2 commits into
v3.xfrom
591-coverage-xml-serializer

Conversation

@Art4

@Art4 Art4 commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

 tests/Unit/Serializer/XmlSerializerTest.php
+└── testCreateFromStringWithTooDeeplyNestedXmlThrowsException

Tests only, no change in src/. Part 3 of 3 of #591, line coverage is 100 % with it. No changelog entry.

The ticket asked whether the JsonException in XmlSerializer::normalize() can be triggered. It can, so the catch stays:

createFromString(xml with 255 nested levels below the root, two same-named elements per level)
  libxml parses it            # within the limit of libxml 2.9 and 2.15
  json_encode(element)        # each level adds an object and a list, two comments in the leaf add three more
    depth > 512  ->  JsonException  ->  SerializerException

Closes #591

Evidence

  • Before: uncovered lines XmlSerializer.php 107, 108.
    After: Classes: 100.00% (31/31), Methods: 100.00% (199/199), Lines: 100.00% (1336/1336).
  • QA on PHP 8.2: PHPUnit OK (869 tests, 4402 assertions), PHPStan [OK] No errors, code style 0 files to fix, Rector dry run exit 0. Behat not run locally, no production code changed.

Merge Danger

Door: two-way

Blast Radius: tests only. The new test sits in a narrow window between two limits: libxml 2.15 parses at most 255 levels below the root, and json_encode() only fails above depth 512, which this input exceeds by one. A libxml version with a lower limit, or a change in how SimpleXML encodes comments, would fail the test. It was run with libxml 2.9.14 and 2.15.4.

Summary by CodeRabbit

  • Tests
    • Added coverage for deeply nested XML input that exceeds the parser’s stack-depth limit, verifying that it produces a clear serialization error. No user-facing behavior changes are included in this update.

@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change adds a unit test that checks whether XmlSerializer::createFromString() throws a SerializerException for deeply nested XML.

Changes: XML serializer depth-error test

Layer / File(s) Summary
Test deeply nested XML
tests/Unit/Serializer/XmlSerializerTest.php
The test wraps repeated <item> elements around an XML fragment and checks for the “Maximum stack depth exceeded” error.

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

Change: Other

Merge Risk: Minimal · up to 8bbde

This PR adds a unit test for the serializer's deep-nesting error path and changes no production code. It is safe to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the change: a new test that checks the encoding error for too deeply nested XML. The raw summary says the PR adds a test that asserts SerializerException with the maximum-stack-depth e…
Linked Issues check Passed Issue #591 part 3 asks whether the JsonException in XmlSerializer::normalize() can be triggered. If yes, add a test in the existing method test file. The diff adds testCreateFromStringWithTooDeeplyNes…
Out of Scope Changes check Passed The whole-PR diff (merge base 9de45b7 to head 8bbde63) changes only tests/Unit/Serializer/XmlSerializerTest.php. It adds one test method of 18 lines. The test covers the JsonException catch path in Xm…
Full details: Title check

Explanation

The title names the change: a new test that checks the encoding error for too deeply nested XML. The raw summary says the PR adds a test that asserts SerializerException with the maximum-stack-depth encoding error, and the objectives confirm it tests the JsonException path in XmlSerializer::normalize(). The title is specific and matches the main change. It does not mention the coverage goal, which is acceptable.

Full details: Linked Issues check

Explanation

Issue #591 part 3 asks whether the JsonException in XmlSerializer::normalize() can be triggered. If yes, add a test in the existing method test file. The diff adds testCreateFromStringWithTooDeeplyNestedXmlThrowsException to tests/Unit/Serializer/XmlSerializerTest.php. The test builds XML with 255 levels below the root and two same-named elements per level, plus two comments in the leaf. Its comments explain that json_encode() depth exceeds 512 for this input. It expects SerializerException with the message 'Catched error "Maximum stack depth exceeded" while encoding SimpleXMLElement', which matches the wrapped JsonException path. The PR states the test passed on libxml 2.9.14 and 2.15.4, and reports PHPUnit OK. The reviewer did not execute the test, so the triggerability claim rests on the author's reported run and the code path in the test comments. No production change was made, which matches the issue rule that production changes are allowed only to remove unreachable code. The test is the requested outcome.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff (merge base 9de45b7 to head 8bbde63) changes only tests/Unit/Serializer/XmlSerializerTest.php. It adds one test method of 18 lines. The test covers the JsonException catch path in XmlSerializer::normalize() that issue #591 names. No production code, configuration, coverage thresholds, or other test files changed. The PR does not include the other coverage gaps listed in #591, which the issue assigns to separate PRs. No unrelated change was found.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e4d33caf-e060-4c32-98f7-b9b077211a43
Commits

Reviewing files that changed from the base of the PR and between 9de45b7 and 8bbde63.

Files selected for processing: 1

  • tests/Unit/Serializer/XmlSerializerTest.php

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 0 remain after this review.

@codecov

codecov Bot commented Nov 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (9de45b7) to head (8bbde63).

Additional details and impacted files
@@              Coverage Diff              @@
##               v3.x      #595      +/-   ##
=============================================
+ Coverage     99.86%   100.00%   +0.13%     
  Complexity      456       456              
=============================================
  Files            31        31              
  Lines          1505      1505              
=============================================
+ Hits           1503      1505       +2     
+ Misses            2         0       -2     

☔ 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.

@Art4
Art4 marked this pull request as ready for review November 26, 2026 20:48
@Art4
Art4 merged commit 749292d into v3.x Nov 26, 2026
19 checks passed
@Art4
Art4 deleted the 591-coverage-xml-serializer branch November 26, 2026 20:55
@Art4 Art4 added this to the v3.1.0 milestone Nov 26, 2026
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.

Reach 100% code coverage

1 participant