CsvReaderTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. namespace Ddeboer\DataImport\Tests\Reader;
  3. use Ddeboer\DataImport\Reader\CsvReader;
  4. class CsvReaderTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function testReadCsvFileWithColumnHeaders()
  7. {
  8. $file = new \SplFileObject(__DIR__.'/../Fixtures/data_column_headers.csv');
  9. $csvReader = new CsvReader($file);
  10. $csvReader->setHeaderRowNumber(0);
  11. $this->assertEquals(
  12. array(
  13. 'id', 'number', 'description'
  14. ),
  15. $csvReader->getFields()
  16. );
  17. foreach ($csvReader as $row) {
  18. $this->assertNotNull($row['id']);
  19. $this->assertNotNull($row['number']);
  20. $this->assertNotNull($row['description']);
  21. }
  22. $this->assertEquals(
  23. array(
  24. 'id' => 6,
  25. 'number' => '456',
  26. 'description' => 'Another description'
  27. ),
  28. $csvReader->getRow(2)
  29. );
  30. }
  31. public function testReadCsvFileWithoutColumnHeaders()
  32. {
  33. $file = new \SplFileObject(__DIR__.'/../Fixtures/data_no_column_headers.csv');
  34. $csvReader = new CsvReader($file);
  35. $this->assertEmpty($csvReader->getColumnHeaders());
  36. }
  37. public function testReadCsvFileWithManualColumnHeaders()
  38. {
  39. $file = new \SplFileObject(__DIR__.'/../Fixtures/data_no_column_headers.csv');
  40. $csvReader = new CsvReader($file);
  41. $csvReader->setColumnHeaders(array('id', 'number', 'description'));
  42. foreach ($csvReader as $row) {
  43. $this->assertNotNull($row['id']);
  44. $this->assertNotNull($row['number']);
  45. $this->assertNotNull($row['description']);
  46. }
  47. }
  48. public function testReadCsvFileWithTrailingBlankLines()
  49. {
  50. $file = new \SplFileObject(__DIR__.'/../Fixtures/data_blank_lines.csv');
  51. $csvReader = new CsvReader($file);
  52. $csvReader->setColumnHeaders(array('id', 'number', 'description'));
  53. foreach ($csvReader as $row) {
  54. $this->assertNotNull($row['id']);
  55. $this->assertNotNull($row['number']);
  56. $this->assertNotNull($row['description']);
  57. }
  58. }
  59. public function testCountWithoutHeaders()
  60. {
  61. $file = new \SplFileObject(__DIR__.'/../Fixtures/data_no_column_headers.csv');
  62. $csvReader = new CsvReader($file);
  63. $this->assertEquals(3, $csvReader->count());
  64. }
  65. public function testCountWithHeaders()
  66. {
  67. $file = new \SplFileObject(__DIR__.'/../Fixtures/data_column_headers.csv');
  68. $csvReader = new CsvReader($file);
  69. $csvReader->setHeaderRowNumber(0);
  70. $this->assertEquals(3, $csvReader->count(), 'Row count should not include header');
  71. }
  72. public function testCountWithFewerElementsThanColumnHeadersNotStrict()
  73. {
  74. $file = new \SplFileObject(__DIR__.'/../Fixtures/data_fewer_elements_than_column_headers.csv');
  75. $csvReader = new CsvReader($file);
  76. $csvReader->setStrict(false);
  77. $csvReader->setHeaderRowNumber(0);
  78. $this->assertEquals(3, $csvReader->count());
  79. }
  80. public function testCountWithMoreElementsThanColumnHeadersNotStrict()
  81. {
  82. $file = new \SplFileObject(__DIR__.'/../Fixtures/data_more_elements_than_column_headers.csv');
  83. $csvReader = new CsvReader($file);
  84. $csvReader->setStrict(false);
  85. $csvReader->setHeaderRowNumber(0);
  86. $this->assertEquals(3, $csvReader->count());
  87. $this->assertFalse($csvReader->hasErrors());
  88. $this->assertEquals(array(6, 456, 'Another description'), array_values($csvReader->getRow(2)));
  89. }
  90. public function testCountDoesNotMoveFilePointer()
  91. {
  92. $file = new \SplFileObject(__DIR__.'/../Fixtures/data_column_headers.csv');
  93. $csvReader = new CsvReader($file);
  94. $csvReader->setHeaderRowNumber(0);
  95. $key_before_count = $csvReader->key();
  96. $csvReader->count();
  97. $key_after_count = $csvReader->key();
  98. $this->assertEquals($key_after_count, $key_before_count);
  99. }
  100. public function testVaryingElementCountWithColumnHeadersNotStrict()
  101. {
  102. $file = new \SplFileObject(__DIR__.'/../Fixtures/data_column_headers_varying_element_count.csv');
  103. $csvReader = new CsvReader($file);
  104. $csvReader->setStrict(false);
  105. $csvReader->setHeaderRowNumber(0);
  106. $this->assertEquals(4, $csvReader->count());
  107. $this->assertFalse($csvReader->hasErrors());
  108. }
  109. public function testVaryingElementCountWithoutColumnHeadersNotStrict()
  110. {
  111. $file = new \SplFileObject(__DIR__.'/../Fixtures/data_no_column_headers_varying_element_count.csv');
  112. $csvReader = new CsvReader($file);
  113. $csvReader->setStrict(false);
  114. $csvReader->setColumnHeaders(array('id', 'number', 'description'));
  115. $this->assertEquals(5, $csvReader->count());
  116. $this->assertFalse($csvReader->hasErrors());
  117. }
  118. public function testInvalidCsv()
  119. {
  120. $file = new \SplFileObject(__DIR__.'/../Fixtures/data_column_headers_varying_element_count.csv');
  121. $reader = new CsvReader($file);
  122. $reader->setHeaderRowNumber(0);
  123. $this->assertTrue($reader->hasErrors());
  124. $this->assertCount(2, $reader->getErrors());
  125. $errors = $reader->getErrors();
  126. $this->assertEquals(2, key($errors));
  127. $this->assertEquals(array('123', 'test'), current($errors));
  128. next($errors);
  129. $this->assertEquals(3, key($errors));
  130. $this->assertEquals(array('7', '7890', 'Some more info', 'too many columns'), current($errors));
  131. }
  132. public function testLastRowInvalidCsv()
  133. {
  134. $file = new \SplFileObject(__DIR__.'/../Fixtures/data_no_column_headers_varying_element_count.csv');
  135. $reader = new CsvReader($file);
  136. $reader->setColumnHeaders(array('id', 'number', 'description'));
  137. $this->assertTrue($reader->hasErrors());
  138. $this->assertCount(3, $reader->getErrors());
  139. $errors = $reader->getErrors();
  140. $this->assertEquals(1, key($errors));
  141. $this->assertEquals(array('6', 'strictly invalid'), current($errors));
  142. next($errors);
  143. $this->assertEquals(3, key($errors));
  144. $this->assertEquals(array('3','230','Yet more info','Even more info'), current($errors));
  145. next($errors);
  146. $this->assertEquals(4, key($errors));
  147. $this->assertEquals(array('strictly invalid'), current($errors));
  148. }
  149. public function testLineBreaks()
  150. {
  151. $reader = $this->getReader('data_cr_breaks.csv');
  152. $this->assertCount(3, $reader);
  153. }
  154. /**
  155. * @expectedException \Ddeboer\DataImport\Exception\DuplicateHeadersException description
  156. */
  157. public function testDuplicateHeadersThrowsException()
  158. {
  159. $reader = $this->getReader('data_column_headers_duplicates.csv');
  160. $reader->setHeaderRowNumber(0);
  161. }
  162. public function testDuplicateHeadersIncrement()
  163. {
  164. $reader = $this->getReader('data_column_headers_duplicates.csv');
  165. $reader->setHeaderRowNumber(0, CsvReader::DUPLICATE_HEADERS_INCREMENT);
  166. $reader->rewind();
  167. $current = $reader->current();
  168. $this->assertEquals(
  169. array('id', 'description', 'description1', 'description2', 'details', 'details1', 'last'),
  170. $reader->getColumnHeaders()
  171. );
  172. $this->assertEquals(
  173. array(
  174. 'id' => '50',
  175. 'description' => 'First',
  176. 'description1' => 'Second',
  177. 'description2' => 'Third',
  178. 'details' => 'Details1',
  179. 'details1' => 'Details2',
  180. 'last' => 'Last one'
  181. ),
  182. $current
  183. );
  184. }
  185. public function testDuplicateHeadersMerge()
  186. {
  187. $reader = $this->getReader('data_column_headers_duplicates.csv');
  188. $reader->setHeaderRowNumber(0, CsvReader::DUPLICATE_HEADERS_MERGE);
  189. $reader->rewind();
  190. $current = $reader->current();
  191. $this->assertCount(4, $reader->getColumnHeaders());
  192. $expected = array(
  193. 'id' => '50',
  194. 'description' => array('First', 'Second', 'Third'),
  195. 'details' => array('Details1', 'Details2'),
  196. 'last' => 'Last one'
  197. );
  198. $this->assertEquals($expected, $current);
  199. }
  200. public function testMaximumNesting()
  201. {
  202. if (!function_exists('xdebug_is_enabled')) {
  203. $this->markTestSkipped('xDebug is not installed');
  204. }
  205. $xdebug_start = !xdebug_is_enabled();
  206. if ($xdebug_start) {
  207. xdebug_enable();
  208. }
  209. ini_set('xdebug.max_nesting_level', 200);
  210. $file = new \SplTempFileObject();
  211. for($i = 0; $i < 500; $i++) {
  212. $file->fwrite("1,2,3\n");
  213. }
  214. $reader = new CsvReader($file);
  215. $reader->rewind();
  216. $reader->setStrict(true);
  217. $reader->setColumnHeaders(array('one','two'));
  218. $current = $reader->current();
  219. $this->assertEquals(null, $current);
  220. if ($xdebug_start) {
  221. xdebug_disable();
  222. }
  223. }
  224. protected function getReader($filename)
  225. {
  226. $file = new \SplFileObject(__DIR__.'/../Fixtures/'.$filename);
  227. return new CsvReader($file);
  228. }
  229. }