Passed
Pull Request — master (#4373)
by Owen
23:56 queued 12:19
created

AlignmentTest::testJustifyLastLine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 7
dl 0
loc 9
rs 10
c 1
b 0
f 1
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Style;
6
7
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
8
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9
use PhpOffice\PhpSpreadsheet\Style\Alignment;
10
use PHPUnit\Framework\TestCase;
11
12
class AlignmentTest extends TestCase
13
{
14
    private ?Spreadsheet $spreadsheet = null;
15
16
    protected function tearDown(): void
17
    {
18
        if ($this->spreadsheet !== null) {
19
            $this->spreadsheet->disconnectWorksheets();
20
            $this->spreadsheet = null;
21
        }
22
    }
23
24
    public function testAlignment(): void
25
    {
26
        $this->spreadsheet = new Spreadsheet();
27
        $sheet = $this->spreadsheet->getActiveSheet();
28
        $cell1 = $sheet->getCell('A1');
29
        $cell1->setValue('Cell1');
30
        $cell1->getStyle()->getAlignment()->setTextRotation(45);
31
        self::assertEquals(45, $cell1->getStyle()->getAlignment()->getTextRotation());
32
        $cell2 = $sheet->getCell('A2');
33
        $cell2->setValue('Cell2');
34
        $cell2->getStyle()->getAlignment()->setTextRotation(-45);
35
        self::assertEquals(-45, $cell2->getStyle()->getAlignment()->getTextRotation());
36
        // special value for stacked
37
        $cell3 = $sheet->getCell('A3');
38
        $cell3->setValue('Cell3');
39
        $cell3->getStyle()->getAlignment()->setTextRotation(255);
40
        self::assertEquals(-165, $cell3->getStyle()->getAlignment()->getTextRotation());
41
    }
42
43
    public function testRotationTooHigh(): void
44
    {
45
        $this->expectException(PhpSpreadsheetException::class);
46
        $this->spreadsheet = new Spreadsheet();
47
        $sheet = $this->spreadsheet->getActiveSheet();
48
        $cell1 = $sheet->getCell('A1');
49
        $cell1->setValue('Cell1');
50
        $cell1->getStyle()->getAlignment()->setTextRotation(91);
51
        self::assertEquals(0, $cell1->getStyle()->getAlignment()->getTextRotation());
52
    }
53
54
    public function testRotationTooLow(): void
55
    {
56
        $this->expectException(PhpSpreadsheetException::class);
57
        $this->spreadsheet = new Spreadsheet();
58
        $sheet = $this->spreadsheet->getActiveSheet();
59
        $cell1 = $sheet->getCell('A1');
60
        $cell1->setValue('Cell1');
61
        $cell1->getStyle()->getAlignment()->setTextRotation(-91);
62
        self::assertEquals(0, $cell1->getStyle()->getAlignment()->getTextRotation());
63
    }
64
65
    public function testHorizontal(): void
66
    {
67
        $this->spreadsheet = new Spreadsheet();
68
        $sheet = $this->spreadsheet->getActiveSheet();
69
        $cell1 = $sheet->getCell('A1');
70
        $cell1->setValue('X');
71
        $cell1->getStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT)->setIndent(1);
72
        self::assertEquals(Alignment::HORIZONTAL_LEFT, $cell1->getStyle()->getAlignment()->getHorizontal());
73
        self::assertEquals(1, $cell1->getStyle()->getAlignment()->getIndent());
74
        $cell2 = $sheet->getCell('A2');
75
        $cell2->setValue('Y');
76
        $cell2->getStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT)->setIndent(2);
77
        self::assertEquals(Alignment::HORIZONTAL_RIGHT, $cell2->getStyle()->getAlignment()->getHorizontal());
78
        self::assertEquals(2, $cell2->getStyle()->getAlignment()->getIndent());
79
        $cell3 = $sheet->getCell('A3');
80
        $cell3->setValue('Z');
81
        // indent not supported for next style - changed to 0
82
        $cell3->getStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER_CONTINUOUS)->setIndent(3);
83
        self::assertEquals(Alignment::HORIZONTAL_CENTER_CONTINUOUS, $cell3->getStyle()->getAlignment()->getHorizontal());
84
        self::assertEquals(0, $cell3->getStyle()->getAlignment()->getIndent());
85
    }
86
87
    public function testJustifyLastLine(): void
88
    {
89
        $this->spreadsheet = new Spreadsheet();
90
        $sheet = $this->spreadsheet->getActiveSheet();
91
        $cell1 = $sheet->getCell('A1');
92
        $cell1->setValue('ABC');
93
        $cell1->getStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_DISTRIBUTED);
94
        $cell1->getStyle()->getAlignment()->setJustifyLastLine(true);
95
        self::assertTrue($cell1->getStyle()->getAlignment()->getJustifyLastLine());
96
    }
97
98
    public function testReadOrder(): void
99
    {
100
        $this->spreadsheet = new Spreadsheet();
101
        $sheet = $this->spreadsheet->getActiveSheet();
102
        $cell1 = $sheet->getCell('A1');
103
        $cell1->setValue('ABC');
104
        $cell1->getStyle()->getAlignment()->setReadOrder(0);
105
        self::assertEquals(0, $cell1->getStyle()->getAlignment()->getReadOrder());
106
        $cell1->getStyle()->getAlignment()->setReadOrder(1);
107
        self::assertEquals(1, $cell1->getStyle()->getAlignment()->getReadOrder());
108
        // following not supported - zero is used instead
109
        $cell1->getStyle()->getAlignment()->setReadOrder(-1);
110
        self::assertEquals(0, $cell1->getStyle()->getAlignment()->getReadOrder());
111
        $cell1->getStyle()->getAlignment()->setReadOrder(2);
112
        self::assertEquals(2, $cell1->getStyle()->getAlignment()->getReadOrder());
113
        // following not supported - zero is used instead
114
        $cell1->getStyle()->getAlignment()->setReadOrder(3);
115
        self::assertEquals(0, $cell1->getStyle()->getAlignment()->getReadOrder());
116
    }
117
}
118