PageRenderTime 27ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/core/src/test/php/net/xp_framework/unittest/io/FileIntegrationTest.class.php

https://github.com/ghiata/xp-framework
PHP | 603 lines | 313 code | 75 blank | 215 comment | 14 complexity | 9a421c32f7ff8fb6113a1965d21c6271 MD5 | raw file
  1. <?php namespace net\xp_framework\unittest\io;
  2. use unittest\TestCase;
  3. use io\File;
  4. use io\Folder;
  5. use lang\System;
  6. /**
  7. * TestCase
  8. *
  9. * @see xp://io.File
  10. */
  11. class FileIntegrationTest extends TestCase {
  12. protected static $temp= null;
  13. protected $file= null;
  14. protected $folder= null;
  15. /**
  16. * Verifies TEMP directory is usable and there is enough space
  17. *
  18. */
  19. #[@beforeClass]
  20. public static function verifyTempDir() {
  21. self::$temp= System::tempDir();
  22. if (!is_writeable(self::$temp)) {
  23. throw new \unittest\PrerequisitesNotMetError('$TEMP is not writeable', null, array(self::$temp.' +w'));
  24. }
  25. if (($df= disk_free_space(self::$temp)) < 10240) {
  26. throw new \unittest\PrerequisitesNotMetError('Not enough space available in $TEMP', null, array(sprintf(
  27. 'df %s = %.0fk > 10k',
  28. self::$temp,
  29. $df / 1024
  30. )));
  31. }
  32. }
  33. /**
  34. * Creates file fixture, ensures it doesn't exist before tests start
  35. * running, then creates folder fixture, ensuring it exists and is
  36. * empty.
  37. *
  38. */
  39. public function setUp() {
  40. $unid= getmypid();
  41. $this->file= new File(self::$temp, '.xp-'.$unid.$this->getName().'file');
  42. if (file_exists($this->file->getURI())) {
  43. unlink($this->file->getURI());
  44. }
  45. $this->folder= new Folder($this->file->getPath(), '.xp-'.$unid.$this->getName().'folder');
  46. if (!file_exists($this->folder->getURI())) {
  47. mkdir($this->folder->getURI());
  48. } else {
  49. foreach (scandir($this->folder->getURI()) as $file) {
  50. if ('.' === $file || '..' === $file) continue;
  51. unlink($this->folder->getURI().$file);
  52. }
  53. }
  54. }
  55. /**
  56. * Deletes file and folder fixtures.
  57. *
  58. */
  59. public function tearDown() {
  60. $this->file->isOpen() && $this->file->close();
  61. if (file_exists($this->file->getURI())) {
  62. unlink($this->file->getURI());
  63. }
  64. if (file_exists($this->folder->getURI())) {
  65. foreach (scandir($this->folder->getURI()) as $file) {
  66. if ('.' === $file || '..' === $file) continue;
  67. unlink($this->folder->getURI().$file);
  68. }
  69. rmdir($this->folder->getURI());
  70. }
  71. }
  72. /**
  73. * Fill a given file with data - that is, open it in write mode,
  74. * write the data if not NULL, then finally close it.
  75. *
  76. * @param io.File file
  77. * @param string data default NULL
  78. * @param bool append default FALSE
  79. * @return int number of written bytes or 0 if NULL data was given
  80. * @throws io.IOException
  81. */
  82. protected function writeData($file, $data= null, $append= false) {
  83. $file->open($append ? FILE_MODE_APPEND : FILE_MODE_WRITE);
  84. if (null === $data) {
  85. $written= 0;
  86. } else {
  87. $written= $file->write($data);
  88. }
  89. $file->close();
  90. return $written;
  91. }
  92. /**
  93. * Read data from a file - that is, open it in read mode, read
  94. * the number of bytes specified (or the entire file, if omitted),
  95. * then finally close it.
  96. *
  97. * @param io.File file
  98. * @param int length default -1
  99. * @return string
  100. */
  101. protected function readData($file, $length= -1) {
  102. $file->open(FILE_MODE_READ);
  103. $data= $file->read($length < 0 ? $file->size() : $length);
  104. $file->close();
  105. return $data;
  106. }
  107. /**
  108. * Test exists() method
  109. *
  110. */
  111. #[@test]
  112. public function doesNotExistYet() {
  113. $this->assertFalse($this->file->exists());
  114. }
  115. /**
  116. * Test exists() method
  117. *
  118. */
  119. #[@test]
  120. public function existsAfterCreating() {
  121. $this->writeData($this->file, null);
  122. $this->assertTrue($this->file->exists());
  123. }
  124. /**
  125. * Test exists() and unlink() methods
  126. *
  127. */
  128. #[@test]
  129. public function noLongerExistsAfterDeleting() {
  130. $this->writeData($this->file, null);
  131. $this->file->unlink();
  132. $this->assertFalse($this->file->exists());
  133. }
  134. /**
  135. * Test unlink() method
  136. *
  137. */
  138. #[@test, @expect('io.IOException')]
  139. public function cannotDeleteNonExistant() {
  140. $this->file->unlink();
  141. }
  142. /**
  143. * Test unlink() method
  144. *
  145. */
  146. #[@test, @expect('lang.IllegalStateException')]
  147. public function cannotDeleteOpenFile() {
  148. $this->file->open(FILE_MODE_WRITE);
  149. $this->file->unlink();
  150. }
  151. /**
  152. * Test close() method
  153. *
  154. */
  155. #[@test, @expect('io.IOException')]
  156. public function cannotCloseUnopenedFile() {
  157. $this->file->close();
  158. }
  159. /**
  160. * Test writing to a file
  161. *
  162. */
  163. #[@test]
  164. public function write() {
  165. $this->assertEquals(5, $this->writeData($this->file, 'Hello'));
  166. }
  167. /**
  168. * Test writing to a file, then reading back the data
  169. *
  170. */
  171. #[@test]
  172. public function read() {
  173. with ($data= 'Hello'); {
  174. $this->writeData($this->file, $data);
  175. $this->file->open(FILE_MODE_READ);
  176. $this->assertEquals($data, $this->file->read(strlen($data)));
  177. $this->file->close();
  178. }
  179. }
  180. /**
  181. * Test writing to a file, then reading back the data
  182. *
  183. */
  184. #[@test]
  185. public function read0() {
  186. with ($data= 'Hello'); {
  187. $this->writeData($this->file, $data);
  188. $this->file->open(FILE_MODE_READ);
  189. $this->assertEquals('', $this->file->read(0));
  190. $this->file->close();
  191. }
  192. }
  193. /**
  194. * Test writing to a file, then reading back the data
  195. *
  196. */
  197. #[@test]
  198. public function readAfterEnd() {
  199. with ($data= 'Hello'); {
  200. $this->writeData($this->file, $data);
  201. $this->file->open(FILE_MODE_READ);
  202. $this->assertEquals($data, $this->file->read(strlen($data)));
  203. $this->assertFalse($this->file->read(1));
  204. $this->file->close();
  205. }
  206. }
  207. /**
  208. * Test writing to a file, then reading back the data using gets()
  209. *
  210. */
  211. #[@test]
  212. public function gets() {
  213. with ($data= 'Hello'); {
  214. $this->writeData($this->file, $data);
  215. $this->file->open(FILE_MODE_READ);
  216. $this->assertEquals($data, $this->file->gets());
  217. $this->file->close();
  218. }
  219. }
  220. /**
  221. * Test writing to a file, then reading back the data using gets()
  222. *
  223. */
  224. #[@test]
  225. public function gets0() {
  226. with ($data= 'Hello'); {
  227. $this->writeData($this->file, $data);
  228. $this->file->open(FILE_MODE_READ);
  229. $this->assertEquals('', $this->file->gets(0));
  230. $this->file->close();
  231. }
  232. }
  233. /**
  234. * Test writing to a file, then reading back the data using gets()
  235. *
  236. */
  237. #[@test]
  238. public function getsTwoLines() {
  239. with ($data= "Hello\nWorld\n"); {
  240. $this->writeData($this->file, $data);
  241. $this->file->open(FILE_MODE_READ);
  242. $this->assertEquals("Hello\n", $this->file->gets());
  243. $this->assertEquals("World\n", $this->file->gets());
  244. $this->file->close();
  245. }
  246. }
  247. /**
  248. * Test writing to a file, then reading back the data using gets()
  249. *
  250. */
  251. #[@test]
  252. public function getsAfterEnd() {
  253. with ($data= 'Hello'); {
  254. $this->writeData($this->file, $data);
  255. $this->file->open(FILE_MODE_READ);
  256. $this->assertEquals('Hello', $this->file->gets());
  257. $this->assertFalse($this->file->gets());
  258. $this->file->close();
  259. }
  260. }
  261. /**
  262. * Test writing to a file, then reading back the data using readLine()
  263. *
  264. */
  265. #[@test]
  266. public function readLine() {
  267. with ($data= 'Hello'); {
  268. $this->writeData($this->file, $data);
  269. $this->file->open(FILE_MODE_READ);
  270. $this->assertEquals($data, $this->file->readLine());
  271. $this->file->close();
  272. }
  273. }
  274. /**
  275. * Test writing to a file, then reading back the data using readLine()
  276. *
  277. */
  278. #[@test]
  279. public function readLine0() {
  280. with ($data= 'Hello'); {
  281. $this->writeData($this->file, $data);
  282. $this->file->open(FILE_MODE_READ);
  283. $this->assertEquals('', $this->file->readLine(0));
  284. $this->file->close();
  285. }
  286. }
  287. /**
  288. * Test writing to a file, then reading back the data using readLine()
  289. *
  290. */
  291. #[@test]
  292. public function readLines() {
  293. with ($data= "Hello\nWorld\n"); {
  294. $this->writeData($this->file, $data);
  295. $this->file->open(FILE_MODE_READ);
  296. $this->assertEquals('Hello', $this->file->readLine());
  297. $this->assertEquals('World', $this->file->readLine());
  298. $this->file->close();
  299. }
  300. }
  301. /**
  302. * Test writing to a file, then reading back the data using readLine()
  303. *
  304. */
  305. #[@test]
  306. public function readLinesAfterEnd() {
  307. with ($data= 'Hello'); {
  308. $this->writeData($this->file, $data);
  309. $this->file->open(FILE_MODE_READ);
  310. $this->assertEquals('Hello', $this->file->readLine());
  311. $this->assertFalse($this->file->readLine());
  312. $this->file->close();
  313. }
  314. }
  315. /**
  316. * Test writing to a file, then reading back the data using readChar()
  317. *
  318. */
  319. #[@test]
  320. public function readChar() {
  321. with ($data= 'Hello'); {
  322. $this->writeData($this->file, $data);
  323. $this->file->open(FILE_MODE_READ);
  324. $this->assertEquals($data{0}, $this->file->readChar());
  325. $this->file->close();
  326. }
  327. }
  328. /**
  329. * Test writing to a file, then reading back the data using readChar()
  330. *
  331. */
  332. #[@test]
  333. public function readChars() {
  334. with ($data= 'Hello'); {
  335. $this->writeData($this->file, $data);
  336. $this->file->open(FILE_MODE_READ);
  337. $this->assertEquals($data{0}, $this->file->readChar());
  338. $this->assertEquals($data{1}, $this->file->readChar());
  339. $this->file->close();
  340. }
  341. }
  342. /**
  343. * Test writing to a file, then reading back the data using readChar()
  344. *
  345. */
  346. #[@test]
  347. public function readCharsAfterEnd() {
  348. with ($data= 'H'); {
  349. $this->writeData($this->file, $data);
  350. $this->file->open(FILE_MODE_READ);
  351. $this->assertEquals('H', $this->file->readChar());
  352. $this->assertFalse($this->file->readChar());
  353. $this->file->close();
  354. }
  355. }
  356. /**
  357. * Test writing to a file, then reading back the data
  358. *
  359. */
  360. #[@test]
  361. public function overwritingExistant() {
  362. with ($data= 'Hello World', $appear= 'This should not appear'); {
  363. $this->writeData($this->file, $appear);
  364. $this->writeData($this->file, $data);
  365. $this->file->open(FILE_MODE_READ);
  366. $this->assertEquals($data, $this->file->read(strlen($data)));
  367. $this->file->close();
  368. }
  369. }
  370. /**
  371. * Test writing to a file, then reading back the data
  372. *
  373. */
  374. #[@test]
  375. public function appendingToExistant() {
  376. with ($data= 'Hello World', $appear= 'This should appear'); {
  377. $this->writeData($this->file, $appear);
  378. $this->writeData($this->file, $data, true);
  379. $this->assertEquals($appear.$data, $this->readData($this->file, strlen($appear) + strlen($data)));
  380. }
  381. }
  382. /**
  383. * Test a non-existant file cannot bee opened for reading
  384. *
  385. */
  386. #[@test, @expect('io.FileNotFoundException')]
  387. public function cannotOpenNonExistantForReading() {
  388. $this->file->open(FILE_MODE_READ);
  389. }
  390. /**
  391. * Test copy() method
  392. *
  393. */
  394. #[@test]
  395. public function copying() {
  396. with ($data= 'Hello World'); {
  397. $this->writeData($this->file, $data);
  398. $copy= new File($this->file->getURI().'.copy');
  399. $this->file->copy($copy->getURI());
  400. $read= $this->readData($copy);
  401. $copy->unlink();
  402. $this->assertEquals($data, $read);
  403. }
  404. }
  405. /**
  406. * Test copy() method
  407. *
  408. */
  409. #[@test]
  410. public function copyingOver() {
  411. with ($data= 'Hello World'); {
  412. $this->writeData($this->file, $data);
  413. $copy= new File($this->file->getURI().'.copy');
  414. $this->writeData($copy, 'Copy original content');
  415. $this->file->copy($copy->getURI());
  416. $read= $this->readData($copy);
  417. $copy->unlink();
  418. $this->assertEquals($data, $read);
  419. }
  420. }
  421. /**
  422. * Test copy() method
  423. *
  424. */
  425. #[@test, @expect('lang.IllegalStateException')]
  426. public function cannotCopyOpenFile() {
  427. $this->file->open(FILE_MODE_WRITE);
  428. $this->file->copy('irrelevant');
  429. }
  430. /**
  431. * Test move() method
  432. *
  433. */
  434. #[@test]
  435. public function moving() {
  436. with ($data= 'Hello World'); {
  437. $this->writeData($this->file, $data);
  438. $target= new File($this->file->getURI().'.moved');
  439. $this->file->move($target->getURI());
  440. $read= $this->readData($target);
  441. $target->unlink();
  442. $this->assertEquals($data, $read);
  443. // FIXME I don't think io.File should be updating its URI when
  444. // move() is called. Because it does, this assertion fails!
  445. // $this->assertFalse($this->file->exists());
  446. }
  447. }
  448. /**
  449. * Test move() method
  450. *
  451. */
  452. #[@test, @ignore('Breaks on Win2008 server, need special handling')]
  453. public function movingOver() {
  454. with ($data= 'Hello World'); {
  455. $this->writeData($this->file, $data);
  456. $target= new File($this->file->getURI().'.moved');
  457. $this->writeData($target, 'Target original content');
  458. $this->file->move($target->getURI());
  459. $read= $this->readData($target);
  460. $target->unlink();
  461. $this->assertEquals($data, $read);
  462. // FIXME I don't think io.File should be updating its URI when
  463. // move() is called. Because it does, this assertion fails!
  464. // $this->assertFalse($this->file->exists());
  465. }
  466. }
  467. /**
  468. * Test move() method
  469. *
  470. */
  471. #[@test, @expect('lang.IllegalStateException')]
  472. public function cannotMoveOpenFile() {
  473. $this->file->open(FILE_MODE_WRITE);
  474. $this->file->move('irrelevant');
  475. }
  476. /**
  477. * Test copy() method
  478. *
  479. */
  480. #[@test]
  481. public function copyingToAnotherFile() {
  482. $this->writeData($this->file, null);
  483. $target= new File($this->file->getURI().'.moved');
  484. $this->file->copy($target);
  485. $exists= $target->exists();
  486. $target->unlink();
  487. $this->assertTrue($exists);
  488. }
  489. /**
  490. * Test copy() method
  491. *
  492. */
  493. #[@test]
  494. public function copyingToAnotherFolder() {
  495. $this->writeData($this->file, null);
  496. $target= new File($this->folder, $this->file->getFilename());
  497. $this->file->copy($this->folder);
  498. $exists= $target->exists();
  499. $target->unlink();
  500. $this->assertTrue($exists);
  501. }
  502. /**
  503. * Test move() method
  504. *
  505. */
  506. #[@test]
  507. public function movingToAnotherFile() {
  508. $this->writeData($this->file, null);
  509. $target= new File($this->file->getURI().'.moved');
  510. $this->file->move($target);
  511. $exists= $target->exists();
  512. $target->unlink();
  513. $this->assertTrue($exists);
  514. }
  515. /**
  516. * Test move() method
  517. *
  518. */
  519. #[@test]
  520. public function movingToAnotherFolder() {
  521. $this->writeData($this->file, null);
  522. $target= new File($this->folder, $this->file->getFilename());
  523. $this->file->move($this->folder);
  524. $exists= $target->exists();
  525. $target->unlink();
  526. $this->assertTrue($exists);
  527. }
  528. }