Skip to content

Commit 28ea467

Browse files
committed
Add TestContainer and start using it to reduce overhead
1 parent 5e67979 commit 28ea467

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+788
-685
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
},
2525
"autoload-dev": {
2626
"psr-4": {
27+
"SimpleSAML\\Test\\Container\\": ["tests/Container/"],
2728
"SimpleSAML\\Test\\Helper\\": ["tests/Helper/"],
2829
"SimpleSAML\\Test\\Registry\\": ["tests/Registry/"],
2930
"SimpleSAML\\Test\\XML\\": ["tests/XML/"],
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Container;
6+
7+
/**
8+
* Class \SimpleSAML\XML\Container\AbstractTestContainer
9+
*/
10+
abstract class AbstractTestContainer
11+
{
12+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Container;
6+
7+
use SimpleSAML\XML\Assert\Assert;
8+
9+
/**
10+
* Class \SimpleSAML\XML\Container\TestContainerSingleton
11+
*/
12+
final class TestContainerSingleton
13+
{
14+
protected static AbstractTestContainer $testContainer;
15+
16+
17+
/** Set a container to use. */
18+
public static function setContainer(AbstractTestContainer $testContainer): void
19+
{
20+
self::$testContainer = $testContainer;
21+
}
22+
23+
24+
public static function getContainer(): AbstractTestContainer
25+
{
26+
Assert::notNull(self::$testContainer, 'No container set.');
27+
return self::$testContainer;
28+
}
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\TestUtils;
6+
7+
use SimpleSAML\XML\Container\AbstractTestContainer;
8+
use SimpleSAML\XML\Container\TestContainerSingleton;
9+
10+
/**
11+
* Trait for importing the TestContainer into the unit test.
12+
*
13+
* @package simplesamlphp\xml-common
14+
* @phpstan-ignore trait.unused
15+
*/
16+
trait TestContainerTestTrait
17+
{
18+
protected static AbstractTestContainer $testContainer;
19+
20+
21+
protected static function instantiateTestContainer(): void
22+
{
23+
self::$testContainer = TestContainerSingleton::getContainer();
24+
}
25+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\TestUtils;
6+
7+
use SimpleSAML\XML\Assert\Assert;
8+
use SimpleSAML\XML\Attribute as XMLAttribute;
9+
use SimpleSAML\XML\Chunk;
10+
use SimpleSAML\XML\DOMDocumentFactory;
11+
use SimpleSAML\XML\Type\LangValue;
12+
use SimpleSAML\XMLSchema\Type\LanguageValue;
13+
use SimpleSAML\XMLSchema\Type\StringValue;
14+
15+
use function array_key_exists;
16+
use function sprintf;
17+
18+
/**
19+
* One-time instantiation of common elements in XML, for re-use in unit-tests.
20+
*
21+
* @package simplesamlphp\xml-common
22+
* @phpstan-ignore trait.unused
23+
*/
24+
trait XMLElementsTrait
25+
{
26+
protected const string NAMESPACE = 'urn:x-simplesamlphp:namespace';
27+
28+
29+
protected ?Chunk $chunk = null;
30+
31+
/** @var array<string, \SimpleSAML\XML\Attribute> $lang */
32+
protected array $lang = [];
33+
34+
/** @var array<string, \SimpleSAML\XML\Attribute> $language */
35+
protected array $language = [];
36+
37+
/** @var array<positive-int, \SimpleSAML\XML\Attribute> $attribute */
38+
protected array $attribute = [];
39+
40+
41+
public function getChunk(): Chunk
42+
{
43+
if ($this->chunk === null) {
44+
$this->chunk = new Chunk(DOMDocumentFactory::fromString(
45+
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">Some</ssp:Chunk>',
46+
)->documentElement);
47+
}
48+
49+
return $this->chunk;
50+
}
51+
52+
53+
public function getLang(string $lang = 'en'): XMLAttribute
54+
{
55+
if (array_key_exists($lang, $this->lang)) {
56+
$this->lang[$lang] = LangValue::fromString($lang)->toAttribute();
57+
}
58+
59+
return $this->lang[$lang];
60+
}
61+
62+
63+
public function getLanguage(string $language = 'en'): XMLAttribute
64+
{
65+
if (array_key_exists($language, $this->language)) {
66+
$this->language[$language] = LanguageValue::fromString($language)->toAttribute();
67+
}
68+
69+
return $this->language[$language];
70+
}
71+
72+
73+
/** @param positive-int $x */
74+
public function getXMLAttribute(int $x): XMLAttribute
75+
{
76+
Assert::positiveInteger($x);
77+
78+
if (!array_key_exists($x, $this->attribute)) {
79+
$this->attribute[$x] = new XMLAttribute(
80+
static::NAMESPACE,
81+
'ssp',
82+
sprintf('attr%d', $x),
83+
StringValue::fromString(sprintf('value%d', $x)),
84+
);
85+
}
86+
87+
return $this->attribute[$x];
88+
}
89+
}

tests/Container/TestContainer.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\Container;
6+
7+
use SimpleSAML\XML\Container\AbstractTestContainer;
8+
use SimpleSAML\XML\TestUtils\XMLElementsTrait;
9+
10+
/**
11+
* Class \SimpleSAML\XML\Container\TestContainer
12+
*/
13+
class TestContainer extends AbstractTestContainer
14+
{
15+
use XMLElementsTrait;
16+
}

tests/XMLSchema/XML/AllTest.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
use PHPUnit\Framework\Attributes\CoversClass;
99
use PHPUnit\Framework\Attributes\Group;
1010
use PHPUnit\Framework\TestCase;
11-
use SimpleSAML\XML\Attribute as XMLAttribute;
1211
use SimpleSAML\XML\DOMDocumentFactory;
1312
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
1413
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
14+
use SimpleSAML\XML\TestUtils\TestContainerTestTrait;
1515
use SimpleSAML\XML\Type\LangValue;
1616
use SimpleSAML\XMLSchema\Type\AnyURIValue;
1717
use SimpleSAML\XMLSchema\Type\BooleanValue;
@@ -68,6 +68,7 @@ final class AllTest extends TestCase
6868
{
6969
use SchemaValidationTestTrait;
7070
use SerializableElementTestTrait;
71+
use TestContainerTestTrait;
7172

7273

7374
/**
@@ -79,6 +80,8 @@ public static function setUpBeforeClass(): void
7980
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
8081
dirname(__FILE__, 3) . '/resources/xml/xs/all.xml',
8182
);
83+
84+
self::instantiateTestContainer();
8285
}
8386

8487

@@ -106,24 +109,20 @@ public function testMarshalling(): void
106109
$attributeGroupText = new DOMText('AttributeGroup');
107110
$attributeGroupDocument->appendChild($attributeGroupText);
108111

109-
$attr1 = new XMLAttribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr1', StringValue::fromString('value1'));
110-
$attr2 = new XMLAttribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr2', StringValue::fromString('value2'));
111-
$attr3 = new XMLAttribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr3', StringValue::fromString('value3'));
112-
$attr4 = new XMLAttribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr4', StringValue::fromString('value4'));
113112
$lang = LangValue::fromString('nl');
114113

115114
$documentation1 = new Documentation(
116115
$simpleTypeDocument->childNodes,
117116
$lang,
118117
AnyURIValue::fromString('urn:x-simplesamlphp:source'),
119-
[$attr2],
118+
[self::$testContainer->getXMLAttribute(2)],
120119
);
121120

122121
$annotation1 = new Annotation(
123122
[],
124123
[$documentation1],
125124
IDValue::fromString('phpunit_annotation1'),
126-
[$attr1],
125+
[self::$testContainer->getXMLAttribute(1)],
127126
);
128127

129128
$restriction = new Restriction(
@@ -149,7 +148,7 @@ public function testMarshalling(): void
149148
SimpleDerivationSetValue::fromString('#all'),
150149
null,
151150
IDValue::fromString('phpunit_simpleType'),
152-
[$attr4],
151+
[self::$testContainer->getXMLAttribute(4)],
153152
);
154153

155154
// TopLevelComplexType
@@ -164,7 +163,7 @@ public function testMarshalling(): void
164163
QNameValue::fromString("{http://www.w3.org/2001/XMLSchema}xs:nestedParticle"),
165164
null,
166165
IDValue::fromString('phpunit_group1'),
167-
[$attr4],
166+
[self::$testContainer->getXMLAttribute(4)],
168167
);
169168

170169
$topLevelComplexType = new TopLevelComplexType(
@@ -187,22 +186,22 @@ public function testMarshalling(): void
187186
$anyAttribute1,
188187
null,
189188
IDValue::fromString('phpunit_complexType'),
190-
[$attr4],
189+
[self::$testContainer->getXMLAttribute(4)],
191190
);
192191

193192
// Group
194193
$selector = new Selector(
195194
StringValue::fromString('.//annotation'),
196195
null,
197196
IDValue::fromString('phpunit_selector'),
198-
[$attr4],
197+
[self::$testContainer->getXMLAttribute(4)],
199198
);
200199

201200
$field = new Field(
202201
StringValue::fromString('@id'),
203202
null,
204203
IDValue::fromString('phpunit_field'),
205-
[$attr4],
204+
[self::$testContainer->getXMLAttribute(4)],
206205
);
207206

208207
$keyref = new Keyref(
@@ -212,7 +211,7 @@ public function testMarshalling(): void
212211
[$field],
213212
null,
214213
IDValue::fromString('phpunit_keyref'),
215-
[$attr3],
214+
[self::$testContainer->getXMLAttribute(3)],
216215
);
217216

218217
$narrowMaxMinElement = new NarrowMaxMinElement(
@@ -228,7 +227,7 @@ public function testMarshalling(): void
228227
form: FormChoiceValue::fromEnum(FormChoiceEnum::Qualified),
229228
annotation: null,
230229
id: IDValue::fromString('phpunit_localElement'),
231-
namespacedAttributes: [$attr4],
230+
namespacedAttributes: [self::$testContainer->getXMLAttribute(4)],
232231
);
233232

234233
$all = new All(
@@ -237,7 +236,7 @@ public function testMarshalling(): void
237236
[$narrowMaxMinElement],
238237
$annotation1,
239238
IDValue::fromString('phpunit_all'),
240-
[$attr3],
239+
[self::$testContainer->getXMLAttribute(3)],
241240
);
242241

243242
$this->assertEquals(

tests/XMLSchema/XML/AnnotationTest.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
use PHPUnit\Framework\Attributes\CoversClass;
99
use PHPUnit\Framework\Attributes\Group;
1010
use PHPUnit\Framework\TestCase;
11-
use SimpleSAML\XML\Attribute as XMLAttribute;
1211
use SimpleSAML\XML\DOMDocumentFactory;
1312
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
1413
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
14+
use SimpleSAML\XML\TestUtils\TestContainerTestTrait;
1515
use SimpleSAML\XML\Type\LangValue;
1616
use SimpleSAML\XMLSchema\Type\AnyURIValue;
1717
use SimpleSAML\XMLSchema\Type\IDValue;
18-
use SimpleSAML\XMLSchema\Type\StringValue;
1918
use SimpleSAML\XMLSchema\XML\AbstractOpenAttrs;
2019
use SimpleSAML\XMLSchema\XML\AbstractXsElement;
2120
use SimpleSAML\XMLSchema\XML\Annotation;
@@ -38,6 +37,7 @@ final class AnnotationTest extends TestCase
3837
{
3938
use SchemaValidationTestTrait;
4039
use SerializableElementTestTrait;
40+
use TestContainerTestTrait;
4141

4242

4343
/**
@@ -49,6 +49,8 @@ public static function setUpBeforeClass(): void
4949
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
5050
dirname(__FILE__, 3) . '/resources/xml/xs/annotation.xml',
5151
);
52+
53+
self::instantiateTestContainer();
5254
}
5355

5456

@@ -76,40 +78,37 @@ public function testMarshalling(): void
7678
$text = new DOMText('Other Documentation');
7779
$otherDocumentationDocument->appendChild($text);
7880

79-
$attr1 = new XMLAttribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr1', StringValue::fromString('value1'));
80-
$attr2 = new XMLAttribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr2', StringValue::fromString('value2'));
81-
$attr3 = new XMLAttribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr3', StringValue::fromString('value3'));
8281
$lang = LangValue::fromString('nl');
8382

8483
$appinfo1 = new Appinfo(
8584
$appinfoDocument->childNodes,
8685
AnyURIValue::fromString('urn:x-simplesamlphp:source'),
87-
[$attr1],
86+
[self::$testContainer->getXMLAttribute(1)],
8887
);
8988
$appinfo2 = new Appinfo(
9089
$otherAppinfoDocument->childNodes,
9190
AnyURIValue::fromString('urn:x-simplesamlphp:source'),
92-
[$attr2],
91+
[self::$testContainer->getXMLAttribute(2)],
9392
);
9493

9594
$documentation1 = new Documentation(
9695
$documentationDocument->childNodes,
9796
$lang,
9897
AnyURIValue::fromString('urn:x-simplesamlphp:source'),
99-
[$attr1],
98+
[self::$testContainer->getXMLAttribute(1)],
10099
);
101100
$documentation2 = new Documentation(
102101
$otherDocumentationDocument->childNodes,
103102
$lang,
104103
AnyURIValue::fromString('urn:x-simplesamlphp:source'),
105-
[$attr2],
104+
[self::$testContainer->getXMLAttribute(2)],
106105
);
107106

108107
$annotation = new Annotation(
109108
[$appinfo1, $appinfo2],
110109
[$documentation1, $documentation2],
111110
IDValue::fromString('phpunit'),
112-
[$attr3],
111+
[self::$testContainer->getXMLAttribute(3)],
113112
);
114113

115114
$this->assertEquals(

0 commit comments

Comments
 (0)