Skip to content

Commit

Permalink
Refactor unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 20, 2025
1 parent 74ce7e7 commit a763d89
Show file tree
Hide file tree
Showing 102 changed files with 1,639 additions and 786 deletions.
27 changes: 26 additions & 1 deletion src/Type/AbstractValueType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace SimpleSAML\XML\Type;

use function preg_replace;
use function trim;

/**
* Abstract class to be implemented by all types
*
Expand Down Expand Up @@ -69,7 +72,7 @@ protected function sanitizeValue(string $value): string
* @throws \Exception on failure
* @return void
*/
protected function validateValue(/** @scrutinizer ignore-unused */ string $value): void
protected function validateValue(/** @scrutinizer-ignore */string $value): void
{
/**
* Perform no validation by default.
Expand All @@ -78,6 +81,28 @@ protected function validateValue(/** @scrutinizer ignore-unused */ string $value
}


/**
* Normalize whitespace in the value
*
* @return string
*/
protected static function normalizeWhitespace(string $value): string
{
return preg_replace('/\s/', ' ', $value);
}


/**
* Collapse whitespace
*
* @return string
*/
protected static function collapseWhitespace(string $value): string
{
return trim(preg_replace('/\s+/', ' ', $value));
}


/**
* @param string $value
* @return static
Expand Down
12 changes: 12 additions & 0 deletions src/Type/AnyURIValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
*/
class AnyURIValue extends AbstractValueType
{
/**
* Sanitize the value.
*
* @param string $value The unsanitized value
* @return string
*/
protected function sanitizeValue(string $value): string
{
return static::collapseWhitespace(static::normalizeWhitespace($value));
}


/**
* Validate the value.
*
Expand Down
6 changes: 2 additions & 4 deletions src/Type/Base64BinaryValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use SimpleSAML\XML\Assert\Assert;
use SimpleSAML\XML\Exception\SchemaViolationException;

use function str_replace;
use preg_replace;

/**
* @package simplesaml/xml-common
Expand All @@ -17,14 +17,12 @@ class Base64BinaryValue extends AbstractValueType
/**
* Sanitize the value.
*
* Note: There are no processing rules for xs:base64 regarding whitespace. General consensus is to strip them
*
* @param string $value The unsanitized value
* @return string
*/
protected function sanitizeValue(string $value): string
{
return str_replace(["\f", "\r", "\n", "\t", "\v", ' '], '', $value);
return preg_replace('/\s/', '', $value);
}


Expand Down
4 changes: 1 addition & 3 deletions src/Type/BooleanValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use SimpleSAML\XML\Assert\Assert;
use SimpleSAML\XML\Exception\SchemaViolationException;

use function str_replace;

/**
* @package simplesaml/xml-common
*/
Expand All @@ -22,7 +20,7 @@ class BooleanValue extends AbstractValueType
*/
protected function sanitizeValue(string $value): string
{
return str_replace(["\f", "\r", "\n", "\t", "\v", ' '], '', $value);
return static::collapseWhitespace(static::normalizeWhitespace($value));
}


Expand Down
5 changes: 1 addition & 4 deletions src/Type/DateTimeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
use SimpleSAML\XML\Assert\Assert;
use SimpleSAML\XML\Exception\SchemaViolationException;

use function preg_replace;
use function trim;

/**
* @package simplesaml/xml-common
*/
Expand All @@ -27,7 +24,7 @@ class DateTimeValue extends AbstractValueType
*/
protected function sanitizeValue(string $value): string
{
return trim(preg_replace('/\s+/', ' ', $value));
return static::collapseWhitespace(static::normalizeWhitespace($value));
}


Expand Down
5 changes: 1 addition & 4 deletions src/Type/DateValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
use SimpleSAML\XML\Assert\Assert;
use SimpleSAML\XML\Exception\SchemaViolationException;

use function preg_replace;
use function trim;

/**
* @package simplesaml/xml-common
*/
Expand All @@ -23,7 +20,7 @@ class DateValue extends AbstractValueType
*/
protected function sanitizeValue(string $value): string
{
return trim(preg_replace('/\s+/', ' ', $value));
return static::collapseWhitespace(static::normalizeWhitespace($value));
}


Expand Down
5 changes: 1 addition & 4 deletions src/Type/DayValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
use SimpleSAML\XML\Assert\Assert;
use SimpleSAML\XML\Exception\SchemaViolationException;

use function preg_replace;
use function trim;

/**
* @package simplesaml/xml-common
*/
Expand All @@ -23,7 +20,7 @@ class DayValue extends AbstractValueType
*/
protected function sanitizeValue(string $value): string
{
return trim(preg_replace('/\s+/', ' ', $value));
return static::collapseWhitespace(static::normalizeWhitespace($value));
}


Expand Down
2 changes: 1 addition & 1 deletion src/Type/DecimalValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class DecimalValue extends AbstractValueType
*/
protected function sanitizeValue(string $value): string
{
return str_replace(["\f", "\r", "\n", "\t", "\v", ' '], '', $value);
return static::collapseWhitespace(static::normalizeWhitespace($value));
}


Expand Down
2 changes: 1 addition & 1 deletion src/Type/DoubleValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class DoubleValue extends AbstractValueType
*/
protected function sanitizeValue(string $value): string
{
return str_replace(["\f", "\r", "\n", "\t", "\v", ' '], '', $value);
return static::collapseWhitespace(static::normalizeWhitespace($value));
}


Expand Down
5 changes: 1 addition & 4 deletions src/Type/DurationValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
use SimpleSAML\XML\Assert\Assert;
use SimpleSAML\XML\Exception\SchemaViolationException;

use function preg_replace;
use function trim;

/**
* @package simplesaml/xml-common
*/
Expand All @@ -23,7 +20,7 @@ class DurationValue extends AbstractValueType
*/
protected function sanitizeValue(string $value): string
{
return trim(preg_replace('/\s+/', ' ', $value));
return static::collapseWhitespace(static::normalizeWhitespace($value));
}


Expand Down
2 changes: 1 addition & 1 deletion src/Type/FloatValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class FloatValue extends AbstractValueType
*/
protected function sanitizeValue(string $value): string
{
return str_replace(["\f", "\r", "\n", "\t", "\v", ' '], '', $value);
return static::collapseWhitespace(static::normalizeWhitespace($value));
}


Expand Down
6 changes: 1 addition & 5 deletions src/Type/HexBinaryValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use SimpleSAML\XML\Assert\Assert;
use SimpleSAML\XML\Exception\SchemaViolationException;

use function str_replace;

/**
* @package simplesaml/xml-common
*/
Expand All @@ -17,14 +15,12 @@ class HexBinaryValue extends AbstractValueType
/**
* Sanitize the value.
*
* Note: There are no processing rules for xs:hexBinary regarding whitespace. General consensus is to strip them
*
* @param string $value The unsanitized value
* @return string
*/
protected function sanitizeValue(string $value): string
{
return str_replace(["\f", "\r", "\n", "\t", "\v", ' '], '', $value);
return static::collapseWhitespace(static::normalizeWhitespace($value));
}


Expand Down
2 changes: 1 addition & 1 deletion src/Type/LanguageValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class LanguageValue extends TokenValue
*/
protected function validateValue(string $value): void
{
// Note: content must already be sanitized before validating
// Note: value must already be sanitized before validating
Assert::validLanguage($this->sanitizeValue($value), SchemaViolationException::class);
}
}
5 changes: 1 addition & 4 deletions src/Type/MonthValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
use SimpleSAML\XML\Assert\Assert;
use SimpleSAML\XML\Exception\SchemaViolationException;

