Skip to content

Commit 64b3778

Browse files
committed
test: refactor: extract createFilters() method
1 parent 3ea1ede commit 64b3778

1 file changed

Lines changed: 46 additions & 71 deletions

File tree

tests/system/Filters/FiltersTest.php

Lines changed: 46 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ protected function setUp(): void
5555
$this->response = Services::response();
5656
}
5757

58+
private function createFilters($config, $request = null): Filters
59+
{
60+
$request = $request ?? Services::request();
61+
62+
return new Filters($config, $request, $this->response);
63+
}
64+
5865
public function testProcessMethodDetectsCLI()
5966
{
6067
$_SERVER['argv'] = [
@@ -69,8 +76,10 @@ public function testProcessMethodDetectsCLI()
6976
'cli' => ['foo'],
7077
],
7178
];
72-
$this->request = new CLIRequest(new MockAppConfig());
73-
$filters = new Filters((object) $config, $this->request, $this->response);
79+
$filters = $this->createFilters(
80+
(object) $config,
81+
new CLIRequest(new MockAppConfig())
82+
);
7483

7584
$expected = [
7685
'before' => ['foo'],
@@ -89,8 +98,7 @@ public function testProcessMethodDetectsGetRequests()
8998
'get' => ['foo'],
9099
],
91100
];
92-
$this->request = Services::request();
93-
$filters = new Filters((object) $config, $this->request, $this->response);
101+
$filters = $this->createFilters((object) $config);
94102

95103
$expected = [
96104
'before' => ['foo'],
@@ -113,8 +121,7 @@ public function testProcessMethodRespectsMethod()
113121
'get' => ['bar'],
114122
],
115123
];
116-
$this->request = Services::request();
117-
$filters = new Filters((object) $config, $this->request, $this->response);
124+
$filters = $this->createFilters((object) $config);
118125

119126
$expected = [
120127
'before' => ['bar'],
@@ -137,8 +144,7 @@ public function testProcessMethodIgnoresMethod()
137144
'get' => ['bar'],
138145
],
139146
];
140-
$this->request = Services::request();
141-
$filters = new Filters((object) $config, $this->request, $this->response);
147+
$filters = $this->createFilters((object) $config);
142148

143149
$expected = [
144150
'before' => [],
@@ -167,8 +173,7 @@ public function testProcessMethodProcessGlobals()
167173
],
168174
],
169175
];
170-
$this->request = Services::request();
171-
$filters = new Filters((object) $config, $this->request, $this->response);
176+
$filters = $this->createFilters((object) $config);
172177

173178
$expected = [
174179
'before' => [
@@ -215,8 +220,7 @@ public function testProcessMethodProcessGlobalsWithExcept(array $except)
215220
],
216221
],
217222
];
218-
$this->request = Services::request();
219-
$filters = new Filters((object) $config, $this->request, $this->response);
223+
$filters = $this->createFilters((object) $config);
220224

221225
$uri = 'admin/foo/bar';
222226
$expected = [
@@ -245,8 +249,7 @@ public function testProcessMethodProcessesFiltersBefore()
245249
],
246250
],
247251
];
248-
$this->request = Services::request();
249-
$filters = new Filters((object) $config, $this->request, $this->response);
252+
$filters = $this->createFilters((object) $config);
250253

251254
$uri = 'admin/foo/bar';
252255
$expected = [
@@ -273,8 +276,7 @@ public function testProcessMethodProcessesFiltersAfter()
273276
],
274277
],
275278
];
276-
$this->request = Services::request();
277-
$filters = new Filters((object) $config, $this->request, $this->response);
279+
$filters = $this->createFilters((object) $config);
278280

279281
$uri = 'users/foo/bar';
280282
$expected = [
@@ -319,8 +321,7 @@ public function testProcessMethodProcessesCombined()
319321
],
320322
],
321323
];
322-
$this->request = Services::request();
323-
$filters = new Filters((object) $config, $this->request, $this->response);
324+
$filters = $this->createFilters((object) $config);
324325

325326
$uri = 'admin/foo/bar';
326327
$expected = [
@@ -360,8 +361,7 @@ public function testProcessMethodProcessesCombinedAfterForToolbar()
360361
],
361362
],
362363
];
363-
$this->request = Services::request();
364-
$filters = new Filters((object) $config, $this->request, $this->response);
364+
$filters = $this->createFilters((object) $config);
365365

366366
$uri = 'admin/foo/bar';
367367
$expected = [
@@ -386,8 +386,7 @@ public function testRunThrowsWithInvalidAlias()
386386
'after' => [],
387387
],
388388
];
389-
$this->request = Services::request();
390-
$filters = new Filters((object) $config, $this->request, $this->response);
389+
$filters = $this->createFilters((object) $config);
391390

