Laravel-excel: [PROPOSAL] Asserting that another Job is chained

Created on 22 Mar 2019  路  14Comments  路  Source: Maatwebsite/Laravel-Excel

Versions

  • PHP version: 7.2.4
  • Laravel version: 5.7.28
  • Package version: 3.1.9

Description

In my controller I use the following code:

Excel::import(new AttendeesImport, $request->file('file'))->chain([
  new UploadAccessCodesJob(),
  new GenerateLettersJob()
]);

Further I use the ShouldQueue concern.

In my Feature test I want to assert not only that the file was imported via

/** @test */
public function a_csv_file_with_attendee_data_can_be_imported()
{
    $response = $this->json('POST', '/api/v1/attendees', [
        'file' => new UploadedFile(base_path('tests/exampleAttendeeList.csv'), 'exampleAttendeeList.csv', 'text/csv', null, true)
    ]);
    $response->assertStatus(200);

    Excel::assertImported('exampleAttendeeList.csv');
    Excel::assertQueued('exampleAttendeeList.csv');
}

but also that the UploadAccessCodesJob and the GenerateLettersJob were pushed to the Queue or handled by Laravel-Exel.

How can I archive this?

Thank you in advance.

enhancement help wanted

Most helpful comment

@sanjitkung thanks for the snippet, I've added it myself now, need a bit more work to get it fully work though. Will be in next release!

All 14 comments

Hey @Lednerb, asserting the chain is currently not supported with Excel::fake()

Thanks, @patrickbrouwers for your response.

I've changed the topic respectively.

Feel free to PR it to the ExcelFake class. Currently not really any priority on it from my perspective.

@patrickbrouwers

I'm trying to just use the Queue::fake() to assert that the chained jobs are pushed, but this doesn't seem to work for me. Or maybe I'm just doing something wrong.

Calling class:

class ImportArticlesFromFile
{
    public function run(UploadedFile $uploadedFile, string $templateType, Authenticatable $user)
    {
        $importClass = config("ro-import-templates.{$templateType}.import");

        $import = Import::create(['input_source' => ArticleInputSourceEnum::coerce($templateType)->value]);

        (new $importClass($import))->queue($uploadedFile)->allOnQueue('my-custom-queue')->chain([
            new EnqueueArticlesFromImportForEnrichment($import),
            new SetInputSourcesForImportedArticles($import),
            new NotifyUserOfCompletedArticleImport($user, $import)
        ]);
    }
}

Failing Test case:

  /** @test */
    public function articles_are_enqueued_after_file_import()
    {
        Queue::fake();

        $fileName = 'member_import_template.xlsx';
        $path = $this->getTestImport();

        $file = new UploadedFile(
            $path,
            $fileName,
            null,
            null,
            true
        );

        (new ImportArticlesFromFile)->run($file, 'publisher', $this->testUser);

        Queue::assertPushed(EnqueueArticlesFromImportForEnrichment::class);

Can you open a new issue for this

@denitsa-cm Did you ever open a ticket or resolve this? I currently have the same issue.

@devonmather I think I've forgotten to open a new issue for this. Will do so now. I don't seem to have resolved the issue. I've just ended up asserting that my filed was queued..

@devonmather Oh well, maybe there's no point in opening a new issue, since this issue was exactly for this I think.

Is there any update on this? Running into same problem while testing exports. For time being I extends ExcelFake. Not sure if it is the good solution.

<?php

namespace Tests\Fixtures;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\PendingDispatch;
use Illuminate\Support\Facades\Queue;
use Maatwebsite\Excel\Fakes\ExcelFake as BaseExcelFake;

class ExcelFake extends BaseExcelFake
{
    private $job;
    /**
     * @inheritDoc
     */
    public function queue($export, string $filePath, string $disk = null, string $writerType = null, $diskOptions = [])
    {
        Queue::fake();

        $this->stored[$disk ?? 'default'][$filePath] = $export;
        $this->queued[$disk ?? 'default'][$filePath] = $export;


        return new PendingDispatch($this->job = new class implements ShouldQueue {
            use Queueable;

            public function handle()
            {
                //
            }
        });
    }

    public function assertPushedWithChain($chain) : void
    {
        Queue::assertPushedWithChain(get_class($this->job), $chain);
    }
}

I'm still open to accept a PR which adds that assertion.

@patrickbrouwers, I can make a PR for this.

What do you think about the approach I am using? Will it work ?

@sanjitkung I think that will be fine. I'm assuming this works correctly for you?

Yes, For my use case it works. I will send PR soon.

@sanjitkung thanks for the snippet, I've added it myself now, need a bit more work to get it fully work though. Will be in next release!

Was this page helpful?
0 / 5 - 0 ratings