diff --git a/composer.json b/composer.json index b46e115..41529cc 100644 --- a/composer.json +++ b/composer.json @@ -79,7 +79,8 @@ "michelf/php-markdown": "1.3.*" }, "require-dev": { - "phpunit/phpunit": "3.7.*" + "phpunit/phpunit": "3.7.*", + "twig/twig": "*" }, "suggest": { "phpdocumentor/reflection": "required by yii2-apidoc extension", diff --git a/tests/unit/TestCase.php b/tests/unit/TestCase.php index 147df05..88071c3 100644 --- a/tests/unit/TestCase.php +++ b/tests/unit/TestCase.php @@ -45,6 +45,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase 'id' => 'testapp', 'basePath' => __DIR__, ]; + $defaultConfig['vendorPath'] = dirname(dirname(__DIR__)) . '/vendor'; new $appClass(array_merge($defaultConfig, $config)); } diff --git a/tests/unit/extensions/twig/ViewRendererTest.php b/tests/unit/extensions/twig/ViewRendererTest.php new file mode 100644 index 0000000..88ca9f7 --- /dev/null +++ b/tests/unit/extensions/twig/ViewRendererTest.php @@ -0,0 +1,74 @@ + + */ + +namespace yiiunit\extensions\twig; + + +use yii\web\AssetManager; +use yii\web\JqueryAsset; +use yii\web\View; +use Yii; +use yiiunit\TestCase; + +/** + * @group twig + */ +class ViewRendererTest extends TestCase +{ + protected function setUp() + { + $this->mockApplication(); + } + + /** + * https://github.com/yiisoft/yii2/issues/1755 + */ + public function testLayoutAssets() + { + $view = $this->mockView(); + JqueryAsset::register($view); + $content = $view->renderFile('@yiiunit/extensions/twig/views/layout.twig'); + + $this->assertEquals(1, preg_match('#\s*#', $content), 'content does not contain the jquery js:' . $content); + } + + protected function mockView() + { + return new View([ + 'renderers' => [ + 'twig' => [ + 'class' => 'yii\twig\ViewRenderer', + //'cachePath' => '@runtime/Twig/cache', + 'options' => [ + 'cache' => false + ], + 'globals' => [ + 'html' => '\yii\helpers\Html', + 'pos_begin' => View::POS_BEGIN + ], + 'functions' => [ + 't' => '\Yii::t', + 'json_encode' => '\yii\helpers\Json::encode' + ] + ], + ], + 'assetManager' => $this->mockAssetManager(), + ]); + } + + protected function mockAssetManager() + { + $assetDir = Yii::getAlias('@runtime/assets'); + if (!is_dir($assetDir)) { + mkdir($assetDir, 0777, true); + } + return new AssetManager([ + 'basePath' => $assetDir, + 'baseUrl' => '/assets', + ]); + } +} \ No newline at end of file diff --git a/tests/unit/extensions/twig/views/layout.twig b/tests/unit/extensions/twig/views/layout.twig new file mode 100644 index 0000000..7a53832 --- /dev/null +++ b/tests/unit/extensions/twig/views/layout.twig @@ -0,0 +1,14 @@ +{{ this.beginPage() }} + + + + + {{ html.encode(this.title) }} + {{ this.head() }} + + +{{ this.beginBody() }} + body +{{ this.endBody() }} + +{{ this.endPage() }}