|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpOffice\PhpSpreadsheetTests\Reader\Csv; |
| 6 | + |
| 7 | +use PhpOffice\PhpSpreadsheet\IOFactory; |
| 8 | +use PhpOffice\PhpSpreadsheet\Reader\Csv as CsvReader; |
| 9 | +use PhpOffice\PhpSpreadsheet\Shared\File; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | + |
| 12 | +class NotHtmlTest extends TestCase |
| 13 | +{ |
| 14 | + private string $tempFile = ''; |
| 15 | + |
| 16 | + protected function tearDown(): void |
| 17 | + { |
| 18 | + if ($this->tempFile !== '') { |
| 19 | + unlink($this->tempFile); |
| 20 | + $this->tempFile = ''; |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + public function testHtmlCantRead(): void |
| 25 | + { |
| 26 | + // This test has a file which IOFactory will identify as Csv. |
| 27 | + // So file can be read using either Csv Reader or IOFactory. |
| 28 | + $this->tempFile = $filename = File::temporaryFilename(); |
| 29 | + $cells = [ |
| 30 | + ['1', '<a href="http://example.com">example</a>', '3'], |
| 31 | + ['4', '5', '6'], |
| 32 | + ]; |
| 33 | + $handle = fopen($filename, 'wb'); |
| 34 | + self::assertNotFalse($handle); |
| 35 | + foreach ($cells as $row) { |
| 36 | + fwrite($handle, "{$row[0]},{$row[1]},{$row[2]}\n"); |
| 37 | + } |
| 38 | + fclose($handle); |
| 39 | + self::assertSame('text/html', mime_content_type($filename)); |
| 40 | + self::assertSame('Csv', IOFactory::identify($filename)); |
| 41 | + $reader = new CsvReader(); |
| 42 | + $spreadsheet = $reader->load($filename); |
| 43 | + $sheet = $spreadsheet->getActiveSheet(); |
| 44 | + self::assertSame($cells, $sheet->toArray()); |
| 45 | + $spreadsheet->disconnectWorksheets(); |
| 46 | + } |
| 47 | + |
| 48 | + public function testHtmlCanRead(): void |
| 49 | + { |
| 50 | + // This test has a file which IOFactory will identify as Html. |
| 51 | + // So file has to be read using Csv Reader, not IOFactory. |
| 52 | + $this->tempFile = $filename = File::temporaryFilename(); |
| 53 | + $cells = [ |
| 54 | + ['<a href="http://example.com">example</a>', '<div>hello', '3'], |
| 55 | + ['4', '5', '</div>'], |
| 56 | + ]; |
| 57 | + $handle = fopen($filename, 'wb'); |
| 58 | + self::assertNotFalse($handle); |
| 59 | + foreach ($cells as $row) { |
| 60 | + fwrite($handle, "{$row[0]},{$row[1]},{$row[2]}\n"); |
| 61 | + } |
| 62 | + fclose($handle); |
| 63 | + self::assertSame('text/html', mime_content_type($filename)); |
| 64 | + self::assertSame('Html', IOFactory::identify($filename)); |
| 65 | + $reader = new CsvReader(); |
| 66 | + $spreadsheet = $reader->load($filename); |
| 67 | + $sheet = $spreadsheet->getActiveSheet(); |
| 68 | + self::assertSame($cells, $sheet->toArray()); |
| 69 | + $spreadsheet->disconnectWorksheets(); |
| 70 | + } |
| 71 | +} |
0 commit comments