Passed
Branch master (a3c4c5)
by Tim
02:38 queued 53s
created

SerializableElementTestTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
eloc 27
c 3
b 0
f 0
dl 0
loc 63
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XML\TestUtils;
6
7
use DOMDocument;
8
use PHPUnit\Framework\Attributes\Depends;
9
10
use function class_exists;
11
12
/**
13
 * Test for Serializable XML classes to perform default serialization tests.
14
 *
15
 * @package simplesamlphp\xml-common
16
 */
17
trait SerializableElementTestTrait
18
{
19
    /** @var class-string */
20
    protected static string $testedClass;
21
22
    /** @var \DOMDocument */
23
    protected static DOMDocument $xmlRepresentation;
24
25
26
    /**
27
     * Test creating XML from a class.
28
     */
29
    abstract public function testMarshalling(): void;
30
31
32
    /**
33
     * Test creating a class from XML.
34
     */
35
    public function testUnmarshalling(): void
36
    {
37
        if (!class_exists(self::$testedClass)) {
38
            $this->markTestSkipped(
39
                'Unable to run ' . self::class . '::testUnmarshalling(). Please set ' . self::class
40
                . ':$testedClass to a class-string representing the XML-class being tested',
41
            );
42
        } elseif (empty(self::$xmlRepresentation)) {
43
            $this->markTestSkipped(
44
                'Unable to run ' . self::class . '::testUnmarshalling(). Please set ' . self::class
45
                . ':$xmlRepresentation to a DOMDocument representing the XML-class being tested',
46
            );
47
        } else {
48
            $elt = self::$testedClass::fromXML(self::$xmlRepresentation->documentElement);
49
50
            $this->assertEquals(
51
                self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
52
                strval($elt),
53
            );
54
        }
55
    }
56
57
58
    /**
59
     * Test serialization / unserialization.
60
     */
61
    #[Depends('testMarshalling')]
62
    #[Depends('testUnmarshalling')]
63
    public function testSerialization(): void
64
    {
65
        if (!class_exists(self::$testedClass)) {
66
            $this->markTestSkipped(
67
                'Unable to run ' . self::class . '::testSerialization(). Please set ' . self::class
68
                . ':$testedClass to a class-string representing the XML-class being tested',
69
            );
70
        } elseif (empty(self::$xmlRepresentation)) {
71
            $this->markTestSkipped(
72
                'Unable to run ' . self::class . '::testSerialization(). Please set ' . self::class
73
                . ':$xmlRepresentation to a DOMDocument representing the XML-class being tested',
74
            );
75
        } else {
76
            $this->assertEquals(
77
                self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
78
                strval(unserialize(serialize(self::$testedClass::fromXML(self::$xmlRepresentation->documentElement)))),
79
            );
80
        }
81
    }
82
}
83