AssetControllerTest.php 6.46 KB
Newer Older
1 2 3 4 5 6
<?php

use yiiunit\TestCase;
use yii\console\controllers\AssetController;

/**
7
 * Unit test for [[\yii\console\controllers\AssetController]].
8 9 10 11 12 13 14 15
 * @see AssetController
 */
class AssetControllerTest extends TestCase
{
	/**
	 * @var string path for the test files.
	 */
	protected $testFilePath = '';
16 17 18 19
	/**
	 * @var string test assets path.
	 */
	protected $testAssetsBasePath = '';
20 21 22 23 24

	public function setUp()
	{
		$this->testFilePath = Yii::getAlias('@yiiunit/runtime') . DIRECTORY_SEPARATOR . get_class($this);
		$this->createDir($this->testFilePath);
25 26
		$this->testAssetsBasePath = $this->testFilePath . DIRECTORY_SEPARATOR . 'assets';
		$this->createDir($this->testAssetsBasePath);
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	}

	public function tearDown()
	{
		$this->removeDir($this->testFilePath);
	}

	/**
	 * Creates directory.
	 * @param $dirName directory full name.
	 */
	protected function createDir($dirName)
	{
		if (!file_exists($dirName)) {
			mkdir($dirName, 0777, true);
		}
	}

	/**
	 * Removes directory.
	 * @param $dirName directory full name
	 */
	protected function removeDir($dirName)
	{
		if (!empty($dirName) && file_exists($dirName)) {
			exec("rm -rf {$dirName}");
		}
	}

	/**
	 * Creates test asset controller instance.
	 * @return AssetController
	 */
	protected function createAssetController()
	{
		$module = $this->getMock('yii\\base\\Module', array('fake'), array('console'));
		$assetController = new AssetController('asset', $module);
64
		$assetController->interactive = false;
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
		$assetController->jsCompressor = 'cp {from} {to}';
		$assetController->cssCompressor = 'cp {from} {to}';
		return $assetController;
	}

	/**
	 * Emulates running of the asset controller action.
	 * @param string $actionId id of action to be run.
	 * @param array $args action arguments.
	 * @return string command output.
	 */
	protected function runAssetControllerAction($actionId, array $args=array())
	{
		$controller = $this->createAssetController();
		ob_start();
		ob_implicit_flush(false);
		$params = array(
			\yii\console\Request::ANONYMOUS_PARAMS => $args
		);
		$controller->run($actionId, $params);
		return ob_get_clean();
	}

88 89
	/**
	 * Creates test compress config.
90
	 * @param array[] $bundles asset bundles config.
91 92
	 * @return array config array.
	 */
93
	protected function createCompressConfig(array $bundles)
94 95 96
	{
		$baseUrl = '/test';
		$config = array(
97
			'bundles' => $this->createBundleConfig($bundles),
98 99
			'targets' => array(
				'all' => array(
100
					'basePath' => $this->testAssetsBasePath,
101
					'baseUrl' => $baseUrl,
102 103
					'js' => 'all.js',
					'css' => 'all.css',
104 105 106
				),
			),
			'assetManager' => array(
107
				'basePath' => $this->testAssetsBasePath,
108 109 110 111 112 113 114 115
				'baseUrl' => $baseUrl,
			),
		);
		return $config;
	}

	/**
	 * Creates test bundle configuration.
116
	 * @param array[] $bundles asset bundles config.
117 118
	 * @return array bundle config.
	 */
119
	protected function createBundleConfig(array $bundles)
120
	{
121 122 123 124 125 126 127 128
		foreach ($bundles as $name => $config) {
			if (!array_key_exists('basePath', $config)) {
				$bundles[$name]['basePath'] = $this->testFilePath;
			}
			if (!array_key_exists('baseUrl', $config)) {
				$bundles[$name]['baseUrl'] = '';
			}
		}
129 130 131 132 133 134
		return $bundles;
	}

