WithColumnLimitTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Maatwebsite\Excel\Tests\Concerns;
  3. use Maatwebsite\Excel\Concerns\Importable;
  4. use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
  5. use Maatwebsite\Excel\Concerns\ToArray;
  6. use Maatwebsite\Excel\Concerns\WithColumnLimit;
  7. use Maatwebsite\Excel\Tests\TestCase;
  8. use PHPUnit\Framework\Assert;
  9. class WithColumnLimitTest extends TestCase
  10. {
  11. /**
  12. * Setup the test environment.
  13. */
  14. protected function setUp(): void
  15. {
  16. parent::setUp();
  17. $this->loadLaravelMigrations(['--database' => 'testing']);
  18. }
  19. /**
  20. * @test
  21. */
  22. public function can_import_to_array_with_column_limit()
  23. {
  24. $import = new class implements ToArray, WithColumnLimit
  25. {
  26. use Importable;
  27. /**
  28. * @param array $array
  29. */
  30. public function array(array $array)
  31. {
  32. Assert::assertEquals([
  33. [
  34. 'Patrick Brouwers',
  35. ],
  36. [
  37. 'Taylor Otwell',
  38. ],
  39. ], $array);
  40. }
  41. public function endColumn(): string
  42. {
  43. return 'A';
  44. }
  45. };
  46. $import->import('import-users.xlsx');
  47. }
  48. /**
  49. * @test
  50. */
  51. public function can_import_to_array_with_column_limit_and_skips_empty_rows()
  52. {
  53. $import = new class implements ToArray, WithColumnLimit, SkipsEmptyRows
  54. {
  55. use Importable;
  56. /**
  57. * @param array $array
  58. */
  59. public function array(array $array)
  60. {
  61. Assert::assertEquals([
  62. [
  63. 'Test1',
  64. 'Test2',
  65. null,
  66. null,
  67. ],
  68. [
  69. 'Test3',
  70. 'Test4',
  71. null,
  72. null,
  73. ],
  74. [
  75. 'Test5',
  76. 'Test6',
  77. null,
  78. null,
  79. ],
  80. ], $array);
  81. }
  82. public function endColumn(): string
  83. {
  84. return 'D';
  85. }
  86. };
  87. $import->import('import-empty-rows.xlsx');
  88. }
  89. }