SerializableElementTestTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testSerialization() 0 18 3
A testUnmarshalling() 0 18 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XML\TestUtils;
6
7
use DOMDocument;
8
use PHPUnit\Framework\Attributes\Depends;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\Attributes\Depends was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
 * @phpstan-ignore trait.unused
17
 */
18
trait SerializableElementTestTrait
19
{
20
    /** @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...
21
    protected static string $testedClass;
22
23
    /** @var \DOMDocument */
24
    protected static DOMDocument $xmlRepresentation;
25
26
27
    /**
28
     * Test creating XML from a class.
29
     */
30
    abstract public function testMarshalling(): void;
31
32
33
    /**
34
     * Test creating a class from XML.
35
     */
36
    public function testUnmarshalling(): void
37
    {
38
        if (!class_exists(self::$testedClass)) {
39
            $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

39
            $this->/** @scrutinizer ignore-call */ 
40
                   markTestSkipped(
Loading history...
40
                'Unable to run ' . self::class . '::testUnmarshalling(). Please set ' . self::class
41
                . ':$testedClass to a class-string representing the XML-class being tested',
42
            );
43
        } elseif (empty(self::$xmlRepresentation)) {
44
            $this->markTestSkipped(
45
                'Unable to run ' . self::class . '::testUnmarshalling(). Please set ' . self::class
46
                . ':$xmlRepresentation to a DOMDocument representing the XML-class being tested',
47
            );
48
        } else {
49
            $elt = self::$testedClass::fromXML(self::$xmlRepresentation->documentElement);
50
51
            $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

51
            $this->/** @scrutinizer ignore-call */ 
52
                   assertEquals(
Loading history...
52
                self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
53
                strval($elt),
54
            );
55
        }
56
    }
57
58
59
    /**
60
     * Test serialization / unserialization.
61
     */
62
    #[Depends('testMarshalling')]
63
    #[Depends('testUnmarshalling')]
64
    public function testSerialization(): void
65
    {
66
        if (!class_exists(self::$testedClass)) {
67
            $this->markTestSkipped(
68
                'Unable to run ' . self::class . '::testSerialization(). Please set ' . self::class
69
                . ':$testedClass to a class-string representing the XML-class being tested',
70
            );
71
        } elseif (empty(self::$xmlRepresentation)) {
72
            $this->markTestSkipped(
73
                'Unable to run ' . self::class . '::testSerialization(). Please set ' . self::class
74
                . ':$xmlRepresentation to a DOMDocument representing the XML-class being tested',
75
            );
76
        } else {
77
            $this->assertEquals(
78
                self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
79
                strval(unserialize(serialize(self::$testedClass::fromXML(self::$xmlRepresentation->documentElement)))),
80
            );
81
        }
82
    }
83
}
84