FromViewTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Maatwebsite\Excel\Tests\Concerns;
  3. use Illuminate\Contracts\View\View;
  4. use Illuminate\Support\Collection;
  5. use Maatwebsite\Excel\Concerns\Exportable;
  6. use Maatwebsite\Excel\Concerns\FromView;
  7. use Maatwebsite\Excel\Concerns\WithMultipleSheets;
  8. use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;
  9. use Maatwebsite\Excel\Tests\Data\Stubs\SheetForUsersFromView;
  10. use Maatwebsite\Excel\Tests\TestCase;
  11. class FromViewTest extends TestCase
  12. {
  13. /**
  14. * Setup the test environment.
  15. */
  16. protected function setUp(): void
  17. {
  18. parent::setUp();
  19. $this->withFactories(__DIR__ . '/../Data/Stubs/Database/Factories');
  20. }
  21. /**
  22. * @test
  23. */
  24. public function can_export_from_view()
  25. {
  26. /** @var Collection|User[] $users */
  27. $users = factory(User::class)->times(100)->make();
  28. $export = new class($users) implements FromView
  29. {
  30. use Exportable;
  31. /**
  32. * @var Collection
  33. */
  34. protected $users;
  35. /**
  36. * @param Collection $users
  37. */
  38. public function __construct(Collection $users)
  39. {
  40. $this->users = $users;
  41. }
  42. /**
  43. * @return View
  44. */
  45. public function view(): View
  46. {
  47. return view('users', [
  48. 'users' => $this->users,
  49. ]);
  50. }
  51. };
  52. $response = $export->store('from-view.xlsx');
  53. $this->assertTrue($response);
  54. $contents = $this->readAsArray(__DIR__ . '/../Data/Disks/Local/from-view.xlsx', 'Xlsx');
  55. $expected = $users->map(function (User $user) {
  56. return [
  57. $user->name,
  58. $user->email,
  59. ];
  60. })->prepend(['Name', 'Email'])->toArray();
  61. $this->assertEquals($expected, $contents);
  62. }
  63. /**
  64. * @test
  65. */
  66. public function can_export_multiple_sheets_from_view()
  67. {
  68. /** @var Collection|User[] $users */
  69. $users = factory(User::class)->times(300)->make();
  70. $export = new class($users) implements WithMultipleSheets
  71. {
  72. use Exportable;
  73. /**
  74. * @var Collection
  75. */
  76. protected $users;
  77. /**
  78. * @param Collection $users
  79. */
  80. public function __construct(Collection $users)
  81. {
  82. $this->users = $users;
  83. }
  84. /**
  85. * @return SheetForUsersFromView[]
  86. */
  87. public function sheets(): array
  88. {
  89. return [
  90. new SheetForUsersFromView($this->users->forPage(1, 100)),
  91. new SheetForUsersFromView($this->users->forPage(2, 100)),
  92. new SheetForUsersFromView($this->users->forPage(3, 100)),
  93. ];
  94. }
  95. };
  96. $response = $export->store('from-multiple-view.xlsx');
  97. $this->assertTrue($response);
  98. $contents = $this->readAsArray(__DIR__ . '/../Data/Disks/Local/from-multiple-view.xlsx', 'Xlsx', 0);
  99. $expected = $users->forPage(1, 100)->map(function (User $user) {
  100. return [
  101. $user->name,
  102. $user->email,
  103. ];
  104. })->prepend(['Name', 'Email'])->toArray();
  105. $this->assertEquals(101, sizeof($contents));
  106. $this->assertEquals($expected, $contents);
  107. $contents = $this->readAsArray(__DIR__ . '/../Data/Disks/Local/from-multiple-view.xlsx', 'Xlsx', 2);
  108. $expected = $users->forPage(3, 100)->map(function (User $user) {
  109. return [
  110. $user->name,
  111. $user->email,
  112. ];
  113. })->prepend(['Name', 'Email'])->toArray();
  114. $this->assertEquals(101, sizeof($contents));
  115. $this->assertEquals($expected, $contents);
  116. }
  117. }