fix: move local packages inside repo so remote server can install them
This commit is contained in:
1 parent
50e5f3e381
commit
30b2fb3958
40 files changed
+1671
-2
No files matched your search
Submodule packages/ultra-message deleted from 5616023580.
@@ -0,0 +1,2 @@
|
||||
/vendor/
|
||||
composer.lock
|
||||
@@ -0,0 +1,42 @@
|
||||
{
|
||||
"name": "promoseven/ultra-message",
|
||||
"description": "Laravel WhatsApp integration via UltraMSG API",
|
||||
"type": "library",
|
||||
"license": "MIT",
|
||||
"require": {
|
||||
"php": "^8.2",
|
||||
"illuminate/support": "^11.0|^12.0",
|
||||
"illuminate/http": "^11.0|^12.0",
|
||||
"illuminate/notifications": "^11.0|^12.0",
|
||||
"illuminate/routing": "^11.0|^12.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^11.0",
|
||||
"orchestra/testbench": "^10.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"PromoSeven\\UltraMessage\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"PromoSeven\\UltraMessage\\Tests\\": "tests/"
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"laravel": {
|
||||
"providers": [
|
||||
"PromoSeven\\UltraMessage\\UltraMessageServiceProvider"
|
||||
],
|
||||
"aliases": {
|
||||
"UltraMessage": "PromoSeven\\UltraMessage\\Facades\\UltraMessage"
|
||||
}
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"test": "vendor/bin/phpunit"
|
||||
},
|
||||
"minimum-stability": "stable",
|
||||
"prefer-stable": true
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'instance_id' => env('ULTRAMSG_INSTANCE_ID'),
|
||||
'token' => env('ULTRAMSG_TOKEN'),
|
||||
'webhook_secret' => env('ULTRAMSG_WEBHOOK_SECRET', null),
|
||||
'webhook_path' => env('ULTRAMSG_WEBHOOK_PATH', 'ultra-message/webhook'),
|
||||
'timeout' => env('ULTRAMSG_TIMEOUT', 30),
|
||||
'enabled' => env('ULTRAMSG_ENABLED', true),
|
||||
];
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
|
||||
bootstrap="vendor/autoload.php"
|
||||
colors="true">
|
||||
<testsuites>
|
||||
<testsuite name="Unit">
|
||||
<directory>tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
</phpunit>
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use PromoSeven\UltraMessage\Http\Controllers\WebhookController;
|
||||
|
||||
Route::post(config('ultra-message.webhook_path', 'ultra-message/webhook'), [WebhookController::class, 'handle'])
|
||||
->name('ultra-message.webhook');
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace PromoSeven\UltraMessage\Events;
|
||||
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class UltraMessageWebhookReceived
|
||||
{
|
||||
use Dispatchable, SerializesModels;
|
||||
|
||||
public function __construct(public readonly array $payload) {}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace PromoSeven\UltraMessage\Facades;
|
||||
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
use PromoSeven\UltraMessage\UltraMessageClient;
|
||||
use PromoSeven\UltraMessage\UltraMessageFake;
|
||||
|
||||
/**
|
||||
* @method static array sendText(string $to, string $message, ?string $replyId = null)
|
||||
* @method static array sendImage(string $to, string $imageUrl, string $caption = '')
|
||||
* @method static array sendDocument(string $to, string $fileUrl, string $filename, string $caption = '')
|
||||
* @method static array sendAudio(string $to, string $audioUrl)
|
||||
* @method static array sendVoice(string $to, string $audioUrl)
|
||||
* @method static array sendVideo(string $to, string $videoUrl, string $caption = '')
|
||||
* @method static array sendSticker(string $to, string $stickerUrl)
|
||||
* @method static array sendContact(string $to, string $contactId)
|
||||
* @method static array sendLocation(string $to, float $lat, float $lng, string $address = '')
|
||||
* @method static array sendReaction(string $to, string $messageId, string $emoji)
|
||||
* @method static array deleteMessage(string $messageId)
|
||||
* @method static array getInstanceStatus()
|
||||
* @method static array getChats()
|
||||
* @method static array getContacts()
|
||||
* @method static array getGroups()
|
||||
*
|
||||
* @see \PromoSeven\UltraMessage\UltraMessageClient
|
||||
*/
|
||||
class UltraMessage extends Facade
|
||||
{
|
||||
protected static function getFacadeAccessor(): string
|
||||
{
|
||||
return UltraMessageClient::class;
|
||||
}
|
||||
|
||||
public static function fake(): UltraMessageFake
|
||||
{
|
||||
$fake = new UltraMessageFake();
|
||||
static::swap($fake);
|
||||
return $fake;
|
||||
}
|
||||
|
||||
public static function configUsing(callable $resolver): void
|
||||
{
|
||||
app()->instance('ultra-message.config-resolver', $resolver);
|
||||
app()->forgetInstance(UltraMessageClient::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace PromoSeven\UltraMessage\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Routing\Controller;
|
||||
use PromoSeven\UltraMessage\Events\UltraMessageWebhookReceived;
|
||||
|
||||
class WebhookController extends Controller
|
||||
{
|
||||
public function handle(Request $request): Response
|
||||
{
|
||||
$secret = config('ultra-message.webhook_secret');
|
||||
|
||||
if ($secret) {
|
||||
$signature = $request->header('X-Hub-Signature-256', '');
|
||||
$expected = 'sha256=' . hash_hmac('sha256', $request->getContent(), $secret);
|
||||
|
||||
if (!hash_equals($expected, $signature)) {
|
||||
abort(403, 'Invalid webhook signature.');
|
||||
}
|
||||
}
|
||||
|
||||
event(new UltraMessageWebhookReceived($request->all()));
|
||||
|
||||
return response('OK', 200);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace PromoSeven\UltraMessage;
|
||||
|
||||
use Illuminate\Notifications\Notification;
|
||||
|
||||
class UltraMessageChannel
|
||||
{
|
||||
public function __construct(private UltraMessageClient $client) {}
|
||||
|
||||
public function send(mixed $notifiable, Notification $notification): void
|
||||
{
|
||||
if (!method_exists($notification, 'toUltraMessage')) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var UltraMessageMessage $message */
|
||||
$message = $notification->toUltraMessage($notifiable);
|
||||
|
||||
$to = $message->to ?: $notifiable->routeNotificationFor('ultra_message', $notification);
|
||||
|
||||
if (!$to) {
|
||||
return;
|
||||
}
|
||||
|
||||
match ($message->type) {
|
||||
'text' => $this->client->sendText($to, $message->payload['body'], $message->payload['quoted_id'] ?? null),
|
||||
'image' => $this->client->sendImage($to, $message->payload['image'], $message->payload['caption'] ?? ''),
|
||||
'document' => $this->client->sendDocument($to, $message->payload['document'], $message->payload['filename'], $message->payload['caption'] ?? ''),
|
||||
'audio' => $this->client->sendAudio($to, $message->payload['audio']),
|
||||
'voice' => $this->client->sendVoice($to, $message->payload['audio']),
|
||||
'video' => $this->client->sendVideo($to, $message->payload['video'], $message->payload['caption'] ?? ''),
|
||||
'sticker' => $this->client->sendSticker($to, $message->payload['sticker']),
|
||||
'contact' => $this->client->sendContact($to, $message->payload['contact']),
|
||||
'location' => $this->client->sendLocation($to, $message->payload['lat'], $message->payload['lng'], $message->payload['address'] ?? ''),
|
||||
default => throw new UltraMessageException("Unknown message type: {$message->type}"),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
|
||||
namespace PromoSeven\UltraMessage;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class UltraMessageClient
|
||||
{
|
||||
private const BASE_URL = 'https://api.ultramsg.com';
|
||||
|
||||
private string $instanceId;
|
||||
private string $token;
|
||||
private int $timeout;
|
||||
private bool $enabled;
|
||||
|
||||
public function __construct(array $config)
|
||||
{
|
||||
$this->instanceId = $config['instance_id'] ?? '';
|
||||
$this->token = $config['token'] ?? '';
|
||||
$this->timeout = $config['timeout'] ?? 30;
|
||||
$this->enabled = $config['enabled'] ?? true;
|
||||
}
|
||||
|
||||
protected function post(string $endpoint, array $data): array
|
||||
{
|
||||
if (!$this->enabled) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$response = Http::timeout($this->timeout)
|
||||
->asForm()
|
||||
->post(self::BASE_URL . "/{$this->instanceId}/{$endpoint}", array_merge($data, [
|
||||
'token' => $this->token,
|
||||
]));
|
||||
|
||||
if ($response->failed()) {
|
||||
throw new UltraMessageException("UltraMSG HTTP error: {$response->status()}");
|
||||
}
|
||||
|
||||
$body = $response->json() ?? [];
|
||||
|
||||
if (isset($body['error'])) {
|
||||
throw new UltraMessageException($body['error']);
|
||||
}
|
||||
|
||||
return $body;
|
||||
}
|
||||
|
||||
protected function get(string $endpoint, array $query = []): array
|
||||
{
|
||||
if (!$this->enabled) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$response = Http::timeout($this->timeout)
|
||||
->get(self::BASE_URL . "/{$this->instanceId}/{$endpoint}", array_merge($query, [
|
||||
'token' => $this->token,
|
||||
]));
|
||||
|
||||
if ($response->failed()) {
|
||||
throw new UltraMessageException("UltraMSG HTTP error: {$response->status()}");
|
||||
}
|
||||
|
||||
$body = $response->json() ?? [];
|
||||
|
||||
if (isset($body['error'])) {
|
||||
throw new UltraMessageException($body['error']);
|
||||
}
|
||||
|
||||
return $body;
|
||||
}
|
||||
|
||||
public function sendText(string $to, string $message, ?string $replyId = null): array
|
||||
{
|
||||
$data = ['to' => $to, 'body' => $message];
|
||||
if ($replyId !== null) {
|
||||
$data['quoted_id'] = $replyId;
|
||||
}
|
||||
return $this->post('messages/chat', $data);
|
||||
}
|
||||
|
||||
public function sendImage(string $to, string $imageUrl, string $caption = ''): array
|
||||
{
|
||||
return $this->post('messages/image', [
|
||||
'to' => $to,
|
||||
'image' => $imageUrl,
|
||||
'caption' => $caption,
|
||||
]);
|
||||
}
|
||||
|
||||
public function sendDocument(string $to, string $fileUrl, string $filename, string $caption = ''): array
|
||||
{
|
||||
return $this->post('messages/document', [
|
||||
'to' => $to,
|
||||
'document' => $fileUrl,
|
||||
'filename' => $filename,
|
||||
'caption' => $caption,
|
||||
]);
|
||||
}
|
||||
|
||||
public function sendAudio(string $to, string $audioUrl): array
|
||||
{
|
||||
return $this->post('messages/audio', [
|
||||
'to' => $to,
|
||||
'audio' => $audioUrl,
|
||||
]);
|
||||
}
|
||||
|
||||
public function sendVoice(string $to, string $audioUrl): array
|
||||
{
|
||||
return $this->post('messages/voice', [
|
||||
'to' => $to,
|
||||
'audio' => $audioUrl,
|
||||
]);
|
||||
}
|
||||
|
||||
public function sendVideo(string $to, string $videoUrl, string $caption = ''): array
|
||||
{
|
||||
return $this->post('messages/video', [
|
||||
'to' => $to,
|
||||
'video' => $videoUrl,
|
||||
'caption' => $caption,
|
||||
]);
|
||||
}
|
||||
|
||||
public function sendSticker(string $to, string $stickerUrl): array
|
||||
{
|
||||
return $this->post('messages/sticker', [
|
||||
'to' => $to,
|
||||
'sticker' => $stickerUrl,
|
||||
]);
|
||||
}
|
||||
|
||||
public function sendContact(string $to, string $contactId): array
|
||||
{
|
||||
return $this->post('messages/contact', [
|
||||
'to' => $to,
|
||||
'contact' => $contactId,
|
||||
]);
|
||||
}
|
||||
|
||||
public function sendLocation(string $to, float $lat, float $lng, string $address = ''): array
|
||||
{
|
||||
return $this->post('messages/location', [
|
||||
'to' => $to,
|
||||
'lat' => $lat,
|
||||
'lng' => $lng,
|
||||
'address' => $address,
|
||||
]);
|
||||
}
|
||||
|
||||
public function sendReaction(string $to, string $messageId, string $emoji): array
|
||||
{
|
||||
return $this->post('messages/reaction', [
|
||||
'to' => $to,
|
||||
'msgId' => $messageId,
|
||||
'emoji' => $emoji,
|
||||
]);
|
||||
}
|
||||
|
||||
public function deleteMessage(string $messageId): array
|
||||
{
|
||||
return $this->post('messages/delete', [
|
||||
'msgId' => $messageId,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getInstanceStatus(): array
|
||||
{
|
||||
return $this->get('instance/status');
|
||||
}
|
||||
|
||||
public function getChats(): array
|
||||
{
|
||||
return $this->get('chats/');
|
||||
}
|
||||
|
||||
public function getContacts(): array
|
||||
{
|
||||
return $this->get('contacts/');
|
||||
}
|
||||
|
||||
public function getGroups(): array
|
||||
{
|
||||
return $this->get('groups/');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace PromoSeven\UltraMessage;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class UltraMessageException extends RuntimeException {}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace PromoSeven\UltraMessage;
|
||||
|
||||
use PHPUnit\Framework\Assert;
|
||||
|
||||
class UltraMessageFake extends UltraMessageClient
|
||||
{
|
||||
private array $sent = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
// Skip parent constructor — no HTTP config needed in fake mode
|
||||
}
|
||||
|
||||
protected function post(string $endpoint, array $data): array
|
||||
{
|
||||
$this->sent[] = ['endpoint' => $endpoint, 'data' => $data];
|
||||
return ['sent' => 'ok'];
|
||||
}
|
||||
|
||||
protected function get(string $endpoint, array $query = []): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function assertSent(callable $callback): void
|
||||
{
|
||||
Assert::assertTrue(
|
||||
collect($this->sent)->contains($callback),
|
||||
'Expected UltraMessage was not sent.'
|
||||
);
|
||||
}
|
||||
|
||||
public function assertNotSent(): void
|
||||
{
|
||||
Assert::assertEmpty($this->sent, 'Unexpected UltraMessage messages were sent.');
|
||||
}
|
||||
|
||||
public function assertSentCount(int $count): void
|
||||
{
|
||||
Assert::assertCount($count, $this->sent, "Expected {$count} messages sent, got " . count($this->sent));
|
||||
}
|
||||
|
||||
public function getSent(): array
|
||||
{
|
||||
return $this->sent;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace PromoSeven\UltraMessage;
|
||||
|
||||
class UltraMessageMessage
|
||||
{
|
||||
public string $type;
|
||||
public string $to = '';
|
||||
public array $payload = [];
|
||||
|
||||
private function __construct(string $type, array $payload)
|
||||
{
|
||||
$this->type = $type;
|
||||
$this->payload = $payload;
|
||||
}
|
||||
|
||||
public static function text(string $message, ?string $replyId = null): self
|
||||
{
|
||||
return new self('text', ['body' => $message, 'quoted_id' => $replyId]);
|
||||
}
|
||||
|
||||
public static function image(string $url, string $caption = ''): self
|
||||
{
|
||||
return new self('image', ['image' => $url, 'caption' => $caption]);
|
||||
}
|
||||
|
||||
public static function document(string $url, string $filename, string $caption = ''): self
|
||||
{
|
||||
return new self('document', ['document' => $url, 'filename' => $filename, 'caption' => $caption]);
|
||||
}
|
||||
|
||||
public static function audio(string $url): self
|
||||
{
|
||||
return new self('audio', ['audio' => $url]);
|
||||
}
|
||||
|
||||
public static function voice(string $url): self
|
||||
{
|
||||
return new self('voice', ['audio' => $url]);
|
||||
}
|
||||
|
||||
public static function video(string $url, string $caption = ''): self
|
||||
{
|
||||
return new self('video', ['video' => $url, 'caption' => $caption]);
|
||||
}
|
||||
|
||||
public static function sticker(string $url): self
|
||||
{
|
||||
return new self('sticker', ['sticker' => $url]);
|
||||
}
|
||||
|
||||
public static function contact(string $contactId): self
|
||||
{
|
||||
return new self('contact', ['contact' => $contactId]);
|
||||
}
|
||||
|
||||
public static function location(float $lat, float $lng, string $address = ''): self
|
||||
{
|
||||
return new self('location', ['lat' => $lat, 'lng' => $lng, 'address' => $address]);
|
||||
}
|
||||
|
||||
public function to(string $number): self
|
||||
{
|
||||
$this->to = $number;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace PromoSeven\UltraMessage;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class UltraMessageServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register(): void
|
||||
{
|
||||
$this->mergeConfigFrom(__DIR__ . '/../config/ultra-message.php', 'ultra-message');
|
||||
|
||||
$this->app->singleton('ultra-message.config-resolver', fn() => null);
|
||||
|
||||
$this->app->bind(UltraMessageClient::class, function ($app) {
|
||||
$resolver = $app->make('ultra-message.config-resolver');
|
||||
$config = $resolver ? call_user_func($resolver) : config('ultra-message');
|
||||
|
||||
return new UltraMessageClient($config);
|
||||
});
|
||||
|
||||
$this->app->bind(UltraMessageChannel::class, function ($app) {
|
||||
return new UltraMessageChannel($app->make(UltraMessageClient::class));
|
||||
});
|
||||
}
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
if ($this->app->runningInConsole()) {
|
||||
$this->publishes([
|
||||
__DIR__ . '/../config/ultra-message.php' => config_path('ultra-message.php'),
|
||||
], 'ultra-message-config');
|
||||
}
|
||||
|
||||
$this->loadRoutesFrom(__DIR__ . '/../routes/webhook.php');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace PromoSeven\UltraMessage\Tests;
|
||||
|
||||
use Orchestra\Testbench\TestCase as BaseTestCase;
|
||||
use PromoSeven\UltraMessage\UltraMessageServiceProvider;
|
||||
|
||||
abstract class TestCase extends BaseTestCase
|
||||
{
|
||||
protected function getPackageProviders($app): array
|
||||
{
|
||||
return [UltraMessageServiceProvider::class];
|
||||
}
|
||||
|
||||
protected function getEnvironmentSetUp($app): void
|
||||
{
|
||||
$app['config']->set('ultra-message.instance_id', 'instance123');
|
||||
$app['config']->set('ultra-message.token', 'test-token');
|
||||
$app['config']->set('ultra-message.enabled', true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace PromoSeven\UltraMessage\Tests;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Notifications\Notification;
|
||||
use PromoSeven\UltraMessage\UltraMessageChannel;
|
||||
use PromoSeven\UltraMessage\UltraMessageClient;
|
||||
use PromoSeven\UltraMessage\UltraMessageMessage;
|
||||
|
||||
class UltraMessageChannelTest extends TestCase
|
||||
{
|
||||
public function test_channel_calls_send_text_for_text_message(): void
|
||||
{
|
||||
Http::fake(['api.ultramsg.com/*' => Http::response(['sent' => 'true'], 200)]);
|
||||
|
||||
$client = new UltraMessageClient([
|
||||
'instance_id' => 'instance123',
|
||||
'token' => 'test-token',
|
||||
'timeout' => 30,
|
||||
'enabled' => true,
|
||||
]);
|
||||
$channel = new UltraMessageChannel($client);
|
||||
|
||||
$notifiable = new class {
|
||||
public string $whatsapp_number = '+971501234567';
|
||||
public function routeNotificationFor(string $channel, $notification = null): string
|
||||
{
|
||||
return $this->whatsapp_number;
|
||||
}
|
||||
};
|
||||
|
||||
$notification = new class extends Notification {
|
||||
public function toUltraMessage($notifiable): UltraMessageMessage
|
||||
{
|
||||
return UltraMessageMessage::text('Test message');
|
||||
}
|
||||
};
|
||||
|
||||
$channel->send($notifiable, $notification);
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return str_contains($request->url(), 'messages/chat')
|
||||
&& $request['body'] === 'Test message'
|
||||
&& $request['to'] === '+971501234567';
|
||||
});
|
||||
}
|
||||
|
||||
public function test_channel_uses_message_to_over_notifiable_route(): void
|
||||
{
|
||||
Http::fake(['api.ultramsg.com/*' => Http::response(['sent' => 'true'], 200)]);
|
||||
|
||||
$client = new UltraMessageClient([
|
||||
'instance_id' => 'instance123',
|
||||
'token' => 'test-token',
|
||||
'timeout' => 30,
|
||||
'enabled' => true,
|
||||
]);
|
||||
$channel = new UltraMessageChannel($client);
|
||||
|
||||
$notifiable = new class {
|
||||
public function routeNotificationFor(string $channel, $notification = null): string
|
||||
{
|
||||
return '+9710000000';
|
||||
}
|
||||
};
|
||||
|
||||
$notification = new class extends Notification {
|
||||
public function toUltraMessage($notifiable): UltraMessageMessage
|
||||
{
|
||||
return UltraMessageMessage::text('Override test')->to('+971999999');
|
||||
}
|
||||
};
|
||||
|
||||
$channel->send($notifiable, $notification);
|
||||
|
||||
Http::assertSent(fn($r) => $r['to'] === '+971999999');
|
||||
}
|
||||
|
||||
public function test_channel_skips_when_no_recipient(): void
|
||||
{
|
||||
Http::fake();
|
||||
|
||||
$client = new UltraMessageClient([
|
||||
'instance_id' => 'instance123',
|
||||
'token' => 'test-token',
|
||||
'timeout' => 30,
|
||||
'enabled' => true,
|
||||
]);
|
||||
$channel = new UltraMessageChannel($client);
|
||||
|
||||
$notifiable = new class {
|
||||
public function routeNotificationFor(string $channel, $notification = null): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
$notification = new class extends Notification {
|
||||
public function toUltraMessage($notifiable): UltraMessageMessage
|
||||
{
|
||||
return UltraMessageMessage::text('No recipient');
|
||||
}
|
||||
};
|
||||
|
||||
$channel->send($notifiable, $notification);
|
||||
|
||||
Http::assertNothingSent();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
namespace PromoSeven\UltraMessage\Tests;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use PromoSeven\UltraMessage\UltraMessageClient;
|
||||
use PromoSeven\UltraMessage\UltraMessageException;
|
||||
|
||||
class UltraMessageClientTest extends TestCase
|
||||
{
|
||||
private UltraMessageClient $client;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->client = new UltraMessageClient([
|
||||
'instance_id' => 'instance123',
|
||||
'token' => 'test-token',
|
||||
'timeout' => 30,
|
||||
'enabled' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_send_text_posts_correct_payload(): void
|
||||
{
|
||||
Http::fake([
|
||||
'api.ultramsg.com/*' => Http::response(['sent' => 'true', 'id' => 'msg1'], 200),
|
||||
]);
|
||||
|
||||
$result = $this->client->sendText('+971501234567', 'Hello World');
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return str_contains($request->url(), 'instance123/messages/chat')
|
||||
&& $request['to'] === '+971501234567'
|
||||
&& $request['body'] === 'Hello World'
|
||||
&& $request['token'] === 'test-token';
|
||||
});
|
||||
|
||||
$this->assertEquals(['sent' => 'true', 'id' => 'msg1'], $result);
|
||||
}
|
||||
|
||||
public function test_send_text_throws_on_api_error(): void
|
||||
{
|
||||
Http::fake([
|
||||
'api.ultramsg.com/*' => Http::response(['error' => 'invalid token'], 200),
|
||||
]);
|
||||
|
||||
$this->expectException(UltraMessageException::class);
|
||||
$this->expectExceptionMessage('invalid token');
|
||||
|
||||
$this->client->sendText('+971501234567', 'Hello');
|
||||
}
|
||||
|
||||
public function test_send_text_throws_on_http_failure(): void
|
||||
{
|
||||
Http::fake([
|
||||
'api.ultramsg.com/*' => Http::response([], 500),
|
||||
]);
|
||||
|
||||
$this->expectException(UltraMessageException::class);
|
||||
|
||||
$this->client->sendText('+971501234567', 'Hello');
|
||||
}
|
||||
|
||||
public function test_send_returns_early_when_disabled(): void
|
||||
{
|
||||
Http::fake();
|
||||
|
||||
$client = new UltraMessageClient([
|
||||
'instance_id' => 'instance123',
|
||||
'token' => 'test-token',
|
||||
'timeout' => 30,
|
||||
'enabled' => false,
|
||||
]);
|
||||
|
||||
$result = $client->sendText('+971501234567', 'Hello');
|
||||
|
||||
Http::assertNothingSent();
|
||||
$this->assertEquals([], $result);
|
||||
}
|
||||
|
||||
public function test_send_image_posts_correct_payload(): void
|
||||
{
|
||||
Http::fake([
|
||||
'api.ultramsg.com/*' => Http::response(['sent' => 'true'], 200),
|
||||
]);
|
||||
|
||||
$this->client->sendImage('+971501234567', 'https://example.com/img.jpg', 'Caption');
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return str_contains($request->url(), 'instance123/messages/image')
|
||||
&& $request['image'] === 'https://example.com/img.jpg'
|
||||
&& $request['caption'] === 'Caption';
|
||||
});
|
||||
}
|
||||
|
||||
public function test_send_document_posts_correct_payload(): void
|
||||
{
|
||||
Http::fake([
|
||||
'api.ultramsg.com/*' => Http::response(['sent' => 'true'], 200),
|
||||
]);
|
||||
|
||||
$this->client->sendDocument('+971501234567', 'https://example.com/file.pdf', 'invoice.pdf', 'Your invoice');
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return str_contains($request->url(), 'instance123/messages/document')
|
||||
&& $request['document'] === 'https://example.com/file.pdf'
|
||||
&& $request['filename'] === 'invoice.pdf'
|
||||
&& $request['caption'] === 'Your invoice';
|
||||
});
|
||||
}
|
||||
|
||||
public function test_send_location_posts_correct_payload(): void
|
||||
{
|
||||
Http::fake([
|
||||
'api.ultramsg.com/*' => Http::response(['sent' => 'true'], 200),
|
||||
]);
|
||||
|
||||
$this->client->sendLocation('+971501234567', 25.197197, 55.2721877, 'Dubai, UAE');
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return str_contains($request->url(), 'instance123/messages/location')
|
||||
&& $request['lat'] == 25.197197
|
||||
&& $request['lng'] == 55.2721877
|
||||
&& $request['address'] === 'Dubai, UAE';
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user