summaryrefslogtreecommitdiffstats
path: root/tests/Plugin/CellStyleFixerTest.php
blob: 36a500d0bec4e4df0a3ab54972e3b3e00f161919 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php


namespace PHPExcelFixer\StyleFixer\Plugin;

use PHPExcelFixer\StyleFixer\Util\Sheet;

class CellStyleFixerTest extends BasePluginTest
{
    public function test_execute()
    {
        $output = $this->getMock('\ZipArchive');
        $template = $this->getMock('\ZipArchive');
        $bookUtil = $this->createBookUtil(2, ['Sheet1' => 'xl/worksheets/sheet1.xml', 'Sheet2' => 'xl/worksheets/sheet2.xml']);
        $bookUtil
            ->expects($this->once())
            ->method('makePrintAreaMap')
            ->with($this->isInstanceOf('\ZipArchive'))
            ->will($this->returnValue(['Sheet1' => 'Sheet1!$A$18:$A$18', 'Sheet2' => 'Sheet2!$A$18:$A$18']))
        ;
        $sheetUtil = new Sheet();

        $output
            ->expects($this->exactly(2))
            ->method('getFromName')
            ->with($this->logicalOr('xl/worksheets/sheet1.xml', 'xl/worksheets/sheet2.xml'))
            ->will($this->returnCallback([$this, 'getOutputXml']))
        ;
        $template
            ->expects($this->exactly(2))
            ->method('getFromName')
            ->with($this->logicalOr('xl/worksheets/sheet1.xml', 'xl/worksheets/sheet2.xml'))
            ->will($this->returnCallback([$this, 'getTemplateXml']))
        ;
        $output
            ->expects($this->exactly(2))
            ->method('addFromString')
            ->with($this->logicalOr('xl/worksheets/sheet1.xml', 'xl/worksheets/sheet2.xml'), $this->callback([$this, 'assertXmlHasExpectedContent']))
        ;

        $fixer = new CellStyleFixer($bookUtil, $sheetUtil);
        $fixer->execute($output, $template);
    }

    public function assertXmlHasExpectedContent($xml)
    {
        // 行スタイルが修復されている
        $this->assertContains('<row r="18" s="99">', $xml);
        $this->assertNotContains('<row r="18" s="11">', $xml);
        // セルスタイルが修復されている
        $this->assertContains('<c r="A18" s="33"', $xml);
        $this->assertNotContains('<c r="A18" s="11"', $xml);
        if (strpos($xml, 'B18')) {
            // 印刷範囲外でも値があるセルは修復する
            $this->assertContains('<c r="B18" s="33"', $xml);
            $this->assertNotContains('<c r="B18" s="11"', $xml);
        }
        if (strpos($xml, 'C18')) {
            // 印刷範囲外で値もないセルは修復せずスタイル情報を破棄
            $this->assertContains('<c r="C18"/>', $xml);
            $this->assertNotContains('<c r="C18" s="33"', $xml);
            $this->assertNotContains('<c r="C18" s="11"', $xml);
        }

        return true;
    }
}