PHPackages                             jokersk/sonata - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. jokersk/sonata

ActiveLibrary[Testing &amp; Quality](/categories/testing)

jokersk/sonata
==============

for laravel testing

0.04(4y ago)33001MITPHP

Since May 22Pushed 3y ago1 watchersCompare

[ Source](https://github.com/jokersk/sonata)[ Packagist](https://packagist.org/packages/jokersk/sonata)[ RSS](/packages/jokersk-sonata/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (10)DependenciesVersions (12)Used By (0)

Sonata
======

[](#sonata)

Make your laravel test easier

Install
-------

[](#install)

```
composer require jokersk/sonata

```

in your test file add `Sonata\Traits\LetsPlaySonata` trait, that it!

Use
---

[](#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

[](#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

[](#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

[](#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

[](#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
-------

[](#mocking)

```
$foo = Sonata::createMock('where()->first()->content', 'hello');
```

now you call $foo-&gt;where()-&gt;first()-&gt;content, will equal to 'hello'

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 95.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~46 days

Recently: every ~85 days

Total

11

Last Release

1360d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8236606e09aee05405be84a246f7471ed6af5390b3cbb8d8852ea21e167d990a?d=identicon)[jokersk](/maintainers/jokersk)

---

Top Contributors

[![joeszetocodeman](https://avatars.githubusercontent.com/u/5194890?v=4)](https://github.com/joeszetocodeman "joeszetocodeman (23 commits)")[![lazehang](https://avatars.githubusercontent.com/u/18165612?v=4)](https://github.com/lazehang "lazehang (1 commits)")

### Embed Badge

![Health badge](/badges/jokersk-sonata/health.svg)

```
[![Health](https://phpackages.com/badges/jokersk-sonata/health.svg)](https://phpackages.com/packages/jokersk-sonata)
```

###  Alternatives

[phpspec/prophecy

Highly opinionated mocking framework for PHP 5.3+

8.5k551.7M682](/packages/phpspec-prophecy)[vimeo/psalm

A static analysis tool for finding errors in PHP applications

5.8k77.5M6.7k](/packages/vimeo-psalm)[brianium/paratest

Parallel testing for PHP

2.5k118.8M754](/packages/brianium-paratest)[beberlei/assert

Thin assertion library for input validation in business models.

2.4k96.9M570](/packages/beberlei-assert)[mikey179/vfsstream

Virtual file system to mock the real file system in unit tests.

1.4k108.0M2.7k](/packages/mikey179-vfsstream)[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.1k](/packages/orchestra-testbench)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