	/**
	 * Creates test compress config file.
	 * @param string $fileName output file name.
135 136
	 * @param array[] $bundles asset bundles config.
	 * @throws Exception on failure.
137
	 */
138
	protected function createCompressConfigFile($fileName, array $bundles)
139
	{
140 141 142 143
		$content = '<?php return '.var_export($this->createCompressConfig($bundles), true).';';
		if (file_put_contents($fileName, $content) <= 0) {
			throw new \Exception("Unable to create file '{$fileName}'!");
		}
144 145
	}

146 147 148 149
	/**
	 * Creates test asset file.
	 * @param string $fileRelativeName file name relative to [[testFilePath]]
	 * @param string $content file content
150
	 * @throws Exception on failure.
151
	 */
152
	protected function createAssetSourceFile($fileRelativeName, $content)
153 154 155
	{
		$fileFullName = $this->testFilePath.DIRECTORY_SEPARATOR.$fileRelativeName;
		$this->createDir(dirname($fileFullName));
156 157 158 159 160 161 162 163 164 165 166 167 168 169
		if (file_put_contents($fileFullName, $content)<=0) {
			throw new \Exception("Unable to create file '{$fileFullName}'!");
		}
	}

	/**
	 * Creates a list of asset source files.
	 * @param array $files assert source files in format: file/relative/name => fileContent
	 */
	protected function createAssertSourceFiles(array $files)
	{
		foreach ($files as $name => $content) {
			$this->createAssetSourceFile($name, $content);
		}
170 171
	}

172 173 174 175 176 177 178 179
	// Tests :

	public function testActionTemplate()
	{
		$configFileName = $this->testFilePath . DIRECTORY_SEPARATOR . 'config.php';
		$this->runAssetControllerAction('template', array($configFileName));
		$this->assertTrue(file_exists($configFileName), 'Unable to create config file template!');
	}
180 181 182

	public function testActionCompress()
	{
183 184 185
		// Given :
		$cssFiles = array(
			'css/test_body.css' => 'body {
186 187
				padding-top: 20px;
				padding-bottom: 60px;
188 189 190 191 192
			}',
			'css/test_footer.css' => '.footer {
				margin: 20px;
				display: block;
			}',
193
		);
194 195 196 197
		$this->createAssertSourceFiles($cssFiles);

		$jsFiles = array(
			'js/test_alert.js' => "function test() {
198
				alert('Test message');
199 200 201 202
			}",
			'js/test_sum_ab.js' => "function sumAB(a, b) {
				return a + b;
			}",
203
		);
204
		$this->createAssertSourceFiles($jsFiles);
205

206 207 208 209 210 211
		$bundles = array(
			'app' => array(
				'css' => array_keys($cssFiles),
				'js' => array_keys($jsFiles),
			),
		);;
212
		$bundleFile = $this->testFilePath . DIRECTORY_SEPARATOR . 'bundle.php';
213 214 215

		$configFile = $this->testFilePath . DIRECTORY_SEPARATOR . 'config.php';
		$this->createCompressConfigFile($configFile, $bundles);
216

217
		// When :
218 219
		$this->runAssetControllerAction('compress', array($configFile, $bundleFile));

220
		// Then :
221 222
		$this->assertTrue(file_exists($bundleFile), 'Unable to create output bundle file!');

223
		$compressedCssFileName = $this->testAssetsBasePath . DIRECTORY_SEPARATOR . 'all.css';
224
		$this->assertTrue(file_exists($compressedCssFileName), 'Unable to compress CSS files!');
225
		$compressedJsFileName = $this->testAssetsBasePath . DIRECTORY_SEPARATOR . 'all.js';
226
		$this->assertTrue(file_exists($compressedJsFileName), 'Unable to compress JS files!');
227 228 229 230 231 232 233 234 235

		$compressedCssFileContent = file_get_contents($compressedCssFileName);
		foreach ($cssFiles as $name => $content) {
			$this->assertContains($content, $compressedCssFileContent, "Source of '{$name}' is missing in combined file!");
		}
		$compressedJsFileContent = file_get_contents($compressedJsFileName);
		foreach ($jsFiles as $name => $content) {
			$this->assertContains($content, $compressedJsFileContent, "Source of '{$name}' is missing in combined file!");
		}
236
	}
237
}