Skip to content

Commit

Permalink
Merge pull request #60 from cakephp/plugin-helpers
Browse files Browse the repository at this point in the history
Add unit test for using plugin helpers
  • Loading branch information
othercorey committed Oct 24, 2020
2 parents 247bdc3 + 5417d5e commit f9c2b2b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -46,7 +46,8 @@
"autoload-dev": {
"psr-4": {
"Cake\\TwigView\\Test\\": "tests/",
"TestApp\\": "tests/test_app/src/"
"TestApp\\": "tests/test_app/src/",
"TestTwigView\\": "tests/test_app/plugins/TestTwigView/src/"
}
},
"scripts": {
Expand Down
22 changes: 21 additions & 1 deletion tests/TestCase/View/TwigViewTest.php
Expand Up @@ -236,9 +236,29 @@ public function testHelperFunction()
'viewVars' => ['elementVar' => 'var echoed inside element'],
]);

$output = $view->render('helper_test', false);
$output = $view->render('helper', false);

$expected = "var echoed inside element\n<p>I love CakePHP</p>\n";
$this->assertSame($expected, $output);
}

public function testPluginHelperFunction()
{
$this->loadPlugins(['TestTwigView']);

$view = new AppView(null, null, null, [
'helpers' => ['TestTwigView.Output'],
]);

$expected = "from OutputHelper\nfrom OutputHelper";
$this->assertSame($expected, $view->render('plugin_helper', false));

$view = new AppView();
$view->helpers()->load('TestTwigView.Output');

$expected = "from OutputHelper\nfrom OutputHelper";
$this->assertSame($expected, $view->render('plugin_helper', false));

$this->clearPlugins();
}
}
10 changes: 10 additions & 0 deletions tests/test_app/plugins/TestTwigView/src/Plugin.php
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

namespace TestTwigView;

use Cake\Core\BasePlugin;

class Plugin extends BasePlugin
{
}
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);

/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 1.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace TestTwigView\View\Helper;

use Cake\View\Helper;

/**
* OutputHelper
*/
class OutputHelper extends Helper
{
public function print(): string
{
return 'from OutputHelper';
}
}
File renamed without changes.
2 changes: 2 additions & 0 deletions tests/test_app/templates/plugin_helper.twig
@@ -0,0 +1,2 @@
{{ helper_Output_print() }}
{{ Output.print() }}

0 comments on commit f9c2b2b

Please sign in to comment.