---
title: "Testovanie — Messages for Laravel | Roundly"
description: "Messages::fake() zaznamená každú operáciu na overenie, k tomu stavy factories, trait InteractsWithMessaging a Pest expectations."
url: https://roundly-consulting.com/sk/open-source/navody/messages-for-laravel/testovanie
language: sk
---

[Všetky balíky](https://roundly-consulting.com/sk/open-source.md)

[Messages for Laravel](https://roundly-consulting.com/sk/open-source/navody/messages-for-laravel.md)

# Testovanie

## Fake cez Messages::fake()

Messages::fake() podstrčí zaznamenávajúci fake a vráti ho — ten potom slúži na overovanie:

```php
use RoundlyConsulting\Messages\Facades\Messages;

$fake = Messages::fake();

$alice->sendMessageTo($thread, 'Hi');                          // model traits are recorded too
Messages::thread($thread)->rename('Launch', by: $alice);

$fake->assertSent('Hi', to: $thread);
$fake->assertThreadRenamed($thread, to: 'Launch');
$fake->assertNothingDeleted();
```

Fake je podtyp MessagesManager, takže ho dostanú aj managery vložené cez konštruktor. Každú operáciu naďalej skutočne vykoná — riadky sa zapíšu, udalosti sa spustia — a zaznamená každú úspešnú, nech prišla akoukoľvek cestou: cez fasádu, vložený manager, buildery, handly, trait HasMessaging, Thread::markReadFor() a typing(), Participant::markAsRead(), pomocníkov z InteractsWithMessaging či príkaz messages:prune. Prácu, ktorú akcia robí sama za seba — účastníkov, ktorých pridá start(), či systémové správy — samostatne nezaznamenáva a akcie volané priamo fake obchádzajú.

## Všetky overenia

Ku každému overeniu patrí aj negatívne assertNothing…. Voliteľné argumenty zužujú zhodu; modely sa porovnávajú cez Model::is():

```php
use RoundlyConsulting\Messages\Enums\ParticipantRole;

$fake->assertThreadCreated('Launch');              // start()->create(), or a direct() that created one
$fake->assertNothingCreated();

$fake->assertSent('Hi', to: $thread);              // both arguments optional
$fake->assertSentCount(2);
$fake->assertNothingSent();

$fake->assertThreadRenamed($thread, to: 'Launch crew');
$fake->assertNothingRenamed();

$fake->assertThreadArchived($thread);
$fake->assertNothingArchived();

$fake->assertMarkedRead($thread, by: $bob);
$fake->assertNothingMarkedRead();

$fake->assertTyping($thread, $bob);
$fake->assertNothingTyping();

$fake->assertParticipantAdded($thread, $carol);    // new rows only, not a re-add
$fake->assertNothingAdded();

$fake->assertParticipantRemoved($thread, $carol);  // remove() or leave()
$fake->assertNothingRemoved();

$fake->assertRoleChanged($thread, $carol, ParticipantRole::Admin);
$fake->assertNothingRoleChanged();

$fake->assertOwnershipTransferred($thread, to: $bob);
$fake->assertNothingTransferred();

$fake->assertEdited($message, 'fixed typo');
$fake->assertNothingEdited();

$fake->assertDeleted($message);
$fake->assertNothingDeleted();

$fake->assertPruned(days: 30);                    // Messages::prune() or the messages:prune command
$fake->assertNothingPruned();
```

assertThreadCreated() zaráta aj direct(), ktoré muselo vlákno vytvoriť, a assertParticipantAdded() len nové záznamy, nikdy opätovné pridanie. Na vlastné kontroly vráti recorded() všetky zaznamenané MessagingCall, voliteľne podľa operácie:

```php
use RoundlyConsulting\Messages\Enums\MessagingOperation;

$sends = $fake->recorded(MessagingOperation::Send);   // list<MessagingCall>, in call order

$sends[0]->thread;   // the Thread
$sends[0]->text;     // the body
$sends[0]->result;   // the Message the call returned
```

## Stavy factories

```php
use RoundlyConsulting\Messages\Enums\ParticipantRole;
use RoundlyConsulting\Messages\Models\Message;
use RoundlyConsulting\Messages\Models\Participant;
use RoundlyConsulting\Messages\Models\Thread;

$thread = Thread::factory()->public()->create();   // also ->private(), ->direct(), ->open()

Message::factory()->inThread($thread)->system()->create();

Participant::factory()->inThread($thread)->owner()->read()->create();
Participant::factory()->inThread($thread)->role(ParticipantRole::Admin)->unread()->create();
```

Factory účastníka predvolene nastaví participant\_type na user s náhodným id — pre skutočný model odovzdajte participant\_type a participant\_id. read() nastaví záznam tak, ako ho necháva Messages::markRead(): ukazovateľ na najnovšej správe vlákna a vyplnené read\_at; unread() vynuluje oboje.

## Pomocníci a Pest expectations

Pre testy vašej aplikácie pridá trait InteractsWithMessaging fluentných pomocníkov — všetci idú cez manager, takže ich fake zaznamená — a MessageExpectations zaregistruje Pest expectations:

```php
// tests/Pest.php
RoundlyConsulting\Messages\Testing\MessageExpectations::register();

// in a test
uses(RoundlyConsulting\Messages\Testing\InteractsWithMessaging::class);

$thread = $this->startConversation($alice, $bob);
$this->actingAsParticipant($alice)->sendMessageAs($thread, 'Hi');

expect($thread)
    ->toHaveSentMessage('Hi')
    ->toHaveParticipant($bob)
    ->toHaveUnread($bob)
    ->toHaveRole($alice, ParticipantRole::Owner);

expect($reply)->toBeReplyTo($original);
```

- actingAsParticipant($model) — zapamätá si vykonávateľa (actor) pre sendMessageAs() a markReadAs().
- startConversation(...$models) — skupinové vlákno, prvý model je vlastník.
- directThread($a, $b) — nájde alebo vytvorí priamu konverzáciu.
- sendMessageAs($thread, $body, ?$actor) a markReadAs($thread, ?$actor) — vyhodia výnimku, ak vykonávateľ nie je nastavený ani odovzdaný.
- toHaveSentMessage(?$body), toHaveParticipant(), toHaveUnread(), toHaveRole() na Thread; toBeReplyTo() na Message.

## Kompletný test

```php
use RoundlyConsulting\Messages\Testing\InteractsWithMessaging;

uses(InteractsWithMessaging::class);

it('tracks unread state in a direct thread', function () {
    $alice = User::factory()->create();
    $bob = User::factory()->create();

    $thread = $this->directThread($alice, $bob);
    $this->actingAsParticipant($alice)->sendMessageAs($thread, 'Hi Bob');

    expect($thread)
        ->toHaveSentMessage('Hi Bob')
        ->toHaveUnread($bob);

    $this->markReadAs($thread, $bob);

    expect($bob->unreadCount())->toBe(0);
});
```

## Prejavte lásku k open source

Tento balík je zadarmo pod licenciou MIT. Ak vám šetrí čas, jednorazový príspevok alebo členstvo na Patreone nám pomôže ho ďalej udržiavať, testovať a dokumentovať.

[Ďalšie spôsoby podpory vrátane kryptomien](https://roundly-consulting.com/sk/podporte-nas.md)

Odoslaním daru súhlasíte s našimi [podmienkami prijímania darov](https://roundly-consulting.com/sk/podmienky-darov.md).

[Podporte našu open-source prácu (otvorí sa v novej karte)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Pridajte sa k nám na Patreone (otvorí sa v novej karte)](https://www.patreon.com/cw/roundly)

## Chcete to zabudovať do svojho produktu?

Naše balíky integrujeme do zákazkových Laravel a AI riešení. Napíšte nám, na čom pracujete, a ozveme sa do 48 hodín.

[Ponuka do 48 hodín](https://roundly-consulting.com/sk/kontakt.md) [Prezrieť všetky balíky](https://roundly-consulting.com/sk/open-source/navody/messages-for-laravel.md)
