Живые примеры расширения: своя команда, своё уведомление, дополнена документация

This commit is contained in:
rsgrinko committed 2026-09-16 14:31:50 +03:00
1 parent 18dcae02d1
commit c191da5eb9
11 files changed
+422 -61

No files matched your search

+49
View File
@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
/**
* Пример «событие -> уведомление»: config/events.php подписывает
* NoteCreatedNotification на note.created, см. app/Notifications/NoteCreatedNotification.php.
*/
use App\Models\Note;
use Rsgrinko\Proton\Access\Viewer;
use Rsgrinko\Proton\Events\Events;
use Rsgrinko\Proton\Models\Role;
use Rsgrinko\Proton\Models\User;
use Rsgrinko\Proton\Models\UserNotification;
test('note.created заводит уведомление тем, кто видит чужие данные', function (): void {
$admin = User::register('note_notify_admin_' . bin2hex(random_bytes(3)), 'секрет123', [
'email' => 'note_notify' . bin2hex(random_bytes(3)) . '@example.com',
'role_id' => Role::admin()?->id() ?? 0,
]);
assertTrue(Viewer::fromUser($admin)->can(\Rsgrinko\Proton\Access\Permission::DATA_ALL), 'у встроенного администратора право есть само собой');
$before = UserNotification::unreadFor($admin->id());
$note = Note::create(['title' => 'Заметка для примера уведомления']);
try {
// note.created объявляет контроллер после успешного сохранения
// (NotesController::store()), а не сама модель — зовём событие так же,
// как это делает реальный запрос POST /notes
Events::fire('note.created', ['note' => $note]);
assertSame($before + 1, UserNotification::unreadFor($admin->id()));
$notification = UserNotification::query()
->where('user_id', $admin->id())
->orderBy('id', 'desc')
->first();
assertNotNull($notification);
assertContains((string) $note->title, (string) $notification->raw('title'));
} finally {
$note->forceDelete();
UserNotification::query()->where('user_id', $admin->id())->forceDelete();
$admin->forceDelete();
}
});
+53
View File
@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
/**
* notes:seed — пример своей консольной команды (см. app/Commands/NotesSeedCommand.php
* и config/commands.php).
*/
use App\Commands\NotesSeedCommand;
use App\Models\Note;
use Rsgrinko\Proton\Models\Role;
use Rsgrinko\Proton\Models\User;
test('notes:seed заводит нужное число заметок для указанного пользователя', function (): void {
$user = User::register('notes_seed_' . bin2hex(random_bytes(3)), 'секрет123', [
'email' => 'notes_seed' . bin2hex(random_bytes(3)) . '@example.com',
'role_id' => Role::admin()?->id() ?? 0,
]);
afterTests(static function () use ($user): void {
Note::query()->withTrashed()->where('user_id', $user->id())->forceDelete();
$user->forceDelete();
});
$out = '';
$status = (new NotesSeedCommand())
->withInput([(string) $user->login], ['count' => '3'], static function (string $line) use (&$out): void {
$out .= $line . "\n";
})
->run();
assertSame(0, $status);
assertContains('Заведено заметок: 3', $out);
assertSame(3, Note::query()->where('user_id', $user->id())->count());
});
test('notes:seed без пользователя отвечает понятной ошибкой, а не падением', function (): void {
$status = (new NotesSeedCommand())
->withInput(['нет_такого_логина_' . bin2hex(random_bytes(4))], [], static function (): void {})
->run();
assertSame(1, $status);
});
test('команда зарегистрирована и видна в списке команд', function (): void {
assertTrue(in_array(
NotesSeedCommand::class,
array_merge(...array_values(Rsgrinko\Proton\Console\Application::groups())),
true
));
});