FileHelperTest.php 8.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php

use yii\helpers\base\FileHelper;
use yii\test\TestCase;

/**
 * Unit test for [[yii\helpers\base\FileHelper]]
 * @see FileHelper
 */
class FileHelperTest extends TestCase
{
12 13 14
	/**
	 * @var string test files path.
	 */
15 16 17 18 19 20
	private $testFilePath = '';

	public function setUp()
	{
		$this->testFilePath = Yii::getAlias('@yiiunit/runtime') . DIRECTORY_SEPARATOR . get_class($this);
		$this->createDir($this->testFilePath);
21 22 23
		if (!file_exists($this->testFilePath)) {
			$this->markTestIncomplete('Unit tests runtime directory should have writable permissions!');
		}
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
	}

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

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

	/**
	 * Removes directory.
	 * @param string $dirName directory full name.
	 */
	protected function removeDir($dirName)
	{
Qiang Xue committed
48
		if (!empty($dirName) && is_dir($dirName)) {
49 50 51 52 53 54 55 56 57 58 59 60 61
			if ($handle = opendir($dirName)) {
				while (false !== ($entry = readdir($handle))) {
					if ($entry != '.' && $entry != '..') {
						if (is_dir($dirName . DIRECTORY_SEPARATOR . $entry) === true) {
							$this->removeDir($dirName . DIRECTORY_SEPARATOR . $entry);
						} else {
							unlink($dirName . DIRECTORY_SEPARATOR . $entry);
						}
					}
				}
				closedir($handle);
				rmdir($dirName);
			}
62 63 64
		}
	}

65 66 67 68 69 70 71 72 73 74
	/**
	 * Get file permission mode.
	 * @param string $file file name.
	 * @return string permission mode.
	 */
	protected function getMode($file)
	{
		return substr(sprintf('%o', fileperms($file)), -4);
	}

75 76 77 78 79 80
	/**
	 * Creates test files structure,
	 * @param array $items file system objects to be created in format: objectName => objectContent
	 * Arrays specifies directories, other values - files.
	 * @param string $basePath structure base file path.
	 */
81 82
	protected function createFileStructure(array $items, $basePath = '')
	{
83 84 85 86 87 88 89 90 91 92 93 94 95 96
		if (empty($basePath)) {
			$basePath = $this->testFilePath;
		}
		foreach ($items as $name => $content) {
			$itemName = $basePath . DIRECTORY_SEPARATOR . $name;
			if (is_array($content)) {
				mkdir($itemName, 0777, true);
				$this->createFileStructure($content, $itemName);
			} else {
				file_put_contents($itemName, $content);
			}
		}
	}

97 98 99 100 101 102 103 104 105 106 107 108
	/**
	 * Asserts that file has specific permission mode.
	 * @param integer $expectedMode expected file permission mode.
	 * @param string $fileName file name.
	 * @param string $message error message
	 */
	protected function assertFileMode($expectedMode, $fileName, $message='')
	{
		$expectedMode = sprintf('%o', $expectedMode);
		$this->assertEquals($expectedMode, $this->getMode($fileName), $message);
	}

109 110
	// Tests :

111 112
	public function testCopyDirectory()
	{
113
		$srcDirName = 'test_src_dir';
114 115 116 117
		$files = array(
			'file1.txt' => 'file 1 content',
			'file2.txt' => 'file 2 content',
		);
118 119 120 121 122 123
		$this->createFileStructure(array(
			$srcDirName => $files
		));

		$basePath = $this->testFilePath;
		$srcDirName = $basePath . DIRECTORY_SEPARATOR . $srcDirName;
124 125 126 127 128 129 130 131 132 133 134
		$dstDirName = $basePath . DIRECTORY_SEPARATOR . 'test_dst_dir';

		FileHelper::copyDirectory($srcDirName, $dstDirName);

		$this->assertTrue(file_exists($dstDirName), 'Destination directory does not exist!');
		foreach ($files as $name => $content) {
			$fileName = $dstDirName . DIRECTORY_SEPARATOR . $name;
			$this->assertTrue(file_exists($fileName), 'Directory file is missing!');
			$this->assertEquals($content, file_get_contents($fileName), 'Incorrect file content!');
		}
	}
135

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	/**
	 * @depends testCopyDirectory
	 */
	public function testCopyDirectoryPermissions()
	{
		if (substr(PHP_OS, 0, 3) == 'WIN') {
			$this->markTestSkipped("Can't reliably test it on Windows because fileperms() always return 0777.");
		}

		$srcDirName = 'test_src_dir';
		$subDirName = 'test_sub_dir';
		$fileName = 'test_file.txt';
		$this->createFileStructure(array(
			$srcDirName => array(
				$subDirName => array(),
				$fileName => 'test file content',
			),
		));

		$basePath = $this->testFilePath;
		$srcDirName = $basePath . DIRECTORY_SEPARATOR . $srcDirName;
		$dstDirName = $basePath . DIRECTORY_SEPARATOR . 'test_dst_dir';

		$dirMode = 0755;
		$fileMode = 0755;
		$options = array(
			'dirMode' => $dirMode,
			'fileMode' => $fileMode,
		);
		FileHelper::copyDirectory($srcDirName, $dstDirName, $options);

		$this->assertFileMode($dirMode, $dstDirName, 'Destination directory has wrong mode!');
		$this->assertFileMode($dirMode, $dstDirName . DIRECTORY_SEPARATOR . $subDirName, 'Copied sub directory has wrong mode!');
		$this->assertFileMode($fileMode, $dstDirName . DIRECTORY_SEPARATOR . $fileName, 'Copied file has wrong mode!');
	}

Qiang Xue committed
172
	public function stestRemoveDirectory()
173
	{
174 175 176 177 178 179 180 181 182 183 184 185
		$dirName = 'test_dir_for_remove';
		$this->createFileStructure(array(
			$dirName => array(
				'file1.txt' => 'file 1 content',
				'file2.txt' => 'file 2 content',
				'test_sub_dir' => array(
					'sub_dir_file_1.txt' => 'sub dir file 1 content',
					'sub_dir_file_2.txt' => 'sub dir file 2 content',
				),
			),
		));

186
		$basePath = $this->testFilePath;
187
		$dirName = $basePath . DIRECTORY_SEPARATOR . $dirName;
188 189 190 191 192

		FileHelper::removeDirectory($dirName);

		$this->assertFalse(file_exists($dirName), 'Unable to remove directory!');
	}
193 194 195

