Skip to content

Commit 2a6f6a7

Browse files
Phillip Looktemp
authored andcommitted
feat(mock-request): Support xml content
1 parent 68c1f56 commit 2a6f6a7

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/HttpClientMock/MockRequestBuilder.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Brainbits\FunctionalTestHelpers\HttpClientMock\Exception\NoUriConfigured;
88
use Brainbits\FunctionalTestHelpers\HttpClientMock\Exception\ResponseAlreadyConfigured;
99
use Safe\Exceptions\JsonException;
10+
use Safe\Exceptions\SimplexmlException;
11+
use SimpleXMLElement;
1012
use Symfony\Component\HttpFoundation\File\File;
1113
use Throwable;
1214

@@ -18,6 +20,7 @@
1820
use function Safe\json_decode;
1921
use function Safe\json_encode;
2022
use function Safe\preg_match;
23+
use function Safe\simplexml_load_string;
2124
use function Safe\sprintf;
2225
use function Safe\substr;
2326
use function str_repeat;
@@ -153,6 +156,21 @@ public function isJson(): bool
153156
return true;
154157
}
155158

159+
public function isXml(): bool
160+
{
161+
if (!$this->hasContent()) {
162+
return false;
163+
}
164+
165+
try {
166+
simplexml_load_string($this->content);
167+
} catch (SimplexmlException $e) {
168+
return false;
169+
}
170+
171+
return true;
172+
}
173+
156174
/**
157175
* @return mixed[]
158176
*/
@@ -165,6 +183,24 @@ public function getJson(): ?array
165183
return json_decode($this->content, true);
166184
}
167185

186+
/**
187+
* @param array<string, string> $namespaces
188+
*/
189+
public function getXml(array $namespaces = []): ?SimpleXMLElement
190+
{
191+
if (!$this->isXml()) {
192+
return null;
193+
}
194+
195+
$xml = simplexml_load_string($this->content);
196+
197+
foreach ($namespaces as $prefix => $namespace) {
198+
$xml->registerXPathNamespace($prefix, $namespace);
199+
}
200+
201+
return $xml;
202+
}
203+
168204
public function queryParam(string $key, string $value, string ...$placeholders): self
169205
{
170206
$this->queryParams ??= [];

tests/HttpClientMock/MockRequestBuilderTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,32 @@ public function testResponseBuilderIsResettable(): void
135135

136136
self::assertFalse($mockRequestBuilder->hasResponseBuilder());
137137
}
138+
139+
public function testXmlStringsAreDetectedAsXml(): void
140+
{
141+
$mockRequestBuilder = new MockRequestBuilder();
142+
$mockRequestBuilder->content('<root><first>abc</first></root>');
143+
144+
self::assertTrue($mockRequestBuilder->isXml());
145+
self::assertFalse($mockRequestBuilder->isEmpty());
146+
self::assertFalse($mockRequestBuilder->isJson());
147+
}
148+
149+
public function testXmlStringsAreAccessibleAsSimpleXmlObjects(): void
150+
{
151+
$mockRequestBuilder = new MockRequestBuilder();
152+
$mockRequestBuilder->content('<root><first>abc</first></root>');
153+
154+
self::assertSame('abc', (string) $mockRequestBuilder->getXml()->first);
155+
}
156+
157+
public function testXmlStringsWithNamespacesAreAccessibleAsSimpleXmlObjects(): void
158+
{
159+
$mockRequestBuilder = new MockRequestBuilder();
160+
$mockRequestBuilder->content('<root xmlns="http://example.org/xml"><first>abc</first></root>');
161+
162+
$xml = $mockRequestBuilder->getXml(['x' => 'http://example.org/xml']);
163+
164+
self::assertSame('abc', (string) $xml->xpath('//x:first')[0]);
165+
}
138166
}

0 commit comments

Comments
 (0)