|
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; |
|
|
|
|
|
|
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
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/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: