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

https://github.com/Hywan/atoum · PHP · 581 lines · 550 code · 31 blank · 0 comment · 0 complexity · 6eaacbbcf75ed657bb3a5402b42a6338 MD5 · raw file

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