Skip to content

Commit

Permalink
Write test for lazy collections without queue
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickbrouwers committed Jan 9, 2024
1 parent af25dd3 commit 6fbf739
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 6 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ jobs:
composer require "laravel/framework:${{ matrix.laravel }}.*" "orchestra/testbench:${{ matrix.testbench }}" "laravel/scout:${{ matrix.scout }}" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --no-interaction
- name: Install legacy factories
- name: Remove legacy factories for older versions
run: |
composer require "laravel/legacy-factories" --no-interaction
if: "matrix.laravel >= 8"
composer remove "laravel/legacy-factories" --no-interaction
if: "matrix.laravel < 8"

- name: Execute tests
run: vendor/bin/phpunit --testdox --configuration phpunit.xml.dist
Expand Down
2 changes: 1 addition & 1 deletion .phpunit.cache/test-results

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
},
"require-dev": {
"orchestra/testbench": "^6.0||^7.0||^8.0",
"predis/predis": "^1.1"
"predis/predis": "^1.1",
"laravel/scout": "^7.0||^8.0||^9.0||^10.0",
"laravel/legacy-factories": "^1.3"
},
"autoload": {
"psr-4": {
Expand Down
25 changes: 25 additions & 0 deletions tests/Concerns/FromCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Foundation\Bus\PendingDispatch;
use Maatwebsite\Excel\Tests\Data\Stubs\EloquentLazyCollectionExport;
use Maatwebsite\Excel\Tests\Data\Stubs\EloquentLazyCollectionQueuedExport;
use Maatwebsite\Excel\Tests\Data\Stubs\QueuedExport;
use Maatwebsite\Excel\Tests\Data\Stubs\SheetWith100Rows;
use Maatwebsite\Excel\Tests\TestCase;
Expand Down Expand Up @@ -57,10 +58,34 @@ public function can_export_from_lazy_collection()
{
$export = new EloquentLazyCollectionExport();

$export->store('from-lazy-collection-store.xlsx');

$contents = $this->readAsArray(__DIR__ . '/../Data/Disks/Local/from-lazy-collection-store.xlsx', 'Xlsx');

$this->assertEquals(
$export->collection()->map(
function (array $item) {
return array_values($item);
}
)->toArray(),
$contents
);
}

/**
* @test
*/
public function can_export_from_lazy_collection_with_queue()
{
$export = new EloquentLazyCollectionQueuedExport();

$response = $export->queue('from-lazy-collection-store.xlsx');

$this->assertTrue($response instanceof PendingDispatch);

// Force dispatching via __destruct.
unset($response);

$contents = $this->readAsArray(__DIR__ . '/../Data/Disks/Local/from-lazy-collection-store.xlsx', 'Xlsx');

$this->assertEquals(
Expand Down
2 changes: 1 addition & 1 deletion tests/Data/Stubs/EloquentLazyCollectionExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;

class EloquentLazyCollectionExport implements FromCollection, ShouldQueue
class EloquentLazyCollectionExport implements FromCollection
{
use Exportable;

Expand Down
35 changes: 35 additions & 0 deletions tests/Data/Stubs/EloquentLazyCollectionQueuedExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Maatwebsite\Excel\Tests\Data\Stubs;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\LazyCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;

class EloquentLazyCollectionQueuedExport implements FromCollection, ShouldQueue
{
use Exportable;

public function collection(): LazyCollection
{
return collect([
[
'firstname' => 'Patrick',
'lastname' => 'Brouwers',
],
[
'firstname' => 'Patrick',
'lastname' => 'Brouwers',
],
[
'firstname' => 'Patrick',
'lastname' => 'Brouwers',
],
[
'firstname' => 'Patrick',
'lastname' => 'Brouwers',
],
])->lazy();
}
}

0 comments on commit 6fbf739

Please sign in to comment.