From 064b8b0060f1b062c83f9a052337120fc2fecd51 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 12 Nov 2023 17:32:34 +0900 Subject: [PATCH 1/4] test: add test for classname filter --- tests/system/Test/FilterTestTraitTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/system/Test/FilterTestTraitTest.php b/tests/system/Test/FilterTestTraitTest.php index bebec3857030..c05ea29ed340 100644 --- a/tests/system/Test/FilterTestTraitTest.php +++ b/tests/system/Test/FilterTestTraitTest.php @@ -73,6 +73,14 @@ public function testCallerSupportArray(): void $this->assertSame('http://hellowworld.com', $result->getBody()); } + public function testCallerSupportClassname(): void + { + $caller = $this->getFilterCaller(Customfilter::class, 'before'); + $result = $caller(); + + $this->assertSame('http://hellowworld.com', $result->getBody()); + } + public function testCallerUsesClonedInstance(): void { $caller = $this->getFilterCaller('test-customfilter', 'before'); From 0dc64cd66b8aa1a5c9c8e8833e9cea3c6571549d Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 12 Nov 2023 17:33:01 +0900 Subject: [PATCH 2/4] fix: ErrorException : Undefined variable $filterClasses --- system/Test/FilterTestTrait.php | 37 +++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/system/Test/FilterTestTrait.php b/system/Test/FilterTestTrait.php index 084a6cd082fc..523717e54679 100644 --- a/system/Test/FilterTestTrait.php +++ b/system/Test/FilterTestTrait.php @@ -125,6 +125,10 @@ protected function getFilterCaller($filter, string $position): Closure throw new InvalidArgumentException('Invalid filter position passed: ' . $position); } + if ($filter instanceof FilterInterface) { + $filterInstances = [$filter]; + } + if (is_string($filter)) { // Check for an alias (no namespace) if (strpos($filter, '\\') === false) { @@ -132,28 +136,31 @@ protected function getFilterCaller($filter, string $position): Closure throw new RuntimeException("No filter found with alias '{$filter}'"); } - $filterClasses = $this->filtersConfig->aliases[$filter]; + $filterClasses = (array) $this->filtersConfig->aliases[$filter]; + } else { + // FQCN + $filterClasses = [$filter]; } - $filterClasses = (array) $filterClasses; - } + $filterInstances = []; - foreach ($filterClasses as $class) { - // Get an instance - $filter = new $class(); + foreach ($filterClasses as $class) { + // Get an instance + $filter = new $class(); - if (! $filter instanceof FilterInterface) { - throw FilterException::forIncorrectInterface(get_class($filter)); + if (! $filter instanceof FilterInterface) { + throw FilterException::forIncorrectInterface(get_class($filter)); + } + + $filterInstances[] = $filter; } } $request = clone $this->request; if ($position === 'before') { - return static function (?array $params = null) use ($filterClasses, $request) { - foreach ($filterClasses as $class) { - $filter = new $class(); - + return static function (?array $params = null) use ($filterInstances, $request) { + foreach ($filterInstances as $filter) { $result = $filter->before($request, $params); // @TODO The following logic is in Filters class. @@ -177,10 +184,8 @@ protected function getFilterCaller($filter, string $position): Closure $response = clone $this->response; - return static function (?array $params = null) use ($filterClasses, $request, $response) { - foreach ($filterClasses as $class) { - $filter = new $class(); - + return static function (?array $params = null) use ($filterInstances, $request, $response) { + foreach ($filterInstances as $filter) { $result = $filter->after($request, $response, $params); // @TODO The following logic is in Filters class. From 7dc70874518006c7946c19aaa46fc8e1a351dd84 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 12 Nov 2023 18:55:42 +0900 Subject: [PATCH 3/4] test: add test for Filter instance --- tests/system/Test/FilterTestTraitTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/system/Test/FilterTestTraitTest.php b/tests/system/Test/FilterTestTraitTest.php index c05ea29ed340..300bc2b47425 100644 --- a/tests/system/Test/FilterTestTraitTest.php +++ b/tests/system/Test/FilterTestTraitTest.php @@ -81,6 +81,14 @@ public function testCallerSupportClassname(): void $this->assertSame('http://hellowworld.com', $result->getBody()); } + public function testCallerSupportsFilterInstance(): void + { + $caller = $this->getFilterCaller(new Customfilter(), 'before'); + $result = $caller(); + + $this->assertSame('http://hellowworld.com', $result->getBody()); + } + public function testCallerUsesClonedInstance(): void { $caller = $this->getFilterCaller('test-customfilter', 'before'); From e7bd962eaa59f9431e286f72b487b24394d08aa1 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 12 Nov 2023 18:56:36 +0900 Subject: [PATCH 4/4] test: fix test method names --- tests/system/Test/FilterTestTraitTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/system/Test/FilterTestTraitTest.php b/tests/system/Test/FilterTestTraitTest.php index 300bc2b47425..dda0ca0d3265 100644 --- a/tests/system/Test/FilterTestTraitTest.php +++ b/tests/system/Test/FilterTestTraitTest.php @@ -63,7 +63,7 @@ public function testGetCallerInvalidPosition(): void $this->getFilterCaller('test-customfilter', 'banana'); } - public function testCallerSupportArray(): void + public function testCallerSupportsArray(): void { $this->filtersConfig->aliases['test-customfilter'] = [Customfilter::class]; @@ -73,7 +73,7 @@ public function testCallerSupportArray(): void $this->assertSame('http://hellowworld.com', $result->getBody()); } - public function testCallerSupportClassname(): void + public function testCallerSupportsClassname(): void { $caller = $this->getFilterCaller(Customfilter::class, 'before'); $result = $caller();