Issues (26)

src/Checks/XmlCheck.php (1 issue)

Labels
Severity
1
<?php declare(strict_types=1);
2
3
4
namespace Pitchart\Phlunit\Checks;
5
6
use PHPUnit\Framework\Assert;
7
use Pitchart\Phlunit\Checks\Mixin\ConstraintCheck;
8
use Pitchart\Phlunit\Checks\Mixin\FluentChecks;
9
use Pitchart\Phlunit\Checks\Mixin\TypeCheck;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Pitchart\Phlunit\Checks\TypeCheck. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use Pitchart\Phlunit\Checks\Mixin\WithMessage;
11
use Pitchart\Phlunit\Constraint\Xml\MatchesSchema;
12
use Pitchart\Phlunit\Constraint\Xml\XmlUtility;
13
14
class XmlCheck implements FluentCheck
15
{
16
    use TypeCheck, FluentChecks, WithMessage, ConstraintCheck;
17
18
    /**
19
     * @var \DOMDocument
20
     */
21
    private $value;
22
23
    /**
24
     * XmlCheck constructor.
25
     *
26
     * @param string|\DOMDocument $value
27
     */
28 11
    public function __construct($value)
29
    {
30 11
        $this->value = XmlUtility::load($value);
31 11
    }
32
33 2
    public function isEqualTo($expected): self
34
    {
35 2
        Assert::assertXmlStringEqualsXmlString($expected, $this->value, $this->message);
36 2
        return $this;
37
    }
38
39 2
    public function isNotEqualTo($expected): self
40
    {
41 2
        Assert::assertXmlStringNotEqualsXmlString($expected, $this->value, $this->message);
42 2
        return $this;
43
    }
44
45 2
    public function isEqualToFileContent(string $path): self
46
    {
47 2
        Assert::assertXmlStringEqualsXmlFile($path, $this->value, $this->message);
48 2
        return $this;
49
    }
50
51 2
    public function isNotEqualToFileContent(string $path): self
52
    {
53 2
        Assert::assertXmlStringNotEqualsXmlFile($path, $this->value, $this->message);
54 2
        return $this;
55
    }
56
57 3
    public function matchesSchema(string $schema): self
58
    {
59 3
        Assert::assertThat($this->value, new MatchesSchema($schema), $this->message);
60 3
        return $this;
61
    }
62
}
63