Skip to content

Commit

Permalink
Merge branch '0.5'
Browse files Browse the repository at this point in the history
* 0.5:
  Fix rendering Twig hook message exception
  [Fix] Add missing getting started links to the summary
  Fix form without data id
  • Loading branch information
GSadee committed Nov 10, 2024
2 parents a1e6644 + 5444413 commit 9e646dc
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
8 changes: 8 additions & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

* [About Sylius Stack](README.md)

## Admin UI

* [Getting started](admin-ui/getting-started.md)

## Bootstrap Admin UI

* [Getting started](bootstrap-admin-ui/getting-started.md)

## 🍀 Twig Extra

* [Getting started](twig-extra/getting-started.md)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@
{% set form = hookable_metadata.context.form %}
{% endif %}

{% if resource is not defined %}
{% set resource = hookable_metadata.context.resource %}
{% endif %}

{% form_theme form '@SyliusBootstrapAdminUi/shared/form_theme.html.twig' %}

<div class="container-xl" {% if attributes is defined %} {{ attributes }} {% endif %}>
{{ form_start(form, {'attr': {'novalidate': 'novalidate', 'id': form.vars.id}}) }}
<div class="card-body">
{% if hookable_metadata.configuration.method is defined %}
<input type="hidden" name="_method" value="{{ hookable_metadata.configuration.method }}"/>
{% elseif form.vars.data.id is not null %}
{% elseif form.vars.data.id|default(null) is not null %}
<input type="hidden" name="_method" value="PUT"/>
{% endif %}
{{ form_errors(form) }}
{{ form_widget(form._token) }}

{% hook 'form' with { form } %}
{% hook 'form' with { form, resource } %}
</div>
{{ form_end(form, {render_rest: hookable_metadata.configuration.render_rest|default(false)}) }}
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,22 @@

namespace Sylius\TwigHooks\Hookable\Renderer\Exception;

use Twig\Error\Error;
use Twig\Error\RuntimeError;
use Twig\Source;

class HookRenderException extends RuntimeError
{
public function __construct(string $message, ?int $lineno = null, ?Source $source = null, ?\Throwable $previous = null)
{
$lineno ??= $previous?->getLine() ?? -1;
$source ??= $previous instanceof Error ? $previous->getSourceContext() : null;

parent::__construct(
$message,
$lineno,
$source,
$previous,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ public function render(AbstractHookable $hookable, HookableMetadata $metadata):
return $this->twig->render($hookable->template, [
HooksRuntime::HOOKABLE_METADATA => $metadata,
]);
} catch (HookRenderException $exception) {
throw $exception;
} catch (\Throwable $exception) {
throw new HookRenderException(
sprintf(
'An error occurred during rendering the "%s" hook in the "%s" hookable',
'An error occurred during rendering the "%s" hook in the "%s" hookable. %s',
$hookable->name,
$hookable->hookName,
$exception->getMessage(),
),
previous: $exception,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sylius\TwigHooks\Hookable\Metadata\HookableMetadata;
use Sylius\TwigHooks\Hookable\Renderer\Exception\HookRenderException;
use Sylius\TwigHooks\Hookable\Renderer\HookableTemplateRenderer;
use Tests\Sylius\TwigHooks\Utils\MotherObject\HookableComponentMotherObject;
use Tests\Sylius\TwigHooks\Utils\MotherObject\HookableTemplateMotherObject;
use Twig\Environment as Twig;
use Twig\Error\Error;

final class HookableTemplateRendererTest extends TestCase
{
Expand Down Expand Up @@ -65,6 +67,22 @@ public function testItRendersHookableTemplate(): void
$this->assertSame('some-rendered-template', $renderedTemplate);
}

public function testItThrowsAnExceptionWhenTwigThrowsAnError(): void
{
$metadata = $this->createMock(HookableMetadata::class);

$this->twig->expects($this->once())->method('render')->with('some-template', [
'hookable_metadata' => $metadata,
])->willThrowException(new Error('Unable to find the template.'));

$hookable = HookableTemplateMotherObject::withTarget('some-template');

$this->expectException(HookRenderException::class);
$this->expectExceptionMessage('An error occurred during rendering the "some_name" hook in the "some_hook" hookable. Unable to find the template at line 76.');

$this->getTestSubject()->render($hookable, $metadata);
}

private function getTestSubject(): HookableTemplateRenderer
{
return new HookableTemplateRenderer($this->twig);
Expand Down

0 comments on commit 9e646dc

Please sign in to comment.