FileLibTest.php 6.1 KB

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