PageRenderTime 59ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

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

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