Completed
Push — master ( ccebf0...15abdf )
by Mark
289:16 queued 224:15
created

CellsTest::testGetHighestColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 15
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Collection;
4
5
use PhpOffice\PhpSpreadsheet\Cell\Cell;
6
use PhpOffice\PhpSpreadsheet\Collection\Cells;
7
use PhpOffice\PhpSpreadsheet\Collection\Memory;
8
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
10
use PHPUnit\Framework\TestCase;
11
12
class CellsTest extends TestCase
13
{
14
    public function testCollectionCell()
15
    {
16
        $spreadsheet = new Spreadsheet();
17
        $sheet = $spreadsheet->getActiveSheet();
18
        $collection = $sheet->getCellCollection();
19
20
        // Assert empty state
21
        self::assertEquals([], $collection->getCoordinates(), 'cell list should be empty');
22
        self::assertEquals([], $collection->getSortedCoordinates(), 'sorted cell list should be empty');
23
        self::assertNull($collection->get('B2'), 'getting non-existing cell must return null');
24
        self::assertFalse($collection->has('B2'), 'non-existing cell should be non-existent');
25
26
        // Add one cell
27
        $cell1 = $sheet->getCell('B2');
28
        self::assertSame($cell1, $collection->add('B2', $cell1), 'adding a cell should return the cell');
29
30
        // Assert cell presence
31
        self::assertEquals(['B2'], $collection->getCoordinates(), 'cell list should contains the cell');
32
        self::assertEquals(['B2'], $collection->getSortedCoordinates(), 'sorted cell list contains the cell');
33
        self::assertSame($cell1, $collection->get('B2'), 'should get exact same object');
34
        self::assertTrue($collection->has('B2'), 'cell should exists');
35
36
        // Add a second cell
37
        $cell2 = $sheet->getCell('A1');
38
        self::assertSame($cell2, $collection->add('A1', $cell2), 'adding a second cell should return the cell');
39
        self::assertEquals(['B2', 'A1'], $collection->getCoordinates(), 'cell list should contains the cell');
40
        self::assertEquals(['A1', 'B2'], $collection->getSortedCoordinates(), 'sorted cell list contains the cell');
41
42
        // Assert collection copy
43
        $sheet2 = $spreadsheet->createSheet();
44
        $collection2 = $collection->cloneCellCollection($sheet2);
45
        self::assertTrue($collection2->has('A1'));
46
        $copiedCell2 = $collection2->get('A1');
47
        self::assertNotSame($cell2, $copiedCell2, 'copied cell should not be the same object any more');
48
        self::assertSame($collection2, $copiedCell2->getParent(), 'copied cell should be owned by the copied collection');
49
        self::assertSame('A1', $copiedCell2->getCoordinate(), 'copied cell should keep attributes');
50
51
        // Assert deletion
52
        $collection->delete('B2');
53
        self::assertFalse($collection->has('B2'), 'cell should have been deleted');
54
        self::assertEquals(['A1'], $collection->getCoordinates(), 'cell list should contains the cell');
55
56
        // Assert update
57
        $cell2 = $sheet->getCell('A1');
58
        self::assertSame($sheet->getCellCollection(), $collection);
59
        self::assertSame($cell2, $collection->update($cell2), 'should update existing cell');
60
61
        $cell3 = $sheet->getCell('C3');
62
        self::assertSame($cell3, $collection->update($cell3), 'should silently add non-existing cell');
63
        self::assertEquals(['A1', 'C3'], $collection->getCoordinates(), 'cell list should contains the cell');
64
    }
65
66
    public function testCacheLastCell()
67
    {
68
        $workbook = new Spreadsheet();
69
        $cells = ['A1', 'A2'];
70
        $sheet = $workbook->getActiveSheet();
71
        $sheet->setCellValue('A1', 1);
72
        $sheet->setCellValue('A2', 2);
73
        self::assertEquals($cells, $sheet->getCoordinates(), 'list should include last added cell');
74
    }
75
76
    public function testCanGetCellAfterAnotherIsDeleted()
77
    {
78
        $workbook = new Spreadsheet();
79
        $sheet = $workbook->getActiveSheet();
80
        $collection = $sheet->getCellCollection();
81
        $sheet->setCellValue('A1', 1);
82
        $sheet->setCellValue('A2', 1);
83
        $collection->delete('A1');
84
        $sheet->setCellValue('A3', 1);
85
        self::assertNotNull($collection->get('A2'), 'should be able to get back the cell even when another cell was deleted while this one was the current one');
86
    }
87
88
    public function testThrowsWhenCellCannotBeRetrievedFromCache()
89
    {
90
        $this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
91
92
        $collection = $this->getMockBuilder(Cells::class)
93
            ->setConstructorArgs([new Worksheet(), new Memory()])
94
            ->setMethods(['has'])
95
            ->getMock();
96
97
        $collection->method('has')
98
            ->willReturn(true);
99
100
        $collection->get('A2');
101
    }
102
103
    public function testThrowsWhenCellCannotBeStoredInCache()
104
    {
105
        $this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
106
107
        $cache = $this->createMock(Memory::class);
108
        $cell = $this->createMock(Cell::class);
109
        $cache->method('set')
110
            ->willReturn(false);
111
112
        $collection = new Cells(new Worksheet(), $cache);
113
114
        $collection->add('A1', $cell);
115
        $collection->add('A2', $cell);
116
    }
117
118
    public function testGetHighestColumn()
119
    {
120
        $workbook = new Spreadsheet();
121
        $sheet = $workbook->getActiveSheet();
122
        $collection = $sheet->getCellCollection();
123
124
        // check for empty sheet
125
        $this->assertEquals('A', $collection->getHighestColumn());
126
        $this->assertEquals('A', $collection->getHighestColumn(1));
127
128
        // set a value and check again
129
        $sheet->getCell('C4')->setValue(1);
130
        $this->assertEquals('C', $collection->getHighestColumn());
131
        $this->assertEquals('A', $collection->getHighestColumn(1));
132
        $this->assertEquals('C', $collection->getHighestColumn(4));
133
    }
134
}
135