AlignmentTest.php 4.6 KB

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