WithLimitTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Maatwebsite\Excel\Tests\Concerns;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Maatwebsite\Excel\Concerns\Importable;
  5. use Maatwebsite\Excel\Concerns\ToArray;
  6. use Maatwebsite\Excel\Concerns\ToModel;
  7. use Maatwebsite\Excel\Concerns\WithLimit;
  8. use Maatwebsite\Excel\Concerns\WithStartRow;
  9. use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;
  10. use Maatwebsite\Excel\Tests\TestCase;
  11. use PHPUnit\Framework\Assert;
  12. class WithLimitTest extends TestCase
  13. {
  14. /**
  15. * Setup the test environment.
  16. */
  17. protected function setUp(): void
  18. {
  19. parent::setUp();
  20. $this->loadLaravelMigrations(['--database' => 'testing']);
  21. }
  22. /**
  23. * @test
  24. */
  25. public function can_import_a_limited_section_of_rows_to_model_with_different_start_row()
  26. {
  27. $import = new class implements ToModel, WithStartRow, WithLimit
  28. {
  29. use Importable;
  30. /**
  31. * @param array $row
  32. * @return Model
  33. */
  34. public function model(array $row): Model
  35. {
  36. return new User([
  37. 'name' => $row[0],
  38. 'email' => $row[1],
  39. 'password' => 'secret',
  40. ]);
  41. }
  42. /**
  43. * @return int
  44. */
  45. public function startRow(): int
  46. {
  47. return 5;
  48. }
  49. /**
  50. * @return int
  51. */
  52. public function limit(): int
  53. {
  54. return 1;
  55. }
  56. };
  57. $import->import('import-users-with-different-heading-row.xlsx');
  58. $this->assertDatabaseHas('users', [
  59. 'name' => 'Patrick Brouwers',
  60. 'email' => 'patrick@maatwebsite.nl',
  61. ]);
  62. $this->assertDatabaseMissing('users', [
  63. 'name' => 'Taylor Otwell',
  64. 'email' => 'taylor@laravel.com',
  65. ]);
  66. }
  67. /**
  68. * @test
  69. */
  70. public function can_import_to_array_with_limit()
  71. {
  72. $import = new class implements ToArray, WithLimit
  73. {
  74. use Importable;
  75. /**
  76. * @param array $array
  77. */
  78. public function array(array $array)
  79. {
  80. Assert::assertEquals([
  81. [
  82. 'Patrick Brouwers',
  83. 'patrick@maatwebsite.nl',
  84. ],
  85. ], $array);
  86. }
  87. /**
  88. * @return int
  89. */
  90. public function limit(): int
  91. {
  92. return 1;
  93. }
  94. };
  95. $import->import('import-users.xlsx');
  96. }
  97. /**
  98. * @test
  99. */
  100. public function can_set_limit_bigger_than_row_size()
  101. {
  102. $import = new class implements ToArray, WithLimit
  103. {
  104. use Importable;
  105. /**
  106. * @param array $array
  107. */
  108. public function array(array $array)
  109. {
  110. Assert::assertCount(2, $array);
  111. }
  112. /**
  113. * @return int
  114. */
  115. public function limit(): int
  116. {
  117. return 10;
  118. }
  119. };
  120. $import->import('import-users.xlsx');
  121. }
  122. }