Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
namespace Hamcrest\Xml;
3
 
4
class HasXPathTest extends \Hamcrest\AbstractMatcherTest
5
{
6
    protected static $xml;
7
    protected static $doc;
8
    protected static $html;
9
 
10
    public static function setUpBeforeClass()
11
    {
12
        self::$xml = <<<XML
13
<?xml version="1.0"?>
14
<users>
15
    <user>
16
        <id>alice</id>
17
        <name>Alice Frankel</name>
18
        <role>admin</role>
19
    </user>
20
    <user>
21
        <id>bob</id>
22
        <name>Bob Frankel</name>
23
        <role>user</role>
24
    </user>
25
    <user>
26
        <id>charlie</id>
27
        <name>Charlie Chan</name>
28
        <role>user</role>
29
    </user>
30
</users>
31
XML;
32
        self::$doc = new \DOMDocument();
33
        self::$doc->loadXML(self::$xml);
34
 
35
        self::$html = <<<HTML
36
<html>
37
    <head>
38
        <title>Home Page</title>
39
    </head>
40
    <body>
41
        <h1>Heading</h1>
42
        <p>Some text</p>
43
    </body>
44
</html>
45
HTML;
46
    }
47
 
48
    protected function createMatcher()
49
    {
50
        return \Hamcrest\Xml\HasXPath::hasXPath('/users/user');
51
    }
52
 
53
    public function testMatchesWhenXPathIsFound()
54
    {
55
        assertThat('one match', self::$doc, hasXPath('user[id = "bob"]'));
56
        assertThat('two matches', self::$doc, hasXPath('user[role = "user"]'));
57
    }
58
 
59
    public function testDoesNotMatchWhenXPathIsNotFound()
60
    {
61
        assertThat(
62
            'no match',
63
            self::$doc,
64
            not(hasXPath('user[contains(id, "frank")]'))
65
        );
66
    }
67
 
68
    public function testMatchesWhenExpressionWithoutMatcherEvaluatesToTrue()
69
    {
70
        assertThat(
71
            'one match',
72
            self::$doc,
73
            hasXPath('count(user[id = "bob"])')
74
        );
75
    }
76
 
77
    public function testDoesNotMatchWhenExpressionWithoutMatcherEvaluatesToFalse()
78
    {
79
        assertThat(
80
            'no matches',
81
            self::$doc,
82
            not(hasXPath('count(user[id = "frank"])'))
83
        );
84
    }
85
 
86
    public function testMatchesWhenExpressionIsEqual()
87
    {
88
        assertThat(
89
            'one match',
90
            self::$doc,
91
            hasXPath('count(user[id = "bob"])', 1)
92
        );
93
        assertThat(
94
            'two matches',
95
            self::$doc,
96
            hasXPath('count(user[role = "user"])', 2)
97
        );
98
    }
99
 
100
    public function testDoesNotMatchWhenExpressionIsNotEqual()
101
    {
102
        assertThat(
103
            'no match',
104
            self::$doc,
105
            not(hasXPath('count(user[id = "frank"])', 2))
106
        );
107
        assertThat(
108
            'one match',
109
            self::$doc,
110
            not(hasXPath('count(user[role = "admin"])', 2))
111
        );
112
    }
113
 
114
    public function testMatchesWhenContentMatches()
115
    {
116
        assertThat(
117
            'one match',
118
            self::$doc,
119
            hasXPath('user/name', containsString('ice'))
120
        );
121
        assertThat(
122
            'two matches',
123
            self::$doc,
124
            hasXPath('user/role', equalTo('user'))
125
        );
126
    }
127
 
128
    public function testDoesNotMatchWhenContentDoesNotMatch()
129
    {
130
        assertThat(
131
            'no match',
132
            self::$doc,
133
            not(hasXPath('user/name', containsString('Bobby')))
134
        );
135
        assertThat(
136
            'no matches',
137
            self::$doc,
138
            not(hasXPath('user/role', equalTo('owner')))
139
        );
140
    }
141
 
142
    public function testProvidesConvenientShortcutForHasXPathEqualTo()
143
    {
144
        assertThat('matches', self::$doc, hasXPath('count(user)', 3));
145
        assertThat('matches', self::$doc, hasXPath('user[2]/id', 'bob'));
146
    }
147
 
148
    public function testProvidesConvenientShortcutForHasXPathCountEqualTo()
149
    {
150
        assertThat('matches', self::$doc, hasXPath('user[id = "charlie"]', 1));
151
    }
152
 
153
    public function testMatchesAcceptsXmlString()
154
    {
155
        assertThat('accepts XML string', self::$xml, hasXPath('user'));
156
    }
157
 
158
    public function testMatchesAcceptsHtmlString()
159
    {
160
        assertThat('accepts HTML string', self::$html, hasXPath('body/h1', 'Heading'));
161
    }
162
 
163
    public function testHasAReadableDescription()
164
    {
165
        $this->assertDescription(
166
            'XML or HTML document with XPath "/users/user"',
167
            hasXPath('/users/user')
168
        );
169
        $this->assertDescription(
170
            'XML or HTML document with XPath "count(/users/user)" <2>',
171
            hasXPath('/users/user', 2)
172
        );
173
        $this->assertDescription(
174
            'XML or HTML document with XPath "/users/user/name"'
175
            . ' a string starting with "Alice"',
176
            hasXPath('/users/user/name', startsWith('Alice'))
177
        );
178
    }
179
 
180
    public function testHasAReadableMismatchDescription()
181
    {
182
        $this->assertMismatchDescription(
183
            'XPath returned no results',
184
            hasXPath('/users/name'),
185
            self::$doc
186
        );
187
        $this->assertMismatchDescription(
188
            'XPath expression result was <3F>',
189
            hasXPath('/users/user', 2),
190
            self::$doc
191
        );
192
        $this->assertMismatchDescription(
193
            'XPath returned ["alice", "bob", "charlie"]',
194
            hasXPath('/users/user/id', 'Frank'),
195
            self::$doc
196
        );
197
    }
198
}