SchemaValidationTestTrait::testSchemaValidation()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 18
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 32
rs 9.6666
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
use SimpleSAML\XML\DOMDocumentFactory;
10
11
use function class_exists;
12
13
/**
14
 * Test for AbstractElement classes to perform schema validation tests.
15
 *
16
 * @package simplesamlphp\xml-common
17
 * @phpstan-ignore trait.unused
18
 */
19
trait SchemaValidationTestTrait
20
{
21
    /** @var string|null */
22
    protected static ?string $schemaFile = null;
23
24
    /** @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...
25
    protected static string $testedClass;
26
27
    /** @var \DOMDocument */
28
    protected static DOMDocument $xmlRepresentation;
29
30
31
    /**
32
     * Test schema validation.
33
     */
34
    #[Depends('testSerialization')]
35
    public function testSchemaValidation(): void
36
    {
37
        if (!class_exists(self::$testedClass)) {
38
            $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

38
            $this->/** @scrutinizer ignore-call */ 
39
                   markTestSkipped(
Loading history...
39
                'Unable to run ' . self::class . '::testSchemaValidation(). 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 . '::testSchemaValidation(). Please set ' . self::class
45
                . ':$xmlRepresentation to a DOMDocument representing the XML-class being tested',
46
            );
47
        } else {
48
            // Validate before serialization
49
            self::$testedClass::schemaValidate(self::$xmlRepresentation, self::$schemaFile);
50
51
            // Perform serialization
52
            $class = self::$testedClass::fromXML(self::$xmlRepresentation->documentElement);
53
            $serializedClass = $class->toXML();
54
55
            // Validate after serialization
56
            self::$testedClass::schemaValidate($serializedClass->ownerDocument, self::$schemaFile);
57
58
            // Validate after normalization
59
            if (self::$testedClass::getNormalization() === true) {
60
                $normalizedDoc = DOMDocumentFactory::normalizeDocument($serializedClass->ownerDocument);
61
                self::$testedClass::schemaValidate($normalizedDoc, self::$schemaFile);
62
            }
63
64
            // If we got this far and no exceptions were thrown, consider this test passed!
65
            $this->addToAssertionCount(1);
0 ignored issues
show
Bug introduced by
It seems like addToAssertionCount() 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

65
            $this->/** @scrutinizer ignore-call */ 
66
                   addToAssertionCount(1);
Loading history...
66
        }
67
    }
68
69
70
    abstract public function testSerialization(): void;
71
}
72