Skip to content

Commit 19624a2

Browse files
committed
Add type for xml:id attribute
1 parent 4222319 commit 19624a2

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

src/XML/Type/IDValue.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\IDValue as BaseIDValue;
10+
use SimpleSAML\XMLSchema\Type\Helper\AttributeTypeInterface;
11+
12+
/**
13+
* @package simplesaml/xml-common
14+
*/
15+
class IDValue extends BaseIDValue implements AttributeTypeInterface
16+
{
17+
/** @var string */
18+
public const SCHEMA_TYPE = 'xs:ID';
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', 'id', $this);
29+
}
30+
}

tests/XML/Type/IDValueTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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\IDTest;
10+
use SimpleSAML\XML\Constants as C;
11+
use SimpleSAML\XML\Type\IDValue;
12+
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
13+
use SimpleSAML\XMLSchema\Type\Builtin\{IDValue as BaseIDValue, StringValue};
14+
15+
/**
16+
* Class \SimpleSAML\Test\XML\Type\IDValueTest
17+
*
18+
* @package simplesamlphp/xml-common
19+
*/
20+
#[CoversClass(IDValue::class)]
21+
final class IDValueTest extends TestCase
22+
{
23+
/**
24+
* @param boolean $shouldPass
25+
* @param string $id
26+
*/
27+
#[DataProviderExternal(IDTest::class, 'provideValidID')]
28+
#[DependsOnClass(IDTest::class)]
29+
public function testID(bool $shouldPass, string $id): void
30+
{
31+
try {
32+
IDValue::fromString($id);
33+
$this->assertTrue($shouldPass);
34+
} catch (SchemaViolationException $e) {
35+
$this->assertFalse($shouldPass);
36+
}
37+
}
38+
39+
40+
/**
41+
* Test helpers
42+
*/
43+
public function testHelpers(): void
44+
{
45+
$id = IDValue::fromString('phpunit');
46+
$attr = $id->toAttribute();
47+
48+
$this->assertEquals($attr->getNamespaceURI(), C::NS_XML);
49+
$this->assertEquals($attr->getNamespacePrefix(), 'xml');
50+
$this->assertEquals($attr->getAttrName(), 'id');
51+
$this->assertEquals($attr->getAttrValue(), 'phpunit');
52+
}
53+
}

0 commit comments

Comments
 (0)