|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
namespace PhpQuery\Tests; |
|
3
|
|
|
use PhpQuery\PhpQuery; |
|
4
|
|
|
use PhpQuery\PhpQueryObject; |
|
5
|
|
|
|
|
6
|
|
|
PhpQuery::use_function(__NAMESPACE__); |
|
7
|
|
|
|
|
8
|
|
|
class BasicTest extends \PHPUnit_Framework_TestCase { |
|
9
|
|
|
function provider() { |
|
|
|
|
|
|
10
|
|
|
// TODO change filename |
|
11
|
|
|
return array( |
|
12
|
|
|
array( |
|
13
|
|
|
PhpQuery::newDocumentFile(__DIR__ . '/test.html') |
|
14
|
|
|
) |
|
15
|
|
|
); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param PhpQueryObject $pq |
|
20
|
|
|
* @dataProvider provider |
|
21
|
|
|
* @return void |
|
22
|
|
|
*/ |
|
23
|
|
|
function testFilterWithPseudoclass($pq) { |
|
|
|
|
|
|
24
|
|
|
$pq = $pq->find('p')->filter('.body:gt(1)'); |
|
25
|
|
|
$result = array( |
|
26
|
|
|
'p.body', |
|
27
|
|
|
); |
|
28
|
|
|
|
|
29
|
|
|
$this->assertTrue($pq->whois() == $result); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param PhpQueryObject $pq |
|
34
|
|
|
* @dataProvider provider |
|
35
|
|
|
* @return void |
|
36
|
|
|
*/ |
|
37
|
|
|
function testSlice($pq) { |
|
|
|
|
|
|
38
|
|
|
$testResult = array( |
|
39
|
|
|
'li#testID', |
|
40
|
|
|
); |
|
41
|
|
|
$pq = $pq->find('li')->slice(1, 2); |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
$this->assertTrue($pq->whois() == $testResult); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param PhpQueryObject $pq |
|
48
|
|
|
* @dataProvider provider |
|
49
|
|
|
* @return void |
|
50
|
|
|
*/ |
|
51
|
|
|
function testSlice2($pq) { |
|
|
|
|
|
|
52
|
|
|
// SLICE2 |
|
53
|
|
|
$testResult = array( |
|
54
|
|
|
'li#testID', |
|
55
|
|
|
'li', |
|
56
|
|
|
'li#i_have_nested_list', |
|
57
|
|
|
'li.nested', |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
$pq = $pq->find('li')->slice(1, -1); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
$this->assertTrue($pq->whois() == $testResult); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return void |
|
67
|
|
|
*/ |
|
68
|
|
|
function testMultiInsert() { |
|
|
|
|
|
|
69
|
|
|
// Multi-insert |
|
70
|
|
|
$pq = PhpQuery::newDocument('<li><span class="field1"></span><span class="field1"></span></li>')->find('.field1')->php('longlongtest'); |
|
71
|
|
|
$validResult = '<li><span class="field1"><php>longlongtest</php></span><span class="field1"><php>longlongtest</php></span></li>'; |
|
72
|
|
|
similar_text($pq->htmlOuter(), $validResult, $similarity); |
|
73
|
|
|
|
|
74
|
|
|
$this->assertGreaterThan(80, $similarity); |
|
75
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param PhpQueryObject $pq |
|
80
|
|
|
* @dataProvider provider |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
function testIndex($pq) { |
|
|
|
|
|
|
84
|
|
|
$testResult = 1; |
|
85
|
|
|
$pq = $pq->find('p')->index($pq->find('p.title:first')); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertTrue($pq == $testResult); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param PhpQueryObject $pq |
|
92
|
|
|
* @dataProvider provider |
|
93
|
|
|
* @return void |
|
94
|
|
|
*/ |
|
95
|
|
|
function testClone($pq) { |
|
|
|
|
|
|
96
|
|
|
$testResult = 3; |
|
97
|
|
|
$document = null; |
|
98
|
|
|
$pq = $pq->toReference($document)->find('p:first'); |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
View Code Duplication |
foreach (array( |
|
|
|
|
|
|
101
|
|
|
0, |
|
102
|
|
|
1, |
|
103
|
|
|
2 |
|
104
|
|
|
) as $i) { |
|
105
|
|
|
$pq->clone()->addClass("clone-test")->addClass("class-$i")->insertBefore($pq); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$size = $document->find('.clone-test')->size(); |
|
109
|
|
|
$this->assertEquals($testResult, $size); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param PhpQueryObject $pq |
|
114
|
|
|
* @dataProvider provider |
|
115
|
|
|
* @return void |
|
116
|
|
|
*/ |
|
117
|
|
|
function testNextSibling($pq) { |
|
|
|
|
|
|
118
|
|
|
$testResult = 3; |
|
|
|
|
|
|
119
|
|
|
$document = null; |
|
|
|
|
|
|
120
|
|
|
$result = $pq->find('li:first')->next()->next()->prev()->is('#testID'); |
|
121
|
|
|
|
|
122
|
|
|
$this->assertTrue($result); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param PhpQueryObject $pq |
|
127
|
|
|
* @dataProvider provider |
|
128
|
|
|
* @return void |
|
129
|
|
|
*/ |
|
130
|
|
|
function testSimpleDataInsertion($pq) { |
|
|
|
|
|
|
131
|
|
|
$testName = 'Simple data insertion'; |
|
|
|
|
|
|
132
|
|
|
$testResult = <<<EOF |
|
133
|
|
|
<div class="articles"> |
|
134
|
|
|
div.articles text node |
|
135
|
|
|
<ul> |
|
136
|
|
|
|
|
137
|
|
|
<li> |
|
138
|
|
|
<p>This is paragraph of first LI</p> |
|
139
|
|
|
<p class="title">News 1 title</p> |
|
140
|
|
|
<p class="body">News 1 body</p> |
|
141
|
|
|
</li> |
|
142
|
|
|
|
|
143
|
|
|
<li> |
|
144
|
|
|
<p>This is paragraph of first LI</p> |
|
145
|
|
|
<p class="title">News 2 title</p> |
|
146
|
|
|
<p class="body">News 2 body</p> |
|
147
|
|
|
</li> |
|
148
|
|
|
<li> |
|
149
|
|
|
<p>This is paragraph of first LI</p> |
|
150
|
|
|
<p class="title">News 3</p> |
|
151
|
|
|
<p class="body">News 3 body</p> |
|
152
|
|
|
</li> |
|
153
|
|
|
</ul> |
|
154
|
|
|
<p class="after">paragraph after UL</p> |
|
155
|
|
|
</div> |
|
156
|
|
|
EOF; |
|
157
|
|
|
$expected_pq = PhpQuery::newDocumentHTML($testResult); |
|
158
|
|
|
$rows = array( |
|
159
|
|
|
array( |
|
160
|
|
|
'title' => 'News 1 title', |
|
161
|
|
|
'body' => 'News 1 body', |
|
162
|
|
|
), |
|
163
|
|
|
array( |
|
164
|
|
|
'title' => 'News 2 title', |
|
165
|
|
|
'body' => 'News 2 body', |
|
166
|
|
|
), |
|
167
|
|
|
array( |
|
168
|
|
|
'title' => 'News 3', |
|
169
|
|
|
'body' => 'News 3 body', |
|
170
|
|
|
), |
|
171
|
|
|
); |
|
172
|
|
|
$articles = $pq->find('.articles ul'); |
|
173
|
|
|
$rowSrc = $articles->find('li')->remove()->eq(0); |
|
174
|
|
View Code Duplication |
foreach ($rows as $r) { |
|
|
|
|
|
|
175
|
|
|
$row = $rowSrc->_clone(); |
|
176
|
|
|
foreach ($r as $field => $value) { |
|
177
|
|
|
$row->find(".{$field}")->html($value); |
|
178
|
|
|
// die($row->htmlOuter()); |
|
|
|
|
|
|
179
|
|
|
} |
|
180
|
|
|
$row->appendTo($articles); |
|
181
|
|
|
} |
|
182
|
|
|
$result = $pq->find('.articles')->htmlOuter(); |
|
|
|
|
|
|
183
|
|
|
// print htmlspecialchars("<pre>{$result}</pre>").'<br />'; |
|
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
$this->assertEqualXMLStructure($expected_pq->find('.articles')->elements[0], $pq->find('.articles')->elements[0]); |
|
186
|
|
|
// $this->assertEqualXMLStructure(DOMDocument::loadHTML($testResult)->documentElement, DOMDocument::loadHTML($result)->documentElement); |
|
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param PhpQueryObject $pq |
|
|
|
|
|
|
191
|
|
|
* @dataProvider provider |
|
192
|
|
|
* @return void |
|
193
|
|
|
*/ |
|
194
|
|
|
public function testCssParser() { |
|
195
|
|
|
PhpQuery::$enableCssShorthand = FALSE; |
|
196
|
|
|
|
|
197
|
|
|
$expected_html = '<div style="color:red;display:none;margin:20px;padding:10px"><span>Hello World!</span></div>'; |
|
198
|
|
|
$expected_pq = PhpQuery::newDocumentHTML($expected_html); |
|
199
|
|
|
|
|
200
|
|
|
$test_pq = PhpQuery::newDocumentHTML('<div style="margin:10px; padding:10px">'); |
|
|
|
|
|
|
201
|
|
|
$test = pq('div'); |
|
202
|
|
|
$test->append('<span>Hello World!</span>'); |
|
203
|
|
|
$test->hide(); |
|
204
|
|
|
$test->css('color', 'red'); |
|
205
|
|
|
$test->css('margin', '20px'); |
|
206
|
|
|
$this->assertEqualXMLStructure($expected_pq->find('div')->elements[0], $test->elements[0]); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.