@@ -4000,12 +4000,50 @@ public function asUsertheXMLResponseShouldContainMountpointWithTheseKeyAndValueP
40004000 }
40014001
40024002 /**
4003- * @param string $resource // can be resource name, space id or file id
4004- * @param array $properties // ["key" => "value"]
4003+ * @param SimpleXMLElement $responseXmlObject
4004+ * @param array $xpaths
4005+ * @param string $message
4006+ *
4007+ * @return string
4008+ */
4009+ public function buildXpathErrorMessage (SimpleXMLElement $ responseXmlObject , array $ xpaths , string $ message ): string {
4010+ return "Using xpaths: \n\t- " . \join ("\n\t- " , $ xpaths )
4011+ . "\n"
4012+ . $ message
4013+ . "\n\t"
4014+ . "' " . \trim ($ responseXmlObject ->asXML ()) . "' " ;
4015+ }
4016+
4017+ /**
4018+ * @param SimpleXMLElement $responseXmlObject
4019+ * @param string $siblingXpath
4020+ * @param string $siblingToFind
4021+ *
4022+ * @return string
4023+ * @throws Exception
4024+ */
4025+ public function getXpathSiblingValue (SimpleXMLElement $ responseXmlObject , string $ siblingXpath , string $ siblingToFind ): string {
4026+ $ xpaths [] = $ siblingXpath . "/preceding-sibling:: $ siblingToFind " ;
4027+ $ xpaths [] = $ siblingXpath . "/following-sibling:: $ siblingToFind " ;
4028+
4029+ foreach ($ xpaths as $ key => $ xpath ) {
4030+ $ foundSibling = $ responseXmlObject ->xpath ($ xpath );
4031+ if (\count ($ foundSibling )) {
4032+ break ;
4033+ }
4034+ }
4035+ $ errorMessage = $ this ->buildXpathErrorMessage ($ responseXmlObject , $ xpaths , "Could not find sibling '< $ siblingToFind>' element in the XML response " );
4036+ Assert::assertNotEmpty ($ foundSibling , $ errorMessage );
4037+ return \preg_quote ($ foundSibling [0 ]->__toString (), "/ " );
4038+ }
4039+
4040+ /**
4041+ * @param string $resource // can be resource name, space id or file id
4042+ * @param array $properties // ["key" => "value"]
40054043 *
40064044 * @return void
40074045 * @throws GuzzleException
4008- * @throws JsonException
4046+ * @throws Exception
40094047 */
40104048 public function theXMLResponseShouldContain (string $ resource , array $ properties ): void {
40114049 $ responseXmlObject = HttpRequestHelper::getResponseXml ($ this ->featureContext ->getResponse (), __METHOD__ );
@@ -4056,17 +4094,39 @@ public function theXMLResponseShouldContain(string $resource, array $properties)
40564094 $ xpaths [] = $ xpath ;
40574095 }
40584096
4097+ Assert::assertCount (
4098+ 1 ,
4099+ $ foundXmlItem ,
4100+ $ this ->buildXpathErrorMessage ($ responseXmlObject , $ xpaths , "Found multiple elements for '< $ itemToFind>' in the XML response " )
4101+ );
40594102 Assert::assertNotEmpty (
40604103 $ foundXmlItem ,
4061- // all these for the sake of a nice error message
4062- "Using xpaths: \n\t- " . \join ("\n\t- " , $ xpaths )
4063- . "\n"
4064- . "Could not find '< $ itemToFind>' element in the XML response \n\t"
4065- . "' " . \trim ($ responseXmlObject ->asXML ()) . "' "
4104+ $ this ->buildXpathErrorMessage ($ responseXmlObject , $ xpaths , "Could not find '< $ itemToFind>' element in the XML response " )
40664105 );
40674106
40684107 $ actualValue = $ foundXmlItem [0 ]->__toString ();
4069- $ expectedValue = $ this ->featureContext ->substituteInLineCodes ($ property ['value ' ]);
4108+ $ expectedValue = $ property ['value ' ];
4109+ \preg_match_all ("/%self::[a-z0-9-:]+?%/ " , $ expectedValue , $ selfMatches );
4110+ $ substituteFunctions = [];
4111+ if (!empty ($ selfMatches [0 ])) {
4112+ $ siblingXpath = $ xpaths [\count ($ xpaths ) - 1 ];
4113+ foreach ($ selfMatches [0 ] as $ match ) {
4114+ $ siblingToFind = \ltrim ($ match , "/%self::/ " );
4115+ $ siblingToFind = \rtrim ($ siblingToFind , "/%/ " );
4116+ $ substituteFunctions [] = [
4117+ "code " => $ match ,
4118+ "function " =>
4119+ [$ this , "getXpathSiblingValue " ],
4120+ "parameter " => [$ responseXmlObject , $ siblingXpath , $ siblingToFind ]
4121+ ];
4122+ }
4123+ }
4124+ $ expectedValue = $ this ->featureContext ->substituteInLineCodes (
4125+ $ property ['value ' ],
4126+ null ,
4127+ [],
4128+ $ substituteFunctions ,
4129+ );
40704130
40714131 switch ($ itemToFind ) {
40724132 case "oc:fileid " :
0 commit comments