Skip to content

Commit

Permalink
Added ability to provide custom html attributes via {banner} macro
Browse files Browse the repository at this point in the history
  • Loading branch information
tg666 committed Nov 29, 2023
1 parent d12decd commit 8a8f5b2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 25 deletions.
38 changes: 25 additions & 13 deletions src/Bridge/Latte/RendererProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ final class RendererProvider

private bool $debugMode = false;

/** @var array<string, RequestPosition> */
/** @var array<string, array{0: RequestPosition, 1: array<string, mixed>}> */
private array $queue = [];

/** @var array<class-string, array<int, object>> */
Expand Down Expand Up @@ -73,9 +73,9 @@ public function __invoke(object $globals, string $positionCode, array $options =
$position = $this->createPosition($positionCode, $options);

if ($this->renderingMode->shouldBePositionQueued($position, $globals)) {
return $this->addToQueue($position);
return $this->addToQueue($position, $options);
} else {
return $this->render($position);
return $this->render($position, $options);
}
}

Expand All @@ -101,9 +101,11 @@ public function addConfigureClientEventHandler(ConfigureClientEventHandlerInterf
}

/**
* @param array<string, mixed> $options
*
* @throws AmpExceptionInterface
*/
private function render(RequestPosition $position): string
private function render(RequestPosition $position, array $options): string
{
$positionCode = $position->getCode();
$response = $this->fetchResponse(new BannersRequest([$position]));
Expand All @@ -112,13 +114,16 @@ private function render(RequestPosition $position): string
return '';
}

return $this->renderPosition($response->getPosition($positionCode));
return $this->renderPosition($response->getPosition($positionCode), $options);
}

private function addToQueue(RequestPosition $position): string
/**
* @param array<string, mixed> $options *
*/
private function addToQueue(RequestPosition $position, array $options): string
{
$comment = $this->formatHtmlComment($position->getCode());
$this->queue[$comment] = $position;
$this->queue[$comment] = [$position, $options];

return $comment;
}
Expand Down Expand Up @@ -148,7 +153,10 @@ public function renderQueuedPositions($output)
}

$response = $this->fetchResponse(
new BannersRequest(array_values($this->queue)),
new BannersRequest(array_map(
static fn (array $item): RequestPosition => $item[0],
array_values($this->queue),
)),
);

