AssetBundleTest.php 7.03 KB
Newer Older
1 2
<?php
/**
3 4
 *
 *
5 6 7 8 9 10
 * @author Carsten Brandt <mail@cebe.cc>
 */

namespace yiiunit\framework\web;

use Yii;
Alexander Makarov committed
11
use yii\web\View;
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
use yii\web\AssetBundle;
use yii\web\AssetManager;

/**
 * @group web
 */
class AssetBundleTest extends \yiiunit\TestCase
{
	protected function setUp()
	{
		parent::setUp();
		$this->mockApplication();

		Yii::setAlias('@testWeb', '/');
		Yii::setAlias('@testWebRoot', '@yiiunit/data/web');
	}

	protected function getView()
	{
		$view = new View();
Alexander Makarov committed
32
		$view->setAssetManager(new AssetManager([
33 34
			'basePath' => '@testWebRoot/assets',
			'baseUrl' => '@testWeb/assets',
Alexander Makarov committed
35
		]));
36 37 38 39 40 41 42 43 44

		return $view;
	}

	public function testRegister()
	{
		$view = $this->getView();

		$this->assertEmpty($view->assetBundles);
45
		TestSimpleAsset::register($view);
46
		$this->assertEquals(1, count($view->assetBundles));
47 48
		$this->assertArrayHasKey('yiiunit\\framework\\web\\TestSimpleAsset', $view->assetBundles);
		$this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestSimpleAsset'] instanceof AssetBundle);
Carsten Brandt committed
49 50

		$expected = <<<EOF
51
123<script src="/js/jquery.js"></script>4
Carsten Brandt committed
52 53
EOF;
		$this->assertEquals($expected, $view->renderFile('@yiiunit/data/views/rawlayout.php'));
54 55 56 57 58 59 60 61
	}

	public function testSimpleDependency()
	{
		$view = $this->getView();

		$this->assertEmpty($view->assetBundles);
		TestAssetBundle::register($view);
62
		$this->assertEquals(3, count($view->assetBundles));
63 64
		$this->assertArrayHasKey('yiiunit\\framework\\web\\TestAssetBundle', $view->assetBundles);
		$this->assertArrayHasKey('yiiunit\\framework\\web\\TestJqueryAsset', $view->assetBundles);
65
		$this->assertArrayHasKey('yiiunit\\framework\\web\\TestAssetLevel3', $view->assetBundles);
Carsten Brandt committed
66 67
		$this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestAssetBundle'] instanceof AssetBundle);
		$this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset'] instanceof AssetBundle);
68
		$this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestAssetLevel3'] instanceof AssetBundle);
Carsten Brandt committed
69 70

		$expected = <<<EOF
71 72
1<link href="/files/cssFile.css" rel="stylesheet">23<script src="/js/jquery.js"></script>
<script src="/files/jsFile.js"></script>4
Carsten Brandt committed
73 74 75 76 77 78
EOF;
		$this->assertEquals($expected, $view->renderFile('@yiiunit/data/views/rawlayout.php'));
	}

	public function positionProvider()
	{
Alexander Makarov committed
79 80 81 82 83 84 85 86
		return [
			[View::POS_HEAD, true],
			[View::POS_HEAD, false],
			[View::POS_BEGIN, true],
			[View::POS_BEGIN, false],
			[View::POS_END, true],
			[View::POS_END, false],
		];
Carsten Brandt committed
87 88 89 90 91 92 93 94 95
	}

