Passed
Push — master ( 2b1a1f...c49509 )
by Tim
01:32
created

SerializableElementTestTrait::testUnmarshalling()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 18
rs 9.8333
cc 3
nc 3
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XML\TestUtils;
6
7
use DOMDocument;
8
9
use function class_exists;
10
11
/**
12
 * Test for Serializable XML classes to perform default serialization tests.
13
 *
14
 * @package simplesamlphp\xml-common
15
 */
16
trait SerializableElementTestTrait
17
{
18
    /** @var class-string */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
19
    protected static string $testedClass;
20
21
    /** @var \DOMDocument */
22
    protected static DOMDocument $xmlRepresentation;
23
24
25
    /**
26
     * Test creating XML from a class.
27
     */
28
    abstract public function testMarshalling(): void;
29
30
31
    /**
32
     * Test creating a class from XML.
33
     */
34
    public function testUnmarshalling(): void
35
    {
36
        if (!class_exists(self::$testedClass)) {
37
            $this->markTestSkipped(
0 ignored issues
show
Bug introduced by
It seems like markTestSkipped() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
            $this->/** @scrutinizer ignore-call */ 
38
                   markTestSkipped(
Loading history...
38
                'Unable to run ' . self::class . '::testUnmarshalling(). Please set ' . self::class
39
                . ':$testedClass to a class-string representing the XML-class being tested',
40
            );
41
        } elseif (empty(self::$xmlRepresentation)) {
42
            $this->markTestSkipped(
43
                'Unable to run ' . self::class . '::testUnmarshalling(). Please set ' . self::class
44
                . ':$xmlRepresentation to a DOMDocument representing the XML-class being tested',
45
            );
46
        } else {
47
            $elt = self::$testedClass::fromXML(self::$xmlRepresentation->documentElement);
48
49
            $this->assertEquals(
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
            $this->/** @scrutinizer ignore-call */ 
50
                   assertEquals(
Loading history...
50
                self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
51
                strval($elt),
52
            );
53
        }
54
    }
55
56
57
    /**
58
     * Test serialization / unserialization.
59
     *
60
     * @depends testMarshalling
61
     * @depends testUnmarshalling
62
     */
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