if (null === $response) {
Expand All @@ -159,14 +167,14 @@ public function renderQueuedPositions($output)

$replacements = array_filter(
array_map(
function (RequestPosition $requestPosition) use ($response): ?string {
$responsePosition = $response->getPosition($requestPosition->getCode());
function (array $item) use ($response): ?string {
$responsePosition = $response->getPosition($item[0]->getCode());

if (null === $responsePosition) {
return null;
}

return $this->renderPosition($responsePosition);
return $this->renderPosition($responsePosition, $item[1]);
},
$this->queue,
),
Expand Down Expand Up @@ -230,12 +238,16 @@ private function fetchResponse(BannersRequest $request): ?BannersResponse
}

/**
* @param array<string, mixed> $options
*
* @throws RendererException
*/
private function renderPosition(ResponsePosition $position): string
private function renderPosition(ResponsePosition $position, array $options): string
{
try {
return $this->renderer->render($position);
$elementAttributes = (array) ($options['attributes'] ?? []);

return $this->renderer->render($position, $elementAttributes);
} catch (RendererException $e) {
if ($this->debugMode) {
throw $e;
Expand Down
59 changes: 47 additions & 12 deletions tests/Bridge/Latte/RendererProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,47 @@ public function testInvokingDefaultInstanceWithoutResources(): void
$renderer
->shouldReceive('render')
->once()
->with($responsePosition)
->with($responsePosition, [])
->andReturn('<homepage.top>');

Assert::same('<homepage.top>', $provider(new stdClass(), 'homepage.top'));
}

public function testInvokingDefaultInstanceWithAttributes(): void
{
$client = Mockery::mock(AmpClientInterface::class);
$renderer = Mockery::mock(RendererInterface::class);
$provider = new RendererProvider($client, $renderer);

$responsePosition = new ResponsePosition('1234', 'homepage.top', 'Homepage top', 0, ResponsePosition::DisplayTypeSingle, ResponsePosition::BreakpointTypeMin, []);
$response = new BannersResponse([
'homepage.top' => $responsePosition,
]);

$client
->shouldReceive('fetchBanners')
->once()
->with(Mockery::type(BannersRequest::class))
->andReturnUsing(static function (BannersRequest $request) use ($response): BannersResponse {
Assert::equal(
new BannersRequest([
new RequestPosition('homepage.top'),
]),
$request,
);

return $response;
});

$renderer
->shouldReceive('render')
->once()
->with($responsePosition, ['class' => 'my-custom-class'])
->andReturn('<homepage.top>');

Assert::same('<homepage.top>', $provider(new stdClass(), 'homepage.top', ['attributes' => ['class' => 'my-custom-class']]));
}

public function testInvokingDefaultInstanceWithResources(): void
{
$client = Mockery::mock(AmpClientInterface::class);
Expand Down Expand Up @@ -94,7 +129,7 @@ public function testInvokingDefaultInstanceWithResources(): void
$renderer
->shouldReceive('render')
->once()
->with($responsePosition)
->with($responsePosition, [])
->andReturn('<homepage.top>');

Assert::same(
Expand Down Expand Up @@ -140,7 +175,7 @@ public function testClientConfigurationEventsShouldBeInvokedBeforeFirstFetch():
$renderer
->shouldReceive('render')
->twice()
->with($responsePosition)
->with($responsePosition, [])
->andReturn('<homepage.top>');

Assert::same('<homepage.top>', $provider(new stdClass(), 'homepage.top'));
Expand Down Expand Up @@ -242,7 +277,7 @@ public function testExceptionShouldBeThrownWhenRendererThrowsExceptionInDebugMod
$renderer
->shouldReceive('render')
->once()
->with($responsePosition)
->with($responsePosition, [])
->andThrow(new RendererException('Test renderer exception'));

Assert::exception(
Expand Down Expand Up @@ -271,7 +306,7 @@ public function testEmptyStringShouldBeReturnedWhenRendererThrowsExceptionInNonD
$renderer
->shouldReceive('render')
->once()
->with($responsePosition)
->with($responsePosition, [])
->andThrow(new RendererException('Test renderer exception'));

Assert::same('', $provider(new stdClass(), 'homepage.top'));
Expand All @@ -298,7 +333,7 @@ public function testExceptionShouldBeLoggedWhenRendererThrowsExceptionInNonDebug
$renderer
->shouldReceive('render')
->once()
->with($responsePosition)
->with($responsePosition, [])
->andThrow($exception);

$logger
Expand Down Expand Up @@ -342,7 +377,7 @@ public function testPositionsShouldBeQueuedAndReplacedInStringOutput(): void
return true;
});

Assert::same('<!--AMP_POSITION:homepage.top-->', $provider($globals, 'homepage.top'));
Assert::same('<!--AMP_POSITION:homepage.top-->', $provider($globals, 'homepage.top', ['attributes' => ['class' => 'my-custom-class']]));
Assert::same('<!--AMP_POSITION:homepage.bottom-->', $provider($globals, 'homepage.bottom', ['resources' => ['resource' => ['a']]]));
Assert::true($provider->isAnythingQueued());

Expand Down Expand Up @@ -372,11 +407,11 @@ public function testPositionsShouldBeQueuedAndReplacedInStringOutput(): void
$renderer
->shouldReceive('render')
->once()
->with($responsePosition1)
->with($responsePosition1, ['class' => 'my-custom-class'])
->andReturn('<homepage.top>')
->shouldReceive('render')
->once()
->with($responsePosition2)
->with($responsePosition2, [])
->andReturn('<homepage.bottom>');

Assert::same(
Expand Down Expand Up @@ -418,7 +453,7 @@ public function testPositionsShouldBeQueuedAndReplacedInArrayOutput(): void
return true;
});

Assert::same('<!--AMP_POSITION:homepage.top-->', $provider($globals, 'homepage.top'));
Assert::same('<!--AMP_POSITION:homepage.top-->', $provider($globals, 'homepage.top', ['attributes' => ['class' => 'my-custom-class']]));
Assert::same('<!--AMP_POSITION:homepage.bottom-->', $provider($globals, 'homepage.bottom', ['resources' => ['resource' => ['a']]]));
Assert::true($provider->isAnythingQueued());

Expand Down Expand Up @@ -448,11 +483,11 @@ public function testPositionsShouldBeQueuedAndReplacedInArrayOutput(): void
$renderer
->shouldReceive('render')
->once()
->with($responsePosition1)
->with($responsePosition1, ['class' => 'my-custom-class'])
->andReturn('<homepage.top>')
->shouldReceive('render')
->once()
->with($responsePosition2)
->with($responsePosition2, [])
->andReturn('<homepage.bottom>');

Assert::same(
Expand Down

0 comments on commit 8a8f5b2

Please sign in to comment.