Skip to content

Commit

Permalink
Merge pull request #716 from ndm2/2.x-fix-render-event-name
Browse files Browse the repository at this point in the history
2.x - Fix template event name.
  • Loading branch information
markstory authored Oct 23, 2020
2 parents 22f20b5 + 67ff74e commit f1c297c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/View/BakeView.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public function initialize(): void
public function render(?string $template = null, $layout = null): string
{
$viewFileName = $this->_getTemplateFileName($template);
$templateEventName = str_replace(DS, '.', $template);
[, $templateEventName] = pluginSplit($template);
$templateEventName = str_replace(['/', '\\'], '.', $templateEventName);

$this->_currentType = static::TYPE_TEMPLATE;
$this->dispatchEvent('View.beforeRender', [$viewFileName]);
Expand Down
83 changes: 80 additions & 3 deletions tests/TestCase/View/BakeViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,80 @@ public function testNoLineBreaks()
}

/**
* Verify that the proper events are dispatched on template render
* Verify that the proper events are dispatched when rendering a template,
* irrespective of the used directory separator.
*
* @return void
*/
public function testSeparatorRenderEvents()
{
$this->View->set('test', 'success');
$result = $this->View->render('Custom' . DS . 'file');
$this->assertSame(
'success' . "\n",
$result
);

$this->View->set('test', 'success');
$this->View->getEventManager()->on('Bake.beforeRender.Custom.file', function (Event $event) {
$event->getSubject()->set('test', 'separator constant beforeRender');
});
$this->View->getEventManager()->on('Bake.afterRender.Custom.file', function (Event $event) {
$event->getSubject()->set('test', 'separator constant afterRender');
});
$result = $this->View->render('Custom' . DS . 'file');
$this->assertSame(
'separator constant beforeRender' . "\n",
$result
);
$this->assertSame($this->View->get('test'), 'separator constant afterRender');

$this->View->set('test', 'success');
$this->View->getEventManager()->on('Bake.beforeRender.Custom.file', function (Event $event) {
$event->getSubject()->set('test', 'fixed separator beforeRender');
});
$this->View->getEventManager()->on('Bake.afterRender.Custom.file', function (Event $event) {
$event->getSubject()->set('test', 'fixed separator afterRender');
});
$result = $this->View->render('Custom/file');
$this->assertSame(
'fixed separator beforeRender' . "\n",
$result
);
$this->assertSame($this->View->get('test'), 'fixed separator afterRender');
}

/**
* Verify that the proper events are dispatched when rendering a plugin template.
*
* @return void
*/
public function testPluginRenderEvents()
{
$this->View->set('test', 'success');
$result = $this->View->render('Bake.Custom' . DS . 'file');
$this->assertSame(
'success' . "\n",
$result
);

$this->View->set('test', 'success');
$this->View->getEventManager()->on('Bake.beforeRender.Custom.file', function (Event $event) {
$event->getSubject()->set('test', 'plugin template beforeRender');
});
$this->View->getEventManager()->on('Bake.afterRender.Custom.file', function (Event $event) {
$event->getSubject()->set('test', 'plugin template afterRender');
});
$result = $this->View->render('Bake.Custom' . DS . 'file');
$this->assertSame(
'plugin template beforeRender' . "\n",
$result
);
$this->assertSame($this->View->get('test'), 'plugin template afterRender');
}

/**
* Verify that the proper events are dispatched when rendering a template.
*
* @return void
*/
Expand All @@ -162,13 +235,17 @@ public function testCustomRenderEvents()

$this->View->set('test', 'success');
$this->View->getEventManager()->on('Bake.beforeRender.Custom.file', function (Event $event) {
$event->getSubject()->set('test', 'pass');
$event->getSubject()->set('test', 'custom template beforeRender');
});
$this->View->getEventManager()->on('Bake.afterRender.Custom.file', function (Event $event) {
$event->getSubject()->set('test', 'custom template afterRender');
});
$result = $this->View->render('Custom' . DS . 'file');
$this->assertSame(
'pass' . "\n",
'custom template beforeRender' . "\n",
$result
);
$this->assertSame($this->View->get('test'), 'custom template afterRender');
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/test_app/templates/plugin/Bake/Custom/file.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ test }}

0 comments on commit f1c297c

Please sign in to comment.