Skip to content

Commit 070843e

Browse files
minor symfony#61601 [DependencyInjection] Add test case to ensure XML parse exception message includes filename and position (xersion22)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [DependencyInjection] Add test case to ensure XML parse exception message includes filename and position | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | Fix symfony#61572 | License | MIT ## Summary This PR adds a dedicated test to `XmlFileLoaderTest` to ensure that XML parse exceptions include the filename, error details, and position (line and column) in their messages. ## What it does and why it's needed - Ensures error messages from XML parse exceptions always contain: - The XML filename (e.g., `services31.xml`) - The error details (e.g., "This element is not expected") - The position (line X, column Y) - Prevents regressions by automatically checking this behavior in tests - Improves consistency and debuggability of XML parsing errors ## Example When an invalid XML file (such as `services31.xml`) is loaded, the exception message will now contain: - The filename: `services31.xml` - The error detail: "This element is not expected" - The position: `line X, column Y` Commits ------- d6fdc90 [DependencyInjection] Add test case to ensure XML parse exception message includes filename and position
2 parents a57c946 + d6fdc90 commit 070843e

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
3+
<services>
4+
<service id="invalid_service" class="App\Foo">
5+
<bogusTag />
6+
</service>
7+
</services>
8+
</container>

src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,4 +1286,20 @@ public function testLoadServicesWithEnvironment()
12861286

12871287
self::assertInstanceOf(RemoteCallerSocket::class, $container->get(RemoteCaller::class));
12881288
}
1289+
1290+
public function testXmlParseExceptionIncludesFilenameAndPosition()
1291+
{
1292+
$container = new ContainerBuilder();
1293+
$loader = new XmlFileLoader(
1294+
$container,
1295+
new FileLocator(__DIR__.'/../Fixtures/xml')
1296+
);
1297+
1298+
$invalidXMLFileName = 'services31.xml';
1299+
1300+
$this->expectException(InvalidArgumentException::class);
1301+
$this->expectExceptionMessageMatches('/Unable to parse file .*services31\.xml.*bogusTag.*This element is not expected.*line 5, column 0/');
1302+
1303+
$loader->load($invalidXMLFileName);
1304+
}
12891305
}

0 commit comments

Comments
 (0)