FileLibTest.php 6.9 KB

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