Skip to content

Commit

Permalink
fix: ensure early return of response is respected when conditional Mi…
Browse files Browse the repository at this point in the history
…ddleware is involved
  • Loading branch information
holtkamp committed Sep 12, 2024
1 parent 2a02f49 commit 59510ec
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Harmony.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use function array_key_exists;
use function assert;
use function count;

class Harmony implements RequestHandlerInterface
{
Expand Down Expand Up @@ -128,13 +129,17 @@ protected function executeCondition(array $conditionArray): void
return;
}

//When the Condition evaluates to true: instantiate a new/branched Harmony instance to process the conditional Middleware
$harmony = new Harmony($this->request, $this->response);
$callable($harmony, $this->request);

//Add remaining Middleware of the current Harmony instance to the new/branched Harmony instance and run it
for ($i = $this->currentMiddleware + 1; $i < count($this->middleware); $i++) {
$harmony->middleware[] = $this->middleware[$i];
}
$harmony->run();

$this->request = $harmony->request;
$this->response = $harmony->response;

$this->handle($this->request);
}
}
50 changes: 50 additions & 0 deletions tests/HarmonyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,56 @@ public function returnAfterSecondMiddleware(): void
$this->assertEquals(500, $response->getStatusCode());
}

/**
* @test
*/
public function returnResponseWhenConditionIsInvolved(): void
{
$harmony = $this->createHarmony();
$harmony->addMiddleware(new FakeMiddleware());
$harmony->addCondition(
new StubCondition(true),
static fn (Harmony $harmony): Harmony => $harmony->addMiddleware(new InternalServerErrorMiddleware(new DummyResponse()))
);
$middlewareSecond = new SpyMiddleware();
$harmony->addMiddleware($middlewareSecond);

$harmony->run();

$this->assertFalse($middlewareSecond->isInvoked());
}

/**
* @test
*/
public function returnResponseWhenMultipleConditionsAreInvolved(): void
{
$harmony = $this->createHarmony();
$harmony->addMiddleware(new FakeMiddleware());
$middlewareFirst = new SpyMiddleware();
$harmony->addMiddleware($middlewareFirst);

$middlewareSecond = new SpyMiddleware();
$harmony->addCondition(
new StubCondition(true),
static fn (Harmony $harmony): Harmony => $harmony->addMiddleware($middlewareSecond)
);

$harmony->addCondition(
new StubCondition(true),
static fn (Harmony $harmony): Harmony => $harmony->addMiddleware(new InternalServerErrorMiddleware(new DummyResponse()))
);

$middlewareThird = new SpyMiddleware();
$harmony->addMiddleware($middlewareThird);

$harmony->run();

$this->assertTrue($middlewareFirst->isInvoked());
$this->assertTrue($middlewareSecond->isInvoked());
$this->assertFalse($middlewareThird->isInvoked());
}

/**
* @test
*/
Expand Down

0 comments on commit 59510ec

Please sign in to comment.