I am writing my unit tests with phpunit
to Laravel
application. I am using Eloquent
and Woodling
library. I want to test many to many relationship.
I have Users
table and Friends
table. Everything worked, when I tested it manually. I am able to add friends. I wanted to test this functionality.
I created blueprints and I call them with saved
method like this.
$user = Woodling::saved('LonelyUser');
$user2 = Woodling::saved('LonelyUser2');
$users = User::all()->toArray();
var_dump($users);
$user->addFriend($user2);
I got a database constraint error in the last line, because the users were not persisted to database (I know that, because they are not in var_dump
output).
If Woodling::saved
does not persist to database, than what does it do? The docs say, that it calls save
method on model. save
method should persist the model to database.
What is the saved
method purpose and how is it different than retrieve
?
Woodling::saved
persists to database correctly, but if something goes wrong during saving, it doesn't show any errors. In my case save
method was actually Ardent::save
, which does some validations. This validations did not pass and Model::save
was never called.