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 insertAfter and insertBefore middleware extender functions #2063

Merged
merged 3 commits into from
Mar 20, 2020
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
8 changes: 4 additions & 4 deletions src/Extend/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public function remove($middleware)

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;
}
Expand All @@ -81,7 +81,7 @@ public function extend(Container $container, Extension $extension = null)
foreach ($this->insertBeforeMiddlewares as $originalMiddleware => $newMiddleware) {
array_splice(
$existingMiddleware,
array_search($originalMiddleware, $existingMiddleware),
array_search($originalMiddleware, $existingMiddleware) + 1,
0,
$newMiddleware
);
Expand All @@ -90,7 +90,7 @@ public function extend(Container $container, Extension $extension = null)
foreach ($this->insertAfterMiddlewares as $originalMiddleware => $newMiddleware) {
array_splice(
$existingMiddleware,
array_search($originalMiddleware, $existingMiddleware) + 1,
array_search($originalMiddleware, $existingMiddleware),
0,
$newMiddleware
);
Expand Down
38 changes: 38 additions & 0 deletions tests/integration/extenders/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,44 @@ public function can_remove_middleware()
$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
Expand Down