You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/TicketNotificationDispatche...

76 lines
2.8 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\Ticket;
use App\Services\Ticket\TicketEvent;
use App\Services\Ticket\TicketNotificationDispatcher;
use App\Services\Ticket\TicketNotificationTransport;
use App\Services\Ticket\TicketWriteResult;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$capsule = new Capsule();
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$GLOBALS['System_Config'] = ['baseUrl' => 'https://panel.example.test'];
$schema = $capsule->getConnection('default')->getSchemaBuilder();
$schema->create('user', static function (Blueprint $table): void {
$table->increments('id');
$table->string('user_name');
$table->string('email');
$table->boolean('is_admin')->default(false);
});
Capsule::table('user')->insert([
['id' => 7, 'user_name' => 'customer', 'email' => 'owner@example.test', 'is_admin' => 0],
['id' => 9, 'user_name' => 'admin-one', 'email' => 'admin1@example.test', 'is_admin' => 1],
['id' => 10, 'user_name' => 'admin-two', 'email' => 'admin2@example.test', 'is_admin' => 1],
]);
$root = new Ticket();
$root->id = 44;
$root->userid = 7;
$root->title = 'Network issue';
$root->content = 'Cannot connect';
$root->rootid = 0;
$message = clone $root;
$result = new TicketWriteResult($root, $message, '新工单', []);
$transport = new class implements TicketNotificationTransport {
public array $mail = [];
public array $telegram = [];
public function sendMail(string $email, string $subject, string $template, array $data, ?string $event = null): void
{
$this->mail[] = compact('email', 'subject', 'template', 'data', 'event');
if ($email === 'owner@example.test') {
throw new RuntimeException('mail outage');
}
}
public function sendTelegram(TicketEvent $event): void
{
$this->telegram[] = $event;
}
};
$report = (new TicketNotificationDispatcher($transport))->dispatch(TicketEvent::created($result));
$assert(count($transport->mail) === 3,
'A failed owner confirmation blocked administrator email delivery.');
$assert(count($transport->telegram) === 1,
'A failed email blocked the independent Telegram channel.');
$assert($transport->telegram[0] instanceof TicketEvent
&& $transport->telegram[0]->result === $result,
'Telegram transport did not receive the complete ticket event.');
$assert($report->attempted === 4 && $report->succeeded === 3 && count($report->errors) === 1,
'Notification report does not describe isolated channel outcomes.');
echo "Ticket notification isolation checks passed.\n";