PageRenderTime 74ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/file.test.php

https://github.com/FlorianH/Markab
PHP | 472 lines | 335 code | 40 blank | 97 comment | 12 complexity | a182d694fbb26486bb39a234e5a6759b MD5 | raw file
  1. <?php
  2. /**
  3. * FileTest file
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The Open Group Test Suite License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package cake
  16. * @subpackage cake.tests.cases.libs
  17. * @since CakePHP(tm) v 1.2.0.4206
  18. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  19. */
  20. App::import('Core', 'File');
  21. /**
  22. * FileTest class
  23. *
  24. * @package cake
  25. * @subpackage cake.tests.cases.libs
  26. */
  27. class FileTest extends CakeTestCase {
  28. /**
  29. * File property
  30. *
  31. * @var mixed null
  32. * @access public
  33. */
  34. var $File = null;
  35. /**
  36. * testBasic method
  37. *
  38. * @access public
  39. * @return void
  40. */
  41. function testBasic() {
  42. $file = __FILE__;
  43. $this->File =& new File($file);
  44. $result = $this->File->pwd();
  45. $expecting = $file;
  46. $this->assertEqual($result, $expecting);
  47. $result = $this->File->name;
  48. $expecting = basename(__FILE__);
  49. $this->assertEqual($result, $expecting);
  50. $result = $this->File->info();
  51. $expecting = array(
  52. 'dirname' => dirname(__FILE__), 'basename' => basename(__FILE__),
  53. 'extension' => 'php', 'filename' =>'file.test'
  54. );
  55. $this->assertEqual($result, $expecting);
  56. $result = $this->File->ext();
  57. $expecting = 'php';
  58. $this->assertEqual($result, $expecting);
  59. $result = $this->File->name();
  60. $expecting = 'file.test';
  61. $this->assertEqual($result, $expecting);
  62. $result = $this->File->md5();
  63. $expecting = md5_file($file);
  64. $this->assertEqual($result, $expecting);
  65. $result = $this->File->md5(true);
  66. $expecting = md5_file($file);
  67. $this->assertEqual($result, $expecting);
  68. $result = $this->File->size();
  69. $expecting = filesize($file);
  70. $this->assertEqual($result, $expecting);
  71. $result = $this->File->owner();
  72. $expecting = fileowner($file);
  73. $this->assertEqual($result, $expecting);
  74. $result = $this->File->group();
  75. $expecting = filegroup($file);
  76. $this->assertEqual($result, $expecting);
  77. $result = $this->File->Folder();
  78. $this->assertIsA($result, 'Folder');
  79. $this->skipIf(DIRECTORY_SEPARATOR === '\\', '%s File permissions tests not supported on Windows');
  80. $result = $this->File->perms();
  81. $expecting = '0644';
  82. $this->assertEqual($result, $expecting);
  83. }
  84. /**
  85. * testRead method
  86. *
  87. * @access public
  88. * @return void
  89. */
  90. function testRead() {
  91. $file = __FILE__;
  92. $this->File =& new File($file);
  93. $result = $this->File->read();
  94. $expecting = file_get_contents(__FILE__);
  95. $this->assertEqual($result, $expecting);
  96. $this->assertTrue(!is_resource($this->File->handle));
  97. $this->File->lock = true;
  98. $result = $this->File->read();
  99. $expecting = file_get_contents(__FILE__);
  100. $this->assertEqual($result, $expecting);
  101. $this->File->lock = null;
  102. $data = $expecting;
  103. $expecting = substr($data, 0, 3);
  104. $result = $this->File->read(3);
  105. $this->assertEqual($result, $expecting);
  106. $this->assertTrue(is_resource($this->File->handle));
  107. $expecting = substr($data, 3, 3);
  108. $result = $this->File->read(3);
  109. $this->assertEqual($result, $expecting);
  110. }
  111. /**
  112. * testOffset method
  113. *
  114. * @access public
  115. * @return void
  116. */
  117. function testOffset() {
  118. $this->File->close();
  119. $result = $this->File->offset();
  120. $this->assertFalse($result);
  121. $this->assertFalse(is_resource($this->File->handle));
  122. $success = $this->File->offset(0);
  123. $this->assertTrue($success);
  124. $this->assertTrue(is_resource($this->File->handle));
  125. $result = $this->File->offset();
  126. $expecting = 0;
  127. $this->assertIdentical($result, $expecting);
  128. $data = file_get_contents(__FILE__);
  129. $success = $this->File->offset(5);
  130. $expecting = substr($data, 5, 3);
  131. $result = $this->File->read(3);
  132. $this->assertTrue($success);
  133. $this->assertEqual($result, $expecting);
  134. $result = $this->File->offset();
  135. $expecting = 5+3;
  136. $this->assertIdentical($result, $expecting);
  137. }
  138. /**
  139. * testOpen method
  140. *
  141. * @access public
  142. * @return void
  143. */
  144. function testOpen() {
  145. $this->File->handle = null;
  146. $r = $this->File->open();
  147. $this->assertTrue(is_resource($this->File->handle));
  148. $this->assertTrue($r);
  149. $handle = $this->File->handle;
  150. $r = $this->File->open();
  151. $this->assertTrue($r);
  152. $this->assertTrue($handle === $this->File->handle);
  153. $this->assertTrue(is_resource($this->File->handle));
  154. $r = $this->File->open('r', true);
  155. $this->assertTrue($r);
  156. $this->assertFalse($handle === $this->File->handle);
  157. $this->assertTrue(is_resource($this->File->handle));
  158. }
  159. /**
  160. * testClose method
  161. *
  162. * @access public
  163. * @return void
  164. */
  165. function testClose() {
  166. $this->File->handle = null;
  167. $this->assertFalse(is_resource($this->File->handle));
  168. $this->assertTrue($this->File->close());
  169. $this->assertFalse(is_resource($this->File->handle));
  170. $this->File->handle = fopen(__FILE__, 'r');
  171. $this->assertTrue(is_resource($this->File->handle));
  172. $this->assertTrue($this->File->close());
  173. $this->assertFalse(is_resource($this->File->handle));
  174. }
  175. /**
  176. * testCreate method
  177. *
  178. * @access public
  179. * @return void
  180. */
  181. function testCreate() {
  182. $tmpFile = TMP.'tests'.DS.'cakephp.file.test.tmp';
  183. $File =& new File($tmpFile, true, 0777);
  184. $this->assertTrue($File->exists());
  185. }
  186. /**
  187. * testOpeningNonExistantFileCreatesIt method
  188. *
  189. * @access public
  190. * @return void
  191. */
  192. function testOpeningNonExistantFileCreatesIt() {
  193. $someFile =& new File(TMP . 'some_file.txt', false);
  194. $this->assertTrue($someFile->open());
  195. $this->assertEqual($someFile->read(), '');
  196. $someFile->close();
  197. $someFile->delete();
  198. }
  199. /**
  200. * testPrepare method
  201. *
  202. * @access public
  203. * @return void
  204. */
  205. function testPrepare() {
  206. $string = "some\nvery\ncool\r\nteststring here\n\n\nfor\r\r\n\n\r\n\nhere";
  207. if (DS == '\\') {
  208. $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
  209. $expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
  210. } else {
  211. $expected = "some\nvery\ncool\nteststring here\n\n\nfor\n\n\n\n\nhere";
  212. }
  213. $this->assertIdentical(File::prepare($string), $expected);
  214. $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
  215. $expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
  216. $this->assertIdentical(File::prepare($string, true), $expected);
  217. }
  218. /**
  219. * testReadable method
  220. *
  221. * @access public
  222. * @return void
  223. */
  224. function testReadable() {
  225. $someFile =& new File(TMP . 'some_file.txt', false);
  226. $this->assertTrue($someFile->open());
  227. $this->assertTrue($someFile->readable());
  228. $someFile->close();
  229. $someFile->delete();
  230. }
  231. /**
  232. * testWritable method
  233. *
  234. * @access public
  235. * @return void
  236. */
  237. function testWritable() {
  238. $someFile =& new File(TMP . 'some_file.txt', false);
  239. $this->assertTrue($someFile->open());
  240. $this->assertTrue($someFile->writable());
  241. $someFile->close();
  242. $someFile->delete();
  243. }
  244. /**
  245. * testExecutable method
  246. *
  247. * @access public
  248. * @return void
  249. */
  250. function testExecutable() {
  251. $someFile =& new File(TMP . 'some_file.txt', false);
  252. $this->assertTrue($someFile->open());
  253. $this->assertFalse($someFile->executable());
  254. $someFile->close();
  255. $someFile->delete();
  256. }
  257. /**
  258. * testLastAccess method
  259. *
  260. * @access public
  261. * @return void
  262. */
  263. function testLastAccess() {
  264. $someFile =& new File(TMP . 'some_file.txt', false);
  265. $this->assertFalse($someFile->lastAccess());
  266. $this->assertTrue($someFile->open());
  267. $this->assertEqual($someFile->lastAccess(), time());
  268. $someFile->close();
  269. $someFile->delete();
  270. }
  271. /**
  272. * testLastChange method
  273. *
  274. * @access public
  275. * @return void
  276. */
  277. function testLastChange() {
  278. $someFile =& new File(TMP . 'some_file.txt', false);
  279. $this->assertFalse($someFile->lastChange());
  280. $this->assertTrue($someFile->open('r+'));
  281. $this->assertEqual($someFile->lastChange(), time());
  282. $someFile->write('something');
  283. $this->assertEqual($someFile->lastChange(), time());
  284. $someFile->close();
  285. $someFile->delete();
  286. }
  287. /**
  288. * testWrite method
  289. *
  290. * @access public
  291. * @return void
  292. */
  293. function testWrite() {
  294. if (!$tmpFile = $this->_getTmpFile()) {
  295. return false;
  296. };
  297. if (file_exists($tmpFile)) {
  298. unlink($tmpFile);
  299. }
  300. $TmpFile =& new File($tmpFile);
  301. $this->assertFalse(file_exists($tmpFile));
  302. $this->assertFalse(is_resource($TmpFile->handle));
  303. $testData = array('CakePHP\'s', ' test suite', ' was here ...', '');
  304. foreach ($testData as $data) {
  305. $r = $TmpFile->write($data);
  306. $this->assertTrue($r);
  307. $this->assertTrue(file_exists($tmpFile));
  308. $this->assertEqual($data, file_get_contents($tmpFile));
  309. $this->assertTrue(is_resource($TmpFile->handle));
  310. $TmpFile->close();
  311. }
  312. unlink($tmpFile);
  313. }
  314. /**
  315. * testAppend method
  316. *
  317. * @access public
  318. * @return void
  319. */
  320. function testAppend() {
  321. if (!$tmpFile = $this->_getTmpFile()) {
  322. return false;
  323. };
  324. if (file_exists($tmpFile)) {
  325. unlink($tmpFile);
  326. }
  327. $TmpFile =& new File($tmpFile);
  328. $this->assertFalse(file_exists($tmpFile));
  329. $fragments = array('CakePHP\'s', ' test suite', ' was here ...', '');
  330. $data = null;
  331. foreach ($fragments as $fragment) {
  332. $r = $TmpFile->append($fragment);
  333. $this->assertTrue($r);
  334. $this->assertTrue(file_exists($tmpFile));
  335. $data = $data.$fragment;
  336. $this->assertEqual($data, file_get_contents($tmpFile));
  337. $TmpFile->close();
  338. }
  339. }
  340. /**
  341. * testDelete method
  342. *
  343. * @access public
  344. * @return void
  345. */
  346. function testDelete() {
  347. if (!$tmpFile = $this->_getTmpFile()) {
  348. return false;
  349. };
  350. if (!file_exists($tmpFile)) {
  351. touch($tmpFile);
  352. }
  353. $TmpFile =& new File($tmpFile);
  354. $this->assertTrue(file_exists($tmpFile));
  355. $result = $TmpFile->delete();
  356. $this->assertTrue($result);
  357. $this->assertFalse(file_exists($tmpFile));
  358. $TmpFile =& new File('/this/does/not/exist');
  359. $result = $TmpFile->delete();
  360. $this->assertFalse($result);
  361. }
  362. /**
  363. * testCopy method
  364. *
  365. * @access public
  366. * @return void
  367. */
  368. function testCopy() {
  369. $dest = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
  370. $file = __FILE__;
  371. $this->File =& new File($file);
  372. $result = $this->File->copy($dest);
  373. $this->assertTrue($result);
  374. $result = $this->File->copy($dest, true);
  375. $this->assertTrue($result);
  376. $result = $this->File->copy($dest, false);
  377. $this->assertFalse($result);
  378. $this->File->close();
  379. unlink($dest);
  380. $TmpFile =& new File('/this/does/not/exist');
  381. $result = $TmpFile->copy($dest);
  382. $this->assertFalse($result);
  383. $TmpFile->close();
  384. }
  385. /**
  386. * getTmpFile method
  387. *
  388. * @param bool $paintSkip
  389. * @access protected
  390. * @return void
  391. */
  392. function _getTmpFile($paintSkip = true) {
  393. $tmpFile = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
  394. if (is_writable(dirname($tmpFile)) && (!file_exists($tmpFile) || is_writable($tmpFile))) {
  395. return $tmpFile;
  396. };
  397. if ($paintSkip) {
  398. $caller = 'test';
  399. if (function_exists('debug_backtrace')) {
  400. $trace = debug_backtrace();
  401. $caller = $trace[1]['function'] . '()';
  402. }
  403. $assertLine = new SimpleStackTrace(array(__FUNCTION__));
  404. $assertLine = $assertLine->traceMethod();
  405. $shortPath = substr($tmpFile, strlen(ROOT));
  406. $message = '[FileTest] Skipping %s because "%s" not writeable!';
  407. $message = sprintf(__($message, true), $caller, $shortPath) . $assertLine;
  408. $this->_reporter->paintSkip($message);
  409. }
  410. return false;
  411. }
  412. }