Completed
Push — master ( ce1e83...7d4dc7 )
by Adrien
37:08 queued 29:31
created

WorksheetTest::testSetTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests;
4
5
use PhpOffice\PhpSpreadsheet\Spreadsheet;
6
use PhpOffice\PhpSpreadsheet\Worksheet;
7
use PHPUnit_Framework_TestCase;
8
9
class WorksheetTest extends PHPUnit_Framework_TestCase
10
{
11
    public function testSetTitle()
12
    {
13
        $testTitle = str_repeat('a', 31);
14
15
        $worksheet = new Worksheet();
16
        $worksheet->setTitle($testTitle);
17
        $this->assertSame($testTitle, $worksheet->getTitle());
18
    }
19
20 View Code Duplication
    public function setTitleInvalidProvider()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
    {
22
        return [
23
            [str_repeat('a', 32), 'Maximum 31 characters allowed in sheet title.'],
24
            ['invalid*title', 'Invalid character found in sheet title'],
25
        ];
26
    }
27
28
    /**
29
     * @param string $title
30
     * @param string $expectMessage
31
     * @dataProvider setTitleInvalidProvider
32
     */
33 View Code Duplication
    public function testSetTitleInvalid($title, $expectMessage)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        // First, test setting title with validation disabled -- should be successful
36
        $worksheet = new Worksheet();
37
        $worksheet->setTitle($title, true, false);
38
39
        // Next, test again with validation enabled -- this time we should fail
40
        $worksheet = new Worksheet();
41
        $this->expectException(\Exception::class);
42
        $this->expectExceptionMessage($expectMessage);
43
        $worksheet->setTitle($title);
44
    }
45
46 View Code Duplication
    public function testSetTitleDuplicate()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        // Create a Spreadsheet with three Worksheets (the first is created automatically)
49
        $spreadsheet = new Spreadsheet();
50
        $spreadsheet->createSheet();
51
        $spreadsheet->createSheet();
52
53
        // Set unique title -- should be unchanged
54
        $sheet = $spreadsheet->getSheet(0);
55
        $sheet->setTitle('Test Title');
56
        $this->assertSame('Test Title', $sheet->getTitle());
57
58
        // Set duplicate title -- should have numeric suffix appended
59
        $sheet = $spreadsheet->getSheet(1);
60
        $sheet->setTitle('Test Title');
61
        $this->assertSame('Test Title 1', $sheet->getTitle());
62
63
        // Set duplicate title with validation disabled -- should be unchanged
64
        $sheet = $spreadsheet->getSheet(2);
65
        $sheet->setTitle('Test Title', true, false);
66
        $this->assertSame('Test Title', $sheet->getTitle());
67
    }
68
69
    public function testSetCodeName()
70
    {
71
        $testCodeName = str_repeat('a', 31);
72
73
        $worksheet = new Worksheet();
74
        $worksheet->setCodeName($testCodeName);
75
        $this->assertSame($testCodeName, $worksheet->getCodeName());
76
    }
77
78 View Code Duplication
    public function setCodeNameInvalidProvider()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        return [
81
            [str_repeat('a', 32), 'Maximum 31 characters allowed in sheet code name.'],
82
            ['invalid*code*name', 'Invalid character found in sheet code name'],
83
        ];
84
    }
85
86
    /**
87
     * @param string $codeName
88
     * @param string $expectMessage
89
     * @dataProvider setCodeNameInvalidProvider
90
     */
91 View Code Duplication
    public function testSetCodeNameInvalid($codeName, $expectMessage)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
    {
93
        // First, test setting code name with validation disabled -- should be successful
94
        $worksheet = new Worksheet();
95
        $worksheet->setCodeName($codeName, false);
96
97
        // Next, test again with validation enabled -- this time we should fail
98
        $worksheet = new Worksheet();
99
        $this->expectException(\Exception::class);
100
        $this->expectExceptionMessage($expectMessage);
101
        $worksheet->setCodeName($codeName);
102
    }
103
104 View Code Duplication
    public function testSetCodeNameDuplicate()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        // Create a Spreadsheet with three Worksheets (the first is created automatically)
107
        $spreadsheet = new Spreadsheet();
108
        $spreadsheet->createSheet();
109
        $spreadsheet->createSheet();
110
111
        // Set unique code name -- should be massaged to Snake_Case
112
        $sheet = $spreadsheet->getSheet(0);
113
        $sheet->setCodeName('Test Code Name');
114
        $this->assertSame('Test_Code_Name', $sheet->getCodeName());
115
116
        // Set duplicate code name -- should be massaged and have numeric suffix appended
117
        $sheet = $spreadsheet->getSheet(1);
118
        $sheet->setCodeName('Test Code Name');
119
        $this->assertSame('Test_Code_Name_1', $sheet->getCodeName());
120
121
        // Set duplicate code name with validation disabled -- should be unchanged, and unmassaged
122
        $sheet = $spreadsheet->getSheet(2);
123
        $sheet->setCodeName('Test Code Name', false);
124
        $this->assertSame('Test Code Name', $sheet->getCodeName());
125
    }
126
}
127