Skip to content

Commit 1a9ce03

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

File tree

113 files changed

+977
-1686
lines changed

Some content is hidden

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

113 files changed

+977
-1686
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Container;
6+
7+
use SimpleSAML\XML\Attribute as XMLAttribute;
8+
use SimpleSAML\XML\Chunk;
9+
use SimpleSAML\XML\Type\LangValue;
10+
use SimpleSAML\XMLSchema\Type\LanguageValue;
11+
use SimpleSAML\XMLSchema\XML\Appinfo;
12+
13+
/**
14+
* Class \SimpleSAML\XML\Container\AbstractTestContainer
15+
*/
16+
abstract class AbstractTestContainer implements TestContainerInterface
17+
{
18+
abstract public function getAppinfo(int $x = 1): Appinfo;
19+
20+
21+
abstract public function getChunk(): Chunk;
22+
23+
24+
abstract public function getLangValue(string $lang = 'en'): LangValue;
25+
26+
27+
abstract public function getLanguageValue(string $language = 'en'): LanguageValue;
28+
29+
30+
abstract public function getXMLAttribute(int $x = 1): XMLAttribute;
31+
}
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\TestContainerInterface
9+
*/
10+
interface TestContainerInterface
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: 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\Container;
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\Type\LangValue> $lang */
32+
protected array $lang = [];
33+
34+
/** @var array<string, \SimpleSAML\XMLSchema\Type\LanguageValue> $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 getLangValue(string $lang = 'en'): LangValue
54+
{
55+
if (array_key_exists($lang, $this->lang)) {
56+
$this->lang[$lang] = LangValue::fromString($lang);
57+
}
58+
59+
return $this->lang[$lang];
60+
}
61+
62+
63+
public function getLanguageValue(string $language = 'en'): LanguageValue
64+
{
65+
if (array_key_exists($language, $this->language)) {
66+
$this->language[$language] = LanguageValue::fromString($language);
67+
}
68+
69+
return $this->language[$language];
70+
}
71+
72+
73+
/** @param positive-int $x */
74+
public function getXMLAttribute(int $x = 1): 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+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Container;
6+
7+
use DOMText;
8+
use SimpleSAML\XML\DOMDocumentFactory;
9+
use SimpleSAML\XMLSchema\Type\AnyURIValue;
10+
use SimpleSAML\XMLSchema\XML\Appinfo;
11+
12+
use function array_key_exists;
13+
use function sprintf;
14+
15+
/**
16+
* One-time instantiation of common elements in XML, for re-use in unit-tests.
17+
*
18+
* @package simplesamlphp\xml-common
19+
* @phpstan-ignore trait.unused
20+
*/
21+
trait XMLSchemaElementsTrait
22+
{
23+
protected const string SOURCE = 'urn:x-simplesamlphp:source';
24+
25+
26+
/** @var array<positive-int, \SimpleSAML\XMLSchema\XML\Appinfo> */
27+
protected array $appinfo = [];
28+
29+
30+
/** @param positive-int $x */
31+
public function getAppinfo(int $x = 1): Appinfo
32+
{
33+
if (!array_key_exists($x, $this->appinfo)) {
34+
$appinfoDocument = DOMDocumentFactory::create();
35+
$text = new DOMText(sprintf('Application Information (%d)', $x));
36+
$appinfoDocument->appendChild($text);
37+
38+
$this->appinfo[$x] = new Appinfo(
39+
$appinfoDocument->childNodes,
40+
AnyURIValue::fromString(static::SOURCE),
41+
[$this->getXMLAttribute($x)],
42+
);
43+
}
44+
45+
return $this->appinfo[$x];
46+
}
47+
}
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+
}

tests/Container/TestContainer.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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\Container\XMLElementsTrait;
9+
use SimpleSAML\XML\Container\XMLSchemaElementsTrait;
10+
11+
/**
12+
* Class \SimpleSAML\Test\Container\TestContainer
13+
*/
14+
class TestContainer extends AbstractTestContainer
15+
{
16+
use XMLElementsTrait;
17+
use XMLSchemaElementsTrait;
18+
}

0 commit comments

Comments
 (0)