Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Synchro committed Oct 16, 2024
1 parent b57ce07 commit ea1fe6a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tests/Concerns/FromQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Maatwebsite\Excel\Tests\Data\Stubs\FromUsersQueryExport;
use Maatwebsite\Excel\Tests\Data\Stubs\FromUsersQueryExportWithEagerLoad;
use Maatwebsite\Excel\Tests\Data\Stubs\FromUsersQueryExportWithPrepareRows;
use Maatwebsite\Excel\Tests\Data\Stubs\FromUsersQueryWithJoinExport;
use Maatwebsite\Excel\Tests\Data\Stubs\FromUsersScoutExport;
use Maatwebsite\Excel\Tests\TestCase;

Expand Down Expand Up @@ -61,6 +62,23 @@ public function test_can_export_from_query()
$this->assertEquals($allUsers, $contents);
}

public function test_can_export_from_query_with_join()
{
$export = new FromUsersQueryWithJoinExport();

$response = $export->store('from-query-store.xlsx');

$this->assertTrue($response);

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

$allUsers = $export->query->get()->map(function (User $user) {
return array_values($user->toArray());
})->toArray();

$this->assertEquals($allUsers, $contents);
}

public function test_can_export_from_relation_query_queued()
{
$export = new FromGroupUsersQueuedQueryExport();
Expand Down
46 changes: 46 additions & 0 deletions tests/Data/Stubs/FromUsersQueryWithJoinExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Maatwebsite\Excel\Tests\Data\Stubs;

use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Query\Builder;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithCustomChunkSize;
use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;

class FromUsersQueryWithJoinExport implements FromQuery, WithCustomChunkSize
{
use Exportable;

public $query;

public function __construct()
{
$this->query = User::query();
}

/**
* @return Builder|EloquentBuilder|Relation
*/
public function query()
{
return $this->query
->join(
'group_user',
'group_user.user_id',
'=',
'users.id'
)
->select('users.*', 'group_user.group_id as gid');
}

/**
* @return int
*/
public function chunkSize(): int
{
return 1000;
}
}

0 comments on commit ea1fe6a

Please sign in to comment.