jokersk / sonata
for laravel testing
README
Make your laravel test easier
Install
composer require jokersk/sonata
in your test file add Sonata\Traits\LetsPlaySonata
trait, that it!
Use
use Sonata\Traits\LetsPlaySonata; class SomeTest extends TestCase { use LetsPlaySonata, RefreshDatabase; ... }
asume you have a model \App\Models\Post
, we can create model like this
$this->create(Post::class);
if you want to get the created post model, you can
$post = $this->create(Post::class)->getCreated();
create with relations
now we have a model \App\Models\Comment
, and in \App\Models\Post
model have HasMany relation like
public function comments() { return $this->hasMany(Comment::class); }
we can create the models without sonata like this
$post = Post::factory()->create(); $comment = Comment::factory()->create(['post_id' => $post->id]);
but with sonata, you can create the models like
$this->create(Post::class)->with(Comment::class);
no matter HasMany, BelongsTo, BelongsToMany, morphMany, morphToMany
, you can just use with
function, sonata will handle the save, associate, or attach
for you
if you want to get created models you can
[$post, $comment] = $this->create(Post::class)->with(Comment::class)->get([Post::class, Comment::class]);
or
[$post, $comment] = $this->create(Post::class)->with(Comment::class)->get();
Create with attributes
$this->create(Post::class, [ 'title' => 'abc', 'body' => 'hi' ]);
or
$this->set([ 'title' => 'abc', 'body' => 'hi' ])->create(Post::class);
to set attributes to with
function, we can do that
$this->create(Post::class)->with(Comment::class, [ 'body' => 'foo' ]);
Overvide function name
by default Sonata will find the currect function name, but sometimes your function name is unpredictable
eg. Post
model has many Comment
, so you will have a function call comments
, but sometimes will call the function
activeComments
, in this case, you can call
$this->create(Post::class)->by('activeComments')->with(Comment::class);
Create relations from existing model
can use createFrom
methods to create relation with existing model
$post = Post::factory()->create(); $comment = $this->createFrom($post)->with(Comment::class)->get(Comment::class);
Mocking
$foo = Sonata::createMock('where()->first()->content', 'hello');
now you call $foo->where()->first()->content, will equal to 'hello'