Skip to content

Commit 86bea89

Browse files
committed
Add testable CustomSignable and CustomSigned
1 parent 1b604d9 commit 86bea89

File tree

5 files changed

+27
-13
lines changed

5 files changed

+27
-13
lines changed

bin/generate_CustomSignable.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313

1414
$privateKey = PEMCertificatesMock::getPrivateKey(XMLSecurityKey::RSA_SHA256, PEMCertificatesMock::SELFSIGNED_PRIVATE_KEY);
1515
$x = $signable->sign($privateKey);
16-
echo $x->ownerDocument->saveXML();
16+
echo $x;
17+
//var_dump($x);

src/XML/AbstractSignedXMLElement.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,25 @@ abstract class AbstractSignedXMLElement implements SignedElementInterface
3838
* @param \SimpleSAML\XMLSecurity\XML\SignableElementInterface $elt
3939
* @param \SimpleSAML\XMLSecurity\XML\ds\Signature $signature
4040
*/
41-
private function __construct(DOMElement $xml, SignableElementInterface $elt, Signature $signature)
41+
protected function __construct(DOMElement $xml, SignableElementInterface $elt, Signature $signature)
4242
{
4343
$this->setStructure($xml);
4444
$this->setElement($elt);
4545
$this->setSignature($signature);
4646
}
4747

4848

49+
/**
50+
* Output the class as an XML-formatted string
51+
*
52+
* @return string
53+
*/
54+
public function __toString(): string
55+
{
56+
return $this->structure->ownerDocument->saveXML();
57+
}
58+
59+
4960
/**
5061
* Collect the value of the unsigned element
5162
*

src/XML/SignableElementTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
use DOMElement;
88
use DOMNode;
99
use SimpleSAML\Assert\Assert;
10+
use SimpleSAML\XML\Utils as XMLUtils;
1011
use SimpleSAML\XMLSecurity\Utils\Security as XMLSecurityUtils;
1112
use SimpleSAML\XMLSecurity\XML\ds\Signature;
1213
use SimpleSAML\XMLSecurity\XMLSecurityKey;
13-
use SimpleSAML\XML\Utils as XMLUtils;
1414

1515
/**
1616
* Helper trait for processing signed elements.

tests/XML/CustomSignable.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
namespace SimpleSAML\XMLSecurity\Test\XML;
66

77
use DOMElement;
8+
use SimpleSAML\Assert\Assert;
89
use SimpleSAML\XML\AbstractXMLElement;
910
use SimpleSAML\XML\Chunk;
1011
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12+
use SimpleSAML\XML\Exception\MissingElementException;
1113
use SimpleSAML\XML\Exception\TooManyElementsException;
1214
use SimpleSAML\XMLSecurity\XML\SignableElementInterface;
1315
use SimpleSAML\XMLSecurity\XML\SignableElementTrait;
@@ -110,7 +112,8 @@ public static function fromXML(DOMElement $xml): object
110112
Assert::same($xml->localName, 'CustomSignable', InvalidDOMElementException::class);
111113
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
112114

113-
Assert::count($xml->childNodes, 1, TooManyElementsException);
115+
Assert::minCount($xml->childNodes, 1, MissingElementException::class);
116+
Assert::maxCount($xml->childNodes, 2, TooManyElementsException::class);
114117
$element = new Chunk($xml->childNodes[0]);
115118

116119
return new self($element);

tests/XML/CustomSigned.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use DOMElement;
88
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\XML\Chunk;
10+
use SimpleSAML\XML\Exception\MissingElementException;
911
use SimpleSAML\XML\Exception\TooManyElementsException;
1012
use SimpleSAML\XML\Utils as XMLUtils;
1113
use SimpleSAML\XMLSecurity\XML\ds\Signature;
@@ -29,20 +31,17 @@ final class CustomSigned extends AbstractSignedXMLElement
2931
*/
3032
public static function fromXML(DOMElement $xml): object
3133
{
32-
// Empty array
33-
$s = XMLUtils::xpQuery($xml, './ssp:CustomSignable/ds:Signature');
34-
var_dump($s);
35-
36-
// Also empty array
34+
Assert::same($xml->localName, 'CustomSignable', InvalidDOMElementException::class);
35+
Assert::same($xml->namespaceURI, CustomSignable::NS, InvalidDOMElementException::class);
36+
$element = CustomSignable::fromXML($xml);
3737
$signature = Signature::getChildrenOfClass($xml);
38-
Assert::count($signature, 1, TooManyElementsException::class);
3938

40-
$element = CustomSignable::getChildrenOfClass($xml);
41-
Assert::count($element, 1, TooManyElementsException::class);
39+
Assert::minCount($signature, 1, MissingElementException::class);
40+
Assert::minCount($signature, 1, TooManyElementsException::class);
4241

4342
return new self(
4443
$xml,
45-
array_pop($element),
44+
$element,
4645
array_pop($signature)
4746
);
4847
}

0 commit comments

Comments
 (0)