Skip to content

Commit 28fe1d6

Browse files
committed
use a helper method to get an initialized Searcher, use withoutSnippets on all tests
1 parent cb80e60 commit 28fe1d6

10 files changed

Lines changed: 49 additions & 56 deletions

tests/SearcherTest.php

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -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);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
-
22
location: { column: 0, endLine: 2, startLine: 2 }
33
node: { variableName: obj, value: { symbol: ., left: { value: 'hello ', location: { column: 0, endLine: 2, startLine: 2 } }, right: { value: world, location: { column: 0, endLine: 2, startLine: 2 } }, value: '''hello '' . ''world''', location: { column: 0, endLine: 2, startLine: 2 } }, name: obj, location: { column: 0, endLine: 2, startLine: 2 } }
4-
snippet: { snippetLineCount: 2, lines: { 1: { selected: false, lineNumber: 1, value: '<?php', isSelected: false }, 2: { selected: true, lineNumber: 2, value: ' $obj = ''hello '' . ''world'';', isSelected: true } }, lineNumberStart: 2, lineNumberEnd: 2, lineNumbers: [1, 2], selectedBounds: { start: 2, end: 2 } }
4+
snippet: null
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
-
22
location: { column: 0, endLine: 2, startLine: 2 }
33
node: { variableName: obj, value: { symbol: +, left: { symbol: +, left: { value: 1, location: { column: 0, endLine: 2, startLine: 2 } }, right: { value: 3, location: { column: 0, endLine: 2, startLine: 2 } }, value: '1 + 3', location: { column: 0, endLine: 2, startLine: 2 } }, right: { value: 2, location: { column: 0, endLine: 2, startLine: 2 } }, value: '1 + 3 + 2', location: { column: 0, endLine: 2, startLine: 2 } }, name: obj, location: { column: 0, endLine: 2, startLine: 2 } }
4-
snippet: { snippetLineCount: 2, lines: { 1: { selected: false, lineNumber: 1, value: '<?php', isSelected: false }, 2: { selected: true, lineNumber: 2, value: ' $obj = 1 + 3 + 2;', isSelected: true } }, lineNumberStart: 2, lineNumberEnd: 2, lineNumbers: [1, 2], selectedBounds: { start: 2, end: 2 } }
4+
snippet: null
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
-
22
location: { column: 0, endLine: 4, startLine: 4 }
33
node: { variableName: myVar2, value: [{ key: null, value: { value: 1, location: { column: 0, endLine: 4, startLine: 4 } }, location: { column: 0, endLine: 4, startLine: 4 } }, { key: null, value: { value: 2, location: { column: 0, endLine: 4, startLine: 4 } }, location: { column: 0, endLine: 4, startLine: 4 } }, { key: null, value: { value: 3, location: { column: 0, endLine: 4, startLine: 4 } }, location: { column: 0, endLine: 4, startLine: 4 } }], name: myVar2, location: { column: 0, endLine: 4, startLine: 4 } }
4-
snippet: { snippetLineCount: 6, lines: { 1: { selected: false, lineNumber: 1, value: '<?php', isSelected: false }, 2: { selected: false, lineNumber: 2, value: ' $myVar = $obj->methodTwo(MyModel::find(1), $obj->methodOne(''two'', [2, 3]), [$this, ''handlerMethod'']);', isSelected: false }, 3: { selected: false, lineNumber: 3, value: '', isSelected: false }, 4: { selected: true, lineNumber: 4, value: ' $myVar2 = [1, 2, 3];', isSelected: true }, 5: { selected: false, lineNumber: 5, value: ' $myVar2 = [...$myVar2, $myVar->someProp, 4, 5, 6];', isSelected: false }, 6: { selected: false, lineNumber: 6, value: ' $myVar2 = [''one'' => 1, ''two'' => $anotherVar->someMethod()];', isSelected: false } }, lineNumberStart: 4, lineNumberEnd: 4, lineNumbers: [1, 2, 3, 4, 5, 6], selectedBounds: { start: 4, end: 4 } }
4+
snippet: null
55
-
66
location: { column: 0, endLine: 5, startLine: 5 }
77
node: { variableName: myVar2, value: [{ key: null, value: { name: myVar2, location: { column: 0, endLine: 5, startLine: 5 } }, location: { column: 0, endLine: 5, startLine: 5 } }, { key: null, value: { objectName: myVar, propertyName: someProp, location: { column: 0, endLine: 5, startLine: 5 } }, location: { column: 0, endLine: 5, startLine: 5 } }, { key: null, value: { value: 4, location: { column: 0, endLine: 5, startLine: 5 } }, location: { column: 0, endLine: 5, startLine: 5 } }, { key: null, value: { value: 5, location: { column: 0, endLine: 5, startLine: 5 } }, location: { column: 0, endLine: 5, startLine: 5 } }, { key: null, value: { value: 6, location: { column: 0, endLine: 5, startLine: 5 } }, location: { column: 0, endLine: 5, startLine: 5 } }], name: myVar2, location: { column: 0, endLine: 5, startLine: 5 } }
8-
snippet: { snippetLineCount: 6, lines: { 1: { selected: false, lineNumber: 1, value: '<?php', isSelected: false }, 2: { selected: false, lineNumber: 2, value: ' $myVar = $obj->methodTwo(MyModel::find(1), $obj->methodOne(''two'', [2, 3]), [$this, ''handlerMethod'']);', isSelected: false }, 3: { selected: false, lineNumber: 3, value: '', isSelected: false }, 4: { selected: false, lineNumber: 4, value: ' $myVar2 = [1, 2, 3];', isSelected: false }, 5: { selected: true, lineNumber: 5, value: ' $myVar2 = [...$myVar2, $myVar->someProp, 4, 5, 6];', isSelected: true }, 6: { selected: false, lineNumber: 6, value: ' $myVar2 = [''one'' => 1, ''two'' => $anotherVar->someMethod()];', isSelected: false } }, lineNumberStart: 5, lineNumberEnd: 5, lineNumbers: [1, 2, 3, 4, 5, 6], selectedBounds: { start: 5, end: 5 } }
8+
snippet: null
99
-
1010
location: { column: 0, endLine: 6, startLine: 6 }
1111
node: { variableName: myVar2, value: [{ key: { value: one, location: { column: 0, endLine: 6, startLine: 6 } }, value: { value: 1, location: { column: 0, endLine: 6, startLine: 6 } }, location: { column: 0, endLine: 6, startLine: 6 } }, { key: { value: two, location: { column: 0, endLine: 6, startLine: 6 } }, value: { variableName: anotherVar, methodName: someMethod, name: $anotherVar->someMethod, args: { }, location: { column: 0, endLine: 6, startLine: 6 } }, location: { column: 0, endLine: 6, startLine: 6 } }], name: myVar2, location: { column: 0, endLine: 6, startLine: 6 } }
12-
snippet: { snippetLineCount: 6, lines: { 1: { selected: false, lineNumber: 1, value: '<?php', isSelected: false }, 2: { selected: false, lineNumber: 2, value: ' $myVar = $obj->methodTwo(MyModel::find(1), $obj->methodOne(''two'', [2, 3]), [$this, ''handlerMethod'']);', isSelected: false }, 3: { selected: false, lineNumber: 3, value: '', isSelected: false }, 4: { selected: false, lineNumber: 4, value: ' $myVar2 = [1, 2, 3];', isSelected: false }, 5: { selected: false, lineNumber: 5, value: ' $myVar2 = [...$myVar2, $myVar->someProp, 4, 5, 6];', isSelected: false }, 6: { selected: true, lineNumber: 6, value: ' $myVar2 = [''one'' => 1, ''two'' => $anotherVar->someMethod()];', isSelected: true } }, lineNumberStart: 6, lineNumberEnd: 6, lineNumbers: [1, 2, 3, 4, 5, 6], selectedBounds: { start: 6, end: 6 } }
12+
snippet: null
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
-
22
location: { column: 0, endLine: 13, startLine: 13 }
33
node: { name: MyClass, location: { column: 0, endLine: 13, startLine: 13 } }
4-
snippet: { snippetLineCount: 8, lines: { 9: { selected: false, lineNumber: 9, value: ' {', isSelected: false }, 10: { selected: false, lineNumber: 10, value: ' Ray::rateLimiter()->count(5);', isSelected: false }, 11: { selected: false, lineNumber: 11, value: ' }', isSelected: false }, 12: { selected: false, lineNumber: 12, value: '', isSelected: false }, 13: { selected: true, lineNumber: 13, value: ' $obj = new MyClass();', isSelected: true }, 14: { selected: false, lineNumber: 14, value: '', isSelected: false }, 15: { selected: false, lineNumber: 15, value: ' $obj->withData([123])->send();', isSelected: false }, 16: { selected: false, lineNumber: 16, value: '', isSelected: false } }, lineNumberStart: 13, lineNumberEnd: 13, lineNumbers: [9, 10, 11, 12, 13, 14, 15, 16], selectedBounds: { start: 13, end: 13 } }
4+
snippet: null
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
-
22
location: { column: 0, endLine: 10, startLine: 10 }
33
node: { className: Ray, methodName: rateLimiter, name: 'Ray::rateLimiter', args: { }, location: { column: 0, endLine: 10, startLine: 10 } }
4-
snippet: { snippetLineCount: 8, lines: { 6: { selected: false, lineNumber: 6, value: ' echo strtoupper(''test'') . PHP_EOL;', isSelected: false }, 7: { selected: false, lineNumber: 7, value: '', isSelected: false }, 8: { selected: false, lineNumber: 8, value: ' function test1()', isSelected: false }, 9: { selected: false, lineNumber: 9, value: ' {', isSelected: false }, 10: { selected: true, lineNumber: 10, value: ' Ray::rateLimiter()->count(5);', isSelected: true }, 11: { selected: false, lineNumber: 11, value: ' }', isSelected: false }, 12: { selected: false, lineNumber: 12, value: '', isSelected: false }, 13: { selected: false, lineNumber: 13, value: ' $obj = new MyClass();', isSelected: false } }, lineNumberStart: 10, lineNumberEnd: 10, lineNumbers: [6, 7, 8, 9, 10, 11, 12, 13], selectedBounds: { start: 10, end: 10 } }
4+
snippet: null
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
-
22
location: { column: 0, endLine: 18, startLine: 18 }
33
node: { className: AnotherClass, methodName: enabled, name: 'AnotherClass::enabled', args: { }, location: { column: 0, endLine: 18, startLine: 18 } }
4-
snippet: { snippetLineCount: 8, lines: { 13: { selected: false, lineNumber: 13, value: ' $obj = new MyClass();', isSelected: false }, 14: { selected: false, lineNumber: 14, value: '', isSelected: false }, 15: { selected: false, lineNumber: 15, value: ' $obj->withData([123])->send();', isSelected: false }, 16: { selected: false, lineNumber: 16, value: '', isSelected: false }, 17: { selected: false, lineNumber: 17, value: ' $isDisabled = AnotherClass::disabled();', isSelected: false }, 18: { selected: true, lineNumber: 18, value: ' $isEnabled = AnotherClass::enabled();', isSelected: true }, 19: { selected: false, lineNumber: 19, value: '', isSelected: false }, 20: { selected: false, lineNumber: 20, value: ' $test = Ray::$someProperty->enable();', isSelected: false } }, lineNumberStart: 18, lineNumberEnd: 18, lineNumbers: [13, 14, 15, 16, 17, 18, 19, 20], selectedBounds: { start: 18, end: 18 } }
4+
snippet: null
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
-
22
location: { column: 0, endLine: 20, startLine: 20 }
33
node: { objectName: Ray, propertyName: someProperty, location: { column: 0, endLine: 20, startLine: 20 } }
4-
snippet: { snippetLineCount: 8, lines: { 13: { selected: false, lineNumber: 13, value: ' $obj = new MyClass();', isSelected: false }, 14: { selected: false, lineNumber: 14, value: '', isSelected: false }, 15: { selected: false, lineNumber: 15, value: ' $obj->withData([123])->send();', isSelected: false }, 16: { selected: false, lineNumber: 16, value: '', isSelected: false }, 17: { selected: false, lineNumber: 17, value: ' $isDisabled = AnotherClass::disabled();', isSelected: false }, 18: { selected: false, lineNumber: 18, value: ' $isEnabled = AnotherClass::enabled();', isSelected: false }, 19: { selected: false, lineNumber: 19, value: '', isSelected: false }, 20: { selected: true, lineNumber: 20, value: ' $test = Ray::$someProperty->enable();', isSelected: true } }, lineNumberStart: 20, lineNumberEnd: 20, lineNumbers: [13, 14, 15, 16, 17, 18, 19, 20], selectedBounds: { start: 20, end: 20 } }
4+
snippet: null

0 commit comments

Comments
 (0)