WithPropertiesTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Maatwebsite\Excel\Tests\Concerns;
  3. use Maatwebsite\Excel\Concerns\Exportable;
  4. use Maatwebsite\Excel\Concerns\WithProperties;
  5. use Maatwebsite\Excel\Tests\TestCase;
  6. class WithPropertiesTest extends TestCase
  7. {
  8. /**
  9. * @test
  10. */
  11. public function can_set_custom_document_properties()
  12. {
  13. $export = new class implements WithProperties
  14. {
  15. use Exportable;
  16. public function properties(): array
  17. {
  18. return [
  19. 'creator' => 'A',
  20. 'lastModifiedBy' => 'B',
  21. 'title' => 'C',
  22. 'description' => 'D',
  23. 'subject' => 'E',
  24. 'keywords' => 'F',
  25. 'category' => 'G',
  26. 'manager' => 'H',
  27. 'company' => 'I',
  28. ];
  29. }
  30. };
  31. $export->store('with-properties.xlsx');
  32. $spreadsheet = $this->read(__DIR__ . '/../Data/Disks/Local/with-properties.xlsx', 'Xlsx');
  33. $props = $spreadsheet->getProperties();
  34. $this->assertEquals('A', $props->getCreator());
  35. $this->assertEquals('B', $props->getLastModifiedBy());
  36. $this->assertEquals('C', $props->getTitle());
  37. $this->assertEquals('D', $props->getDescription());
  38. $this->assertEquals('E', $props->getSubject());
  39. $this->assertEquals('F', $props->getKeywords());
  40. $this->assertEquals('G', $props->getCategory());
  41. $this->assertEquals('H', $props->getManager());
  42. $this->assertEquals('I', $props->getCompany());
  43. }
  44. /**
  45. * @test
  46. */
  47. public function it_merges_with_default_properties()
  48. {
  49. config()->set('excel.exports.properties.title', 'Default Title');
  50. config()->set('excel.exports.properties.description', 'Default Description');
  51. $export = new class implements WithProperties
  52. {
  53. use Exportable;
  54. public function properties(): array
  55. {
  56. return [
  57. 'description' => 'Custom Description',
  58. ];
  59. }
  60. };
  61. $export->store('with-properties.xlsx');
  62. $spreadsheet = $this->read(__DIR__ . '/../Data/Disks/Local/with-properties.xlsx', 'Xlsx');
  63. $props = $spreadsheet->getProperties();
  64. $this->assertEquals('Default Title', $props->getTitle());
  65. $this->assertEquals('Custom Description', $props->getDescription());
  66. }
  67. /**
  68. * @test
  69. */
  70. public function it_ignores_empty_properties()
  71. {
  72. $export = new class implements WithProperties
  73. {
  74. use Exportable;
  75. public function properties(): array
  76. {
  77. return [
  78. 'description' => '',
  79. ];
  80. }
  81. };
  82. $export->store('with-properties.xlsx');
  83. $spreadsheet = $this->read(__DIR__ . '/../Data/Disks/Local/with-properties.xlsx', 'Xlsx');
  84. $props = $spreadsheet->getProperties();
  85. $this->assertSame('Unknown Creator', $props->getCreator());
  86. $this->assertSame('Untitled Spreadsheet', $props->getTitle());
  87. $this->assertSame('', $props->getDescription());
  88. }
  89. }