FileLibTest.php 6.8 KB

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