Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

withFakeBatch not using Collection::wrap for added jobs on add #47588

Closed
schonhoff opened this issue Jun 27, 2023 · 1 comment
Closed

withFakeBatch not using Collection::wrap for added jobs on add #47588

schonhoff opened this issue Jun 27, 2023 · 1 comment

Comments

@schonhoff
Copy link
Contributor

Laravel Version

10.14.0

PHP Version

8.2.4

Database Driver & Version

MySQL 8.0.32

Description

Hello,

I tried to test out batches for my application to add jobs dynamic. This is an abstraction for my handle code in a batchable job:

    public function handle(): void
    {
        $this->batch()->add(new FirstJob());
        $this->batch()->add(new SecondJob());
    }

The parameters for the add function can be \Illuminate\Support\Enumerable|object|array. My guess is that I can add new Jobs directly in there because they are objects and are wrapped with the Collection::wrap helper. Now I want to test my code with the withFakeBatch method on the batchable job class. In the documentation it is called that the second member of the tuple is the batch and with $batch->added I get a list of all the jobs added. So I wrote my test like this:

    public function test_example(): void
    {
        [$job, $batch] = (new InitialJob())->withFakeBatch();

        $job->handle();

        $this->assertEquals(2, count($batch->added));
    }

The tests throws an error that the count($batch->added) is not 2.

After searching for the issue I found the overwritten add method in the BatchFake Class:

    /**
     * Add additional jobs to the batch.
     *
     * @param  \Illuminate\Support\Enumerable|object|array  $jobs
     * @return self
     */
    public function add($jobs)
    {
        foreach ($jobs as $job) {
            $this->added[] = $job;
        }

        return $this;
    }

In my opinion the method is missing the line

    {
        $jobs = Collection::wrap($jobs);
        foreach ($jobs as $job) {

With this line of code the object is safe to use again and it is more like the "real" add method in the Batch class.

If something is not clear enough I would love to provide more information.

Thanks for the great tool and have a nice day!

Steps To Reproduce

In this repository there is a Test failing with the scenario described above.
https://github.com/schonhoff/bug-with-fake-batch

@schonhoff
Copy link
Contributor Author

schonhoff commented Jun 27, 2023

Closed because the fix got merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant