Completed
Push — develop ( 3560f1...5fce89 )
by Adrien
21:58 queued 15:06
created

ContentTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Writer\Ods\Content;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
6
use PhpOffice\PhpSpreadsheet\Cell\DataType;
7
use PhpOffice\PhpSpreadsheet\Shared\Date;
8
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9
use PhpOffice\PhpSpreadsheet\Style\Color;
10
use PhpOffice\PhpSpreadsheet\Style\Fill;
11
use PhpOffice\PhpSpreadsheet\Style\Font;
12
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
13
use PhpOffice\PhpSpreadsheet\Writer\Ods;
14
use PhpOffice\PhpSpreadsheet\Writer\Ods\Content;
15
16
class ContentTest extends \PHPUnit_Framework_TestCase
17
{
18
    public $samplesPath = __DIR__ . '/../../../data/Writer/Ods';
19
20
    /**
21
     * @var string
22
     */
23
    protected $compatibilityMode;
24
25
    public function setUp()
26
    {
27
        parent::setUp();
28
29
        $this->compatibilityMode = Functions::getCompatibilityMode();
30
        Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
31
    }
32
33
    protected function tearDown()
34
    {
35
        parent::tearDown();
36
        Functions::setCompatibilityMode($this->compatibilityMode);
37
    }
38
39
    public function testWriteEmptySpreadsheet()
40
    {
41
        $content = new Content();
42
        $content->setParentWriter(new Ods(new Spreadsheet()));
43
44
        $xml = $content->write();
45
46
        $this->assertXmlStringEqualsXmlFile($this->samplesPath . '/content-empty.xml', $xml);
47
    }
48
49
    public function testWriteSpreadsheet()
50
    {
51
        $workbook = new Spreadsheet();
52
53
        // Worksheet 1
54
        $worksheet1 = $workbook->getActiveSheet();
55
        $worksheet1->setCellValue('A1', 1); // Number
56
        $worksheet1->setCellValue('B1', 12345.6789); // Number
57
        $worksheet1->setCellValue('C1', '1'); // Number without cast
58
        $worksheet1->setCellValueExplicit('D1', '01234', DataType::TYPE_STRING); // Number casted to string
59
        $worksheet1->setCellValue('E1', 'Lorem ipsum'); // String
60
61
        $worksheet1->setCellValue('A2', true); // Boolean
62
        $worksheet1->setCellValue('B2', false); // Boolean
63
        $worksheet1->setCellValueExplicit(
64
            'C2',
65
            '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))',
66
            DataType::TYPE_FORMULA
67
        ); // Formula
68
69
        $worksheet1->setCellValue('D2', Date::PHPToExcel(1488635026)); // Date
70
        $worksheet1->getStyle('D2')
71
            ->getNumberFormat()
72
            ->setFormatCode(NumberFormat::FORMAT_DATE_DATETIME);
73
74
        // Styles
75
        $worksheet1->getStyle('A1')->getFont()->setBold(true);
76
        $worksheet1->getStyle('B1')->getFont()->setItalic(true);
77
        $worksheet1->getStyle('C1')->getFont()->setName('Courier');
78
        $worksheet1->getStyle('C1')->getFont()->setSize(14);
79
        $worksheet1->getStyle('C1')->getFont()->setColor(new Color(Color::COLOR_BLUE));
80
81
        $worksheet1->getStyle('C1')->getFill()->setFillType(Fill::FILL_SOLID);
82
        $worksheet1->getStyle('C1')->getFill()->setStartColor(new Color(Color::COLOR_RED));
83
84
        $worksheet1->getStyle('C1')->getFont()->setUnderline(Font::UNDERLINE_SINGLE);
85
        $worksheet1->getStyle('C2')->getFont()->setUnderline(Font::UNDERLINE_DOUBLE);
86
        $worksheet1->getStyle('D2')->getFont()->setUnderline(Font::UNDERLINE_NONE);
87
88
        // Worksheet 2
89
        $worksheet2 = $workbook->createSheet();
90
        $worksheet2->setTitle('New Worksheet');
91
        $worksheet2->setCellValue('A1', 2);
92
93
        // Write
94
        $content = new Content();
95
        $content->setParentWriter(new Ods($workbook));
96
97
        $xml = $content->write();
98
99
        $this->assertXmlStringEqualsXmlFile($this->samplesPath . '/content-with-data.xml', $xml);
100
    }
101
}
102