	/**
	 * @dataProvider positionProvider
	 */
	public function testPositionDependency($pos, $jqAlreadyRegistered)
	{
		$view = $this->getView();

Alexander Makarov committed
96 97
		$view->getAssetManager()->bundles['yiiunit\\framework\\web\\TestAssetBundle'] = [
			'jsOptions' => [
Carsten Brandt committed
98
				'position' => $pos,
Alexander Makarov committed
99 100
			],
		];
Carsten Brandt committed
101 102 103 104 105 106

		$this->assertEmpty($view->assetBundles);
		if ($jqAlreadyRegistered) {
			TestJqueryAsset::register($view);
		}
		TestAssetBundle::register($view);
107
		$this->assertEquals(3, count($view->assetBundles));
Carsten Brandt committed
108 109
		$this->assertArrayHasKey('yiiunit\\framework\\web\\TestAssetBundle', $view->assetBundles);
		$this->assertArrayHasKey('yiiunit\\framework\\web\\TestJqueryAsset', $view->assetBundles);
110
		$this->assertArrayHasKey('yiiunit\\framework\\web\\TestAssetLevel3', $view->assetBundles);
Carsten Brandt committed
111 112 113

		$this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestAssetBundle'] instanceof AssetBundle);
		$this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset'] instanceof AssetBundle);
114
		$this->assertTrue($view->assetBundles['yiiunit\\framework\\web\\TestAssetLevel3'] instanceof AssetBundle);
Carsten Brandt committed
115 116 117 118 119

		$this->assertArrayHasKey('position', $view->assetBundles['yiiunit\\framework\\web\\TestAssetBundle']->jsOptions);
		$this->assertEquals($pos, $view->assetBundles['yiiunit\\framework\\web\\TestAssetBundle']->jsOptions['position']);
		$this->assertArrayHasKey('position', $view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset']->jsOptions);
		$this->assertEquals($pos, $view->assetBundles['yiiunit\\framework\\web\\TestJqueryAsset']->jsOptions['position']);
120 121
		$this->assertArrayHasKey('position', $view->assetBundles['yiiunit\\framework\\web\\TestAssetLevel3']->jsOptions);
		$this->assertEquals($pos, $view->assetBundles['yiiunit\\framework\\web\\TestAssetLevel3']->jsOptions['position']);
Carsten Brandt committed
122 123 124 125 126 127 128

		switch($pos)
		{
			case View::POS_HEAD:
				$expected = <<<EOF
1<link href="/files/cssFile.css" rel="stylesheet">
<script src="/js/jquery.js"></script>
129
<script src="/files/jsFile.js"></script>234
Carsten Brandt committed
130 131 132 133
EOF;
			break;
			case View::POS_BEGIN:
				$expected = <<<EOF
134 135
1<link href="/files/cssFile.css" rel="stylesheet">2<script src="/js/jquery.js"></script>
<script src="/files/jsFile.js"></script>34
Carsten Brandt committed
136 137 138 139 140
EOF;
			break;
			default:
			case View::POS_END:
				$expected = <<<EOF
141 142
1<link href="/files/cssFile.css" rel="stylesheet">23<script src="/js/jquery.js"></script>
<script src="/files/jsFile.js"></script>4
Carsten Brandt committed
143 144 145 146 147 148 149 150
EOF;
			break;
		}
		$this->assertEquals($expected, $view->renderFile('@yiiunit/data/views/rawlayout.php'));
	}

	public function positionProvider2()
	{
Alexander Makarov committed
151 152 153 154 155 156
		return [
			[View::POS_BEGIN, true],
			[View::POS_BEGIN, false],
			[View::POS_END, true],
			[View::POS_END, false],
		];
Carsten Brandt committed
157 158 159 160 161 162 163 164 165
	}

	/**
	 * @dataProvider positionProvider
	 */
	public function testPositionDependencyConflict($pos, $jqAlreadyRegistered)
	{
		$view = $this->getView();

Alexander Makarov committed
166 167
		$view->getAssetManager()->bundles['yiiunit\\framework\\web\\TestAssetBundle'] = [
			'jsOptions' => [
Carsten Brandt committed
168
				'position' => $pos - 1,
Alexander Makarov committed
169 170 171 172
			],
		];
		$view->getAssetManager()->bundles['yiiunit\\framework\\web\\TestJqueryAsset'] = [
			'jsOptions' => [
Carsten Brandt committed
173
				'position' => $pos,
Alexander Makarov committed
174 175
			],
		];
Carsten Brandt committed
176 177 178 179 180 181 182

		$this->assertEmpty($view->assetBundles);
		if ($jqAlreadyRegistered) {
			TestJqueryAsset::register($view);
		}
		$this->setExpectedException('yii\\base\\InvalidConfigException');
		TestAssetBundle::register($view);
183
	}
184 185 186 187 188 189 190 191 192 193 194 195

	public function testCircularDependency()
	{
		$this->setExpectedException('yii\\base\\InvalidConfigException');
		TestAssetCircleA::register($this->getView());
	}
}

class TestSimpleAsset extends AssetBundle
{
	public $basePath = '@testWebRoot/js';
	public $baseUrl = '@testWeb/js';
Alexander Makarov committed
196
	public $js = [
197
		'jquery.js',
Alexander Makarov committed
198
	];
199 200 201 202 203 204
}

class TestAssetBundle extends AssetBundle
{
	public $basePath = '@testWebRoot/files';
	public $baseUrl = '@testWeb/files';
Alexander Makarov committed
205
	public $css = [
206
		'cssFile.css',
Alexander Makarov committed
207 208
	];
	public $js = [
209
		'jsFile.js',
Alexander Makarov committed
210 211
	];
	public $depends = [
212
		'yiiunit\\framework\\web\\TestJqueryAsset'
Alexander Makarov committed
213
	];
214 215 216 217 218 219
}

class TestJqueryAsset extends AssetBundle
{
	public $basePath = '@testWebRoot/js';
	public $baseUrl = '@testWeb/js';
Alexander Makarov committed
220
	public $js = [
221
		'jquery.js',
Alexander Makarov committed
222 223
	];
	public $depends = [
224
		'yiiunit\\framework\\web\\TestAssetLevel3'
Alexander Makarov committed
225
	];
226 227 228 229 230 231 232 233 234 235 236 237
}

class TestAssetLevel3 extends AssetBundle
{
	public $basePath = '@testWebRoot/js';
	public $baseUrl = '@testWeb/js';
}

class TestAssetCircleA extends AssetBundle
{
	public $basePath = '@testWebRoot/js';
	public $baseUrl = '@testWeb/js';
Alexander Makarov committed
238
	public $js = [
239
		'jquery.js',
Alexander Makarov committed
240 241
	];
	public $depends = [
242
		'yiiunit\\framework\\web\\TestAssetCircleB'
Alexander Makarov committed
243
	];
244 245 246 247 248 249
}

class TestAssetCircleB extends AssetBundle
{
	public $basePath = '@testWebRoot/js';
	public $baseUrl = '@testWeb/js';
Alexander Makarov committed
250
	public $js = [
251
		'jquery.js',
Alexander Makarov committed
252 253
	];
	public $depends = [
254
		'yiiunit\\framework\\web\\TestAssetCircleA'
Alexander Makarov committed
255
	];
256
}