Skip to content

Commit a6ace2f

Browse files
committed
Add unit tests
1 parent ae8f884 commit a6ace2f

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

tests/Assert/XPathFilterTest.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Test\Assert;
6+
7+
use InvalidArgumentException;
8+
use PHPUnit\Framework\Attributes\CoversClass;
9+
use PHPUnit\Framework\Attributes\DataProvider;
10+
use PHPUnit\Framework\TestCase;
11+
use SimpleSAML\Assert\AssertionFailedException;
12+
use SimpleSAML\XML\Assert\Assert as XMLAssert;
13+
use SimpleSAML\XML\Constants as C;
14+
15+
use function str_pad;
16+
17+
/**
18+
* Class \SimpleSAML\XML\Assert\XPathFilterTest
19+
*
20+
* @package simplesamlphp/xml-common
21+
*/
22+
#[CoversClass(XMLAssert::class)]
23+
final class XPathFilterTest extends TestCase
24+
{
25+
/**
26+
* @param boolean $shouldPass
27+
* @param string $filter
28+
* @param array<string> $axes
29+
* @param array<string> $functions
30+
*/
31+
#[DataProvider('provideXPathFilter')]
32+
public function testDefaultAllowedXPathFilter(
33+
bool $shouldPass,
34+
string $filter,
35+
array $axes = C::DEFAULT_ALLOWED_AXES,
36+
array $functions = C::DEFAULT_ALLOWED_FUNCTIONS,
37+
): void {
38+
try {
39+
XMLAssert::allowedXPathFilter($filter, $axes, $functions);
40+
$this->assertTrue($shouldPass);
41+
} catch (AssertionFailedException $e) {
42+
$this->assertFalse($shouldPass);
43+
}
44+
}
45+
46+
47+
/**
48+
* @return array<string, array{0: bool, 1: string}>
49+
*/
50+
public static function provideXPathFilter(): array
51+
{
52+
return [
53+
// Axes
54+
'ancestor' => [true, 'ancestor::book'],
55+
'ancestor-or-self' => [true, 'ancestor-or-self::book'],
56+
'attribute' => [true, 'attribute::book'],
57+
'child' => [true, 'child::book'],
58+
'descendant' => [true, 'descendant::book'],
59+
'descendant-or-self' => [true, 'descendant-or-self::book'],
60+
'following' => [true, 'following::book'],
61+
'following-sibling' => [true, 'following-sibling::book'],
62+
'namespace' => [false, 'namespace::book'],
63+
'namespace whitelist' => [true, 'namespace::book', ['namespace']],
64+
'parent' => [true, 'parent::book'],
65+
'preceding' => [true, 'preceding::book'],
66+
'preceding-sibling' => [true, 'preceding-sibling::book'],
67+
'self' => [true, 'self::book'],
68+
69+
// Functions
70+
'boolean' => [false, 'boolean(Data/username/text())'],
71+
'ceiling' => [false, 'ceiling(//items/item[1]/price)'],
72+
'concat' => [false, "concat('A', '_', 'B')"],
73+
'contains' => [false, "contains(//username, 'o')"],
74+
'count' => [false, "count(//Sales.Order[Sales.Customer_Order/Sales.Customer/Name = 'Jansen'])"],
75+
'false' => [false, '//Sales.Customer[IsGoldCustomer = false()]'],
76+
'floor' => [false, 'floor(//items/item[1]/price)'],
77+
'id' => [false, 'SalesInvoiceLines[id(1)]'],
78+
'lang' => [false, 'lang("en-US")'],
79+
'last' => [false, 'last()'],
80+
'local-name' => [false, 'local-name(SalesInvoiceLines) '],
81+
'name' => [false, 'name(SalesInvoiceLines)'],
82+
'namespace-uri' => [false, 'namespace-uri(ReportData)'],
83+
'normalize-space' => [false, 'normalize-space(" Hello World ")'],
84+
'not' => [true, "//Sales.Customer[not(Name = 'Jansen')]"],
85+
'number' => [false, 'number("123")'],
86+
'position' => [false, 'position()'],
87+
'round' => [false, 'round(//items/item[1]/price)'],
88+
'starts-with' => [false, "//Sales.Customer[starts-with(Name, 'Jans')]"],
89+
'string' => [false, 'string(123)'],
90+
'string-length' => [false, 'string-length(//email)string-length(//email)'],
91+
'substring' => [false, "/bookstore/book[substring(title,1,5)='Harry']"],
92+
'substring-after' => [false, "/bookstore/book[substring-after(title,1,5)='Harry']"],
93+
'substring-before' => [false, "/bookstore/book[substring-before(title,1,5)='Harry']"],
94+
'sum' => [false, 'sum(//Sales.Order/TotalPrice)'],
95+
'text' => [false, '//lastname/text()'],
96+
'translate' => [false, "translate(//email, '@', '_')"],
97+
'true' => [false, '//Sales.Customer[IsGoldCustomer = true()]'],
98+
99+
// Edge-cases
100+
'unknown axis' => [false, 'unknown::book'],
101+
'unknown function' => [false, 'unknown()'],
102+
'too long' => [false, str_pad('a', 120, 'a')],
103+
];
104+
}
105+
}

0 commit comments

Comments
 (0)