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