HasXPathTest.php
5.0 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace Hamcrest\Xml;
class HasXPathTest extends \Hamcrest\AbstractMatcherTest
{
protected static $xml;
protected static $doc;
protected static $html;
public static function setUpBeforeClass()
{
self::$xml = <<<XML
<?xml version="1.0"?>
<users>
<user>
<id>alice</id>
<name>Alice Frankel</name>
<role>admin</role>
</user>
<user>
<id>bob</id>
<name>Bob Frankel</name>
<role>user</role>
</user>
<user>
<id>charlie</id>
<name>Charlie Chan</name>
<role>user</role>
</user>
</users>
XML;
self::$doc = new \DOMDocument();
self::$doc->loadXML(self::$xml);
self::$html = <<<HTML
<html>
<head>
<title>Home Page</title>
</head>
<body>
<h1>Heading</h1>
<p>Some text</p>
</body>
</html>
HTML;
}
protected function createMatcher()
{
return \Hamcrest\Xml\HasXPath::hasXPath('/users/user');
}
public function testMatchesWhenXPathIsFound()
{
assertThat('one match', self::$doc, hasXPath('user[id = "bob"]'));
assertThat('two matches', self::$doc, hasXPath('user[role = "user"]'));
}
public function testDoesNotMatchWhenXPathIsNotFound()
{
assertThat(
'no match',
self::$doc,
not(hasXPath('user[contains(id, "frank")]'))
);
}
public function testMatchesWhenExpressionWithoutMatcherEvaluatesToTrue()
{
assertThat(
'one match',
self::$doc,
hasXPath('count(user[id = "bob"])')
);
}
public function testDoesNotMatchWhenExpressionWithoutMatcherEvaluatesToFalse()
{
assertThat(
'no matches',
self::$doc,
not(hasXPath('count(user[id = "frank"])'))
);
}
public function testMatchesWhenExpressionIsEqual()
{
assertThat(
'one match',
self::$doc,
hasXPath('count(user[id = "bob"])', 1)
);
assertThat(
'two matches',
self::$doc,
hasXPath('count(user[role = "user"])', 2)
);
}
public function testDoesNotMatchWhenExpressionIsNotEqual()
{
assertThat(
'no match',
self::$doc,
not(hasXPath('count(user[id = "frank"])', 2))
);
assertThat(
'one match',
self::$doc,
not(hasXPath('count(user[role = "admin"])', 2))
);
}
public function testMatchesWhenContentMatches()
{
assertThat(
'one match',
self::$doc,
hasXPath('user/name', containsString('ice'))
);
assertThat(
'two matches',
self::$doc,
hasXPath('user/role', equalTo('user'))
);
}
public function testDoesNotMatchWhenContentDoesNotMatch()
{
assertThat(
'no match',
self::$doc,
not(hasXPath('user/name', containsString('Bobby')))
);
assertThat(
'no matches',
self::$doc,
not(hasXPath('user/role', equalTo('owner')))
);
}
public function testProvidesConvenientShortcutForHasXPathEqualTo()
{
assertThat('matches', self::$doc, hasXPath('count(user)', 3));
assertThat('matches', self::$doc, hasXPath('user[2]/id', 'bob'));
}
public function testProvidesConvenientShortcutForHasXPathCountEqualTo()
{
assertThat('matches', self::$doc, hasXPath('user[id = "charlie"]', 1));
}
public function testMatchesAcceptsXmlString()
{
assertThat('accepts XML string', self::$xml, hasXPath('user'));
}
public function testMatchesAcceptsHtmlString()
{
assertThat('accepts HTML string', self::$html, hasXPath('body/h1', 'Heading'));
}
public function testHasAReadableDescription()
{
$this->assertDescription(
'XML or HTML document with XPath "/users/user"',
hasXPath('/users/user')
);
$this->assertDescription(
'XML or HTML document with XPath "count(/users/user)" <2>',
hasXPath('/users/user', 2)
);
$this->assertDescription(
'XML or HTML document with XPath "/users/user/name"'
. ' a string starting with "Alice"',
hasXPath('/users/user/name', startsWith('Alice'))
);
}
public function testHasAReadableMismatchDescription()
{
$this->assertMismatchDescription(
'XPath returned no results',
hasXPath('/users/name'),
self::$doc
);
$this->assertMismatchDescription(
'XPath expression result was <3F>',
hasXPath('/users/user', 2),
self::$doc
);
$this->assertMismatchDescription(
'XPath returned ["alice", "bob", "charlie"]',
hasXPath('/users/user/id', 'Frank'),
self::$doc
);
}
}