mercredi 21 mars 2018

Laravel / PHPUnit: Asserting sameness of two models

When integration testing (using a database) in Laravel. What is the best way to assert that two instances of the same Eloquent model are the same?

AssertEquals $model $model

Simply using ->assertEquals($modelOne, $modelTwo); wont work as even if they are the same PHPUnit checks class properties such as wasRecentlyCreated and sometimes model ids can be a string or an integer.

AssertEquals $model->id $model->id

The problem here is model one may be an instance of another model e.g:

AssertEquals(Person::find(1)->id, Animal::find(1)->id); returns true.

AssertEquals $model->toArray() $model->toArray()

The issue here is properties are not always cast as the same type, Im not 100% sure why but there are times a models properties will be an int and other times a number string

AssertEquals($address->toArray(), $user->address->toArray())

[ postcode => 2454 ]
[ postcode => "2454" ]

How to assert in a simple, clean and repeatable manner two Eloquent models are the same database record?

Aucun commentaire:

Enregistrer un commentaire