1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Why is this artisan command not running synchronously in testing?

Discussão em 'Outras Linguagens' iniciado por Rory, Outubro 10, 2024 às 07:32.

  1. Rory

    Rory Guest

    I am trying to test an artisan command that asks for user input then makes changes to the database:

    Command Code

    $this->tenant = Tenant::find(search(
    label: 'Select a tenant',
    options: fn(string $value) => $value !== ''
    ? Tenant::whereLike('name', "%$value%")->pluck('name', 'id')->all()
    : []
    ));

    $this->tenant->merge();

    dump(microtime(), Tenant::count());


    Pest Test Code

    $command = $this->artisan('tools:merge')
    ->expectsSearch(
    question: 'Select a tenant',
    answer: self::$tenantId,
    search: 'test',
    answers: Tenant::all()->pluck('name', 'id')->toArray()
    )
    ->assertExitCode(0);

    dump(microtime(), Tenant::count());
    $this->assertDatabaseHas('tenants', ['id' => self::$tenant->id]);


    Via XDebug I can see that the changes to the database are made correctly, but the assertion fails. By dumping the microtime that the commands are run I can see they are not in the order I would expect and that the assertions are being run before the command completes.

    All the documentation says that the command should be run synchronously - why is it not? There is nothing asynchronous in the handle method.

    Continue reading...

Compartilhe esta Página