use function preg_replace;
use function trim;

/**
* @package simplesaml/xml-common
*/
Expand All @@ -23,7 +20,7 @@ class MonthValue extends AbstractValueType
*/
protected function sanitizeValue(string $value): string
{
return trim(preg_replace('/\s+/', ' ', $value));
return static::collapseWhitespace(static::normalizeWhitespace($value));
}


Expand Down
4 changes: 1 addition & 3 deletions src/Type/NormalizedStringValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace SimpleSAML\XML\Type;

use function preg_replace;

/**
* @package simplesaml/xml-common
*/
Expand All @@ -19,6 +17,6 @@ class NormalizedStringValue extends StringValue
*/
protected function sanitizeValue(string $value): string
{
return preg_replace('/\s/', ' ', $value);
return static::normalizeWhitespace($value);
}
}
46 changes: 46 additions & 0 deletions src/Type/QNameValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,25 @@
use SimpleSAML\XML\Assert\Assert;
use SimpleSAML\XML\Exception\SchemaViolationException;

use function explode;

/**
* @package simplesaml/xml-common
*/
class QNameValue extends AbstractValueType
{
/**
* Sanitize the value.
*
* @param string $value The unsanitized value
* @return string
*/
protected function sanitizeValue(string $value): string
{
return static::collapseWhitespace(static::normalizeWhitespace($value));
}


/**
* Validate the value.
*
Expand All @@ -24,4 +38,36 @@ protected function validateValue(string $value): void
// Note: value must already be sanitized before validating
Assert::validQName($this->sanitizeValue($value), SchemaViolationException::class);
}


/**
* Get the namespace-prefix for this qualified name.
*
* @return \SimpleSAML\XML\Type\NCNameValue|null
*/
public function getNamespacePrefix(): ?NCNameValue
{
$qname = explode(':', $this->getValue(), 2);
if (count($qname) === 2) {
return NCNameValue::fromString($qname[0]);
}

return null;
}


/**
* Get the local name for this qualified name.
*
* @return \SimpleSAML\XML\Type\NCNameValue
*/
public function getLocalName(): NCNameValue
{
$qname = explode(':', $this->getValue(), 2);
if (count($qname) === 2) {
return NCNameValue::fromString($qname[1]);
}

return NCNameValue::fromString($qname[0]);
}
}
14 changes: 13 additions & 1 deletion src/Type/TimeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
*/
class TimeValue extends AbstractValueType
{
/**
* Sanitize the value.
*
* @param string $value The unsanitized value
* @return string
*/
protected function sanitizeValue(string $value): string
{
return static::collapseWhitespace(static::normalizeWhitespace($value));
}


/**
* Validate the value.
*
Expand All @@ -21,6 +33,6 @@ class TimeValue extends AbstractValueType
*/
protected function validateValue(string $value): void
{
Assert::validTime($value, SchemaViolationException::class);
Assert::validTime($this->sanitizeValue($value), SchemaViolationException::class);
}
}
5 changes: 1 addition & 4 deletions src/Type/TokenValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace SimpleSAML\XML\Type;

use function preg_replace;
use function trim;

/**
* @package simplesaml/xml-common
*/
Expand All @@ -20,6 +17,6 @@ class TokenValue extends NormalizedStringValue
*/
protected function sanitizeValue(string $value): string
{
return trim(preg_replace('/\s+/', ' ', parent::sanitizeValue($value)));
return static::collapseWhitespace(static::normalizeWhitespace($value));
}
}
Loading

0 comments on commit a763d89

Please sign in to comment.