FileLibTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. App::uses('FileLib', 'Tools.Utility');
  3. /**
  4. *
  5. */
  6. class FileLibTest extends CakeTestCase {
  7. /**
  8. * Test method
  9. *
  10. * @return void
  11. */
  12. public function testReadCsv1() {
  13. $handler = new FileLib(TMP . 'test.txt', true);
  14. $pre = '"First", "Last Name", "Email"' . NL . '"Example", "Firsty", "test@test.com"'; //.NL.'"Next", "Secondy", "again@test.com"'
  15. $handler->write($pre);
  16. $handler->close();
  17. $handler2 = new FileLib(TMP . 'test.txt', true);
  18. $is = $handler2->readCsv();
  19. $expected = [[
  20. 'First',
  21. 'Last Name',
  22. 'Email'], [
  23. 'Example',
  24. 'Firsty',
  25. 'test@test.com']];
  26. $status = $this->assertEquals($expected, $is);
  27. $this->_printArrays($status, $is, $expected, $pre);
  28. }
  29. /**
  30. * FileLibTest::testReadCsv2() with umlauts
  31. *
  32. * @return void
  33. */
  34. public function testReadCsv2() {
  35. $handler = new FileLib(TMP . 'test.txt', true);
  36. $pre = '\'First\', \'Last Name\', \'Email\'' . NL . '\'Example Äs\', \'Firsty üs\', \'test@test.com sß\'';
  37. $handler->write($pre);
  38. $handler->close();
  39. $handler2 = new FileLib(TMP . 'test.txt', true);
  40. $is = $handler2->readCsv(['enclosure' => '\'']);
  41. $expected = [[
  42. 'First',
  43. 'Last Name',
  44. 'Email'
  45. ], [
  46. 'Example Äs',
  47. 'Firsty üs',
  48. 'test@test.com sß'
  49. ]
  50. ];
  51. $status = $this->assertEquals($expected, $is);
  52. $this->_printArrays($status, $is, $expected, $pre);
  53. }
  54. /**
  55. * Tests that tmpfile() hack works. One should use readCsvFromString() instead, though.
  56. *
  57. * @return void
  58. */
  59. public function testReadCsvFromTmpFile() {
  60. $message = '"First"; "Last Name"; "Email"' . NL . '"Example Äs"; "Firsty üs"; "test@test.com sß"';
  61. $handle = tmpfile();
  62. $meta = stream_get_meta_data($handle);
  63. $filename = $meta['uri'];
  64. $this->assertTrue(file_exists($filename));
  65. $File = new FileLib($filename);
  66. $File->write($message);
  67. // This seems to be necessary in this case - fclose($handle) doesn't do the trick
  68. $File->handle = $handle;
  69. $array = $File->readCsv(['delimiter' => ';']);
  70. $File->close();
  71. $this->assertFalse(file_exists($filename));
  72. $this->assertTrue(count($array) === 2);
  73. $this->assertEquals('Last Name', $array[0][1]);
  74. }
  75. /**
  76. * FileLibTest::testReadCsvFromString()
  77. *
  78. * @return void
  79. */
  80. public function testReadCsvFromString() {
  81. $csv = '\'First\', \'Last Name\', \'Email\'' . NL . '\'Example Äs\', \'Firsty üs\', \'test@test.com\'';
  82. $array = FileLib::readCsvFromString($csv, ['enclosure' => '\'']);
  83. $this->assertEquals('Last Name', $array[0][1]);
  84. $csv = '\'First\', \'Last Name\', \'Email\'' . NL . '\'Example Äs\', \'Firsty üs\', \'test@test.com \\\' sß\'';
  85. $array = FileLib::readCsvFromString($csv, ['enclosure' => '\'']);
  86. $this->assertEquals('test@test.com \\\' sß', $array[1][2]);
  87. $csv = '\'First\', \'Last Name\', \'Email\'' . NL . '\'Example Äs\', \'Firsty üs\', \'test@test.com \'\' sß\'';
  88. $array = FileLib::readCsvFromString($csv, ['enclosure' => '\'']);
  89. $this->assertEquals('test@test.com \' sß', $array[1][2]);
  90. }
  91. /**
  92. * FileLibTest::testReadCsvFromString()
  93. *
  94. * @expectedException InternalErrorException
  95. * @return void
  96. */
  97. public function testReadCsvFromStringInvalidMultibyteDelimiter() {
  98. $csv = 'äFirstä, äLast Nameä, äEmailä' . NL . 'äExample Äsä, äFirsty üsä, ätest@test.com ää sßä';
  99. $array = FileLib::readCsvFromString($csv, ['enclosure' => 'ä', 'delimiter' => 'ä']);
  100. $this->assertEquals('test@test.com ä sß', $array[1][2]);
  101. }
  102. /**
  103. * Test method
  104. *
  105. * @return void
  106. */
  107. public function testReadWithTags1() {
  108. $handler = new FileLib(TMP . 'test.txt', true);
  109. $pre = '<h1>Header</h1><p><b>Bold Text</b></p><hr />Between to lines<hr></p><h4>Some Subheader</h4>Some more text at the end';
  110. $handler->write($pre);
  111. $handler->close();
  112. $handler2 = new FileLib(TMP . 'test.txt', true);
  113. $is = $handler2->readWithTags();
  114. $expected = '<h1>Header</h1><p><b>Bold Text</b></p>Between to lines</p>Some SubheaderSome more text at the end';
  115. $status = $this->assertEquals($expected, $is);
  116. $this->_printArrays($status, $is, $expected, $pre);
  117. }
  118. /**
  119. * Test csv file generation from array
  120. */
  121. public function testWriteCsv() {
  122. $handler = new FileLib(TMP . 'test.csv', true);
  123. $array = [
  124. [
  125. 'header1',
  126. 'header2',
  127. 'header3'],
  128. [
  129. 'v1a',
  130. 'v1b',
  131. 'v1c'],
  132. [
  133. 'v2a',
  134. 'v2b',
  135. 'v2c'],
  136. ];
  137. $res = $handler->writeCsv($array);
  138. $this->assertTrue($res);
  139. $handler = new FileLib(TMP . 'test.csv', true);
  140. $res = $handler->readCsv();
  141. $this->assertEquals($array, $res);
  142. }
  143. /**
  144. * Test method
  145. *
  146. * @return void
  147. */
  148. public function testReadWithPattern1() {
  149. $handler = new FileLib(TMP . 'test.txt', true);
  150. $pre = 'First' . TB . 'LastName' . TB . 'Email' . NL . 'Example' . TB . 'Firsty' . TB . 'test@test.com';
  151. //$pre = 'First, Last Name, Email'.PHP_EOL.'Example, Firsty, test@test.com';
  152. //$pre = 'First-LastName-Email'.NL.'Example-Firsty-test@test.com';
  153. $handler->write($pre);
  154. $handler->close();
  155. $handler2 = new FileLib(TMP . 'test.txt', true);
  156. $is = $handler2->readWithPattern('%s' . TB . '%s' . TB . '%s');
  157. $expected = [[
  158. 'First',
  159. 'LastName',
  160. 'Email'], [
  161. 'Example',
  162. 'Firsty',
  163. 'test@test.com']];
  164. $status = $this->assertEquals($expected, $is);
  165. $this->_printArrays($status, $is, $expected, $pre);
  166. }
  167. public function testReadWithPattern2() {
  168. $handler = new FileLib(TMP . 'test.txt', true);
  169. $pre = '2-33-44' . NL . '5-66-77';
  170. //$pre = 'First, Last Name, Email'.PHP_EOL.'Example, Firsty, test@test.com';
  171. $handler->write($pre);
  172. $handler->close();
  173. $handler2 = new FileLib(TMP . 'test.txt', true);
  174. $is = $handler2->readWithPattern('%d-%d-%d');
  175. $expected = [[
  176. '2',
  177. '33',
  178. '44'], [
  179. '5',
  180. '66',
  181. '77']];
  182. $status = $this->assertEquals($expected, $is);
  183. $this->_printArrays($status, $is, $expected, $pre);
  184. }
  185. public function testTransfer() {
  186. $handler = new FileLib(TMP . 'test.txt', true);
  187. $pre = '"First", "Last Name", "Email"' . NL . '"Example", "Firsty", "test@test.com"' . NL . '"Next", "Secondy", "again@test.com"';
  188. $handler->write($pre);
  189. $handler->close();
  190. $handler2 = new FileLib(TMP . 'test.txt', true);
  191. $is = $handler2->readCsv();
  192. $is = $handler2->transfer($is);
  193. //pr($is);
  194. $expected = [[
  195. 'first' => 'Example',
  196. 'last_name' => 'Firsty',
  197. 'email' => 'test@test.com'], [
  198. 'first' => 'Next',
  199. 'last_name' => 'Secondy',
  200. 'email' => 'again@test.com']];
  201. $this->assertEquals($expected, $is);
  202. }
  203. public function testTransferWithManualKeys() {
  204. $handler = new FileLib(TMP . 'test.txt', true);
  205. $pre = '"First", "Last Name", "Email"' . NL . '"Example", "Firsty", "test@test.com"' . NL . '"Next", "Secondy", "again@test.com"';
  206. $handler->write($pre);
  207. $handler->close();
  208. $handler2 = new FileLib(TMP . 'test.txt', true);
  209. $is = $handler2->readCsv();
  210. array_shift($is);
  211. $is = $handler2->transfer($is, ['keys' => [
  212. 'X',
  213. 'Y',
  214. 'Z'], 'preserve_keys' => true]);
  215. //pr($is);
  216. $expected = [[
  217. 'X' => 'Example',
  218. 'Y' => 'Firsty',
  219. 'Z' => 'test@test.com'], [
  220. 'X' => 'Next',
  221. 'Y' => 'Secondy',
  222. 'Z' => 'again@test.com']];
  223. $this->assertEquals($expected, $is);
  224. }
  225. public function testReadCsvWithEmpty() {
  226. $handler = new FileLib(TMP . 'test.txt', true);
  227. $pre = '"First", "Last Name", "Email"' . NL . ',,' . NL . '"Next", "Secondy", "again@test.com"';
  228. $handler->write($pre);
  229. $handler->close();
  230. $handler2 = new FileLib(TMP . 'test.txt', true);
  231. $is = $handler2->readCsv(['removeEmpty' => true]);
  232. array_shift($is);
  233. $is = $handler2->transfer($is, ['keys' => [
  234. 'X',
  235. 'Y',
  236. 'Z'], 'preserve_keys' => true]);
  237. //pr($is);
  238. $expected = [[
  239. 'X' => 'Next',
  240. 'Y' => 'Secondy',
  241. 'Z' => 'again@test.com']];
  242. $this->assertEquals($expected, $is);
  243. }
  244. /**
  245. * Test BOM
  246. *
  247. * @return void
  248. */
  249. public function testBOM() {
  250. $folder = CakePlugin::path('Tools') . 'Test' . DS . 'test_files' . DS . 'txt' . DS;
  251. $fileOK = $folder . 'ok.php';
  252. $fileNOK = $folder . 'nok.php';
  253. $result = FileLib::hasByteOrderMark(file_get_contents($fileOK));
  254. $this->assertFalse($result);
  255. $result = FileLib::hasByteOrderMark(file_get_contents($fileNOK));
  256. $this->assertTrue($result);
  257. $tmpFileNOK = TMP . 'nok.php';
  258. copy($fileNOK, $tmpFileNOK);
  259. $result = FileLib::removeByteOrderMark(file_get_contents($tmpFileNOK));
  260. //file_put_contents($tmpFileNOK, $result);
  261. //$result = FileLib::hasByteOrderMark(file_get_contents($tmpFileNOK));
  262. $result = FileLib::hasByteOrderMark($result);
  263. $this->assertFalse($result);
  264. unlink($tmpFileNOK);
  265. }
  266. /** Helper Functions **/
  267. /**
  268. * FileLibTest::_printArrays()
  269. *
  270. * @param mixed $status
  271. * @param mixed $is
  272. * @param mixed $expected
  273. * @param mixed $pre
  274. * @return void
  275. */
  276. protected function _printArrays($status, $is, $expected, $pre = null) {
  277. if (!isset($_GET['show_passes']) || !$_GET['show_passes']) {
  278. return false;
  279. }
  280. echo 'Result:';
  281. pr($is);
  282. echo 'Expected:';
  283. pr($expected);
  284. }
  285. }