Testing
On this page
SocialPublisher::fake() replaces every call to the API with a fake that records each post: your tests check what would have been published, and where, without any network or API key.
The fake
use QuietSocial\Sdk\Post;
use QuietSocial\Social\Facades\SocialPublisher;
it('publishes a published article to LinkedIn', function (): void {
$fake = SocialPublisher::fake();
Article::factory()->create(['is_published' => true]);
$fake->assertPublished(Article::class, fn (Post $post, array $to): bool => in_array('linkedin', $to, true));
});
The fake serves the automatic synchronisation and the manual builder alike: nothing leaves, every post is recorded.
Assertions
| Method | Checks |
|---|---|
assertPublished(Article::class, ?Closure $check) |
A post of this class was sent, and one of them passes the check, which receives the Post and its targets. |
assertCancelled(Article::class, ?Closure $check) |
A post of this class was cancelled; the check receives the Post as it had been sent. |
assertNothingPublished() |
No post was sent. |
published() returns every Post sent, in order. A failed assertion says what was published, and goes through PHPUnit when it is installed.
What the fake does
- Fake publications are scheduled (or draft, or pending approval, as the post asks) and can be cancelled.
- Every network has one destination able to publish: a model without explicit targets targets every network it declares.
markPublished(?string $class)makes the fake publications, of every class or of one only, look published: the next change of their model raisesSocialPostOutdatedinstead of replacing them.
use Illuminate\Support\Facades\Event;
use QuietSocial\Social\Events\SocialPostOutdated;
Event::fake([SocialPostOutdated::class]);
$fake = SocialPublisher::fake();
$article = Article::factory()->create(['is_published' => true]);
$fake->markPublished(Article::class);
$article->update(['excerpt' => 'Corrected after publication.']);
Event::assertDispatched(SocialPostOutdated::class);
Fake only the events of the package: an Event::fake() without a list also fakes Eloquent events, and no save triggers the synchronisation any more.
The queue and the cache
With the sync queue (the default of a Laravel test suite), the job runs during the save and the fake sees the post at once.
With Queue::fake(), the job does not run at all and nothing reaches the fake: check instead that the job was queued.
use Illuminate\Support\Facades\Queue;
use QuietSocial\Social\Jobs\SyncSocialPost;
Queue::fake();
Article::factory()->create(['is_published' => true]);
Queue::assertPushed(SyncSocialPost::class);
The array cache store of a test suite supports locks: the synchronisation runs there as in production.