@@ -28,61 +28,54 @@ class Issue126Test extends TestCase
2828 private const NESTED_XML = '<?xml version="1.0"?><root><p><i>a</i><b>x</b></p><p><i>b</i></p><i>c</i></root> ' ;
2929
3030 /**
31- * The tables from #123 and #124, as find() answers them.
31+ * The tables from #123 and #124 against each factory's document: the selector, what
32+ * find() matches, and the list items left behind.
3233 *
3334 * @return array
3435 */
3536 public function selectorProvider (): array
3637 {
37- return [
38- 'matches (#123 ) ' => [' li:matches(.x) ' , ['b ' ]],
39- 'not eq(0) (#124 ) ' => [' li:not(:eq(0)) ' , [ ' b ' , ' c ' ]],
40- 'not first (#124 ) ' => [' li:not(:first) ' , [ ' b ' , ' c ' ]],
41- 'not last (#124 ) ' => [' li:not(:last) ' , [ ' a ' , ' b ' ]],
42- 'not odd (#124 ) ' => [' li:not(:odd) ' , [ ' a ' , ' c ' ]],
43- 'not class (#124 ) ' => [' li:not(.x) ' , [ ' a ' , ' c ' ]],
38+ $ selectors = [
39+ 'li: matches(.x ) ' => [[ ' b ' ] , ['a ' , ' c ' ]],
40+ 'li: not(: eq(0)) ' => [[ ' b ' , ' c ' ], [ ' a ' ]],
41+ 'li: not(:first ) ' => [[ ' b ' , ' c ' ], [ ' a ' ]],
42+ 'li: not(:last ) ' => [[ ' a ' , ' b ' ], [ ' c ' ]],
43+ 'li: not(:odd ) ' => [[ ' a ' , ' c ' ], [ ' b ' ]],
44+ 'li: not(.x ) ' => [[ ' a ' , ' c ' ], [ ' b ' ]],
4445 ];
45- }
4646
47- /**
48- * Each selector from selectorProvider() against each factory's document.
49- *
50- * @return array
51- */
52- public function factorySelectorProvider (): array
53- {
5447 $ cases = [];
5548 foreach (['qp ' => self ::LIST_XML , 'htmlqp ' => self ::LIST_HTML , 'html5qp ' => self ::LIST_HTML ] as $ factory => $ markup ) {
56- foreach ($ this -> selectorProvider () as $ name => $ case ) {
57- $ cases [$ factory . ': ' . $ name ] = array_merge ( [$ factory , $ markup] , $ case ) ;
49+ foreach ($ selectors as $ selector => list ( $ matched , $ left ) ) {
50+ $ cases [$ factory . ' ' . $ selector ] = [$ factory , $ markup , $ selector , $ matched , $ left ] ;
5851 }
5952 }
6053
6154 return $ cases ;
6255 }
6356
6457 /**
65- * @dataProvider factorySelectorProvider
58+ * @dataProvider selectorProvider
6659 */
67- public function testRemoveAgreesWithFind (string $ factory , string $ markup , string $ selector , array $ expected )
60+ public function testRemoveAgreesWithFind (string $ factory , string $ markup , string $ selector , array $ matched , array $ left )
6861 {
69- $ this ->assertSame ($ expected , $ this ->ids ($ factory ($ markup )->find ($ selector )));
62+ $ this ->assertSame ($ matched , $ this ->ids ($ factory ($ markup )->find ($ selector )));
7063
7164 $ qp = $ factory ($ markup );
72- $ this ->assertSame ($ expected , $ this ->ids ($ qp ->remove ($ selector )));
73- $ this ->assertSame (array_values ( array_diff ([ ' a ' , ' b ' , ' c ' ], $ expected )) , $ this ->ids ($ qp ->find ('li ' )));
65+ $ this ->assertSame ($ matched , $ this ->ids ($ qp ->remove ($ selector )));
66+ $ this ->assertSame ($ left , $ this ->ids ($ qp ->find ('li ' )));
7467 }
7568
7669 /**
77- * @dataProvider factorySelectorProvider
70+ * @dataProvider selectorProvider
7871 */
79- public function testReplaceAllAgreesWithFind (string $ factory , string $ markup , string $ selector , array $ expected )
72+ public function testReplaceAllAgreesWithFind (string $ factory , string $ markup , string $ selector , array $ matched , array $ left )
8073 {
8174 $ document = $ factory ($ markup )->get (0 )->ownerDocument ;
8275 qp ('<?xml version="1.0"?><root><z/></root> ' , 'z ' )->replaceAll ($ selector , $ document );
8376
84- $ this ->assertSame (array_values ( array_diff ([ ' a ' , ' b ' , ' c ' ], $ expected )) , $ this ->ids (qp ($ document )->find ('li ' )));
85- $ this ->assertCount (count ($ expected ), qp ($ document )->find ('z ' ));
77+ $ this ->assertSame ($ left , $ this ->ids (qp ($ document )->find ('li ' )));
78+ $ this ->assertCount (count ($ matched ), qp ($ document )->find ('z ' ));
8679 }
8780
8881 public function testRemoveSearchesDescendantsOnly ()
@@ -119,42 +112,24 @@ public function testReplaceAllSearchesTheWholeDocument()
119112 $ this ->assertSame (['z ' , 'z ' , 'i ' ], $ this ->tags (qp ($ document )->children ()));
120113 }
121114
122- /**
123- * @return string[]
124- */
125115 private function ids (DOMQuery $ qp ): array
126116 {
127- $ ids = [];
128- foreach ($ qp ->get () as $ node ) {
129- $ ids [] = $ node ->getAttribute ('id ' );
130- }
131-
132- return $ ids ;
117+ return array_map (function ($ node ) {
118+ return $ node ->getAttribute ('id ' );
119+ }, $ qp ->get ());
133120 }
134121
135- /**
136- * @return string[]
137- */
138122 private function text (DOMQuery $ qp ): array
139123 {
140- $ text = [];
141- foreach ($ qp ->get () as $ node ) {
142- $ text [] = $ node ->textContent ;
143- }
144-
145- return $ text ;
124+ return array_map (function ($ node ) {
125+ return $ node ->textContent ;
126+ }, $ qp ->get ());
146127 }
147128
148- /**
149- * @return string[]
150- */
151129 private function tags (DOMQuery $ qp ): array
152130 {
153- $ tags = [];
154- foreach ($ qp ->get () as $ node ) {
155- $ tags [] = $ node ->tagName ;
156- }
157-
158- return $ tags ;
131+ return array_map (function ($ node ) {
132+ return $ node ->tagName ;
133+ }, $ qp ->get ());
159134 }
160135}
0 commit comments