mercredi 29 novembre 2017

Laravel 5.4 ModelNotFoundException when testing database with SQLite

I have encountered a problem with my feature tests in Laravel 5.4. Whenever I try to run my database tests with the SQLite memory configuration I get this error:

1) Tests\Feature\ParseAccountingFilesTest::testExample
│/Middleware/ValidatePostSize.php(27): Illuminate\Routing\Pipeline->Illuminate\R Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [App\QueuedAction] 1

What I think is strange is the fact that this error is to my knowledge generally encountered only whenever the query builder uses one of the -OrFail() methods. My code works perfectly fine outside the tests when using a database queue driver and MySQL as the database driver. I have debugged the problem and found that it is caused by the following statement in the class constructor below:

$this->scheduledAction = $action;

This error makes no sense to me. There should be no query involved, only a simple allocation. This class is used as a simple parent class for the actual job classes to keep track of what actions have already been queued.

abstract class SchedulesOnce implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $scheduledAction;

    public function __construct(string $id, string $type)
    {
        $action = new QueuedAction();
        $action->id = $id;
        $action->type = $type;
        $action->save();
        $this->scheduledAction = $action;
    }

    // Execute the job
    public function handle()
    {
        $this->execute();

        // Delete the queue reservation
        $this->scheduledAction->delete();
    }

    // Child functionality
    protected abstract function execute();
}

Here are the relevant lines in my phpunit.xml

<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>

Aucun commentaire:

Enregistrer un commentaire