Skip to content

Commit 4222319

Browse files
committed
Add type for xml:base attribute
1 parent 8501b48 commit 4222319

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

src/XML/Type/BaseValue.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Type;
6+
7+
use SimpleSAML\XML\Attribute;
8+
use SimpleSAML\XML\Constants as C;
9+
use SimpleSAML\XMLSchema\Type\Builtin\AnyURIValue;
10+
use SimpleSAML\XMLSchema\Type\Helper\AttributeTypeInterface;
11+
12+
/**
13+
* @package simplesaml/xml-common
14+
*/
15+
class BaseValue extends AnyURIValue implements AttributeTypeInterface
16+
{
17+
/** @var string */
18+
public const SCHEMA_TYPE = 'xs:anyURI';
19+
20+
21+
/**
22+
* Convert this value to an attribute
23+
*
24+
* @return \SimpleSAML\XML\Attribute
25+
*/
26+
public function toAttribute(): Attribute
27+
{
28+
return new Attribute(C::NS_XML, 'xml', 'base', $this);
29+
}
30+
}

tests/XML/Type/BaseValueTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\XML\Type;
6+
7+
use PHPUnit\Framework\Attributes\{CoversClass, DataProvider, DataProviderExternal, DependsOnClass};
8+
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\Test\XML\Assert\AnyURITest;
10+
use SimpleSAML\XML\Constants as C;
11+
use SimpleSAML\XML\Type\BaseValue;
12+
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
13+
use SimpleSAML\XMLSchema\Type\Builtin\{AnyURIValue, StringValue};
14+
15+
/**
16+
* Class \SimpleSAML\Test\XML\Type\BaseValueTest
17+
*
18+
* @package simplesamlphp/xml-common
19+
*/
20+
#[CoversClass(BaseValue::class)]
21+
final class BaseValueTest extends TestCase
22+
{
23+
/**
24+
* @param boolean $shouldPass
25+
* @param string $base
26+
*/
27+
#[DataProvider('provideValidBase')]
28+
#[DataProviderExternal(AnyURITest::class, 'provideValidURI')]
29+
#[DependsOnClass(AnyURITest::class)]
30+
public function testAnyURI(bool $shouldPass, string $base): void
31+
{
32+
try {
33+
BaseValue::fromString($base);
34+
$this->assertTrue($shouldPass);
35+
} catch (SchemaViolationException $e) {
36+
$this->assertFalse($shouldPass);
37+
}
38+
}
39+
40+
41+
/**
42+
* Test helpers
43+
*/
44+
public function testHelpers(): void
45+
{
46+
$base = BaseValue::fromString('urn:x-simplesamlphp:namespace');
47+
$attr = $base->toAttribute();
48+
49+
$this->assertEquals($attr->getNamespaceURI(), C::NS_XML);
50+
$this->assertEquals($attr->getNamespacePrefix(), 'xml');
51+
$this->assertEquals($attr->getAttrName(), 'base');
52+
$this->assertEquals($attr->getAttrValue(), 'urn:x-simplesamlphp:namespace');
53+
}
54+
55+
56+
/**
57+
*/
58+
public function testEquals(): void
59+
{
60+
// Assert that two identical values are equal
61+
$this->assertTrue(AnyURIValue::fromString('hello')->equals(AnyURIValue::fromString('hello')));
62+
$this->assertTrue(AnyURIValue::fromString('hello')->equals(StringValue::fromString('hello')));
63+
$this->assertTrue(AnyURIValue::fromString('hello')->equals('hello'));
64+
65+
// Assert that two different values are not equal
66+
$this->assertFalse(AnyURIValue::fromString('hello')->equals(AnyURIValue::fromString('world')));
67+
$this->assertFalse(AnyURIValue::fromString('hello')->equals(StringValue::fromString('world')));
68+
$this->assertFalse(AnyURIValue::fromString('hello')->equals('world'));
69+
}
70+
71+
72+
/**
73+
* @return array<string, array{0: true, 1: string}>
74+
*/
75+
public static function provideValidBase(): array
76+
{
77+
return [
78+
'trailing newline' => [true, "https://sts.windows.net/{tenantid}/\n"],
79+
];
80+
}
81+
}

0 commit comments

Comments
 (0)