PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/treuter/xp-framework
PHP | 361 lines | 143 code | 36 blank | 182 comment | 0 complexity | 4c6bf51eedf8ffb5255b023a95f6dac7 MD5 | raw file
  1. <?php
  2. /* This class is part of the XP framework
  3. *
  4. * $Id$
  5. */
  6. uses(
  7. 'unittest.TestCase',
  8. 'io.Folder',
  9. 'io.File',
  10. 'lang.Runtime'
  11. );
  12. /**
  13. * TestCase
  14. *
  15. * @see xp://io.File
  16. */
  17. class FileTest extends TestCase {
  18. /**
  19. * Return a file that is known to exist
  20. *
  21. * @return string
  22. */
  23. protected function fileKnownToExist() {
  24. return realpath(Runtime::getInstance()->getExecutable()->getFilename());
  25. }
  26. /**
  27. * Test equals() method
  28. *
  29. */
  30. #[@test]
  31. public function sameInstanceIsEqual() {
  32. $f= new File($this->fileKnownToExist());
  33. $this->assertEquals($f, $f);
  34. }
  35. /**
  36. * Test equals() method
  37. *
  38. */
  39. #[@test]
  40. public function sameFileIsEqual() {
  41. $fn= $this->fileKnownToExist();
  42. $this->assertEquals(new File($fn), new File($fn));
  43. }
  44. /**
  45. * Test equals() method
  46. *
  47. */
  48. #[@test]
  49. public function differentFilesAreNotEqual() {
  50. $this->assertNotEquals(new File($this->fileKnownToExist()), new File(__FILE__));
  51. }
  52. /**
  53. * Test hashCode() method
  54. *
  55. */
  56. #[@test]
  57. public function hashCodesNotEqualForTwoFileHandles() {
  58. $fn= $this->fileKnownToExist();
  59. $this->assertNotEquals(
  60. create(new File(fopen($fn, 'r')))->hashCode(),
  61. create(new File(fopen($fn, 'r')))->hashCode()
  62. );
  63. }
  64. /**
  65. * Test hashCode() method
  66. *
  67. */
  68. #[@test]
  69. public function hashCodesEqualForSameFileHandles() {
  70. $fn= fopen($this->fileKnownToExist(), 'r');
  71. $this->assertEquals(
  72. create(new File($fn))->hashCode(),
  73. create(new File($fn))->hashCode()
  74. );
  75. }
  76. /**
  77. * Test hashCode() method
  78. *
  79. */
  80. #[@test]
  81. public function hashCodesEqualForSameFiles() {
  82. $fn= $this->fileKnownToExist();
  83. $this->assertEquals(
  84. create(new File($fn))->hashCode(),
  85. create(new File($fn))->hashCode()
  86. );
  87. }
  88. /**
  89. * Test hashCode() method
  90. *
  91. */
  92. #[@test]
  93. public function hashCodesNotEqualForHandleAndUri() {
  94. $fn= $this->fileKnownToExist();
  95. $this->assertNotEquals(
  96. create(new File(fopen($fn, 'r')))->hashCode(),
  97. create(new File($fn))->hashCode()
  98. );
  99. }
  100. /**
  101. * Test getURI() method
  102. *
  103. */
  104. #[@test]
  105. public function getURI() {
  106. $fn= $this->fileKnownToExist();
  107. $this->assertEquals($fn, create(new File($fn))->getURI());
  108. }
  109. /**
  110. * Test getPath() method
  111. *
  112. * @see php://pathinfo
  113. */
  114. #[@test]
  115. public function getPath() {
  116. $fn= $this->fileKnownToExist();
  117. $info= pathinfo($fn);
  118. $this->assertEquals($info['dirname'], create(new File($fn))->getPath());
  119. }
  120. /**
  121. * Test getFileName() method
  122. *
  123. * @see php://pathinfo
  124. */
  125. #[@test]
  126. public function getFileName() {
  127. $fn= $this->fileKnownToExist();
  128. $info= pathinfo($fn);
  129. $this->assertEquals($info['basename'], create(new File($fn))->getFileName());
  130. }
  131. /**
  132. * Test getExtension() method
  133. *
  134. * @see php://pathinfo
  135. */
  136. #[@test]
  137. public function getExtension() {
  138. $fn= $this->fileKnownToExist();
  139. $info= pathinfo($fn);
  140. $this->assertEquals(
  141. isset($info['extension']) ? $info['extension'] : NULL,
  142. create(new File($fn))->getExtension()
  143. );
  144. }
  145. /**
  146. * Test NUL character is not allowed
  147. *
  148. */
  149. #[@test, @expect('lang.IllegalArgumentException')]
  150. public function nulCharacterNotAllowedInFilename() {
  151. new File("editor.txt\0.html");
  152. }
  153. /**
  154. * Test NUL character is not allowed
  155. *
  156. */
  157. #[@test, @expect('lang.IllegalArgumentException')]
  158. public function nulCharacterNotInTheBeginningOfFilename() {
  159. new File("\0editor.txt");
  160. }
  161. /**
  162. * Test creating a file with an empty filename
  163. *
  164. */
  165. #[@test, @expect('lang.IllegalArgumentException')]
  166. public function emptyFilenameNotAllowed() {
  167. new File('');
  168. }
  169. /**
  170. * Test creating a file with an empty filename
  171. *
  172. */
  173. #[@test, @expect('lang.IllegalArgumentException')]
  174. public function nullFilenameNotAllowed() {
  175. new File(NULL);
  176. }
  177. /**
  178. * Test composing filename by File("php://stdin")
  179. *
  180. */
  181. #[@test, @expect('lang.IllegalArgumentException')]
  182. public function filterScheme() {
  183. new File('php://filter/read=string.toupper|string.rot13/resource=http://www.example.comn');
  184. }
  185. /**
  186. * Test file object
  187. *
  188. */
  189. #[@test]
  190. public function newInstance() {
  191. $fn= $this->fileKnownToExist();
  192. $this->assertEquals($fn, create(new File($fn))->getURI());
  193. }
  194. /**
  195. * Test composing filename by File(Folder, string)
  196. *
  197. */
  198. #[@test]
  199. public function composingFromFolderAndString() {
  200. $fn= $this->fileKnownToExist();
  201. $this->assertEquals($fn, create(new File(new Folder(dirname($fn)), basename($fn)))->getURI());
  202. }
  203. /**
  204. * Test composing filename by File(Folder, string)
  205. *
  206. */
  207. #[@test]
  208. public function composingFromStringAndString() {
  209. $fn= $this->fileKnownToExist();
  210. $this->assertEquals($fn, create(new File(dirname($fn), basename($fn)))->getURI());
  211. }
  212. /**
  213. * Test composing filename by File(resource)
  214. *
  215. */
  216. #[@test]
  217. public function fromResource() {
  218. $this->assertNull(create(new File(fopen($this->fileKnownToExist(), 'r')))->getURI());
  219. }
  220. /**
  221. * Test composing filename by File("php://stderr")
  222. *
  223. */
  224. #[@test]
  225. public function stderr() {
  226. $this->assertEquals('php://stderr', create(new File('php://stderr'))->getURI());
  227. }
  228. /**
  229. * Test composing filename by File("php://stdout")
  230. *
  231. */
  232. #[@test]
  233. public function stdout() {
  234. $this->assertEquals('php://stdout', create(new File('php://stdout'))->getURI());
  235. }
  236. /**
  237. * Test composing filename by File("php://stdin")
  238. *
  239. */
  240. #[@test]
  241. public function stdin() {
  242. $this->assertEquals('php://stdin', create(new File('php://stdin'))->getURI());
  243. }
  244. /**
  245. * Test composing filename by File("xar://...")
  246. *
  247. */
  248. #[@test]
  249. public function xarSchemeAllowed() {
  250. $this->assertEquals('xar://test.xar?test.txt', create(new File('xar://test.xar?test.txt'))->getURI());
  251. }
  252. /**
  253. * Test composing filename by File("res://...")
  254. *
  255. */
  256. #[@test]
  257. public function resSchemeAllowed() {
  258. $this->assertEquals('res://test.txt', create(new File('res://test.txt'))->getURI());
  259. }
  260. /**
  261. * Test getFileName()
  262. *
  263. */
  264. #[@test]
  265. public function xarSchemeFileNameInRoot() {
  266. $this->assertEquals('test.txt', create(new File('xar://test.xar?test.txt'))->getFileName());
  267. }
  268. /**
  269. * Test getPath()
  270. *
  271. */
  272. #[@test]
  273. public function xarSchemePathInRoot() {
  274. $this->assertEquals('xar://test.xar?', create(new File('xar://test.xar?test.txt'))->getPath());
  275. }
  276. /**
  277. * Test getFileName()
  278. *
  279. */
  280. #[@test]
  281. public function xarSchemeFileNameInSubdir() {
  282. $this->assertEquals('test.txt', create(new File('xar://test.xar?dir/test.txt'))->getFileName());
  283. }
  284. /**
  285. * Test getPath()
  286. *
  287. */
  288. #[@test]
  289. public function xarSchemePathInSubdir() {
  290. $this->assertEquals('xar://test.xar?dir', create(new File('xar://test.xar?dir/test.txt'))->getPath());
  291. }
  292. /**
  293. * Test getFileName()
  294. *
  295. */
  296. #[@test]
  297. public function resSchemeFileName() {
  298. $this->assertEquals('test.txt', create(new File('res://test.txt'))->getFileName());
  299. }
  300. /**
  301. * Test getPath()
  302. *
  303. */
  304. #[@test]
  305. public function resSchemePath() {
  306. $this->assertEquals('res://', create(new File('res://test.txt'))->getPath());
  307. }
  308. /**
  309. * Test getPath()
  310. *
  311. */
  312. #[@test]
  313. public function resSchemePathInSubDir() {
  314. $this->assertEquals('res://dir', create(new File('res://dir/test.txt'))->getPath());
  315. }
  316. /**
  317. * Test getPath()
  318. *
  319. */
  320. #[@test]
  321. public function resSchemeExtension() {
  322. $this->assertEquals('txt', create(new File('res://test.txt'))->getExtension());
  323. }
  324. }
  325. ?>