-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathXmlMatchesXsdTest.php
More file actions
163 lines (139 loc) · 4.78 KB
/
XmlMatchesXsdTest.php
File metadata and controls
163 lines (139 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer / PHPUnit Constraint XmlMatchesXsd.
*
* (c) SpacePossum
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\PhpunitConstraintXmlMatchesXsd\Tests\Constraint;
use PhpCsFixer\PhpunitConstraintXmlMatchesXsd\Constraint\XmlMatchesXsd;
use PHPUnit\Framework\TestCase;
/**
* @internal
*
* @covers \PhpCsFixer\PhpunitConstraintXmlMatchesXsd\Constraint\XmlMatchesXsd
*/
final class XmlMatchesXsdTest extends TestCase
{
public function testAssertXMLMatchesXSD(): void
{
$constraint = new XmlMatchesXsd($this->getXSD());
$sampleFile = $this->getAssetsDir().'xliff_sample.xml';
$content = @file_get_contents($sampleFile);
if (false === $content) {
$error = error_get_last();
throw new \RuntimeException(\sprintf(
'Failed to read content of the sample file "%s".%s',
$content,
$error ? ' '.$error['message'] : ''
));
}
$constraint->evaluate($content); // should not throw an exception
self::assertTrue($constraint->evaluate($content, '', true));
}
public function testXMLValidConstraintBasics(): void
{
$constraint = new XmlMatchesXsd('');
self::assertSame(1, $constraint->count());
self::assertSame('matches XSD', $constraint->toString());
}
public function testXMLValidConstraintFalse(): void
{
$this->expectException(
'PHPUnit\Framework\ExpectationFailedException'
);
$this->expectExceptionMessageRegex(
'#^Failed asserting that boolean\# matches XSD\.$#'
);
$constraint = new XmlMatchesXsd($this->getXSD());
$constraint->evaluate(false);
}
public function testXMLValidConstraintInt(): void
{
$this->expectException(
'PHPUnit\Framework\ExpectationFailedException'
);
$this->expectExceptionMessageRegex(
'#^Failed asserting that integer\#1 matches XSD\.$#'
);
$constraint = new XmlMatchesXsd($this->getXSD());
$constraint->evaluate(1);
}
public function testXMLValidConstraintInvalidXML(): void
{
$this->expectException(
'PHPUnit\Framework\ExpectationFailedException'
);
$this->expectExceptionMessageRegex(
'#^Failed asserting that <a></b> matches XSD.[\n]\[error \d{1,}\](?s).*\.$#'
);
$constraint = new XmlMatchesXsd($this->getXSD());
$constraint->evaluate('<a></b>');
}
public function testXMLValidConstraintNotMatchingXML(): void
{
$this->expectException(
'PHPUnit\Framework\ExpectationFailedException'
);
$this->expectExceptionMessageRegex(
'#^Failed asserting that <a></a> matches XSD.[\n]\[error \d{1,}\](?s).*\.$#'
);
$constraint = new XmlMatchesXsd($this->getXSD());
$constraint->evaluate('<a></a>');
}
public function testXMLValidConstraintNull(): void
{
$this->expectException(
'PHPUnit\Framework\ExpectationFailedException'
);
$this->expectExceptionMessageRegex(
'#^Failed asserting that null matches XSD\.$#'
);
$constraint = new XmlMatchesXsd($this->getXSD());
$constraint->evaluate(null);
}
public function testXMLValidConstraintObject(): void
{
$this->expectException(
'PHPUnit\Framework\ExpectationFailedException'
);
$this->expectExceptionMessageRegex(
'#^Failed asserting that stdClass\# matches XSD\.$#'
);
$constraint = new XmlMatchesXsd($this->getXSD());
$constraint->evaluate(new \stdClass());
}
/**
* @return string
*/
private function getXSD()
{
return file_get_contents($this->getAssetsDir().'xliff-core-1.2-strict.xsd');
}
/**
* @return string
*/
private function getAssetsDir()
{
return __DIR__.'/../Fixtures/XmlMatchesXsdTest/';
}
/**
* @param string $pattern
*/
private function expectExceptionMessageRegex($pattern): void
{
if (method_exists($this, 'expectExceptionMessageRegExp')) {
$this->expectExceptionMessageMatches($pattern);
} elseif (method_exists($this, 'expectExceptionMessageMatches')) {
$this->expectExceptionMessageMatches($pattern);
} elseif (method_exists($this, 'expectDeprecationMessageMatches')) {
$this->expectDeprecationMessageMatches($pattern);
} else {
throw new \RuntimeException('Unknown how to match against exception message.');
}
}
}