SpreadsheetCoverageTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests;
  3. use PhpOffice\PhpSpreadsheet\Exception as SSException;
  4. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5. use PhpOffice\PhpSpreadsheet\Style\Style;
  6. use PHPUnit\Framework\TestCase;
  7. class SpreadsheetCoverageTest extends TestCase
  8. {
  9. /** @var ?Spreadsheet */
  10. private $spreadsheet;
  11. /** @var ?Spreadsheet */
  12. private $spreadsheet2;
  13. protected function tearDown(): void
  14. {
  15. if ($this->spreadsheet !== null) {
  16. $this->spreadsheet->disconnectWorksheets();
  17. $this->spreadsheet = null;
  18. }
  19. if ($this->spreadsheet2 !== null) {
  20. $this->spreadsheet2->disconnectWorksheets();
  21. $this->spreadsheet2 = null;
  22. }
  23. }
  24. public function testDocumentProperties(): void
  25. {
  26. $this->spreadsheet = new Spreadsheet();
  27. $properties = $this->spreadsheet->getProperties();
  28. $properties->setCreator('Anyone');
  29. $properties->setTitle('Description');
  30. $this->spreadsheet2 = new Spreadsheet();
  31. self::assertNotEquals($properties, $this->spreadsheet2->getProperties());
  32. $properties2 = clone $properties;
  33. $this->spreadsheet2->setProperties($properties2);
  34. self::assertEquals($properties, $this->spreadsheet2->getProperties());
  35. }
  36. public function testDocumentSecurity(): void
  37. {
  38. $this->spreadsheet = new Spreadsheet();
  39. $security = $this->spreadsheet->getSecurity();
  40. $security->setLockRevision(true);
  41. $revisionsPassword = 'revpasswd';
  42. $security->setRevisionsPassword($revisionsPassword);
  43. $this->spreadsheet2 = new Spreadsheet();
  44. self::assertNotEquals($security, $this->spreadsheet2->getSecurity());
  45. $security2 = clone $security;
  46. $this->spreadsheet2->setSecurity($security2);
  47. self::assertEquals($security, $this->spreadsheet2->getSecurity());
  48. }
  49. public function testCellXfCollection(): void
  50. {
  51. $this->spreadsheet = new Spreadsheet();
  52. $sheet = $this->spreadsheet->getActiveSheet();
  53. $sheet->getStyle('A1')->getFont()->setName('font1');
  54. $sheet->getStyle('A2')->getFont()->setName('font2');
  55. $sheet->getStyle('A3')->getFont()->setName('font3');
  56. $sheet->getStyle('B1')->getFont()->setName('font1');
  57. $sheet->getStyle('B2')->getFont()->setName('font2');
  58. $collection = $this->spreadsheet->getCellXfCollection();
  59. self::assertCount(4, $collection);
  60. $font1Style = $collection[1];
  61. self::assertTrue($this->spreadsheet->cellXfExists($font1Style));
  62. self::assertSame('font1', $this->spreadsheet->getCellXfCollection()[1]->getFont()->getName());
  63. self::assertSame('font1', $sheet->getStyle('A1')->getFont()->getName());
  64. self::assertSame('font2', $sheet->getStyle('A2')->getFont()->getName());
  65. self::assertSame('font3', $sheet->getStyle('A3')->getFont()->getName());
  66. self::assertSame('font1', $sheet->getStyle('B1')->getFont()->getName());
  67. self::assertSame('font2', $sheet->getStyle('B2')->getFont()->getName());
  68. $this->spreadsheet->removeCellXfByIndex(1);
  69. self::assertFalse($this->spreadsheet->cellXfExists($font1Style));
  70. self::assertSame('font2', $this->spreadsheet->getCellXfCollection()[1]->getFont()->getName());
  71. self::assertSame('Calibri', $sheet->getStyle('A1')->getFont()->getName());
  72. self::assertSame('font2', $sheet->getStyle('A2')->getFont()->getName());
  73. self::assertSame('font3', $sheet->getStyle('A3')->getFont()->getName());
  74. self::assertSame('Calibri', $sheet->getStyle('B1')->getFont()->getName());
  75. self::assertSame('font2', $sheet->getStyle('B2')->getFont()->getName());
  76. }
  77. public function testInvalidRemoveCellXfByIndex(): void
  78. {
  79. $this->expectException(SSException::class);
  80. $this->expectExceptionMessage('CellXf index is out of bounds.');
  81. $this->spreadsheet = new Spreadsheet();
  82. $sheet = $this->spreadsheet->getActiveSheet();
  83. $sheet->getStyle('A1')->getFont()->setName('font1');
  84. $sheet->getStyle('A2')->getFont()->setName('font2');
  85. $sheet->getStyle('A3')->getFont()->setName('font3');
  86. $sheet->getStyle('B1')->getFont()->setName('font1');
  87. $sheet->getStyle('B2')->getFont()->setName('font2');
  88. $this->spreadsheet->removeCellXfByIndex(5);
  89. }
  90. public function testInvalidRemoveDefaultStyle(): void
  91. {
  92. $this->expectException(SSException::class);
  93. $this->expectExceptionMessage('No default style found for this workbook');
  94. // Removing default style probably should be disallowed.
  95. $this->spreadsheet = new Spreadsheet();
  96. $this->spreadsheet->removeCellXfByIndex(0);
  97. $this->spreadsheet->getDefaultStyle();
  98. }
  99. public function testCellStyleXF(): void
  100. {
  101. $this->spreadsheet = new Spreadsheet();
  102. $collection = $this->spreadsheet->getCellStyleXfCollection();
  103. self::assertCount(1, $collection);
  104. $styleXf = $collection[0];
  105. self::assertSame($styleXf, $this->spreadsheet->getCellStyleXfByIndex(0));
  106. $hash = $styleXf->getHashCode();
  107. self::assertSame($styleXf, $this->spreadsheet->getCellStyleXfByHashCode($hash));
  108. self::assertFalse($this->spreadsheet->getCellStyleXfByHashCode($hash . 'x'));
  109. }
  110. public function testInvalidRemoveCellStyleXfByIndex(): void
  111. {
  112. $this->expectException(SSException::class);
  113. $this->expectExceptionMessage('CellStyleXf index is out of bounds.');
  114. $this->spreadsheet = new Spreadsheet();
  115. $this->spreadsheet->removeCellStyleXfByIndex(5);
  116. }
  117. public function testInvalidFirstSheetIndex(): void
  118. {
  119. $this->expectException(SSException::class);
  120. $this->expectExceptionMessage('First sheet index must be a positive integer.');
  121. $this->spreadsheet = new Spreadsheet();
  122. $this->spreadsheet->setFirstSheetIndex(-1);
  123. }
  124. public function testInvalidVisibility(): void
  125. {
  126. $this->expectException(SSException::class);
  127. $this->expectExceptionMessage('Invalid visibility value.');
  128. $this->spreadsheet = new Spreadsheet();
  129. $this->spreadsheet->setVisibility(Spreadsheet::VISIBILITY_HIDDEN);
  130. self::assertSame(Spreadsheet::VISIBILITY_HIDDEN, $this->spreadsheet->getVisibility());
  131. $this->spreadsheet->setVisibility(null);
  132. self::assertSame(Spreadsheet::VISIBILITY_VISIBLE, $this->spreadsheet->getVisibility());
  133. $this->spreadsheet->setVisibility('badvalue');
  134. }
  135. public function testInvalidTabRatio(): void
  136. {
  137. $this->expectException(SSException::class);
  138. $this->expectExceptionMessage('Tab ratio must be between 0 and 1000.');
  139. $this->spreadsheet = new Spreadsheet();
  140. $this->spreadsheet->setTabRatio(2000);
  141. }
  142. public function testCopy(): void
  143. {
  144. $this->spreadsheet = new Spreadsheet();
  145. $sheet = $this->spreadsheet->getActiveSheet();
  146. $sheet->getStyle('A1')->getFont()->setName('font1');
  147. $sheet->getStyle('A2')->getFont()->setName('font2');
  148. $sheet->getStyle('A3')->getFont()->setName('font3');
  149. $sheet->getStyle('B1')->getFont()->setName('font1');
  150. $sheet->getStyle('B2')->getFont()->setName('font2');
  151. $sheet->getCell('A1')->setValue('this is a1');
  152. $sheet->getCell('A2')->setValue('this is a2');
  153. $sheet->getCell('A3')->setValue('this is a3');
  154. $sheet->getCell('B1')->setValue('this is b1');
  155. $sheet->getCell('B2')->setValue('this is b2');
  156. $this->spreadsheet2 = $this->spreadsheet->copy();
  157. $copysheet = $this->spreadsheet2->getActiveSheet();
  158. $copysheet->getStyle('A2')->getFont()->setName('font12');
  159. $copysheet->getCell('A2')->setValue('this was a2');
  160. self::assertSame('font1', $sheet->getStyle('A1')->getFont()->getName());
  161. self::assertSame('font2', $sheet->getStyle('A2')->getFont()->getName());
  162. self::assertSame('font3', $sheet->getStyle('A3')->getFont()->getName());
  163. self::assertSame('font1', $sheet->getStyle('B1')->getFont()->getName());
  164. self::assertSame('font2', $sheet->getStyle('B2')->getFont()->getName());
  165. self::assertSame('this is a1', $sheet->getCell('A1')->getValue());
  166. self::assertSame('this is a2', $sheet->getCell('A2')->getValue());
  167. self::assertSame('this is a3', $sheet->getCell('A3')->getValue());
  168. self::assertSame('this is b1', $sheet->getCell('B1')->getValue());
  169. self::assertSame('this is b2', $sheet->getCell('B2')->getValue());
  170. self::assertSame('font1', $copysheet->getStyle('A1')->getFont()->getName());
  171. self::assertSame('font12', $copysheet->getStyle('A2')->getFont()->getName());
  172. self::assertSame('font3', $copysheet->getStyle('A3')->getFont()->getName());
  173. self::assertSame('font1', $copysheet->getStyle('B1')->getFont()->getName());
  174. self::assertSame('font2', $copysheet->getStyle('B2')->getFont()->getName());
  175. self::assertSame('this is a1', $copysheet->getCell('A1')->getValue());
  176. self::assertSame('this was a2', $copysheet->getCell('A2')->getValue());
  177. self::assertSame('this is a3', $copysheet->getCell('A3')->getValue());
  178. self::assertSame('this is b1', $copysheet->getCell('B1')->getValue());
  179. self::assertSame('this is b2', $copysheet->getCell('B2')->getValue());
  180. }
  181. public function testClone(): void
  182. {
  183. $this->expectException(SSException::class);
  184. $this->expectExceptionMessage('Do not use clone on spreadsheet. Use spreadsheet->copy() instead.');
  185. $this->spreadsheet = new Spreadsheet();
  186. $this->spreadsheet2 = clone $this->spreadsheet;
  187. }
  188. }