FileLibTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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(1024, ',', '"');
  19. $expected = array(array(
  20. 'First',
  21. 'Last Name',
  22. 'Email'), array(
  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(1024, ',', '\'');
  41. $expected = array(array(
  42. 'First',
  43. 'Last Name',
  44. 'Email'
  45. ), array(
  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. * Test method
  56. *
  57. * @return void
  58. */
  59. public function testReadWithTags1() {
  60. $handler = new FileLib(TMP . 'test.txt', true);
  61. $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';
  62. $handler->write($pre);
  63. $handler->close();
  64. $handler2 = new FileLib(TMP . 'test.txt', true);
  65. $is = $handler2->readWithTags();
  66. $expected = '<h1>Header</h1><p><b>Bold Text</b></p>Between to lines</p>Some SubheaderSome more text at the end';
  67. $status = $this->assertEquals($expected, $is);
  68. $this->_printArrays($status, $is, $expected, $pre);
  69. }
  70. /**
  71. * Test csv file generation from array
  72. */
  73. public function testWriteCsv() {
  74. $handler = new FileLib(TMP . 'test.csv', true);
  75. $array = array(
  76. array(
  77. 'header1',
  78. 'header2',
  79. 'header3'),
  80. array(
  81. 'v1a',
  82. 'v1b',
  83. 'v1c'),
  84. array(
  85. 'v2a',
  86. 'v2b',
  87. 'v2c'),
  88. );
  89. $res = $handler->writeCsv($array);
  90. $this->assertTrue($res);
  91. $handler = new FileLib(TMP . 'test.csv', true);
  92. $res = $handler->readCsv(1024);
  93. $this->assertEquals($array, $res);
  94. }
  95. /**
  96. * Test method
  97. *
  98. * @return void
  99. */
  100. public function testReadWithPattern1() {
  101. $handler = new FileLib(TMP . 'test.txt', true);
  102. $pre = 'First' . TB . 'LastName' . TB . 'Email' . NL . 'Example' . TB . 'Firsty' . TB . 'test@test.com';
  103. //$pre = 'First, Last Name, Email'.PHP_EOL.'Example, Firsty, test@test.com';
  104. //$pre = 'First-LastName-Email'.NL.'Example-Firsty-test@test.com';
  105. $handler->write($pre);
  106. $handler->close();
  107. $handler2 = new FileLib(TMP . 'test.txt', true);
  108. $is = $handler2->readWithPattern('%s' . TB . '%s' . TB . '%s');
  109. $expected = array(array(
  110. 'First',
  111. 'LastName',
  112. 'Email'), array(
  113. 'Example',
  114. 'Firsty',
  115. 'test@test.com'));
  116. $status = $this->assertEquals($expected, $is);
  117. $this->_printArrays($status, $is, $expected, $pre);
  118. }
  119. public function testReadWithPattern2() {
  120. $handler = new FileLib(TMP . 'test.txt', true);
  121. $pre = '2-33-44' . NL . '5-66-77';
  122. //$pre = 'First, Last Name, Email'.PHP_EOL.'Example, Firsty, test@test.com';
  123. $handler->write($pre);
  124. $handler->close();
  125. $handler2 = new FileLib(TMP . 'test.txt', true);
  126. $is = $handler2->readWithPattern('%d-%d-%d');
  127. $expected = array(array(
  128. '2',
  129. '33',
  130. '44'), array(
  131. '5',
  132. '66',
  133. '77'));
  134. $status = $this->assertEquals($expected, $is);
  135. $this->_printArrays($status, $is, $expected, $pre);
  136. }
  137. public function testTransfer() {
  138. $handler = new FileLib(TMP . 'test.txt', true);
  139. $pre = '"First", "Last Name", "Email"' . NL . '"Example", "Firsty", "test@test.com"' . NL . '"Next", "Secondy", "again@test.com"';
  140. $handler->write($pre);
  141. $handler->close();
  142. $handler2 = new FileLib(TMP . 'test.txt', true);
  143. $is = $handler2->readCsv(1024, ',', '"');
  144. $is = $handler2->transfer($is);
  145. //pr($is);
  146. $expected = array(array(
  147. 'first' => 'Example',
  148. 'last_name' => 'Firsty',
  149. 'email' => 'test@test.com'), array(
  150. 'first' => 'Next',
  151. 'last_name' => 'Secondy',
  152. 'email' => 'again@test.com'));
  153. $this->assertEquals($expected, $is);
  154. }
  155. public function testTransferWithManualKeys() {
  156. $handler = new FileLib(TMP . 'test.txt', true);
  157. $pre = '"First", "Last Name", "Email"' . NL . '"Example", "Firsty", "test@test.com"' . NL . '"Next", "Secondy", "again@test.com"';
  158. $handler->write($pre);
  159. $handler->close();
  160. $handler2 = new FileLib(TMP . 'test.txt', true);
  161. $is = $handler2->readCsv(1024, ',', '"');
  162. array_shift($is);
  163. $is = $handler2->transfer($is, array('keys' => array(
  164. 'X',
  165. 'Y',
  166. 'Z'), 'preserve_keys' => true));
  167. //pr($is);
  168. $expected = array(array(
  169. 'X' => 'Example',
  170. 'Y' => 'Firsty',
  171. 'Z' => 'test@test.com'), array(
  172. 'X' => 'Next',
  173. 'Y' => 'Secondy',
  174. 'Z' => 'again@test.com'));
  175. $this->assertEquals($expected, $is);
  176. }
  177. public function testReadCsvWithEmpty() {
  178. $handler = new FileLib(TMP . 'test.txt', true);
  179. $pre = '"First", "Last Name", "Email"' . NL . ',,' . NL . '"Next", "Secondy", "again@test.com"';
  180. $handler->write($pre);
  181. $handler->close();
  182. $handler2 = new FileLib(TMP . 'test.txt', true);
  183. $is = $handler2->readCsv(1024, ',', '"', 'rb', false, true);
  184. array_shift($is);
  185. $is = $handler2->transfer($is, array('keys' => array(
  186. 'X',
  187. 'Y',
  188. 'Z'), 'preserve_keys' => true));
  189. //pr($is);
  190. $expected = array(array(
  191. 'X' => 'Next',
  192. 'Y' => 'Secondy',
  193. 'Z' => 'again@test.com'));
  194. $this->assertEquals($expected, $is);
  195. }
  196. /**
  197. * Test BOM
  198. *
  199. * @return void
  200. */
  201. public function testBOM() {
  202. $folder = CakePlugin::path('Tools') . 'Test' . DS . 'test_files' . DS . 'txt' . DS;
  203. $fileOK = $folder . 'ok.php';
  204. $fileNOK = $folder . 'nok.php';
  205. $result = FileLib::hasByteOrderMark(file_get_contents($fileOK));
  206. $this->assertFalse($result);
  207. $result = FileLib::hasByteOrderMark(file_get_contents($fileNOK));
  208. $this->assertTrue($result);
  209. $tmpFileNOK = TMP . 'nok.php';
  210. copy($fileNOK, $tmpFileNOK);
  211. $result = FileLib::removeByteOrderMark(file_get_contents($tmpFileNOK));
  212. //file_put_contents($tmpFileNOK, $result);
  213. //$result = FileLib::hasByteOrderMark(file_get_contents($tmpFileNOK));
  214. $result = FileLib::hasByteOrderMark($result);
  215. $this->assertFalse($result);
  216. unlink($tmpFileNOK);
  217. }
  218. /** Helper Functions **/
  219. public function _printArrays($status, $is, $expected, $pre = null) {
  220. if (!isset($_GET['show_passes']) || !$_GET['show_passes']) {
  221. return false;
  222. }
  223. if ($pre !== null) {
  224. //echo 'pre:';
  225. //pr($pre);
  226. }
  227. //echo 'is:';
  228. //pr($is);
  229. if (!$status) {
  230. //echo 'expected:';
  231. //pr($expected);
  232. }
  233. }
  234. }