MessageControllerTest.php 11.4 KB
Newer Older
1 2 3 4 5 6 7 8
<?php

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

/**
 * Unit test for [[\yii\console\controllers\MessageController]].
 * @see MessageController
9 10
 *
 * @group console
11 12 13 14 15 16 17 18 19
 */
class MessageControllerTest extends TestCase
{
	protected $sourcePath = '';
	protected $messagePath = '';
	protected $configFileName = '';

	public function setUp()
	{
Qiang Xue committed
20
		$this->mockApplication();
21 22
		$this->sourcePath = Yii::getAlias('@yiiunit/runtime/test_source');
		$this->createDir($this->sourcePath);
23 24 25
		if (!file_exists($this->sourcePath)) {
			$this->markTestIncomplete('Unit tests runtime directory should have writable permissions!');
		}
26 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
		$this->messagePath = Yii::getAlias('@yiiunit/runtime/test_messages');
		$this->createDir($this->messagePath);
		$this->configFileName = Yii::getAlias('@yiiunit/runtime') . DIRECTORY_SEPARATOR . 'message_controller_test_config.php';
	}

	public function tearDown()
	{
		$this->removeDir($this->sourcePath);
		$this->removeDir($this->messagePath);
		if (file_exists($this->configFileName)) {
			unlink($this->configFileName);
		}
	}

	/**
	 * 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)) {
			$this->removeFileSystemObject($dirName);
		}
	}

	/**
	 * Removes file system object: directory or file.
	 * @param string $fileSystemObjectFullName file system object full name.
	 */
	protected function removeFileSystemObject($fileSystemObjectFullName)
	{
		if (!is_dir($fileSystemObjectFullName)) {
			unlink($fileSystemObjectFullName);
		} else {
			$dirHandle = opendir($fileSystemObjectFullName);
			while (($fileSystemObjectName = readdir($dirHandle)) !== false) {
				if ($fileSystemObjectName==='.' || $fileSystemObjectName==='..') {
					continue;
				}
				$this->removeFileSystemObject($fileSystemObjectFullName . DIRECTORY_SEPARATOR . $fileSystemObjectName);
			}
			closedir($dirHandle);
			rmdir($fileSystemObjectFullName);
		}
	}

	/**
	 * Creates test message controller instance.
	 * @return MessageController message command instance.
	 */
	protected function createMessageController()
	{
		$module = $this->getMock('yii\\base\\Module', array('fake'), array('console'));
90 91 92
		$messageController = new MessageController('message', $module);
		$messageController->interactive = false;
		return $messageController;
93 94 95 96 97 98 99 100
	}

	/**
	 * Emulates running of the message controller action.
	 * @param string $actionId id of action to be run.
	 * @param array $args action arguments.
	 * @return string command output.
	 */
101
	protected function runMessageControllerAction($actionId, array $args = array())
102 103 104 105
	{
		$controller = $this->createMessageController();
		ob_start();
		ob_implicit_flush(false);
Qiang Xue committed
106
		$controller->run($actionId, $args);
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
		return ob_get_clean();
	}

	/**
	 * Creates message command config file at {@link configFileName}
	 * @param array $config message command config.
	 */
	protected function composeConfigFile(array $config)
	{
		if (file_exists($this->configFileName)) {
			unlink($this->configFileName);
		}
		$fileContent = '<?php return ' . var_export($config, true) . ';';
		file_put_contents($this->configFileName, $fileContent);
	}

	/**
	 * Creates source file with given content
	 * @param string $content file content
	 * @param string|null $name file self name
	 */
	protected function createSourceFile($content, $name = null)
	{
		if (empty($name)) {
			$name = md5(uniqid()) . '.php';
		}
		file_put_contents($this->sourcePath . DIRECTORY_SEPARATOR . $name, $content);
	}

	/**
	 * Creates message file with given messages.
	 * @param string $name file name
	 * @param array $messages messages.
	 */
	protected function createMessageFile($name, array $messages = array())
	{
		$fileName = $this->messagePath . DIRECTORY_SEPARATOR . $name;
		if (file_exists($fileName)) {
			unlink($fileName);
		} else {
			$dirName = dirname($fileName);
			if (!file_exists($dirName)) {
				mkdir($dirName, 0777, true);
			}
		}
		$fileContent = '<?php return ' . var_export($messages, true) . ';';
		file_put_contents($fileName, $fileContent);
	}

