Completed
Push — develop ( 962367...96f3f6 )
by Adrien
30:43
created

MergedCells::readMergedCells()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 12
rs 9.4285
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests;
4
5
use PhpOffice\PhpSpreadsheet\IOFactory;
6
use PhpOffice\PhpSpreadsheet\Spreadsheet;
7
use PHPUnit\Framework\TestCase;
8
9
class MergedCells extends TestCase
10
{
11
    public function providerMergedCells()
12
    {
13
        return [
14
//            ['Html'],
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
15
//            ['Xls'],
16
//            ['Xlsx'],
17
            ['Ods'],
18
        ];
19
    }
20
21
    /**
22
     * @dataProvider providerMergedCells
23
     *
24
     * @param string $format
25
     */
26
    public function testMergedCells($format)
27
    {
28
        $filename = tempnam(sys_get_temp_dir(), strtolower($format));
29
        $this->writeMergedCells($filename, $format);
30
        $actual = $this->readMergedCells($filename, $format);
31
        unlink($filename);
1 ignored issue
show
Bug introduced by
It seems like $filename can also be of type false; however, parameter $filename of unlink() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
        unlink(/** @scrutinizer ignore-type */ $filename);
Loading history...
32
33
        self::assertSame(1, $actual, "Format $format failed, could not read 1 merged cell");
34
    }
35
36
    private function writeMergedCells($filename, $format)
37
    {
38
        $spreadsheet = new Spreadsheet();
39
        $spreadsheet->setActiveSheetIndex(0);
40
        $spreadsheet->getActiveSheet()->setCellValue('A1', '1');
41
        $spreadsheet->getActiveSheet()->setCellValue('B1', '2');
42
        $spreadsheet->getActiveSheet()->setCellValue('A2', '33');
43
        $spreadsheet->getActiveSheet()->mergeCells('A2:B2');
44
45
        $writer = IOFactory::createWriter($spreadsheet, $format);
46
47
        $writer->save($filename);
48
    }
49
50
    private function readMergedCells($filename, $format)
51
    {
52
        $reader = IOFactory::createReader($format);
53
        $spreadsheet = $reader->load($filename);
54
        $n = 0;
55
        foreach ($spreadsheet->getWorksheetIterator() as $worksheet) {
56
            foreach ($worksheet->getMergeCells() as $cells) {
57
                ++$n;
58
            }
59
        }
60
61
        return $n;
62
    }
63
}
64