SchemaValidationTestTrait::testSchemaValidation()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 0
dl 0
loc 26
rs 9.7666
c 0
b 0
f 0
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 AbstractElement classes to perform schema validation tests.
14
 *
15
 * @package simplesamlphp\xml-common
16
 * @phpstan-ignore trait.unused
17
 */
18
trait SchemaValidationTestTrait
19
{
20
    /** @var string|null */
21
    protected static ?string $schemaFile = null;
22
23
    /** @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...
24
    protected static string $testedClass;
25
26
    /** @var \DOMDocument */
27
    protected static DOMDocument $xmlRepresentation;
28
29
30
    /**
31
     * Test schema validation.
32
     */
33
    #[Depends('testSerialization')]
34
    public function testSchemaValidation(): 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 . '::testSchemaValidation(). 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 . '::testSchemaValidation(). Please set ' . self::class
44
                . ':$xmlRepresentation to a DOMDocument representing the XML-class being tested',
45
            );
46
        } else {
47
            // Validate before serialization
48
            self::$testedClass::schemaValidate(self::$xmlRepresentation, self::$schemaFile);
49
50
            // Perform serialization
51
            $class = self::$testedClass::fromXML(self::$xmlRepresentation->documentElement);
52
            $serializedClass = $class->toXML();
53
54
            // Validate after serialization
55
            self::$testedClass::schemaValidate($serializedClass->ownerDocument, self::$schemaFile);
56
57
            // If we got this far and no exceptions were thrown, consider this test passed!
58
            $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

58
            $this->/** @scrutinizer ignore-call */ 
59
                   addToAssertionCount(1);
Loading history...
59
        }
60
    }
61
62
63
    abstract public function testSerialization(): void;
64
}
65