392391
$this->expectException(FilterException::class);
393392

@@ -406,8 +405,7 @@ public function testCustomFiltersLoad()
406405
'after' => [],
407406
],
408407
];
409-
$this->request = Services::request();
410-
$filters = new Filters((object) $config, $this->request, $this->response);
408+
$filters = $this->createFilters((object) $config);
411409

412410
$uri = 'admin/foo/bar';
413411
$request = $filters->run($uri, 'before');
@@ -426,8 +424,7 @@ public function testRunThrowsWithInvalidClassType()
426424
'after' => [],
427425
],
428426
];
429-
$this->request = Services::request();
430-
$filters = new Filters((object) $config, $this->request, $this->response);
427+
$filters = $this->createFilters((object) $config);
431428

432429
$this->expectException(FilterException::class);
433430

@@ -446,8 +443,7 @@ public function testRunDoesBefore()
446443
'after' => [],
447444
],
448445
];
449-
$this->request = Services::request();
450-
$filters = new Filters((object) $config, $this->request, $this->response);
446+
$filters = $this->createFilters((object) $config);
451447

452448
$uri = 'admin/foo/bar';
453449
$request = $filters->run($uri, 'before');
@@ -466,8 +462,7 @@ public function testRunDoesAfter()
466462
'after' => ['google'],
467463
],
468464
];
469-
$this->request = Services::request();
470-
$filters = new Filters((object) $config, $this->request, $this->response);
465+
$filters = $this->createFilters((object) $config);
471466

472467
$uri = 'admin/foo/bar';
473468
$response = $filters->run($uri, 'after');
@@ -486,8 +481,7 @@ public function testShortCircuit()
486481
'after' => [],
487482
],
488483
];
489-
$this->request = Services::request();
490-
$filters = new Filters((object) $config, $this->request, $this->response);
484+
$filters = $this->createFilters((object) $config);
491485

492486
$uri = 'admin/foo/bar';
493487
$response = $filters->run($uri, 'before');
@@ -513,8 +507,7 @@ public function testOtherResult()
513507
'after' => [],
514508
],
515509
];
516-
$this->request = Services::request();
517-
$filters = new Filters((object) $config, $this->request, $this->response);
510+
$filters = $this->createFilters((object) $config);
518511

519512
$uri = 'admin/foo/bar';
520513
$response = $filters->run($uri, 'before');
@@ -542,8 +535,7 @@ public function testBeforeExceptString()
542535
],
543536
],
544537
];
545-
$this->request = Services::request();
546-
$filters = new Filters((object) $config, $this->request, $this->response);
538+
$filters = $this->createFilters((object) $config);
547539

548540
$uri = 'admin/foo/bar';
549541
$expected = [
@@ -575,8 +567,7 @@ public function testBeforeExceptInapplicable()
575567
],
576568
],
577569
];
578-
$this->request = Services::request();
579-
$filters = new Filters((object) $config, $this->request, $this->response);
570+
$filters = $this->createFilters((object) $config);
580571

581572
$uri = 'admin/foo/bar';
582573
$expected = [
@@ -609,8 +600,7 @@ public function testAfterExceptString()
609600
],
610601
],
611602
];
612-
$this->request = Services::request();
613-
$filters = new Filters((object) $config, $this->request, $this->response);
603+
$filters = $this->createFilters((object) $config);
614604

615605
$uri = 'admin/foo/bar';
616606
$expected = [
@@ -642,8 +632,7 @@ public function testAfterExceptInapplicable()
642632
],
643633
],
644634
];
645-
$this->request = Services::request();
646-
$filters = new Filters((object) $config, $this->request, $this->response);
635+
$filters = $this->createFilters((object) $config);
647636

648637
$uri = 'admin/foo/bar';
649638
$expected = [
@@ -669,8 +658,7 @@ public function testAddFilter()
669658
'after' => [],
670659
],
671660
];
672-
$this->request = Services::request();
673-
$filters = new Filters((object) $config, $this->request, $this->response);
661+
$filters = $this->createFilters((object) $config);
674662

675663
$filters = $filters->addFilter('Some\Class', 'some_alias');
676664
$filters = $filters->initialize('admin/foo/bar');
@@ -699,9 +687,8 @@ public function testInitializeTwice()
699687
{
700688
$_SERVER['REQUEST_METHOD'] = 'GET';
701689

702-
$config = [];
703-
$this->request = Services::request();
704-
$filters = new Filters((object) $config, $this->request, $this->response);
690+
$config = [];
691+
$filters = $this->createFilters((object) $config);
705692

706693
$filters = $filters
707694
->addFilter('Some\OtherClass', 'another', 'before', 'globals')
@@ -723,8 +710,7 @@ public function testEnableFilter()
723710
'after' => [],
724711
],
725712
];
726-
$this->request = Services::request();
727-
$filters = new Filters((object) $config, $this->request, $this->response);
713+
$filters = $this->createFilters((object) $config);
728714

