@@ -11,13 +11,25 @@ class SearcherTest extends TestCase
1111{
1212 use MatchesSnapshots;
1313
14+ /** @var Searcher */
15+ protected $ searcher ;
16+
17+ protected function setUp (): void
18+ {
19+ parent ::setUp ();
20+
21+ $ this ->searcher = $ this ->getSearcher ();
22+ }
23+
24+ protected function getSearcher (): Searcher
25+ {
26+ return (new Searcher ())->withoutSnippets ();
27+ }
28+
1429 /** @test */
1530 public function it_searches_code_strings ()
1631 {
17- $ searcher = new Searcher ();
18-
19- $ results = $ searcher
20- ->withoutSnippets ()
32+ $ results = $ this ->searcher
2133 ->functions (['strtolower ' , 'strtoupper ' ])
2234 ->searchCode ('<? ' . "php \n\$myVar = strtolower('test'); \n" );
2335
@@ -27,18 +39,15 @@ public function it_searches_code_strings()
2739 /** @test */
2840 public function it_searches_for_function_calls ()
2941 {
30- $ searcher = new Searcher ();
3142 $ file = new File (tests_path ('data/file1.php ' ));
3243
33- $ results = $ searcher
34- ->withoutSnippets ()
44+ $ results = $ this ->searcher
3545 ->functions (['strtolower ' , 'strtoupper ' ])
3646 ->search ($ file );
3747
3848 $ this ->assertMatchesSnapshot ($ results ->results );
3949
40- $ results = $ searcher
41- ->withoutSnippets ()
50+ $ results = $ this ->searcher
4251 ->functions (['strtoupper ' , 'printf ' , 'strtolower ' ])
4352 ->search ($ file );
4453
@@ -48,11 +57,9 @@ public function it_searches_for_function_calls()
4857 /** @test */
4958 public function it_searches_for_function_calls_without_snippets ()
5059 {
51- $ searcher = new Searcher ();
5260 $ file = new File (tests_path ('data/file1.php ' ));
5361
54- $ results = $ searcher
55- ->withoutSnippets ()
62+ $ results = $ this ->searcher
5663 ->functions (['strtolower ' , 'strtoupper ' ])
5764 ->search ($ file );
5865
@@ -62,12 +69,8 @@ public function it_searches_for_function_calls_without_snippets()
6269 /** @test */
6370 public function it_searches_for_multi_line_function_calls ()
6471 {
65- $ searcher = new Searcher ();
66- $ file = new File (tests_path ('data/file1.php ' ));
67-
68- $ results = $ searcher
72+ $ results = $ this ->searcher
6973 ->functions (['strtolower ' , 'strtoupper ' ])
70- ->withoutSnippets ()
7174 ->searchCode ('<? ' ."php
7275 \$myStr = strtolower(
7376 'test '.
@@ -81,10 +84,9 @@ public function it_searches_for_multi_line_function_calls()
8184 /** @test */
8285 public function it_searches_for_static_method_calls ()
8386 {
84- $ searcher = new Searcher ();
8587 $ file = new File (tests_path ('data/file1.php ' ));
8688
87- $ results = $ searcher
89+ $ results = $ this -> searcher
8890 ->static (['MyClass ' , 'Ray ' ])
8991 ->search ($ file );
9092
@@ -94,10 +96,9 @@ public function it_searches_for_static_method_calls()
9496 /** @test */
9597 public function it_searches_for_static_property_accesses ()
9698 {
97- $ searcher = new Searcher ();
9899 $ file = new File (tests_path ('data/file1.php ' ));
99100
100- $ results = $ searcher
101+ $ results = $ this -> searcher
101102 ->static (['Ray::$someProperty ' ])
102103 ->search ($ file );
103104
@@ -107,10 +108,9 @@ public function it_searches_for_static_property_accesses()
107108 /** @test */
108109 public function it_searches_for_static_method_calls_containing_the_class_and_method_name ()
109110 {
110- $ searcher = new Searcher ();
111111 $ file = new File (tests_path ('data/file1.php ' ));
112112
113- $ results = $ searcher
113+ $ results = $ this -> searcher
114114 ->static (['AnotherClass::enabled ' ])
115115 ->search ($ file );
116116
@@ -121,10 +121,9 @@ public function it_searches_for_static_method_calls_containing_the_class_and_met
121121 /** @test */
122122 public function it_searches_for_var_assignments ()
123123 {
124- $ searcher = new Searcher ();
125124 $ file = new File (tests_path ('data/file1.php ' ));
126125
127- $ results = $ searcher
126+ $ results = $ this -> searcher
128127 ->assignments (['obj ' ])
129128 ->search ($ file );
130129
@@ -134,10 +133,9 @@ public function it_searches_for_var_assignments()
134133 /** @test */
135134 public function it_searches_for_classes_created_by_new ()
136135 {
137- $ searcher = new Searcher ();
138136 $ file = new File (tests_path ('data/file1.php ' ));
139137
140- $ results = $ searcher
138+ $ results = $ this -> searcher
141139 ->classes (['MyClass ' ])
142140 ->search ($ file );
143141
@@ -147,12 +145,10 @@ public function it_searches_for_classes_created_by_new()
147145 /** @test */
148146 public function it_searches_for_class_definitions ()
149147 {
150- $ searcher = new Searcher ();
151148 $ file = new File (tests_path ('data/file3.php ' ));
152149
153- $ results = $ searcher
150+ $ results = $ this -> searcher
154151 ->classes (['MyClass1 ' ])
155- ->withoutSnippets ()
156152 ->search ($ file );
157153
158154 $ this ->assertMatchesSnapshot ($ results ->results );
@@ -161,14 +157,14 @@ public function it_searches_for_class_definitions()
161157 /** @test */
162158 public function it_only_returns_the_functions_being_searched_for ()
163159 {
164- $ results = ( new Searcher () )
160+ $ results = $ this -> getSearcher ( )
165161 ->functions (['strtolower ' ])
166162 ->searchCode ('<? ' . "php \n\$myVar = strtolower(strtoupper('test')); \n" );
167163
168164 $ this ->assertCount (1 , $ results ->results );
169165 $ this ->assertEquals ('strtolower ' , $ results ->results [0 ]->node ->name ());
170166
171- $ results = ( new Searcher ())
167+ $ results = $ this -> searcher
172168 ->functions (['strtoupper ' ])
173169 ->searchCode ('<? ' . "php \n\$myVar = strtolower(strtoupper('test')); \n" );
174170
@@ -179,7 +175,7 @@ public function it_only_returns_the_functions_being_searched_for()
179175 /** @test */
180176 public function it_finds_methods ()
181177 {
182- $ results = ( new Searcher ())
178+ $ results = $ this -> searcher
183179 ->methods (['methodTwo ' ])
184180 ->searchCode ('<? ' . "php \n\$myVar = \$obj->methodOne('one'); \$obj->methodTwo( \$obj->methodOne('two')); \n" );
185181
@@ -190,7 +186,7 @@ public function it_finds_methods()
190186 /** @test */
191187 public function it_transforms_nested_calls_and_arguments ()
192188 {
193- $ results = ( new Searcher ())
189+ $ results = $ this -> searcher
194190 ->methods (['methodTwo ' ])
195191 ->searchCode ('<? ' . "php \$obj->methodTwo(MyModel::find(1), \$obj->methodOne('two', [2, 3]), [ \$this, 'handlerMethod']); \n" );
196192
@@ -201,7 +197,7 @@ public function it_transforms_nested_calls_and_arguments()
201197 /** @test */
202198 public function it_finds_complex_assignments ()
203199 {
204- $ results = ( new Searcher ())
200+ $ results = $ this -> searcher
205201 ->assignments (['myVar2 ' ])
206202 ->searchCode ('<? ' . "php
207203 \$myVar = \$obj->methodTwo(MyModel::find(1), \$obj->methodOne('two', [2, 3]), [ \$this, 'handlerMethod']); \n
@@ -217,7 +213,7 @@ public function it_finds_complex_assignments()
217213 /** @test */
218214 public function it_finds_binary_operations ()
219215 {
220- $ results = ( new Searcher ())
216+ $ results = $ this -> searcher
221217 ->assignments (['obj ' ])
222218 ->searchCode ('<? ' . "php
223219 \$obj = 1 + 3 + 2;
@@ -230,7 +226,7 @@ public function it_finds_binary_operations()
230226 /** @test */
231227 public function it_finds_assign_operations ()
232228 {
233- $ results = ( new Searcher ())
229+ $ results = $ this -> searcher
234230 ->assignments (['obj ' ])
235231 ->searchCode ('<? ' . "php
236232 \$obj = 'hello ' . 'world';
@@ -243,7 +239,7 @@ public function it_finds_assign_operations()
243239 /** @test */
244240 public function it_finds_variables ()
245241 {
246- $ results = ( new Searcher ())
242+ $ results = $ this -> searcher
247243 ->variables (['obj ' ])
248244 ->searchCode ('<? ' . "php \n\$myVar = \$obj->methodOne('one'); \$obj->methodTwo('two'); \n" );
249245
@@ -255,7 +251,7 @@ public function it_finds_variables()
255251 /** @test */
256252 public function it_finds_variables_using_regex ()
257253 {
258- $ results = ( new Searcher ())
254+ $ results = $ this -> searcher
259255 ->variables (['/obj[AB]/ ' ])
260256 ->searchCode ('<? ' . "php \n\$objC = \$objA->methodOne('one'); \$objB->methodTwo('two'); \n" );
261257
@@ -267,19 +263,16 @@ public function it_finds_variables_using_regex()
267263 /** @test */
268264 public function it_searches_for_function_definitions ()
269265 {
270- $ searcher = new Searcher ();
271266 $ file = new File (tests_path ('data/file2.php ' ));
272267
273- $ results = $ searcher
268+ $ results = $ this -> getSearcher ()
274269 ->functions (['test2 ' ])
275- ->withoutSnippets ()
276270 ->search ($ file );
277271
278272 $ this ->assertMatchesSnapshot ($ results ->results );
279273
280- $ results = $ searcher
274+ $ results = $ this -> getSearcher ()
281275 ->functions (['/test\d+/ ' ])
282- ->withoutSnippets ()
283276 ->search ($ file );
284277
285278 $ this ->assertMatchesSnapshot ($ results ->results );
0 commit comments