mirror of
https://github.com/flarum/framework.git
synced 2025-02-01 06:29:45 +08:00
fix insertAfter and insertBefore middleware extender functions (#2063)
This commit is contained in:
parent
b10a17529d
commit
0bc06e1bb1
|
@ -49,14 +49,14 @@ class Middleware implements ExtenderInterface
|
|||
|
||||
public function insertBefore($originalMiddleware, $newMiddleware)
|
||||
{
|
||||
$this->replaceMiddlewares[$originalMiddleware] = $newMiddleware;
|
||||
$this->insertBeforeMiddlewares[$originalMiddleware] = $newMiddleware;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function insertAfter($originalMiddleware, $newMiddleware)
|
||||
{
|
||||
$this->replaceMiddlewares[$originalMiddleware] = $newMiddleware;
|
||||
$this->insertAfterMiddlewares[$originalMiddleware] = $newMiddleware;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
@ -81,7 +81,7 @@ class Middleware implements ExtenderInterface
|
|||
foreach ($this->insertBeforeMiddlewares as $originalMiddleware => $newMiddleware) {
|
||||
array_splice(
|
||||
$existingMiddleware,
|
||||
array_search($originalMiddleware, $existingMiddleware),
|
||||
array_search($originalMiddleware, $existingMiddleware) + 1,
|
||||
0,
|
||||
$newMiddleware
|
||||
);
|
||||
|
@ -90,7 +90,7 @@ class Middleware implements ExtenderInterface
|
|||
foreach ($this->insertAfterMiddlewares as $originalMiddleware => $newMiddleware) {
|
||||
array_splice(
|
||||
$existingMiddleware,
|
||||
array_search($originalMiddleware, $existingMiddleware) + 1,
|
||||
array_search($originalMiddleware, $existingMiddleware),
|
||||
0,
|
||||
$newMiddleware
|
||||
);
|
||||
|
|
|
@ -84,6 +84,44 @@ class MiddlewareTest extends TestCase
|
|||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertArrayNotHasKey('X-First-Test-Middleware', $response->getHeaders());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function can_insert_before_middleware()
|
||||
{
|
||||
$this->add_first_middleware();
|
||||
$this->extend(
|
||||
(new Extend\Middleware('forum'))->insertBefore(FirstTestMiddleware::class, SecondTestMiddleware::class)
|
||||
);
|
||||
|
||||
$response = $this->send($this->request('GET', '/'));
|
||||
$headers = $response->getHeaders();
|
||||
$newMiddlewarePosition = array_search('X-Second-Test-Middleware', array_keys($headers));
|
||||
$originalMiddlewarePosition = array_search('X-First-Test-Middleware', array_keys($headers));
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertLessThan($originalMiddlewarePosition, $newMiddlewarePosition);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function can_insert_after_middleware()
|
||||
{
|
||||
$this->add_first_middleware();
|
||||
$this->extend(
|
||||
(new Extend\Middleware('forum'))->insertAfter(FirstTestMiddleware::class, SecondTestMiddleware::class)
|
||||
);
|
||||
|
||||
$response = $this->send($this->request('GET', '/'));
|
||||
$headers = $response->getHeaders();
|
||||
$newMiddlewarePosition = array_search('X-Second-Test-Middleware', array_keys($headers));
|
||||
$originalMiddlewarePosition = array_search('X-First-Test-Middleware', array_keys($headers));
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertGreaterThan($originalMiddlewarePosition, $newMiddlewarePosition);
|
||||
}
|
||||
}
|
||||
|
||||
class FirstTestMiddleware implements MiddlewareInterface
|
||||
|
|
Loading…
Reference in New Issue
Block a user