	// Tests:

158
	public function testActionConfig()
159
	{
160
		$configFileName = $this->configFileName;
161
		$this->runMessageControllerAction('config', array($configFileName));
162
		$this->assertTrue(file_exists($configFileName), 'Unable to create config file template!');
163 164 165 166
	}

	public function testConfigFileNotExist()
	{
167
		$this->setExpectedException('yii\\console\\Exception');
168
		$this->runMessageControllerAction('extract', array('not_existing_file.php'));
169 170 171 172 173 174 175 176
	}

	public function testCreateTranslation()
	{
		$language = 'en';

		$category = 'test_category';
		$message = 'test message';
177
		$sourceFileContent = "Yii::t('{$category}', '{$message}')";
178 179 180 181 182 183 184
		$this->createSourceFile($sourceFileContent);

		$this->composeConfigFile(array(
			'languages' => array($language),
			'sourcePath' => $this->sourcePath,
			'messagePath' => $this->messagePath,
		));
185
		$this->runMessageControllerAction('extract', array($this->configFileName));
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

		$this->assertTrue(file_exists($this->messagePath . DIRECTORY_SEPARATOR . $language), 'No language dir created!');
		$messageFileName = $this->messagePath . DIRECTORY_SEPARATOR . $language . DIRECTORY_SEPARATOR . $category . '.php';
		$this->assertTrue(file_exists($messageFileName), 'No message file created!');
		$messages = require($messageFileName);
		$this->assertTrue(is_array($messages), 'Unable to compose messages!');
		$this->assertTrue(array_key_exists($message, $messages), 'Source message is missing!');
	}

	/**
	 * @depends testCreateTranslation
	 */
	public function testNothingNew()
	{
		$language = 'en';

		$category = 'test_category';
		$message = 'test message';
		$sourceFileContent = "Yii::t('{$category}', '{$message}')";
		$this->createSourceFile($sourceFileContent);

		$this->composeConfigFile(array(
			'languages' => array($language),
			'sourcePath' => $this->sourcePath,
			'messagePath' => $this->messagePath,
		));
212
		$this->runMessageControllerAction('extract', array($this->configFileName));
213 214 215 216 217 218 219 220

		$messageFileName = $this->messagePath . DIRECTORY_SEPARATOR . $language . DIRECTORY_SEPARATOR . $category . '.php';

		// check file not overwritten:
		$messageFileContent = file_get_contents($messageFileName);
		$messageFileContent .= '// some not generated by command content';
		file_put_contents($messageFileName, $messageFileContent);

221
		$this->runMessageControllerAction('extract', array($this->configFileName));
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

		$this->assertEquals($messageFileContent, file_get_contents($messageFileName));
	}

	/**
	 * @depends testCreateTranslation
	 */
	public function testMerge()
	{
		$language = 'en';
		$category = 'test_category';
		$messageFileName = $language . DIRECTORY_SEPARATOR . $category . '.php';

		$existingMessage = 'test existing message';
		$existingMessageContent = 'test existing message content';
		$this->createMessageFile($messageFileName, array(
			$existingMessage => $existingMessageContent
		));

		$newMessage = 'test new message';
		$sourceFileContent = "Yii::t('{$category}', '{$existingMessage}')";
		$sourceFileContent .= "Yii::t('{$category}', '{$newMessage}')";
		$this->createSourceFile($sourceFileContent);

		$this->composeConfigFile(array(
			'languages' => array($language),
			'sourcePath' => $this->sourcePath,
			'messagePath' => $this->messagePath,
			'overwrite' => true,
		));
252
		$this->runMessageControllerAction('extract', array($this->configFileName));
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283

		$messages = require($this->messagePath . DIRECTORY_SEPARATOR . $messageFileName);
		$this->assertTrue(array_key_exists($newMessage, $messages), 'Unable to add new message!');
		$this->assertTrue(array_key_exists($existingMessage, $messages), 'Unable to keep existing message!');
		$this->assertEquals('', $messages[$newMessage], 'Wrong new message content!');
		$this->assertEquals($existingMessageContent, $messages[$existingMessage], 'Unable to keep existing message content!');
	}

