|
| 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