Conditions | 4 |
Paths | 5 |
Total Lines | 86 |
Code Lines | 57 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
16 | public function testLoadSaveXlsxWithUnparsedData() |
||
17 | { |
||
18 | $sampleFilename = './data/Writer/XLSX/form_pass_print.xlsm'; |
||
19 | $resultFilename = tempnam(File::sysGetTempDir(), 'phpspreadsheet-test'); |
||
20 | Settings::setLibXmlLoaderOptions(null); // reset to default options |
||
21 | $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx(); |
||
22 | $excel = $reader->load($sampleFilename); |
||
23 | |||
24 | $excel->getSheet(1)->setCellValue('B1', '222'); |
||
25 | |||
26 | $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($excel); |
||
27 | $writer->save($resultFilename); |
||
28 | self::assertFileExists($resultFilename); |
||
29 | |||
30 | $resultZip = new ZipArchive(); |
||
31 | $resultZip->open($resultFilename); |
||
32 | $resultContentTypesRaw = $resultZip->getFromName('[Content_Types].xml'); |
||
33 | $resultControlPropRaw = $resultZip->getFromName('xl/ctrlProps/ctrlProp1.xml'); |
||
34 | $resultDrawingRaw = $resultZip->getFromName('xl/drawings/drawing1.xml'); |
||
35 | $resultVmlDrawingRaw = $resultZip->getFromName('xl/drawings/vmlDrawing1.vml'); |
||
36 | $resultPrinterSettingsRaw = $resultZip->getFromName('xl/printerSettings/printerSettings1.bin'); |
||
37 | $resultVbaProjectRaw = $resultZip->getFromName('xl/vbaProject.bin'); |
||
38 | $resultWorkbookRaw = $resultZip->getFromName('xl/workbook.xml'); |
||
39 | $resultSheet1RelsRaw = $resultZip->getFromName('xl/worksheets/_rels/sheet1.xml.rels'); |
||
40 | $resultSheet1Raw = $resultZip->getFromName('xl/worksheets/sheet1.xml'); |
||
41 | $resultSheet2Raw = $resultZip->getFromName('xl/worksheets/sheet2.xml'); |
||
42 | if (false === $resultZip->close()) { |
||
43 | throw new Exception("Could not close zip file \"{$resultFilename}\"."); |
||
44 | } |
||
45 | unlink($resultFilename); |
||
46 | |||
47 | // [Content_Types].xml |
||
48 | $this->assertTrue(strpos($resultContentTypesRaw, 'application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings') > 0, 'Content type for printerSettings not found!'); |
||
49 | $this->assertTrue(strpos($resultContentTypesRaw, 'application/vnd.ms-office.vbaProject') > 0, 'Content type for VbaProject not found!'); |
||
50 | $this->assertTrue(strpos($resultContentTypesRaw, 'application/vnd.ms-excel.controlproperties+xml') > 0, 'Content type for ctrlProp not found!'); |
||
51 | |||
52 | // xl/ctrlProps/ctrlProp1.xml |
||
53 | $this->assertTrue(!empty($resultControlPropRaw), 'ctrlProp not found!'); |
||
54 | |||
55 | // xl/drawings/drawing1.xml |
||
56 | $this->assertTrue(strpos($resultDrawingRaw, '<mc:AlternateContent') > 0, 'AlternateContent at drawing.xml not found!'); |
||
57 | |||
58 | // xl/drawings/vmlDrawing1.vml |
||
59 | $this->assertTrue(!empty($resultVmlDrawingRaw), 'vmlDrawing not found!'); |
||
60 | |||
61 | // xl/printerSettings/printerSettings1.bin |
||
62 | $this->assertTrue(!empty($resultPrinterSettingsRaw), 'printerSettings.bin not found!'); |
||
63 | |||
64 | // xl/vbaProject.bin |
||
65 | $this->assertTrue(!empty($resultVbaProjectRaw), 'vbaProject.bin not found!'); |
||
66 | |||
67 | // xl/workbook.xml |
||
68 | $xmlWorkbook = simplexml_load_string($resultWorkbookRaw, 'SimpleXMLElement', Settings::getLibXmlLoaderOptions()); |
||
69 | if (!$xmlWorkbook->workbookProtection) { |
||
70 | $this->fail('workbook.xml/workbookProtection not found!'); |
||
71 | } else { |
||
72 | $this->assertEquals($xmlWorkbook->workbookProtection['workbookPassword'], 'CBEB', 'workbook.xml/workbookProtection[workbookPassword] is wrong!'); |
||
73 | $this->assertEquals($xmlWorkbook->workbookProtection['lockStructure'], 'true', 'workbook.xml/workbookProtection[lockStructure] is wrong!'); |
||
74 | |||
75 | $this->assertEquals($xmlWorkbook->sheets->sheet[0]['state'], '', 'workbook.xml/sheets/sheet[0][state] is wrong!'); |
||
76 | $this->assertEquals($xmlWorkbook->sheets->sheet[1]['state'], 'hidden', 'workbook.xml/sheets/sheet[1][state] is wrong!'); |
||
77 | } |
||
78 | unset($xmlWorkbook); |
||
79 | |||
80 | // xl/worksheets/_rels/sheet1.xml.rels |
||
81 | $this->assertTrue(strpos($resultSheet1RelsRaw, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/printerSettings') > 0, 'Sheet relation with printerSettings not found!'); |
||
82 | $this->assertTrue(strpos($resultSheet1RelsRaw, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing') > 0, 'Sheet relation with vmlDrawing not found!'); |
||
83 | $this->assertTrue(strpos($resultSheet1RelsRaw, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/ctrlProp') > 0, 'Sheet relation with ctrlProp not found!'); |
||
84 | |||
85 | // xl/worksheets/sheet1.xml |
||
86 | $this->assertTrue(strpos($resultSheet1Raw, '<mc:AlternateContent') > 0, 'AlternateContent at sheet1.xml not found!'); |
||
87 | $xmlWorksheet = simplexml_load_string($resultSheet1Raw, 'SimpleXMLElement', Settings::getLibXmlLoaderOptions()); |
||
88 | $pageSetupAttributes = $xmlWorksheet->pageSetup->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships'); |
||
89 | $this->assertTrue(!empty($pageSetupAttributes['id']), 'sheet1.xml/pageSetup[r:id] not found!'); |
||
90 | if (!$xmlWorksheet->sheetProtection) { |
||
91 | $this->fail('sheet1.xml/sheetProtection not found!'); |
||
92 | } else { |
||
93 | $this->assertEquals($xmlWorksheet->sheetProtection['password'], 'CBEB', 'sheet1.xml/sheetProtection[password] is wrong!'); |
||
94 | $this->assertEquals($xmlWorksheet->sheetProtection['sheet'], 'true', 'sheet1.xml/sheetProtection[sheet] is wrong!'); |
||
95 | $this->assertEquals($xmlWorksheet->sheetProtection['objects'], 'true', 'sheet1.xml/sheetProtection[objects] is wrong!'); |
||
96 | $this->assertEquals($xmlWorksheet->sheetProtection['scenarios'], 'true', 'sheet1.xml/sheetProtection[scenarios] is wrong!'); |
||
97 | } |
||
98 | unset($xmlWorksheet); |
||
99 | |||
100 | // xl/worksheets/sheet2.xml |
||
101 | $this->assertTrue(!empty($resultSheet2Raw), 'sheet2.xml not found!'); |
||
102 | } |
||
104 |