-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added new concern WithCount. Allows export classes that implement the FromQuery concern, to provide their custom count implementation, instead of relying to the query's count. * Added tests for the WithCount concern. * Updated docs regarding the WithCount concern. * Removed code duplication and reduced LOC. * Fixed test on Laravel 5.5. * Package orchestra/database is needed for Laravel 5.5 builds. * Installing orchestra/database unconditionally. * Renamed trait WithCount to the more expressive WithCustomQuerySize. * Fixed some code styling issues. * ConsoleServiceProvider is always available. No need to check for its existence. * Renamed `count` to `querySize`. * Added explanatory comments in the `querySize` method's docblock.
- Loading branch information
1 parent
203ec61
commit 61bb806
Showing
11 changed files
with
243 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Maatwebsite\Excel\Concerns; | ||
|
||
interface WithCustomQuerySize | ||
{ | ||
/** | ||
* Queued exportables are processed in chunks; each chunk being a job pushed to the queue by the QueuedWriter. | ||
* In case of exportables that implement the FromQuery concern, the number of jobs is calculated by dividing the $query->count() by the chunk size. | ||
* Depending on the implementation of the query() method (eg. When using a groupBy clause), this calculation might not be correct. | ||
* | ||
* When this is the case, you should use this method to provide a custom calculation of the query size. | ||
* | ||
* @return int | ||
*/ | ||
public function querySize(): int; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace Maatwebsite\Excel\Tests\Concerns; | ||
|
||
use Maatwebsite\Excel\Tests\TestCase; | ||
use Maatwebsite\Excel\Tests\Data\Stubs\Database\User; | ||
use Maatwebsite\Excel\Tests\Data\Stubs\Database\Group; | ||
use Maatwebsite\Excel\Tests\Data\Stubs\AfterQueueExportJob; | ||
use Maatwebsite\Excel\Tests\Data\Stubs\FromQueryWithCustomQuerySize; | ||
|
||
class WithCustomQuerySizeTest extends TestCase | ||
{ | ||
/** | ||
* Setup the test environment. | ||
*/ | ||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->loadLaravelMigrations(['--database' => 'testing']); | ||
$this->loadMigrationsFrom(dirname(__DIR__) . '/Data/Stubs/Database/Migrations'); | ||
$this->withFactories(dirname(__DIR__) . '/Data/Stubs/Database/Factories'); | ||
|
||
factory(Group::class)->times(5)->create()->each(function ($group) { | ||
$group->users()->attach(factory(User::class)->times(rand(1, 3))->create()); | ||
}); | ||
|
||
config()->set('excel.exports.chunk_size', 2); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function can_export_with_custom_count() | ||
{ | ||
$export = new FromQueryWithCustomQuerySize(); | ||
|
||
$export->queue('export-from-query-with-count.xlsx', null, 'Xlsx')->chain([ | ||
new AfterQueueExportJob(dirname(__DIR__) . '/Data/Disks/Local/export-from-query-with-count.xlsx'), | ||
]); | ||
|
||
$actual = $this->readAsArray(dirname(__DIR__) . '/Data/Disks/Local/export-from-query-with-count.xlsx', 'Xlsx'); | ||
|
||
$this->assertCount(Group::count(), $actual); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getPackageProviders($app) | ||
{ | ||
return [ | ||
\Orchestra\Database\ConsoleServiceProvider::class, | ||
]; | ||
|
||
return parent::getPackageAliases($app); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
use Faker\Generator as Faker; | ||
use Maatwebsite\Excel\Tests\Data\Stubs\Database\Group; | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Model Factories | ||
|-------------------------------------------------------------------------- | ||
| | ||
| This directory should contain each of the model factory definitions for | ||
| your application. Factories provide a convenient way to generate new | ||
| model instances for testing / seeding your application's database. | ||
| | ||
*/ | ||
$factory->define(Group::class, function (Faker $faker) { | ||
return [ | ||
'name' => $faker->word, | ||
]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Maatwebsite\Excel\Tests\Data\Stubs\Database; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsToMany; | ||
|
||
class Group extends Model | ||
{ | ||
protected $guarded = []; | ||
|
||
public function users(): BelongsToMany | ||
{ | ||
return $this->belongsToMany(User::class); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
tests/Data/Stubs/Database/Migrations/0000_00_00_000000_create_groups_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateGroupsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('groups', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->string('name'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('groups'); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
tests/Data/Stubs/Database/Migrations/0000_00_00_000001_create_group_user_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateGroupUserTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('group_user', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->unsignedInteger('group_id'); | ||
$table->unsignedInteger('user_id'); | ||
|
||
$table->foreign('user_id') | ||
->references('id') | ||
->on('users') | ||
->onDelete('cascade'); | ||
|
||
$table->foreign('group_id') | ||
->references('id') | ||
->on('groups') | ||
->onDelete('cascade'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('group_user'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Maatwebsite\Excel\Tests\Data\Stubs; | ||
|
||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Database\Query\Builder; | ||
use Maatwebsite\Excel\Concerns\FromQuery; | ||
use Maatwebsite\Excel\Concerns\Exportable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Maatwebsite\Excel\Concerns\WithMapping; | ||
use Maatwebsite\Excel\Concerns\WithCustomQuerySize; | ||
use Maatwebsite\Excel\Tests\Data\Stubs\Database\Group; | ||
|
||
class FromQueryWithCustomQuerySize implements FromQuery, WithCustomQuerySize, WithMapping, ShouldQueue | ||
{ | ||
use Exportable; | ||
|
||
/** | ||
* @return Builder | ||
*/ | ||
public function query() | ||
{ | ||
$query = Group::with('users') | ||
->join('group_user', 'groups.id', '=', 'group_user.group_id') | ||
->select('groups.*', DB::raw('count(group_user.user_id) as number_of_users')) | ||
->groupBy('groups.id') | ||
->orderBy('number_of_users'); | ||
|
||
return $query; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function querySize(): int | ||
{ | ||
return Group::has('users')->count(); | ||
} | ||
|
||
/** | ||
* @param Group $row | ||
* | ||
* @return array | ||
*/ | ||
public function map($row): array | ||
{ | ||
return [ | ||
$row->id, | ||
$row->name, | ||
$row->number_of_users, | ||
]; | ||
} | ||
} |