PageRenderTime 185ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/framework/web/AssetBundleTest.php

https://gitlab.com/brucealdridge/yii2
PHP | 273 lines | 225 code | 33 blank | 15 comment | 3 complexity | cef8a5452119ddd0982eb1c6686cf6f4 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. *
  5. * @author Carsten Brandt <mail@cebe.cc>
  6. */
  7. namespace yiiunit\framework\web;
  8. use Yii;
  9. use yii\web\View;
  10. use yii\web\AssetBundle;
  11. use yii\web\AssetManager;
  12. /**
  13. * @group web
  14. */
  15. class AssetBundleTest extends \yiiunit\TestCase
  16. {
  17. protected function setUp()
  18. {
  19. parent::setUp();
  20. $this->mockApplication();
  21. Yii::setAlias('@testWeb', '/');
  22. Yii::setAlias('@testWebRoot', '@yiiunit/data/web');
  23. }
  24. protected function getView()
  25. {
  26. $view = new View();
  27. $view->setAssetManager(new AssetManager([
  28. 'basePath' => '@testWebRoot/assets',
  29. 'baseUrl' => '@testWeb/assets',
  30. ]));
  31. return $view;
  32. }
  33. public function testRegister()
  34. {
  35. $view = $this->getView();
  36. $this->assertEmpty($view->assetBundles);
  37. TestSimpleAsset::register($view);
  38. $this->assertEquals(1, count($view->assetBundles));
  39. $this->assertArrayHasKey('yiiunit\\framework\\web\\TestSimpleAsset', $view->assetBundles);
  40. $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestSimpleAsset'] instanceof AssetBundle);
  41. $expected = <<<EOF
  42. 123<script src="/js/jquery.js"></script>4
  43. EOF;
  44. $this->assertEquals($expected, $view->renderFile('@yiiunit/data/views/rawlayout.php'));
  45. }
  46. public function testSimpleDependency()
  47. {
  48. $view = $this->getView();
  49. $this->assertEmpty($view->assetBundles);
  50. TestAssetBundle::register($view);
  51. $this->assertEquals(3, count($view->assetBundles));
  52. $this->assertArrayHasKey('yiiunit\\framework\\web\\TestAssetBundle', $view->assetBundles);
  53. $this->assertArrayHasKey('yiiunit\\framework\\web\\TestJqueryAsset', $view->assetBundles);
  54. $this->assertArrayHasKey('yiiunit\\framework\\web\\TestAssetLevel3', $view->assetBundles);
  55. $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestAssetBundle'] instanceof AssetBundle);
  56. $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset'] instanceof AssetBundle);
  57. $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestAssetLevel3'] instanceof AssetBundle);
  58. $expected = <<<EOF
  59. 1<link href="/files/cssFile.css" rel="stylesheet">23<script src="/js/jquery.js"></script>
  60. <script src="/files/jsFile.js"></script>4
  61. EOF;
  62. $this->assertEqualsWithoutLE($expected, $view->renderFile('@yiiunit/data/views/rawlayout.php'));
  63. }
  64. public function positionProvider()
  65. {
  66. return [
  67. [View::POS_HEAD, true],
  68. [View::POS_HEAD, false],
  69. [View::POS_BEGIN, true],
  70. [View::POS_BEGIN, false],
  71. [View::POS_END, true],
  72. [View::POS_END, false],
  73. ];
  74. }
  75. /**
  76. * @dataProvider positionProvider
  77. */
  78. public function testPositionDependency($pos, $jqAlreadyRegistered)
  79. {
  80. $view = $this->getView();
  81. $view->getAssetManager()->bundles['yiiunit\\framework\\web\\TestAssetBundle'] = [
  82. 'jsOptions' => [
  83. 'position' => $pos,
  84. ],
  85. ];
  86. $this->assertEmpty($view->assetBundles);
  87. if ($jqAlreadyRegistered) {
  88. TestJqueryAsset::register($view);
  89. }
  90. TestAssetBundle::register($view);
  91. $this->assertEquals(3, count($view->assetBundles));
  92. $this->assertArrayHasKey('yiiunit\\framework\\web\\TestAssetBundle', $view->assetBundles);
  93. $this->assertArrayHasKey('yiiunit\\framework\\web\\TestJqueryAsset', $view->assetBundles);
  94. $this->assertArrayHasKey('yiiunit\\framework\\web\\TestAssetLevel3', $view->assetBundles);
  95. $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestAssetBundle'] instanceof AssetBundle);
  96. $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset'] instanceof AssetBundle);
  97. $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestAssetLevel3'] instanceof AssetBundle);
  98. $this->assertArrayHasKey('position', $view->assetBundles['yiiunit\\framework\\web\\TestAssetBundle']->jsOptions);
  99. $this->assertEquals($pos, $view->assetBundles['yiiunit\\framework\\web\\TestAssetBundle']->jsOptions['position']);
  100. $this->assertArrayHasKey('position', $view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset']->jsOptions);
  101. $this->assertEquals($pos, $view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset']->jsOptions['position']);
  102. $this->assertArrayHasKey('position', $view->assetBundles['yiiunit\\framework\\web\\TestAssetLevel3']->jsOptions);
  103. $this->assertEquals($pos, $view->assetBundles['yiiunit\\framework\\web\\TestAssetLevel3']->jsOptions['position']);
  104. switch ($pos) {
  105. case View::POS_HEAD:
  106. $expected = <<<EOF
  107. 1<link href="/files/cssFile.css" rel="stylesheet">
  108. <script src="/js/jquery.js"></script>
  109. <script src="/files/jsFile.js"></script>234
  110. EOF;
  111. break;
  112. case View::POS_BEGIN:
  113. $expected = <<<EOF
  114. 1<link href="/files/cssFile.css" rel="stylesheet">2<script src="/js/jquery.js"></script>
  115. <script src="/files/jsFile.js"></script>34
  116. EOF;
  117. break;
  118. default:
  119. case View::POS_END:
  120. $expected = <<<EOF
  121. 1<link href="/files/cssFile.css" rel="stylesheet">23<script src="/js/jquery.js"></script>
  122. <script src="/files/jsFile.js"></script>4
  123. EOF;
  124. break;
  125. }
  126. $this->assertEqualsWithoutLE($expected, $view->renderFile('@yiiunit/data/views/rawlayout.php'));
  127. }
  128. public function positionProvider2()
  129. {
  130. return [
  131. [View::POS_BEGIN, true],
  132. [View::POS_BEGIN, false],
  133. [View::POS_END, true],
  134. [View::POS_END, false],
  135. ];
  136. }
  137. /**
  138. * @dataProvider positionProvider
  139. */
  140. public function testPositionDependencyConflict($pos, $jqAlreadyRegistered)
  141. {
  142. $view = $this->getView();
  143. $view->getAssetManager()->bundles['yiiunit\\framework\\web\\TestAssetBundle'] = [
  144. 'jsOptions' => [
  145. 'position' => $pos - 1,
  146. ],
  147. ];
  148. $view->getAssetManager()->bundles['yiiunit\\framework\\web\\TestJqueryAsset'] = [
  149. 'jsOptions' => [
  150. 'position' => $pos,
  151. ],
  152. ];
  153. $this->assertEmpty($view->assetBundles);
  154. if ($jqAlreadyRegistered) {
  155. TestJqueryAsset::register($view);
  156. }
  157. $this->setExpectedException('yii\\base\\InvalidConfigException');
  158. TestAssetBundle::register($view);
  159. }
  160. public function testCircularDependency()
  161. {
  162. $this->setExpectedException('yii\\base\\InvalidConfigException');
  163. TestAssetCircleA::register($this->getView());
  164. }
  165. public function testDuplicateAssetFile()
  166. {
  167. $view = $this->getView();
  168. $this->assertEmpty($view->assetBundles);
  169. TestSimpleAsset::register($view);
  170. $this->assertEquals(1, count($view->assetBundles));
  171. $this->assertArrayHasKey('yiiunit\\framework\\web\\TestSimpleAsset', $view->assetBundles);
  172. $this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestSimpleAsset'] instanceof AssetBundle);
  173. // register TestJqueryAsset which also has the jquery.js
  174. TestJqueryAsset::register($view);
  175. $expected = <<<EOF
  176. 123<script src="/js/jquery.js"></script>4
  177. EOF;
  178. $this->assertEquals($expected, $view->renderFile('@yiiunit/data/views/rawlayout.php'));
  179. }
  180. }
  181. class TestSimpleAsset extends AssetBundle
  182. {
  183. public $basePath = '@testWebRoot/js';
  184. public $baseUrl = '@testWeb/js';
  185. public $js = [
  186. 'jquery.js',
  187. ];
  188. }
  189. class TestAssetBundle extends AssetBundle
  190. {
  191. public $basePath = '@testWebRoot/files';
  192. public $baseUrl = '@testWeb/files';
  193. public $css = [
  194. 'cssFile.css',
  195. ];
  196. public $js = [
  197. 'jsFile.js',
  198. ];
  199. public $depends = [
  200. 'yiiunit\\framework\\web\\TestJqueryAsset'
  201. ];
  202. }
  203. class TestJqueryAsset extends AssetBundle
  204. {
  205. public $basePath = '@testWebRoot/js';
  206. public $baseUrl = '@testWeb/js';
  207. public $js = [
  208. 'jquery.js',
  209. ];
  210. public $depends = [
  211. 'yiiunit\\framework\\web\\TestAssetLevel3'
  212. ];
  213. }
  214. class TestAssetLevel3 extends AssetBundle
  215. {
  216. public $basePath = '@testWebRoot/js';
  217. public $baseUrl = '@testWeb/js';
  218. }
  219. class TestAssetCircleA extends AssetBundle
  220. {
  221. public $basePath = '@testWebRoot/js';
  222. public $baseUrl = '@testWeb/js';
  223. public $js = [
  224. 'jquery.js',
  225. ];
  226. public $depends = [
  227. 'yiiunit\\framework\\web\\TestAssetCircleB'
  228. ];
  229. }
  230. class TestAssetCircleB extends AssetBundle
  231. {
  232. public $basePath = '@testWebRoot/js';
  233. public $baseUrl = '@testWeb/js';
  234. public $js = [
  235. 'jquery.js',
  236. ];
  237. public $depends = [
  238. 'yiiunit\\framework\\web\\TestAssetCircleA'
  239. ];
  240. }