	/**
	 * @depends testMerge
	 */
	public function testNoLongerNeedTranslation()
	{
		$language = 'en';
		$category = 'test_category';
		$messageFileName = $language . DIRECTORY_SEPARATOR . $category . '.php';

		$oldMessage = 'test old message';
		$oldMessageContent = 'test old message content';
		$this->createMessageFile($messageFileName, array(
			$oldMessage => $oldMessageContent
		));

		$sourceFileContent = "Yii::t('{$category}', 'some new message')";
		$this->createSourceFile($sourceFileContent);

		$this->composeConfigFile(array(
			'languages' => array($language),
			'sourcePath' => $this->sourcePath,
			'messagePath' => $this->messagePath,
			'overwrite' => true,
284
			'removeUnused' => false,
285
		));
286
		$this->runMessageControllerAction('extract', array($this->configFileName));
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

		$messages = require($this->messagePath . DIRECTORY_SEPARATOR . $messageFileName);

		$this->assertTrue(array_key_exists($oldMessage, $messages), 'No longer needed message removed!');
		$this->assertEquals('@@' . $oldMessageContent . '@@', $messages[$oldMessage], 'No longer needed message content does not marked properly!');
	}

	/**
	 * @depends testMerge
	 */
	public function testMergeWithContentZero()
	{
		$language = 'en';
		$category = 'test_category';
		$messageFileName = $language . DIRECTORY_SEPARATOR . $category . '.php';

		$zeroMessage = 'test zero message';
		$zeroMessageContent = '0';
		$falseMessage = 'test false message';
		$falseMessageContent = 'false';
		$this->createMessageFile($messageFileName, array(
			$zeroMessage => $zeroMessageContent,
			$falseMessage => $falseMessageContent,
		));

		$newMessage = 'test new message';
		$sourceFileContent = "Yii::t('{$category}', '{$zeroMessage}')";
		$sourceFileContent .= "Yii::t('{$category}', '{$falseMessage}')";
		$sourceFileContent .= "Yii::t('{$category}', '{$newMessage}')";
		$this->createSourceFile($sourceFileContent);

		$this->composeConfigFile(array(
			'languages' => array($language),
			'sourcePath' => $this->sourcePath,
			'messagePath' => $this->messagePath,
			'overwrite' => true,
		));
324
		$this->runMessageControllerAction('extract', array($this->configFileName));
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

		$messages = require($this->messagePath . DIRECTORY_SEPARATOR . $messageFileName);
		$this->assertTrue($zeroMessageContent === $messages[$zeroMessage], 'Message content "0" is lost!');
		$this->assertTrue($falseMessageContent === $messages[$falseMessage], 'Message content "false" is lost!');
	}

	/**
	 * @depends testCreateTranslation
	 */
	public function testMultiplyTranslators()
	{
		$language = 'en';
		$category = 'test_category';

		$translators = array(
			'Yii::t',
			'Custom::translate',
		);

		$sourceMessages = array(
			'first message',
			'second message',
		);
		$sourceFileContent = '';
		foreach ($sourceMessages as $key => $message) {
			$sourceFileContent .= $translators[$key] . "('{$category}', '{$message}');\n";
		}
		$this->createSourceFile($sourceFileContent);

		$this->composeConfigFile(array(
			'languages' => array($language),
			'sourcePath' => $this->sourcePath,
			'messagePath' => $this->messagePath,
			'translator' => $translators,
		));
360
		$this->runMessageControllerAction('extract', array($this->configFileName));
361 362 363 364 365 366 367 368 369

		$messageFileName = $this->messagePath . DIRECTORY_SEPARATOR . $language . DIRECTORY_SEPARATOR . $category . '.php';
		$messages = require($messageFileName);

		foreach ($sourceMessages as $sourceMessage) {
			$this->assertTrue(array_key_exists($sourceMessage, $messages));
		}
	}
}