viglucci's avatar

Boolean type casts

I have a unit test that is failing due to the boolean column on a table apparently not being cast properly.

The schema I defined is as so:

Schema::create('entries', function (Blueprint $table) {
    ...
    $table->boolean('is_archived')->default(false);
    ...
});

And an eloquent model that contains the following cast:

protected $casts = [
    'is_archived' => 'boolean'
];

However, when I retrieve the records from the database the value is either "0" or "1" on the model.

The model which is retrieved from the database was originally created by a factory.

$entry = factory(\App\Entry::class)->create([
    ...
    'is_archived' => true
]);

Debugger example: https://i.imgur.com/YyypzhK.png

Is it possible that there is some edge case with attribute casting when using the sqlite driver or creating records using factories?

The odd thing as well is that as far as I can tell this works fine when running with MySQL, and is only an issue when running phpunit.

Thanks

0 likes
1 reply
viglucci's avatar
viglucci
OP
Best Answer
Level 1

This turned out to be a logic error, not a database or casting error.

My HTTP request to the controller modifies the record and the record being asserted against in the test was thus out of date.

Calling $entry = $entry->fresh(); resolved this.

Please or to participate in this conversation.