XML requests and responses
Raw XML
matchXmlBody()
accepts XML text, a resource, a PSR-7 stream, or a DOMDocument
:
$pock->matchMethod(RequestMethod::POST)
->matchUri('https://api.example.com/users')
->matchXmlBody('<user><name>Jane</name><active>true</active></user>')
->reply(201)
->withHeader('Content-Type', 'application/xml')
->withXml('<user><id>42</id><name>Jane</name></user>');
When the DOM and XSL extensions are available, pock parses the XML and sorts nodes and attributes before comparison. Formatting and node order therefore do not cause a mismatch. Without that support, pock falls back to exact text comparison, so whitespace and ordering matter.
Malformed or empty parsed XML throws Pock\Exception\XmlException
.
DTO serialization
matchSerializedXmlBody()
serializes arrays or objects before passing the result to the XML matcher. withXml()
uses the same serializer discovery for responses:
$pock->matchSerializedXmlBody(new CreateUser('Jane'))
->reply(201)
->withXml(new User(42, 'Jane'));
JMS Serializer is preferred when both supported serializers are installed. Symfony Serializer requires its XML encoder and object normalizer. To select configured instances explicitly:
use Pock\Factory\XmlSerializerFactory;
use Pock\Serializer\SymfonySerializerAdapter;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
$serializer = new Serializer([new ObjectNormalizer()], [new XmlEncoder()]);
XmlSerializerFactory::setSerializer(new SymfonySerializerAdapter($serializer, 'xml'));
If no XML serializer can be discovered, serializer-aware object and array calls throw XmlException
. Raw strings do not require a serializer.