parser_csv.test 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * @file
  4. * Tests for ParserCSV library.
  5. */
  6. /**
  7. * Test aggregating a feed as node items.
  8. *
  9. * Using DrupalWebTestCase as DrupalUnitTestCase is broken in SimpleTest 2.8.
  10. * Not inheriting from Feeds base class as ParserCSV should be moved out of
  11. * Feeds at some time.
  12. */
  13. class ParserCSVTest extends DrupalWebTestCase {
  14. protected $profile = 'testing';
  15. public static function getInfo() {
  16. return array(
  17. 'name' => 'CSV Parser unit tests',
  18. 'description' => 'Base level test for Feeds\' built in CSV parser.',
  19. 'group' => 'Feeds',
  20. );
  21. }
  22. /**
  23. * Test method.
  24. */
  25. public function test() {
  26. drupal_load('module', 'feeds');
  27. feeds_include_library('ParserCSV.inc', 'ParserCSV');
  28. $this->_testSimple();
  29. $this->_testBatching();
  30. }
  31. /**
  32. * Simple test of parsing functionality.
  33. */
  34. protected function _testSimple() {
  35. $file = $this->absolutePath() . '/tests/feeds/nodes.csv';
  36. include $this->absolutePath() . '/tests/feeds/nodes.csv.php';
  37. $iterator = new ParserCSVIterator($file);
  38. $parser = new ParserCSV();
  39. $parser->setDelimiter(',');
  40. $rows = $parser->parse($iterator);
  41. $this->assertFalse($parser->lastLinePos(), t('Parser reports all lines parsed'));
  42. $this->assertEqual(md5(serialize($rows)), md5(serialize($control_result)), t('Parsed result matches control result.'));
  43. }
  44. /**
  45. * Test batching.
  46. */
  47. protected function _testBatching() {
  48. $file = $this->absolutePath() . '/tests/feeds/nodes.csv';
  49. include $this->absolutePath() . '/tests/feeds/nodes.csv.php';
  50. // Set up parser with 2 lines to parse per call.
  51. $iterator = new ParserCSVIterator($file);
  52. $parser = new ParserCSV();
  53. $parser->setDelimiter(',');
  54. $parser->setLineLimit(2);
  55. $rows = array();
  56. $pos = 0;
  57. // Call parser until all lines are parsed, then compare to control result.
  58. do {
  59. $parser->setStartByte($pos);
  60. $rows = array_merge($rows, $parser->parse($iterator));
  61. $pos = $parser->lastLinePos();
  62. $this->assertTrue($parser->lastLinePos() || count($rows) == 10, t('Parser reports line limit correctly'));
  63. }
  64. while ($pos = $parser->lastLinePos());
  65. $this->assertEqual(md5(serialize($rows)), md5(serialize($control_result)), t('Parsed result matches control result.'));
  66. }
  67. /**
  68. * Absolute path to feeds.
  69. */
  70. public function absolutePath() {
  71. return DRUPAL_ROOT . '/' . drupal_get_path('module', 'feeds');
  72. }
  73. }