PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/units/classes/mock/streams/fs/file.php

http://github.com/mageekguy/atoum
PHP | 586 lines | 553 code | 31 blank | 2 comment | 0 complexity | 30596134d3d6d4031e17cef1935da2c9 MD5 | raw file
  1. <?php
  2. namespace mageekguy\atoum\tests\units\mock\streams\fs;
  3. use
  4. mageekguy\atoum,
  5. mageekguy\atoum\mock\stream,
  6. mageekguy\atoum\mock\streams\fs\file as testedClass
  7. ;
  8. require_once __DIR__ . '/../../../../runner.php';
  9. class file extends atoum\test
  10. {
  11. public function testClass()
  12. {
  13. $this->testedClass->extends('mageekguy\atoum\mock\stream');
  14. }
  15. public function testGet()
  16. {
  17. $this
  18. ->if($file = testedClass::get())
  19. ->then
  20. ->object($file)->isInstanceOf('mageekguy\atoum\mock\stream\controller')
  21. ->castToString($file)->isNotEmpty()
  22. ->string(file_get_contents($file))->isEmpty()
  23. ->variable($fileResource = fopen($file, 'r'))->isNotEqualTo(false)
  24. ->boolean(is_readable($file))->isTrue()
  25. ->boolean(is_writable($file))->isTrue()
  26. ->boolean(rename($file, testedClass::defaultProtocol . '://' . uniqid()))->isTrue()
  27. ->boolean(fclose($fileResource))->isTrue()
  28. ->boolean(unlink($file))->isTrue()
  29. ->if($file = testedClass::get($path = uniqid()))
  30. ->then
  31. ->object($file)->isInstanceOf('mageekguy\atoum\mock\stream\controller')
  32. ->castToString($file)->isEqualTo(testedClass::defaultProtocol . '://' . $path)
  33. ->string(file_get_contents($file))->isEmpty()
  34. ->variable($fileResource = fopen($file, 'r'))->isNotEqualTo(false)
  35. ->boolean(is_readable($file))->isTrue()
  36. ->boolean(is_writable($file))->isTrue()
  37. ->boolean(rename($file, testedClass::defaultProtocol . '://' . uniqid()))->isTrue()
  38. ->boolean(fclose($fileResource))->isTrue()
  39. ->boolean(unlink($file))->isTrue()
  40. ->if($file = testedClass::get($path = uniqid()))
  41. ->and($otherFile = testedClass::get($path))
  42. ->then
  43. ->object($otherFile)->isIdenticalTo($file)
  44. ->if($resource = fopen($file, 'r'))
  45. ->and($otherResource = fopen($otherFile, 'w'))
  46. ->then
  47. ->integer(fwrite($resource, uniqid()))->isZero()
  48. ->integer(fwrite($otherResource, 'abcdefghijklmnopqrstuvwxyz'))->isEqualTo(26)
  49. ;
  50. }
  51. public function testFileSize()
  52. {
  53. $this
  54. ->if($file = testedClass::get())
  55. ->then
  56. ->integer(filesize($file))->isEqualTo(0)
  57. ->if($file->contains($data = ('abcdefghijklmnopqrstuvwxyz' . PHP_EOL)))
  58. ->then
  59. ->integer(filesize($file))->isEqualTo(strlen($data))
  60. ;
  61. }
  62. public function testFilePerms()
  63. {
  64. $this
  65. ->if($file = testedClass::get())
  66. ->then
  67. ->integer(fileperms($file))->isEqualTo(0100644)
  68. ;
  69. }
  70. /** @php 5.4 */
  71. public function testChmod()
  72. {
  73. $this
  74. ->if($file = testedClass::get())
  75. ->and(chmod($file, 755))
  76. ->then
  77. ->integer(fileperms($file))->isEqualTo(0100755)
  78. ;
  79. }
  80. public function testFileType()
  81. {
  82. $this
  83. ->if($file = testedClass::get())
  84. ->then
  85. ->string(filetype($file))->isEqualTo('file')
  86. ;
  87. }
  88. public function testFileOwner()
  89. {
  90. $this
  91. ->if($file = testedClass::get())
  92. ->then
  93. ->integer(fileowner($file))->isEqualTo(getmyuid())
  94. ;
  95. }
  96. public function testFileGroup()
  97. {
  98. $this
  99. ->if($file = testedClass::get())
  100. ->then
  101. ->integer(filegroup($file))->isEqualTo(getmygid())
  102. ;
  103. }
  104. public function testIsFile()
  105. {
  106. $this
  107. ->if($file = testedClass::get())
  108. ->then
  109. ->boolean(is_file($file))->isTrue()
  110. ->if($file->notExists())
  111. ->then
  112. ->boolean(is_file($file))->isFalse()
  113. ;
  114. }
  115. public function testIsDir()
  116. {
  117. $this
  118. ->if($file = testedClass::get())
  119. ->then
  120. ->boolean(is_dir($file))->isFalse()
  121. ;
  122. }
  123. public function testIsLink()
  124. {
  125. $this
  126. ->if($file = testedClass::get())
  127. ->then
  128. ->boolean(is_link($file))->isFalse()
  129. ;
  130. }
  131. public function testFileExists()
  132. {
  133. $this
  134. ->if($file = testedClass::get())
  135. ->then
  136. ->boolean(file_exists($file))->isTrue()
  137. ;
  138. }
  139. public function testIsReadable()
  140. {
  141. $this
  142. ->if($file = testedClass::get())
  143. ->and($file->isNotReadable())
  144. ->then
  145. ->boolean(is_readable($file))->isFalse()
  146. ->if($file->isReadable())
  147. ->then
  148. ->boolean(is_readable($file))->isTrue()
  149. ;
  150. }
  151. public function testIsWritable()
  152. {
  153. $this
  154. ->if($file = testedClass::get())
  155. ->and($file->isNotWritable())
  156. ->then
  157. ->boolean(is_writable($file))->isFalse()
  158. ->if($file->isWritable())
  159. ->then
  160. ->boolean(is_writable($file))->isTrue()
  161. ;
  162. }
  163. public function testIsExecutable()
  164. {
  165. $this
  166. ->if($file = testedClass::get())
  167. ->then
  168. ->boolean(is_executable($file))->isFalse()
  169. ;
  170. }
  171. public function testFopen()
  172. {
  173. $this
  174. ->if($file = testedClass::get())
  175. ->and($file->notExists())
  176. ->then
  177. ->boolean(fopen($file, 'r'))->isFalse()
  178. ->error->withType(E_WARNING)->exists()
  179. ->if($file->exists())
  180. ->then
  181. ->variable(fopen($file, 'r'))->isNotFalse()
  182. ->variable($resource = fopen($file, 'a'))->isNotFalse()
  183. ->integer(fseek($resource, 0))->isZero()
  184. ->integer(ftell($resource))->isZero()
  185. ->if($file->contains('abcdefghijklmnopqrstuvwxyz'))
  186. ->then
  187. ->variable($resource = fopen($file, 'a'))->isNotFalse()
  188. ->integer(ftell($resource))->isZero()
  189. ->string(fread($resource, 1))->isEmpty()
  190. ->integer(fseek($resource, 0))->isZero()
  191. ->integer(ftell($resource))->isZero()
  192. ->string(fread($resource, 1))->isEmpty()
  193. ->integer(fwrite($resource, 'A'))->isEqualTo(1)
  194. ->integer(fseek($resource, 0))->isZero()
  195. ->integer(ftell($resource))->isZero()
  196. ->string(fread($resource, 1))->isEmpty()
  197. ->string($file->getContents())->isEqualTo('abcdefghijklmnopqrstuvwxyz' . PHP_EOL . 'A')
  198. ->then
  199. ->variable($resource = fopen($file, 'r'))->isNotFalse()
  200. ->integer(ftell($resource))->isZero()
  201. ->string(fread($resource, 1))->isEqualTo('a')
  202. ->integer(fseek($resource, 0))->isZero()
  203. ->integer(ftell($resource))->isZero()
  204. ->string(fread($resource, 1))->isEqualTo('a')
  205. ;
  206. }
  207. public function testFreadAndFileGetContents()
  208. {
  209. $this
  210. ->if($file = testedClass::get())
  211. ->and($file->contains($data = 'abcdefghijklmnopqrstuvwxyz' . PHP_EOL))
  212. ->and($resource = fopen($file, 'r'))
  213. ->then
  214. ->string(fread($resource, 1))->isEqualTo('a')
  215. ->string(fread($resource, 1))->isEqualTo('b')
  216. ->string(fread($resource, 2))->isEqualTo('cd')
  217. ->string(fread($resource, 4096))->isEqualTo('efghijklmnopqrstuvwxyz' . PHP_EOL)
  218. ->string(fread($resource, 1))->isEmpty()
  219. ->string(file_get_contents($file))->isEqualTo($data)
  220. ->string(fread($resource, 1))->isEmpty()
  221. ->if(fseek($resource, 0))
  222. ->then
  223. ->string(fread($resource, 1))->isEqualTo('a')
  224. ->string(fread($resource, 1))->isEqualTo('b')
  225. ->string(fread($resource, 2))->isEqualTo('cd')
  226. ->string(file_get_contents($file))->isEqualTo($data)
  227. ->string(fread($resource, 8192))->isEqualTo('efghijklmnopqrstuvwxyz' . PHP_EOL)
  228. ->string(fread($resource, 1))->isEmpty()
  229. ->if($file->isEmpty())
  230. ->and($resource = fopen($file, 'r'))
  231. ->then
  232. ->string(fread($resource, 1))->isEmpty()
  233. ->string(fread($resource, 1))->isEmpty()
  234. ->string(fread($resource, 2))->isEmpty()
  235. ->string(fread($resource, 8192))->isEmpty()
  236. ->string(fread($resource, 1))->isEmpty()
  237. ->string(file_get_contents($file))->isEmpty()
  238. ->string(fread($resource, 1))->isEmpty()
  239. ;
  240. }
  241. public function testFeof()
  242. {
  243. $this
  244. ->if($file = testedClass::get())
  245. ->and($resource = fopen($file, 'r'))
  246. ->then
  247. ->boolean(feof($resource))->isFalse()
  248. ->if(fread($resource, 1))
  249. ->then
  250. ->boolean(feof($resource))->isTrue()
  251. ->if($file->contains('abcdefghijklmnopqrstuvwxyz'))
  252. ->and($resource = fopen($file, 'r'))
  253. ->then
  254. ->boolean(feof($resource))->isFalse()
  255. ->if(fread($resource, 1))
  256. ->then
  257. ->boolean(feof($resource))->isFalse()
  258. ->if(fread($resource, 4096))
  259. ->then
  260. ->boolean(feof($resource))->isTrue()
  261. ->if($file = testedClass::get())
  262. ->and($file->contains(
  263. ($line1 = 'un' . PHP_EOL) .
  264. ($line2 = 'deux' . PHP_EOL) .
  265. ($line3 = 'trois' . PHP_EOL) .
  266. ($line4 = 'quatre' . PHP_EOL) .
  267. ($line5 = 'cinq' . PHP_EOL) .
  268. PHP_EOL
  269. )
  270. )
  271. ->and($resource = fopen($file, 'r'))
  272. ->then
  273. ->boolean(feof($resource))->isFalse()
  274. ->if($line = fgets($resource))
  275. ->then
  276. ->boolean(feof($resource))->isFalse()
  277. ->string($line)->isEqualTo($line1)
  278. ->if($line = fgets($resource))
  279. ->then
  280. ->boolean(feof($resource))->isFalse()
  281. ->string($line)->isEqualTo($line2)
  282. ->if($line = fgets($resource))
  283. ->then
  284. ->boolean(feof($resource))->isFalse()
  285. ->string($line)->isEqualTo($line3)
  286. ->if($line = fgets($resource))
  287. ->then
  288. ->boolean(feof($resource))->isFalse()
  289. ->string($line)->isEqualTo($line4)
  290. ->if($line = fgets($resource))
  291. ->then
  292. ->string($line)->isEqualTo($line5)
  293. ->boolean(feof($resource))->isFalse()
  294. ;
  295. }
  296. public function testFlock()
  297. {
  298. $this
  299. ->if($file = testedClass::get(uniqid()))
  300. ->and($resource = fopen($file, 'w'))
  301. ->then
  302. ->boolean(flock($resource, LOCK_EX))->isTrue()
  303. ->boolean(flock($resource, LOCK_EX|LOCK_NB))->isTrue()
  304. ->boolean(flock($resource, LOCK_SH))->isTrue()
  305. ->boolean(flock($resource, LOCK_SH|LOCK_NB))->isTrue()
  306. ->boolean(flock($resource, LOCK_UN))->isTrue()
  307. ;
  308. }
  309. public function testFtell()
  310. {
  311. $this
  312. ->if($file = testedClass::get(uniqid()))
  313. ->and($resource = fopen($file, 'w'))
  314. ->then
  315. ->integer(ftell($resource))->isZero()
  316. ->if(fseek($resource, $offset = rand(1, 4096)))
  317. ->then
  318. ->integer(ftell($resource))->isEqualTo($offset)
  319. ->boolean(feof($resource))->isFalse()
  320. ;
  321. }
  322. public function testFgets()
  323. {
  324. $this
  325. ->if($file = testedClass::get())
  326. ->and($file->contains(
  327. ($line0 = 'un' . PHP_EOL) .
  328. ($line1 = 'deux' . PHP_EOL) .
  329. ($line2 = 'trois' . PHP_EOL) .
  330. ($line3 = 'quatre' . PHP_EOL) .
  331. ($line4 = 'cinq' . PHP_EOL) .
  332. PHP_EOL
  333. )
  334. )
  335. ->and($resource = fopen($file, 'r'))
  336. ->then
  337. ->string(fgets($resource))->isEqualTo($line0)
  338. ->string(fgets($resource))->isEqualTo($line1)
  339. ->string(fgets($resource))->isEqualTo($line2)
  340. ->string(fgets($resource))->isEqualTo($line3)
  341. ->string(fgets($resource))->isEqualTo($line4)
  342. ->string(fgets($resource))->isEqualTo(PHP_EOL)
  343. ->boolean(fgets($resource))->isFalse()
  344. ;
  345. }
  346. public function testFseek()
  347. {
  348. $this
  349. ->if($file = testedClass::get(uniqid()))
  350. ->and($resource = fopen($file, 'w'))
  351. ->then
  352. ->integer(fseek($resource, 4096))->isZero()
  353. ->if($file = testedClass::get())
  354. ->and($file->contains(
  355. ($line0 = 'un' . PHP_EOL) .
  356. ($line1 = 'deux' . PHP_EOL) .
  357. ($line2 = 'trois' . PHP_EOL) .
  358. ($line3 = 'quatre' . PHP_EOL) .
  359. ($line4 = 'cinq' . PHP_EOL) .
  360. PHP_EOL
  361. )
  362. )
  363. ->and($fileObject = new \splFileObject($file))
  364. ->then
  365. ->boolean($fileObject->eof())->isFalse()
  366. ->if($fileObject->seek(1))
  367. ->then
  368. ->boolean($fileObject->eof())->isFalse()
  369. ->string($fileObject->current())->isEqualTo($line1)
  370. ->if($fileObject->seek(2))
  371. ->then
  372. ->boolean($fileObject->eof())->isFalse()
  373. ->string($fileObject->current())->isEqualTo($line2)
  374. ->if($fileObject->seek(3))
  375. ->then
  376. ->boolean($fileObject->eof())->isFalse()
  377. ->string($fileObject->current())->isEqualTo($line3)
  378. ->if($fileObject->seek(4))
  379. ->then
  380. ->boolean($fileObject->eof())->isFalse()
  381. ->string($fileObject->current())->isEqualTo($line4)
  382. ->if($fileObject->seek(0))
  383. ->then
  384. ->boolean($fileObject->eof())->isFalse()
  385. ->string($fileObject->current())->isEqualTo($line0)
  386. ->if($fileObject->seek(6))
  387. ->then
  388. ->boolean($fileObject->eof())->isTrue()
  389. ->boolean($fileObject->valid())->isFalse()
  390. ->string($fileObject->current())->isEmpty()
  391. ->if($fileObject->seek(5))
  392. ->then
  393. ->boolean($fileObject->eof())->isFalse()
  394. ->string($fileObject->current())->isEqualTo(PHP_EOL)
  395. ->if($fileObject->seek(4))
  396. ->then
  397. ->boolean($fileObject->eof())->isFalse()
  398. ->string($fileObject->current())->isEqualTo($line4)
  399. ->if($fileObject->seek(3))
  400. ->then
  401. ->boolean($fileObject->eof())->isFalse()
  402. ->string($fileObject->current())->isEqualTo($line3)
  403. ->if($fileObject->seek(4))
  404. ->then
  405. ->boolean($fileObject->eof())->isFalse()
  406. ->string($fileObject->current())->isEqualTo($line4)
  407. ->if($fileObject->seek(5))
  408. ->then
  409. ->boolean($fileObject->eof())->isFalse()
  410. ->string($fileObject->current())->isEqualTo(PHP_EOL)
  411. ->if($fileObject = new \splFileObject($file))
  412. ->then
  413. ->integer($fileObject->key())->isZero()
  414. ->string($fileObject->current())->isEqualTo($line0)
  415. ->boolean($fileObject->eof())->isFalse()
  416. ->if($fileObject->next())
  417. ->then
  418. ->integer($fileObject->key())->isEqualTo(1)
  419. ->string($fileObject->current())->isEqualTo($line1)
  420. ->boolean($fileObject->eof())->isFalse()
  421. ->if($fileObject->next())
  422. ->then
  423. ->integer($fileObject->key())->isEqualTo(2)
  424. ->string($fileObject->current())->isEqualTo($line2)
  425. ->boolean($fileObject->eof())->isFalse()
  426. ->if($fileObject->next())
  427. ->then
  428. ->integer($fileObject->key())->isEqualTo(3)
  429. ->string($fileObject->current())->isEqualTo($line3)
  430. ->boolean($fileObject->eof())->isFalse()
  431. ->if($fileObject->next())
  432. ->then
  433. ->integer($fileObject->key())->isEqualTo(4)
  434. ->string($fileObject->current())->isEqualTo($line4)
  435. ->boolean($fileObject->eof())->isFalse()
  436. ->if($fileObject->next())
  437. ->then
  438. ->integer($fileObject->key())->isEqualTo(5)
  439. ->string($fileObject->current())->isEqualTo(PHP_EOL)
  440. ->boolean($fileObject->eof())->isFalse()
  441. ->if($fileObject->next())
  442. ->then
  443. ->integer($fileObject->key())->isEqualTo(6)
  444. ->string($fileObject->current())->isEmpty()
  445. ->boolean($fileObject->eof())->isTrue()
  446. ->if($file = testedClass::get())
  447. ->and($file->contains(
  448. ($line0 = 'un' . PHP_EOL) .
  449. ($line1 = 'deux' . PHP_EOL) .
  450. ($line2 = 'trois' . PHP_EOL) .
  451. ($line3 = 'quatre' . PHP_EOL) .
  452. ($line4 = 'cinq' . PHP_EOL)
  453. )
  454. )
  455. ->and($fileObject = new \splFileObject($file))
  456. ->and($fileObject->seek(4))
  457. ->then
  458. ->string($fileObject->current())->isEqualTo($line4)
  459. ->boolean($fileObject->eof())->isFalse()
  460. ->boolean($fileObject->valid())->isTrue()
  461. ;
  462. }
  463. /** @php 5.4 */
  464. public function testFtruncate()
  465. {
  466. $this
  467. ->if($file = testedClass::get(uniqid()))
  468. ->and($resource = fopen($file, 'w'))
  469. ->then
  470. ->boolean(ftruncate($resource, 0))->isTrue()
  471. ->string(file_get_contents($file))->isEmpty()
  472. ->if($file->contains($data = 'abcdefghijklmnopqrstuvwxyz'))
  473. ->then
  474. ->boolean(ftruncate($resource, 4))->isTrue()
  475. ->string(file_get_contents($file))->isEqualTo('abcd')
  476. ->boolean(ftruncate($resource, 8))->isTrue()
  477. ->string(file_get_contents($file))->isEqualTo('abcd' . "\0\0\0\0")
  478. ->boolean(ftruncate($resource, 0))->isTrue()
  479. ->string(file_get_contents($file))->isEmpty()
  480. ;
  481. }
  482. public function testFwriteAndFilePutContents()
  483. {
  484. $this
  485. ->if($file = testedClass::get())
  486. ->and($resource = fopen($file, 'r'))
  487. ->then
  488. ->integer(fwrite($resource, 'a'))->isZero()
  489. ->if($resource = fopen($file, 'w'))
  490. ->then
  491. ->integer(fwrite($resource, 'a'))->isEqualTo(1)
  492. ->string(file_get_contents($file))->isEqualTo('a')
  493. ;
  494. }
  495. public function testRename()
  496. {
  497. $this
  498. ->if($file = testedClass::get($path = uniqid()))
  499. ->then
  500. ->boolean(rename($file, $newPath = testedClass::defaultProtocol . '://' . uniqid()))->isTrue()
  501. ->string($file->getPath())->isEqualTo($newPath)
  502. ->object($file)
  503. ->isIdenticalTo(testedClass::get($newPath))
  504. ->isNotIdenticalTo(testedClass::get($path))
  505. ;
  506. }
  507. public function testCopy()
  508. {
  509. $this
  510. ->if($file = testedClass::get($path = uniqid()))
  511. ->then
  512. ->boolean(copy($file, $newPath = testedClass::defaultProtocol . '://' . uniqid()))->isTrue()
  513. ->string($file->getPath())->isEqualTo(testedClass::defaultProtocol . '://' . $path)
  514. ->array(stat($file))->isEqualTo(stat(testedClass::get($newPath)))
  515. ->string($file->getContents())->isEqualTo(testedClass::get($newPath)->getContents())
  516. ->if($file->contains(uniqid()))
  517. ->then
  518. ->boolean(copy($file, $otherNewPath = testedClass::defaultProtocol . '://' . uniqid()))->isTrue()
  519. ->string($file->getPath())->isEqualTo(testedClass::defaultProtocol . '://' . $path)
  520. ->string($file->getContents())->isNotEqualTo(testedClass::get($newPath)->getContents())
  521. ->string($file->getContents())->isEqualTo(testedClass::get($otherNewPath)->getContents())
  522. ->array(stat($file))->isNotEqualTo(stat(testedClass::get($newPath)))
  523. ->array(stat($file))->isEqualTo(stat(testedClass::get($otherNewPath)))
  524. ;
  525. }
  526. public function testUnlink()
  527. {
  528. $this
  529. ->if($file = testedClass::get(uniqid()))
  530. ->then
  531. ->boolean(unlink($file))->isTrue()
  532. ->boolean(is_file($file))->isFalse()
  533. ->if($file = testedClass::get(uniqid()))
  534. ->and($file->notExists())
  535. ->then
  536. ->boolean(unlink($file))->isFalse()
  537. ->if($file->exists())
  538. ->and($file->isNotWritable())
  539. ->then
  540. ->boolean(unlink($file))->isFalse()
  541. ->and($file->isWritable())
  542. ->then
  543. ->boolean(unlink($file))->isTrue()
  544. ;
  545. }
  546. public function testOpendir()
  547. {
  548. $this
  549. ->if($file = testedClass::get(uniqid()))
  550. ->then
  551. ->boolean(opendir($file))->isFalse()
  552. ->error->withType(E_WARNING)->exists()
  553. ;
  554. }
  555. }