ColumnTest.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Worksheet\Table;
  3. use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
  4. use PhpOffice\PhpSpreadsheet\Worksheet\Table;
  5. use PhpOffice\PhpSpreadsheet\Worksheet\Table\Column;
  6. class ColumnTest extends SetupTeardown
  7. {
  8. protected function initTable(): Table
  9. {
  10. $sheet = $this->getSheet();
  11. $sheet->getCell('G1')->setValue('Heading');
  12. $sheet->getCell('G2')->setValue(2);
  13. $sheet->getCell('G3')->setValue(3);
  14. $sheet->getCell('G4')->setValue(4);
  15. $sheet->getCell('H1')->setValue('Heading2');
  16. $sheet->getCell('H2')->setValue(1);
  17. $sheet->getCell('H3')->setValue(2);
  18. $sheet->getCell('H4')->setValue(3);
  19. $this->maxRow = $maxRow = 4;
  20. $table = new Table();
  21. $table->setRange("G1:H$maxRow");
  22. return $table;
  23. }
  24. public function testVariousGets(): void
  25. {
  26. $table = $this->initTable();
  27. $column = $table->getColumn('H');
  28. $result = $column->getColumnIndex();
  29. self::assertEquals('H', $result);
  30. }
  31. public function testGetBadColumnIndex(): void
  32. {
  33. $this->expectException(PhpSpreadsheetException::class);
  34. $this->expectExceptionMessage('Column is outside of current table range.');
  35. $table = $this->initTable();
  36. $table->getColumn('B');
  37. }
  38. public function testSetColumnIndex(): void
  39. {
  40. $table = $this->initTable();
  41. $column = $table->getColumn('H');
  42. $column->setShowFilterButton(false);
  43. $expectedResult = 'G';
  44. $result = $column->setColumnIndex($expectedResult);
  45. self::assertInstanceOf(Column::class, $result);
  46. $result = $result->getColumnIndex();
  47. self::assertEquals($expectedResult, $result);
  48. }
  49. public function testVariousSets(): void
  50. {
  51. $table = $this->initTable();
  52. $column = $table->getColumn('H');
  53. $result = $column->setShowFilterButton(false);
  54. self::assertInstanceOf(Column::class, $result);
  55. self::assertFalse($column->getShowFilterButton());
  56. $label = 'Total';
  57. $result = $column->setTotalsRowLabel($label);
  58. self::assertInstanceOf(Column::class, $result);
  59. self::assertEquals($label, $column->getTotalsRowLabel());
  60. $function = 'sum';
  61. $result = $column->setTotalsRowFunction($function);
  62. self::assertInstanceOf(Column::class, $result);
  63. self::assertEquals($function, $column->getTotalsRowFunction());
  64. $formula = '=SUM(Sales_Data[[#This Row],[Q1]:[Q4]])';
  65. $result = $column->setColumnFormula($formula);
  66. self::assertInstanceOf(Column::class, $result);
  67. self::assertEquals($formula, $column->getColumnFormula());
  68. }
  69. public function testTable(): void
  70. {
  71. $table = $this->initTable();
  72. $column = new Column('H');
  73. $column->setTable($table);
  74. self::assertEquals($table, $column->getTable());
  75. }
  76. }