Skip to content

Commit

Permalink
Merge pull request #8195 from kenjis/fix-FilterTestTrait
Browse files Browse the repository at this point in the history
fix: FilterTestTrait Undefined variable $filterClasses
  • Loading branch information
kenjis authored Nov 18, 2023
2 parents 7160f66 + e7bd962 commit d980e99
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
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

0 comments on commit d980e99

Please sign in to comment.