| 
									
										
										
										
											2017-04-03 02:52:35 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace PhpOffice\PhpSpreadsheetTests\Reader; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-17 07:34:40 +00:00
										 |  |  | use PhpOffice\PhpSpreadsheet\Reader\Csv; | 
					
						
							| 
									
										
										
										
											2017-11-08 15:48:01 +00:00
										 |  |  | use PHPUnit\Framework\TestCase; | 
					
						
							| 
									
										
										
										
											2017-04-03 02:52:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-08 15:48:01 +00:00
										 |  |  | class CsvTest extends TestCase | 
					
						
							| 
									
										
										
										
											2017-04-03 02:52:35 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-12-28 03:22:01 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @dataProvider providerDelimiterDetection | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string $filename | 
					
						
							|  |  |  |      * @param string $expectedDelimiter | 
					
						
							|  |  |  |      * @param string $cell | 
					
						
							|  |  |  |      * @param float|int|string $expectedValue | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function testDelimiterDetection($filename, $expectedDelimiter, $cell, $expectedValue) | 
					
						
							| 
									
										
										
										
											2017-04-17 16:51:53 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-12-17 07:34:40 +00:00
										 |  |  |         $reader = new Csv(); | 
					
						
							| 
									
										
										
										
											2017-09-20 05:55:42 +00:00
										 |  |  |         self::assertNull($reader->getDelimiter()); | 
					
						
							| 
									
										
										
										
											2017-04-17 16:51:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $spreadsheet = $reader->load($filename); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-28 03:22:01 +00:00
										 |  |  |         self::assertSame($expectedDelimiter, $reader->getDelimiter(), 'should be able to infer the delimiter'); | 
					
						
							| 
									
										
										
										
											2017-04-17 16:51:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-28 03:22:01 +00:00
										 |  |  |         $actual = $spreadsheet->getActiveSheet()->getCell($cell)->getValue(); | 
					
						
							|  |  |  |         self::assertSame($expectedValue, $actual, 'should be able to retrieve correct value'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function providerDelimiterDetection() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return [ | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 __DIR__ . '/../../data/Reader/CSV/enclosure.csv', | 
					
						
							|  |  |  |                 ',', | 
					
						
							|  |  |  |                 'C4', | 
					
						
							|  |  |  |                 'username2', | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 __DIR__ . '/../../data/Reader/CSV/semicolon_separated.csv', | 
					
						
							|  |  |  |                 ';', | 
					
						
							|  |  |  |                 'C2', | 
					
						
							|  |  |  |                 '25,5', | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2018-10-10 14:27:14 +00:00
										 |  |  |             [ | 
					
						
							|  |  |  |                 __DIR__ . '/../../data/Reader/CSV/line_break_in_enclosure.csv', | 
					
						
							|  |  |  |                 ',', | 
					
						
							|  |  |  |                 'A3', | 
					
						
							|  |  |  |                 'Test', | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2019-02-25 22:20:50 +00:00
										 |  |  |             [ | 
					
						
							|  |  |  |                 __DIR__ . '/../../data/Reader/CSV/line_break_in_enclosure_with_escaped_quotes.csv', | 
					
						
							|  |  |  |                 ',', | 
					
						
							|  |  |  |                 'A3', | 
					
						
							|  |  |  |                 'Test', | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2017-12-28 03:22:01 +00:00
										 |  |  |             [ | 
					
						
							|  |  |  |                 __DIR__ . '/../../data/Reader/HTML/csv_with_angle_bracket.csv', | 
					
						
							|  |  |  |                 ',', | 
					
						
							|  |  |  |                 'B1', | 
					
						
							|  |  |  |                 'Number of items with weight <= 50kg', | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 __DIR__ . '/../../../samples/Reader/sampleData/example1.csv', | 
					
						
							|  |  |  |                 ',', | 
					
						
							|  |  |  |                 'I4', | 
					
						
							|  |  |  |                 '100%', | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 __DIR__ . '/../../../samples/Reader/sampleData/example2.csv', | 
					
						
							|  |  |  |                 ',', | 
					
						
							|  |  |  |                 'D8', | 
					
						
							|  |  |  |                 -58.373161, | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2018-10-26 17:14:45 +00:00
										 |  |  |             [ | 
					
						
							|  |  |  |                 'data/Reader/CSV/empty.csv', | 
					
						
							|  |  |  |                 ',', | 
					
						
							|  |  |  |                 'A1', | 
					
						
							|  |  |  |                 null, | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 'data/Reader/CSV/no_delimiter.csv', | 
					
						
							|  |  |  |                 ',', | 
					
						
							|  |  |  |                 'A1', | 
					
						
							|  |  |  |                 'SingleLine', | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2017-12-28 03:22:01 +00:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2017-04-17 16:51:53 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-05 12:33:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @dataProvider providerCanLoad | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param bool $expected | 
					
						
							|  |  |  |      * @param string $filename | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function testCanLoad($expected, $filename) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $reader = new Csv(); | 
					
						
							|  |  |  |         self::assertSame($expected, $reader->canRead($filename)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function providerCanLoad() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return [ | 
					
						
							|  |  |  |             [false, 'data/Reader/Ods/data.ods'], | 
					
						
							|  |  |  |             [false, 'data/Reader/Xml/WithoutStyle.xml'], | 
					
						
							|  |  |  |             [true, 'data/Reader/CSV/enclosure.csv'], | 
					
						
							|  |  |  |             [true, 'data/Reader/CSV/semicolon_separated.csv'], | 
					
						
							| 
									
										
										
										
											2018-06-25 02:12:27 +00:00
										 |  |  |             [true, 'data/Reader/CSV/contains_html.csv'], | 
					
						
							|  |  |  |             [true, 'data/Reader/CSV/csv_without_extension'], | 
					
						
							| 
									
										
										
										
											2018-02-05 12:33:23 +00:00
										 |  |  |             [true, 'data/Reader/HTML/csv_with_angle_bracket.csv'], | 
					
						
							|  |  |  |             [true, 'data/Reader/CSV/empty.csv'], | 
					
						
							|  |  |  |             [true, '../samples/Reader/sampleData/example1.csv'], | 
					
						
							|  |  |  |             [true, '../samples/Reader/sampleData/example2.csv'], | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-05-23 01:31:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function testEscapeCharacters() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $reader = (new Csv())->setEscapeCharacter('"'); | 
					
						
							|  |  |  |         $worksheet = $reader->load(__DIR__ . '/../../data/Reader/CSV/backslash.csv') | 
					
						
							|  |  |  |             ->getActiveSheet(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $expected = [ | 
					
						
							|  |  |  |             ['field 1', 'field 2\\'], | 
					
						
							|  |  |  |             ['field 3\\', 'field 4'], | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertSame('"', $reader->getEscapeCharacter()); | 
					
						
							|  |  |  |         $this->assertSame($expected, $worksheet->toArray()); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-04-03 02:52:35 +00:00
										 |  |  | } |