Skip to content

Commit

Permalink
Make ConvertEmptyStringsToNullTest middleware terminable
Browse files Browse the repository at this point in the history
  • Loading branch information
pghiuzan committed Dec 11, 2023
1 parent 0b70cda commit d2f733f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ protected function transform($key, $value)
return $value === '' ? null : $value;
}

/**
* Clean up callbacks list when the app terminates.
*
* @return void
*/
public function terminate()
{
static::$skipCallbacks = [];
}

/**
* Register a callback that instructs the middleware to be skipped.
*
Expand Down
20 changes: 20 additions & 0 deletions tests/Foundation/Http/Middleware/ConvertEmptyStringsToNullTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,24 @@ public function testSkipConvertsEmptyStringsToNull()
$this->assertSame('', $request->get('baz'));
});
}

public function testItClearsOutSkipCallbacksWhenTerminated(): void
{
$middleware = new ConvertEmptyStringsToNull();

ConvertEmptyStringsToNull::skipWhen(fn (Request $request) => true);

$middleware->terminate();

$symfonyRequest = new SymfonyRequest([
'foo' => '',
]);

$symfonyRequest->server->set('REQUEST_METHOD', 'GET');
$request = Request::createFromBase($symfonyRequest);

$middleware->handle($request, function (Request $request) {
$this->assertSame(null, $request->get('foo'));
});
}
}

0 comments on commit d2f733f

Please sign in to comment.