Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: FilterTestTrait Undefined variable $filterClasses #8195

Merged
merged 4 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions system/Test/FilterTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,35 +125,42 @@ 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) {
if (! isset($this->filtersConfig->aliases[$filter])) {
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.
Expand All @@ -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.
Expand Down
18 changes: 17 additions & 1 deletion tests/system/Test/FilterTestTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand All @@ -73,6 +73,22 @@ public function testCallerSupportArray(): void
$this->assertSame('http://hellowworld.com', $result->getBody());
}

public function testCallerSupportsClassname(): void
{
$caller = $this->getFilterCaller(Customfilter::class, 'before');
$result = $caller();

$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');
Expand Down
Loading