CommentTest.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests;
  3. use PhpOffice\PhpSpreadsheet\Comment;
  4. use PhpOffice\PhpSpreadsheet\RichText\RichText;
  5. use PhpOffice\PhpSpreadsheet\RichText\TextElement;
  6. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  7. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  8. use PhpOffice\PhpSpreadsheet\Style\Color;
  9. use PHPUnit\Framework\TestCase;
  10. class CommentTest extends TestCase
  11. {
  12. public function testCreateComment(): void
  13. {
  14. $comment = new Comment();
  15. self::assertEquals('Author', $comment->getAuthor());
  16. self::assertEquals('96pt', $comment->getWidth());
  17. self::assertEquals('59.25pt', $comment->getMarginLeft());
  18. self::assertEquals('1.5pt', $comment->getMarginTop());
  19. self::assertEquals('55.5pt', $comment->getHeight());
  20. self::assertInstanceOf(Color::class, $comment->getFillColor());
  21. self::assertEquals('FFFFFFE1', $comment->getFillColor()->getARGB());
  22. self::assertInstanceOf(RichText::class, $comment->getText());
  23. self::assertEquals(Alignment::HORIZONTAL_GENERAL, $comment->getAlignment());
  24. self::assertFalse($comment->getVisible());
  25. }
  26. public function testSetAuthor(): void
  27. {
  28. $comment = new Comment();
  29. $comment->setAuthor('Mark Baker');
  30. self::assertEquals('Mark Baker', $comment->getAuthor());
  31. }
  32. public function testSetMarginLeft(): void
  33. {
  34. $comment = new Comment();
  35. $comment->setMarginLeft('20pt');
  36. self::assertEquals('20pt', $comment->getMarginLeft());
  37. }
  38. public function testSetMarginTop(): void
  39. {
  40. $comment = new Comment();
  41. $comment->setMarginTop('2.5pt');
  42. self::assertEquals('2.5pt', $comment->getMarginTop());
  43. }
  44. public function testSetWidth(): void
  45. {
  46. $comment = new Comment();
  47. $comment->setWidth('120pt');
  48. self::assertEquals('120pt', $comment->getWidth());
  49. }
  50. public function testSetHeight(): void
  51. {
  52. $comment = new Comment();
  53. $comment->setHeight('60px');
  54. self::assertEquals('60px', $comment->getHeight());
  55. }
  56. public function testSetFillColor(): void
  57. {
  58. $comment = new Comment();
  59. $comment->setFillColor(new Color('RED'));
  60. self::assertEquals(Color::COLOR_RED, $comment->getFillColor()->getARGB());
  61. }
  62. public function testSetAlignment(): void
  63. {
  64. $comment = new Comment();
  65. $comment->setAlignment(Alignment::HORIZONTAL_CENTER);
  66. self::assertEquals(Alignment::HORIZONTAL_CENTER, $comment->getAlignment());
  67. }
  68. public function testSetText(): void
  69. {
  70. $comment = new Comment();
  71. $test = new RichText();
  72. $test->addText(new TextElement('This is a test comment'));
  73. $comment->setText($test);
  74. self::assertEquals('This is a test comment', (string) $comment);
  75. }
  76. public function testRemoveComment(): void
  77. {
  78. $spreadsheet = new Spreadsheet();
  79. $sheet = $spreadsheet->getActiveSheet();
  80. $sheet->getComment('A2')->getText()->createText('Comment to delete');
  81. self::assertArrayHasKey('A2', $sheet->getComments());
  82. $sheet->removeComment('A2');
  83. self::assertEmpty($sheet->getComments());
  84. }
  85. }