729715
$filters = $filters->initialize('admin/foo/bar');
730716
$filters->enableFilter('google', 'before');
@@ -744,8 +730,7 @@ public function testEnableFilterWithArguments()
744730
'after' => [],
745731
],
746732
];
747-
$this->request = Services::request();
748-
$filters = new Filters((object) $config, $this->request, $this->response);
733+
$filters = $this->createFilters((object) $config);
749734

750735
$filters = $filters->initialize('admin/foo/bar');
751736
$filters->enableFilter('role:admin , super', 'before');
@@ -776,8 +761,7 @@ public function testEnableFilterWithNoArguments()
776761
'after' => [],
777762
],
778763
];
779-
$this->request = Services::request();
780-
$filters = new Filters((object) $config, $this->request, $this->response);
764+
$filters = $this->createFilters((object) $config);
781765

782766
$filters = $filters->initialize('admin/foo/bar');
783767
$filters->enableFilter('role', 'before');
@@ -808,8 +792,7 @@ public function testEnableNonFilter()
808792
'after' => [],
809793
],
810794
];
811-
$this->request = Services::request();
812-
$filters = new Filters((object) $config, $this->request, $this->response);
795+
$filters = $this->createFilters((object) $config);
813796

814797
$filters = $filters->initialize('admin/foo/bar');
815798
$filters->enableFilter('goggle', 'before');
@@ -846,8 +829,7 @@ public function testMatchesURICaseInsensitively()
846829
],
847830
],
848831
];
849-
$this->request = Services::request();
850-
$filters = new Filters((object) $config, $this->request, $this->response);
832+
$filters = $this->createFilters((object) $config);
851833

852834
$uri = 'admin/foo/bar';
853835
$expected = [
@@ -883,8 +865,7 @@ public function testFilterMatching()
883865
],
884866
],
885867
];
886-
$this->request = Services::request();
887-
$filters = new Filters((object) $config, $this->request, $this->response);
868+
$filters = $this->createFilters((object) $config);
888869

889870
$uri = 'admin';
890871
$actual = $filters->initialize($uri)->getFilters();
@@ -922,8 +903,7 @@ public function testGlobalFilterMatching()
922903
],
923904
],
924905
];
925-
$this->request = Services::request();
926-
$filters = new Filters((object) $config, $this->request, $this->response);
906+
$filters = $this->createFilters((object) $config);
927907

928908
$uri = 'admin';
929909
$actual = $filters->initialize($uri)->getFilters();
@@ -971,8 +951,7 @@ public function testCombinedFilterMatching()
971951
],
972952
],
973953
];
974-
$this->request = Services::request();
975-
$filters = new Filters((object) $config, $this->request, $this->response);
954+
$filters = $this->createFilters((object) $config);
976955

977956
$uri = 'admin123';
978957
$expected = [
@@ -1016,8 +995,7 @@ public function testSegmentedFilterMatching()
1016995
],
1017996
],
1018997
];
1019-
$this->request = Services::request();
1020-
$filters = new Filters((object) $config, $this->request, $this->response);
998+
$filters = $this->createFilters((object) $config);
1021999

10221000
$uri = 'admin/123';
10231001
$expected = [
@@ -1049,8 +1027,7 @@ public function testFilterAlitasMultiple()
10491027
],
10501028
],
10511029
];
1052-
$this->request = Services::request();
1053-
$filters = new Filters((object) $config, $this->request, $this->response);
1030+
$filters = $this->createFilters((object) $config);
10541031

10551032
$uri = 'admin/foo/bar';
10561033
$request = $filters->run($uri, 'before');
@@ -1074,8 +1051,7 @@ public function testFilterClass()
10741051
],
10751052
],
10761053
];
1077-
$this->request = Services::request();
1078-
$filters = new Filters((object) $config, $this->request, $this->response);
1054+
$filters = $this->createFilters((object) $config);
10791055

10801056
$filters->run('admin/foo/bar', 'before');
10811057

@@ -1103,8 +1079,7 @@ public function testReset()
11031079
],
11041080
],
11051081
];
1106-
$this->request = Services::request();
1107-
$filters = new Filters((object) $config, $this->request, $this->response);
1082+
$filters = $this->createFilters((object) $config);
11081083

11091084
$uri = 'admin';
11101085
$this->assertSame(['foo'], $filters->initialize($uri)->getFilters()['before']);

0 commit comments

Comments
 (0)