Completed
Pull Request — master (#3)
by Sankar
02:31
created

ExcelWriterTest::testWriteItemWithoutSheetTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace Port\Excel\Tests;
4
5
use Port\Excel\ExcelWriter;
6
use PhpOffice\PhpSpreadsheet\IOFactory;
7
use PHPUnit\Framework\TestCase;
8
9
class ExcelWriterTest extends TestCase
10
{
11
    public function setUp()
12
    {
13
        if (!extension_loaded('zip')) {
14
            $this->markTestSkipped();
15
        }
16
    }
17
18
    public function testWriteItemAppendWithSheetTitle()
19
    {
20
        $file = tempnam(sys_get_temp_dir(), null);
21
22
        $writer = new ExcelWriter(new \SplFileObject($file, 'w'), 'Sheet 1');
23
24
        $writer->prepare();
25
        $writer->writeItem(array('first', 'last'));
26
27
        $writer->writeItem(array(
28
            'first' => 'James',
29
            'last'  => 'Bond'
30
        ));
31
32
        $writer->writeItem(array(
33
            'first' => '',
34
            'last'  => 'Dr. No'
35
        ));
36
37
        $writer->finish();
38
39
        // Open file with append mode ('a') to add a sheet
40
        $writer = new ExcelWriter(new \SplFileObject($file, 'a'), 'Sheet 2');
41
42
        $writer->prepare();
43
44
        $writer->writeItem(array('first', 'last'));
45
46
        $writer->writeItem(array(
47
            'first' => 'Miss',
48
            'last'  => 'Moneypenny'
49
        ));
50
51
        $writer->finish();
52
53
        $excel = IOFactory::load($file);
54
55
        $this->assertTrue($excel->sheetNameExists('Sheet 1'));
56
        $this->assertEquals(3, $excel->getSheetByName('Sheet 1')->getHighestRow());
57
58
        $this->assertTrue($excel->sheetNameExists('Sheet 2'));
59
        $this->assertEquals(2, $excel->getSheetByName('Sheet 2')->getHighestRow());
60
    }
61
62
    /**
63
     * Test that column names not prepended to first row if ExcelWriter's 4-th
64
     * parameter not given
65
     *
66
     * @author  Igor Mukhin <[email protected]>
67
     */
68
    public function testHeaderNotPrependedByDefault()
69
    {
70
        $file = tempnam(sys_get_temp_dir(), null);
71
72
        $writer = new ExcelWriter(new \SplFileObject($file, 'w'), null, 'Xlsx');
73
        $writer->prepare();
74
        $writer->writeItem(array(
75
            'col 1 name'=>'col 1 value',
76
            'col 2 name'=>'col 2 value',
77
            'col 3 name'=>'col 3 value'
78
        ));
79
        $writer->finish();
80
81
        $excel = IOFactory::load($file);
82
        $sheet = $excel->getActiveSheet()->toArray();
83
84
        # Values should be at first line
85
        $this->assertEquals(array('col 1 value', 'col 2 value', 'col 3 value'), $sheet[0]);
86
    }
87
88
89
    /**
90
     * Test that column names prepended at first row
91
     * and values have been written at second line
92
     * if ExcelWriter's 4-th parameter set to true
93
     *
94
     * @author  Igor Mukhin <[email protected]>
95
     */
96
    public function testHeaderPrependedWhenOptionSetToTrue()
97
    {
98
        $file = tempnam(sys_get_temp_dir(), null);
99
100
        $writer = new ExcelWriter(new \SplFileObject($file, 'w'), null, 'Xlsx', true);
101
        $writer->prepare();
102
        $writer->writeItem(array(
103
            'col 1 name'=>'col 1 value',
104
            'col 2 name'=>'col 2 value',
105
            'col 3 name'=>'col 3 value'
106
        ));
107
        $writer->finish();
108
109
        $excel = IOFactory::load($file);
110
        $sheet = $excel->getActiveSheet()->toArray();
111
112
        # Check column names at first line
113
        $this->assertEquals(array('col 1 name', 'col 2 name', 'col 3 name'), $sheet[0]);
114
115
        # Check values at second line
116
        $this->assertEquals(array('col 1 value', 'col 2 value', 'col 3 value'), $sheet[1]);
117
    }
118
}
119