	public function testFindFiles()
	{
196 197 198 199 200 201 202 203 204 205 206
		$dirName = 'test_dir';
		$this->createFileStructure(array(
			$dirName => array(
				'file_1.txt' => 'file 1 content',
				'file_2.txt' => 'file 2 content',
				'test_sub_dir' => array(
					'file_1_1.txt' => 'sub dir file 1 content',
					'file_1_2.txt' => 'sub dir file 2 content',
				),
			),
		));
207
		$basePath = $this->testFilePath;
208 209 210 211 212 213
		$dirName = $basePath . DIRECTORY_SEPARATOR . $dirName;
		$expectedFiles = array(
			$dirName . DIRECTORY_SEPARATOR . 'file_1.txt',
			$dirName . DIRECTORY_SEPARATOR . 'file_2.txt',
			$dirName . DIRECTORY_SEPARATOR . 'test_sub_dir' . DIRECTORY_SEPARATOR . 'file_1_1.txt',
			$dirName . DIRECTORY_SEPARATOR . 'test_sub_dir' . DIRECTORY_SEPARATOR . 'file_1_2.txt',
214 215 216 217
		);

		$foundFiles = FileHelper::findFiles($dirName);
		sort($expectedFiles);
Qiang Xue committed
218
		sort($foundFiles);
219 220
		$this->assertEquals($expectedFiles, $foundFiles);
	}
221

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
	/**
	 * @depends testFindFiles
	 */
	public function testFindFileFilter()
	{
		$dirName = 'test_dir';
		$passedFileName = 'passed.txt';
		$this->createFileStructure(array(
			$dirName => array(
				$passedFileName => 'passed file content',
				'declined.txt' => 'declined file content',
			),
		));
		$basePath = $this->testFilePath;
		$dirName = $basePath . DIRECTORY_SEPARATOR . $dirName;

		$options = array(
Qiang Xue committed
239 240
			'filter' => function($path) use ($passedFileName) {
				return $passedFileName == basename($path);
241 242 243 244 245 246
			}
		);
		$foundFiles = FileHelper::findFiles($dirName, $options);
		$this->assertEquals(array($dirName . DIRECTORY_SEPARATOR . $passedFileName), $foundFiles);
	}

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
	/**
	 * @depends testFindFiles
	 */
	public function testFindFilesExclude()
	{
		$dirName = 'test_dir';
		$fileName = 'test_file.txt';
		$excludeFileName = 'exclude_file.txt';
		$this->createFileStructure(array(
			$dirName => array(
				$fileName => 'file content',
				$excludeFileName => 'exclude file content',
			),
		));
		$basePath = $this->testFilePath;
		$dirName = $basePath . DIRECTORY_SEPARATOR . $dirName;

		$options = array(
Qiang Xue committed
265
			'except' => array($excludeFileName),
266 267 268 269
		);
		$foundFiles = FileHelper::findFiles($dirName, $options);
		$this->assertEquals(array($dirName . DIRECTORY_SEPARATOR . $fileName), $foundFiles);
	}
270 271 272 273

	public function testMkdir() {
		$basePath = $this->testFilePath;
		$dirName = $basePath . DIRECTORY_SEPARATOR . 'test_dir_level_1' . DIRECTORY_SEPARATOR . 'test_dir_level_2';
Qiang Xue committed
274
		FileHelper::mkdir($dirName);
275 276
		$this->assertTrue(file_exists($dirName), 'Unable to create directory recursively!');
	}
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

	public function testGetMimeTypeByExtension()
	{
		$magicFile = $this->testFilePath . DIRECTORY_SEPARATOR . 'mime_type.php';
		$mimeTypeMap = array(
			'txa' => 'application/json',
			'txb' => 'another/mime',
		);
		$magicFileContent = '<?php return ' . var_export($mimeTypeMap, true) . ';';
		file_put_contents($magicFile, $magicFileContent);

		foreach ($mimeTypeMap as $extension => $mimeType) {
			$fileName = 'test.' . $extension;
			$this->assertNull(FileHelper::getMimeTypeByExtension($fileName));
			$this->assertEquals($mimeType, FileHelper::getMimeTypeByExtension($fileName, $magicFile));
		}
	}
Qiang Xue committed
294
}