CellsTest.php 7.2 KB

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