writeDataToMulitpleSheetsAndReturnSheets('test_get_sheet_index.xlsx'); $this->assertEquals(2, count($sheets), '2 sheets should have been created'); $this->assertEquals(0, $sheets[0]->getIndex(), 'The first sheet should be index 0'); $this->assertEquals(1, $sheets[1]->getIndex(), 'The second sheet should be index 1'); } /** * @return void */ public function testGetSheetName() { $sheets = $this->writeDataToMulitpleSheetsAndReturnSheets('test_get_sheet_name.xlsx'); $this->assertEquals(2, count($sheets), '2 sheets should have been created'); $this->assertEquals('Sheet1', $sheets[0]->getName(), 'Invalid name for the first sheet'); $this->assertEquals('Sheet2', $sheets[1]->getName(), 'Invalid name for the second sheet'); } /** * @return void */ public function testSetSheetNameShouldCreateSheetWithCustomName() { $fileName = 'test_set_name_should_create_sheet_with_custom_name.xlsx'; $customSheetName = 'CustomName'; $this->writeDataAndReturnSheetWithCustomName($fileName, $customSheetName); $this->assertSheetNameEquals($customSheetName, $fileName, "The sheet name should have been changed to '$customSheetName'"); } /** * @expectedException \Box\Spout\Writer\Exception\InvalidSheetNameException * @return void */ public function testSetSheetNameShouldThrowWhenNameIsAlreadyUsed() { $fileName = 'test_set_name_with_non_unique_name.xlsx'; $this->createGeneratedFolderIfNeeded($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName); /** @var \Box\Spout\Writer\XLSX\Writer $writer */ $writer = WriterFactory::create(Type::XLSX); $writer->openToFile($resourcePath); $customSheetName = 'Sheet name'; $sheet = $writer->getCurrentSheet(); $sheet->setName($customSheetName); $writer->addNewSheetAndMakeItCurrent(); $sheet = $writer->getCurrentSheet(); $sheet->setName($customSheetName); } /** * @param string $fileName * @param string $sheetName * @return Sheet */ private function writeDataAndReturnSheetWithCustomName($fileName, $sheetName) { $this->createGeneratedFolderIfNeeded($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName); /** @var \Box\Spout\Writer\XLSX\Writer $writer */ $writer = WriterFactory::create(Type::XLSX); $writer->openToFile($resourcePath); $sheet = $writer->getCurrentSheet(); $sheet->setName($sheetName); $row = EntityFactory::createRow([ EntityFactory::createCell('xlsx--11'), EntityFactory::createCell('xlsx--12'), ]); $writer->addRow($row); $writer->close(); return $sheet; } /** * @param string $fileName * @return Sheet[] */ private function writeDataToMulitpleSheetsAndReturnSheets($fileName) { $this->createGeneratedFolderIfNeeded($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName); /** @var \Box\Spout\Writer\XLSX\Writer $writer */ $writer = WriterFactory::create(Type::XLSX); $writer->openToFile($resourcePath); $row = EntityFactory::createRow([ EntityFactory::createCell('xlsx--sheet1--11'), EntityFactory::createCell('xlsx--sheet1--12'), ]); $writer->addRow($row); $writer->addNewSheetAndMakeItCurrent(); $row = EntityFactory::createRow([ EntityFactory::createCell('xlsx--sheet2--11'), EntityFactory::createCell('xlsx--sheet2--12'), EntityFactory::createCell('xlsx--sheet2--13'), ]); $writer->addRow($row); $writer->close(); return $writer->getSheets(); } /** * @param string $expectedName * @param string $fileName * @param string $message * @return void */ private function assertSheetNameEquals($expectedName, $fileName, $message = '') { $resourcePath = $this->getGeneratedResourcePath($fileName); $pathToWorkbookFile = $resourcePath . '#xl/workbook.xml'; $xmlContents = file_get_contents('zip://' . $pathToWorkbookFile); $this->assertContains("