* @package generis\test */ class CsvExporterTest extends TaoPhpUnitTestRunner { public function setUp(): void { parent::setUp(); common_session_SessionManager::startSession(new \common_test_TestUserSession()); } /** * @dataProvider dataProvider * @param SplFileInfo $file instance of sample file * @param array $data data to be exported * @param boolean $columnNames * @throws \common_exception_InvalidArgumentType */ public function testExport(SplFileInfo $file, array $data, $columnNames) { $exporter = new CsvExporter($data); $exportedData = $this->normalizeLineEndings($exporter->export($columnNames)); $sampleData = $this->normalizeLineEndings(file_get_contents($file->getPathname())); $this->assertEquals($exportedData, $sampleData); } /** * @dataProvider dataProvider * @param SplFileInfo $file instance of sample file * @param array $data data to be exported * @param boolean $columnNames * @throws \common_exception_InvalidArgumentType */ public function testGetFileExportResponse(SplFileInfo $file, array $data, $columnNames) { $exporter = new CsvExporter($data); /** @var ResponseInterface $originResponse */ $originResponse = (new Response())->withHeader('X-Old-Header', 'old_header_val'); $response = $exporter->getFileExportResponse($originResponse, $columnNames); $this->assertInstanceOf(ResponseInterface::class, $response); $exportedData = $this->normalizeLineEndings($response->getBody()->getContents()); $sampleData = $this->normalizeLineEndings(file_get_contents($file->getPathname())); $this->assertEquals($exportedData, $sampleData); $this->assertCount(4, $response->getHeaders()); $this->assertEquals('old_header_val', $response->getHeader('X-Old-Header')[0]); $this->assertEquals(strlen($exportedData), $response->getHeader('Content-Length')[0]); $this->assertEquals( 'attachment; fileName="' . CsvExporter::FILE_NAME . '"', $response->getHeader('Content-Disposition')[0] ); $this->assertEquals(CsvExporter::CSV_CONTENT_TYPE, $response->getHeader('Content-Type')[0]); } /** * @return array */ public function dataProvider() { $samplesDir = __DIR__ . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR; $withColNamesFile = new SplFileInfo($samplesDir . 'withColNames.csv'); $withoutColNamesFile = new SplFileInfo($samplesDir . 'withoutColNames.csv'); $csvWithColNames = array_map('str_getcsv', file($withColNamesFile->getPathname())); array_walk($csvWithColNames, function (&$a) use ($csvWithColNames) { $a = array_combine($csvWithColNames[0], $a); }); array_shift($csvWithColNames); // remove column header return [ [ 'file' => $withColNamesFile, 'data' => $csvWithColNames, 'columnNames' => true ], [ 'file' => $withoutColNamesFile, 'data' => array_map('str_getcsv', file($withoutColNamesFile->getPathname())), 'columnNames' => false ], ]; } /** * Convert all line-endings to UNIX format * @param $s * @return mixed */ private function normalizeLineEndings($s) { $s = str_replace("\r\n", "\n", $s); $s = str_replace("\r", "\n", $s); // Don't allow out-of-control blank lines $s = preg_replace("/\n{2,}/", "\n\n", $s); return $s; } }