diff --git a/CHANGELOG.md b/CHANGELOG.md index 3515cbf5..24546825 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,7 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added connection state validation with `ensureConnection()` method - **Environment Configuration**: Fixed path handling by removing unreliable `realpath()` usage - **Configuration Loader**: Improved validation and error handling -- **Messaging System**: Fixed PHPUnit mock issues and corrected type signatures +- **Notifier System**: Fixed PHPUnit mock issues and corrected type signatures - **Test Suite**: Renamed test methods to snake_case for consistency - **Database Tests**: Significantly expanded test coverage across connection, migration, pagination, and query builders @@ -44,7 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **FTP Service**: Fixed directory listing parser to handle filenames with spaces - **FTP Service**: Improved error messages with connection details - **Environment Configuration**: Fixed `Env::configure()` error handling -- **Queue Tests**: Fixed mock configuration issues in MessagingTest +- **Queue Tests**: Fixed mock configuration issues in NotifierTest - **Notification Tests**: Added missing timestamp columns in test schema ### Improved diff --git a/readme.md b/readme.md index 8a3cadeb..9c764913 100644 --- a/readme.md +++ b/readme.md @@ -151,7 +151,7 @@ The project is organized into the following directories, each representing an in - **Event/**: Event management and dispatching. - **Http/**: HTTP requests and responses management. - **Mail/**: Email sending and configuration. - - **Messaging/**: Messaging and notifications. + - **Notifier/**: Notifications. - **Middleware/**: Middleware classes for request handling. - **Queue/**: Job queues and background processing. - **Router/**: HTTP request routing. diff --git a/src/Configuration/EnvConfiguration.php b/src/Configuration/EnvConfiguration.php index f4dd4d7d..2ca6a22d 100644 --- a/src/Configuration/EnvConfiguration.php +++ b/src/Configuration/EnvConfiguration.php @@ -13,11 +13,13 @@ class EnvConfiguration extends Configuration */ public function create(Loader $config): void { - Env::configure(base_path('.env.json')); + $this->container->bind('env', function () use ($config) { + Env::configure($config['app.env_file'] ?? null); - $event = Env::getInstance(); + $event = Env::getInstance(); - $this->container->instance('env', $event); + $this->container->instance('env', $event); + }); } /** @@ -25,6 +27,6 @@ public function create(Loader $config): void */ public function run(): void { - // Nothing to do + // $this->container->make('env'); } } diff --git a/src/Configuration/Loader.php b/src/Configuration/Loader.php index f86e3d7f..660511ef 100644 --- a/src/Configuration/Loader.php +++ b/src/Configuration/Loader.php @@ -177,11 +177,13 @@ public function boot(): Loader $container = Capsule::getInstance(); // Load the env configuration first - $this->createConfiguration(EnvConfiguration::class, $container); + $env_config = $this->createConfiguration(EnvConfiguration::class, $container); // Load the .env or .env.json file $this->loadEnvfile(); + $env_config->run(); + // Configuration of services $loaded_configurations = $this->createConfigurations( array_merge([CompassConfiguration::class], $this->configurations()), @@ -339,22 +341,6 @@ public function events(): array ]; } - /** - * __invoke - * - * @param string $key - * @param mixed $value - * @return mixed - */ - public function __invoke(string $key, mixed $value = null): mixed - { - if ($value == null) { - return $this->config[$key]; - } - - return $this->config[$key] = $value; - } - /** * @inheritDoc */ @@ -391,4 +377,20 @@ public function offsetUnset(mixed $offset): void { $this->config->offsetUnset($offset); } + + /** + * __invoke + * + * @param string $key + * @param mixed $value + * @return mixed + */ + public function __invoke(string $key, mixed $value = null): mixed + { + if ($value == null) { + return $this->config[$key]; + } + + return $this->config[$key] = $value; + } } diff --git a/src/Console/Command.php b/src/Console/Command.php index 72cd5fc2..ce69dccb 100644 --- a/src/Console/Command.php +++ b/src/Console/Command.php @@ -21,7 +21,7 @@ use Bow\Console\Command\Generator\GenerateSessionCommand; use Bow\Console\Command\Generator\GenerateAppEventCommand; use Bow\Console\Command\Generator\GenerateExceptionCommand; -use Bow\Console\Command\Generator\GenerateMessagingCommand; +use Bow\Console\Command\Generator\GenerateNotifierCommand; use Bow\Console\Command\Generator\GenerateMigrationCommand; use Bow\Console\Command\Generator\GenerateControllerCommand; use Bow\Console\Command\Generator\GenerateMiddlewareCommand; @@ -59,7 +59,7 @@ class Command extends AbstractCommand "add:listener" => GenerateEventListenerCommand::class, "add:job" => GenerateJobCommand::class, "add:command" => GenerateConsoleCommand::class, - "add:message" => GenerateMessagingCommand::class, + "add:notifier" => GenerateNotifierCommand::class, "run:console" => ReplCommand::class, "run:server" => ServerCommand::class, "run:worker" => WorkerCommand::class, diff --git a/src/Console/Command/Generator/GenerateMessagingCommand.php b/src/Console/Command/Generator/GenerateMessagingCommand.php deleted file mode 100644 index 89915d06..00000000 --- a/src/Console/Command/Generator/GenerateMessagingCommand.php +++ /dev/null @@ -1,40 +0,0 @@ -setting->getMessagingDirectory(), - $messaging - ); - - if ($generator->fileExists()) { - echo Color::red("The messaging already exists"); - - exit(1); - } - - $generator->write('messaging', [ - 'baseNamespace' => $this->namespaces['messaging'] ?? "App\\Messaging", - ]); - - echo Color::green("The messaging has been well created."); - - exit(0); - } -} diff --git a/src/Console/Command/Generator/GenerateNotifierCommand.php b/src/Console/Command/Generator/GenerateNotifierCommand.php new file mode 100644 index 00000000..b5f62d3d --- /dev/null +++ b/src/Console/Command/Generator/GenerateNotifierCommand.php @@ -0,0 +1,39 @@ +setting->getNotifierDirectory(), + $notifier + ); + + if ($generator->fileExists()) { + echo Color::red("The notifier already exists"); + + exit(1); + } + + $generator->write('notifier', [ + 'baseNamespace' => $this->namespaces['notifier'] ?? "App\\Notifier", + ]); + + echo Color::green("The notifier {$this->setting->getNotifierDirectory()}/{$notifier} has been well created."); + exit(0); + } +} diff --git a/src/Console/Command/Generator/GenerateRouterResourceCommand.php b/src/Console/Command/Generator/GenerateRouterResourceCommand.php index 2b89464b..3179f3c2 100644 --- a/src/Console/Command/Generator/GenerateRouterResourceCommand.php +++ b/src/Console/Command/Generator/GenerateRouterResourceCommand.php @@ -99,15 +99,12 @@ private function createResourceController( string $controller, string $model_namespace = '' ): void { - $generator->write( - 'controller/rest', - [ + $generator->write('controller/rest', [ 'modelNamespace' => $model_namespace, 'prefix' => $prefix, 'className' => $controller, 'baseNamespace' => $this->namespaces['controller'] ?? 'App\\Controllers' - ] - ); + ]); echo Color::green('The controller Rest was well created.'); } diff --git a/src/Console/Console.php b/src/Console/Console.php index 3b080b75..58afd969 100644 --- a/src/Console/Console.php +++ b/src/Console/Console.php @@ -63,7 +63,7 @@ class Console 'job', 'command', 'listener', - 'message' + 'notifier' ]; /** @@ -542,7 +542,7 @@ private function help(?string $command = null): int \033[0;33madd:listener\033[00m Create a new event listener \033[0;33madd:job\033[00m Create a new job \033[0;33madd:command\033[00m Create a new console command - \033[0;33madd:message\033[00m Create a new messaging handler + \033[0;33madd:notifier\033[00m Create a new messaging handler \033[0;32mMIGRATION\033[00m Apply migration to database \033[0;33mmigration:migrate\033[00m Run migrations @@ -595,7 +595,7 @@ private function help(?string $command = null): int \033[0;33m$\033[00m php \033[0;34mbow\033[00m add:event name Create a new event listener \033[0;33m$\033[00m php \033[0;34mbow\033[00m add:job name Create a new queue job \033[0;33m$\033[00m php \033[0;34mbow\033[00m add:command name Create a new console command - \033[0;33m$\033[00m php \033[0;34mbow\033[00m add:message name Create a new messaging handler + \033[0;33m$\033[00m php \033[0;34mbow\033[00m add:notifier name Create a new messaging handler \033[0;33m$\033[00m php \033[0;34mbow\033[00m add help Display this help U; diff --git a/src/Console/Setting.php b/src/Console/Setting.php index c6d90e5c..a2d13971 100644 --- a/src/Console/Setting.php +++ b/src/Console/Setting.php @@ -171,11 +171,11 @@ class Setting private array $namespaces = []; /** - * The messaging directory + * The notifier directory * * @var string */ - private string $messaging_directory; + private string $notifier_directory; /** * Command constructor. @@ -507,24 +507,24 @@ public function setMiddlewareDirectory(string $middleware_directory): void } /** - * Get the messaging directory + * Get the notifier directory * * @return string */ - public function getMessagingDirectory(): string + public function getNotifierDirectory(): string { - return $this->messaging_directory; + return $this->notifier_directory; } /** - * Set the messaging directory + * Set the notifier directory * - * @param string $messaging_directory + * @param string $notifier_directory * @return void */ - public function setMessagingDirectory(string $messaging_directory): void + public function setNotifierDirectory(string $notifier_directory): void { - $this->messaging_directory = $messaging_directory; + $this->notifier_directory = $notifier_directory; } /** diff --git a/src/Console/stubs/controller/controller.stub b/src/Console/stubs/controller/controller.stub index f81d92ae..b3e83dbb 100644 --- a/src/Console/stubs/controller/controller.stub +++ b/src/Console/stubs/controller/controller.stub @@ -5,7 +5,7 @@ namespace {baseNamespace}{namespace}; use {baseNamespace}\Controller; use Bow\Http\Request; -class {className} extends Controller +class {className} { // } diff --git a/src/Console/stubs/controller/no-plain.stub b/src/Console/stubs/controller/no-plain.stub index e5d3e9e5..1c84fdc8 100644 --- a/src/Console/stubs/controller/no-plain.stub +++ b/src/Console/stubs/controller/no-plain.stub @@ -5,7 +5,7 @@ namespace {baseNamespace}{namespace}; use {baseNamespace}\Controller; use Bow\Http\Request; -class {className} extends Controller +class {className} { /** * Application entry point diff --git a/src/Console/stubs/controller/rest.stub b/src/Console/stubs/controller/rest.stub index 1cc408c5..e91859c8 100644 --- a/src/Console/stubs/controller/rest.stub +++ b/src/Console/stubs/controller/rest.stub @@ -5,7 +5,7 @@ namespace {baseNamespace}{namespace}; {modelNamespace}use {baseNamespace}\Controller; use Bow\Http\Request; -class {className} extends Controller +class {className} { /** * Start point diff --git a/src/Console/stubs/controller/service.stub b/src/Console/stubs/controller/service.stub index 78c746c1..6870a456 100644 --- a/src/Console/stubs/controller/service.stub +++ b/src/Console/stubs/controller/service.stub @@ -6,7 +6,7 @@ use {baseNamespace}\Controller; use Bow\Http\Request; use {serviceNamespace}\{serviceClassName}; -class {className} extends Controller +class {className} { /** * Instance of {serviceClassName} diff --git a/src/Console/stubs/messaging.stub b/src/Console/stubs/notifier.stub similarity index 92% rename from src/Console/stubs/messaging.stub rename to src/Console/stubs/notifier.stub index 55e63f84..782a6342 100644 --- a/src/Console/stubs/messaging.stub +++ b/src/Console/stubs/notifier.stub @@ -4,9 +4,9 @@ namespace {baseNamespace}{namespace}; use Bow\Database\Barry\Model; use Bow\Mail\Envelop; -use Bow\Messaging\Messaging; +use Bow\Notifier\Notifier; -class {className} extends Messaging +class {className} extends Notifier { /** * Returns the available channels to be used diff --git a/src/Container/Capsule.php b/src/Container/Capsule.php index 75c13d19..581a622f 100644 --- a/src/Container/Capsule.php +++ b/src/Container/Capsule.php @@ -149,6 +149,12 @@ public function make(string $key): mixed return $this->resolve($key); } + if (is_string($this->registers[$key])) { + return $this->instances[$key] = $this->resolve( + $this->registers[$key] + ); + } + if (is_callable($this->registers[$key])) { return $this->instances[$key] = call_user_func_array( $this->registers[$key], diff --git a/src/Container/Compass.php b/src/Container/Compass.php index 5c4b5ca2..4b052d74 100644 --- a/src/Container/Compass.php +++ b/src/Container/Compass.php @@ -380,6 +380,10 @@ private function getInjectParameter(mixed $class): ?object return null; } + if (interface_exists($class_name)) { + return app()->make($class_name); + } + if (!class_exists($class_name)) { throw new InvalidArgumentException( sprintf('class %s not exists', $class_name) diff --git a/src/Mail/Mail.php b/src/Mail/Mail.php index 9ca1f98e..e361b8ba 100644 --- a/src/Mail/Mail.php +++ b/src/Mail/Mail.php @@ -169,7 +169,7 @@ public static function queue(string $template, array $data, callable $cb): void call_user_func_array($cb, [$envelop]); - $producer = new MailQueueProducer($template, $data, $envelop); + $producer = new MailQueueJob($template, $data, $envelop); queue($producer); } @@ -189,7 +189,7 @@ public static function queueOn(string $queue, string $template, array $data, cal call_user_func_array($cb, [$envelop]); - $producer = new MailQueueProducer($template, $data, $envelop); + $producer = new MailQueueJob($template, $data, $envelop); $producer->setQueue($queue); @@ -211,7 +211,7 @@ public static function later(int $delay, string $template, array $data, callable call_user_func_array($cb, [$envelop]); - $producer = new MailQueueProducer($template, $data, $envelop); + $producer = new MailQueueJob($template, $data, $envelop); $producer->setDelay($delay); @@ -234,7 +234,7 @@ public static function laterOn(int $delay, string $queue, string $template, arra call_user_func_array($cb, [$envelop]); - $producer = new MailQueueProducer($template, $data, $envelop); + $producer = new MailQueueJob($template, $data, $envelop); $producer->setQueue($queue); $producer->setDelay($delay); diff --git a/src/Mail/MailQueueProducer.php b/src/Mail/MailQueueJob.php similarity index 93% rename from src/Mail/MailQueueProducer.php rename to src/Mail/MailQueueJob.php index e3a4ecd8..99697161 100644 --- a/src/Mail/MailQueueProducer.php +++ b/src/Mail/MailQueueJob.php @@ -6,7 +6,7 @@ use Bow\View\View; use Throwable; -class MailQueueProducer extends QueueJob +class MailQueueJob extends QueueJob { /** * The message bag @@ -16,7 +16,7 @@ class MailQueueProducer extends QueueJob private array $bags = []; /** - * MailQueueProducer constructor + * MailQueueJob constructor * * @param string $view * @param array $data diff --git a/src/Messaging/Adapters/DatabaseChannelAdapter.php b/src/Notifier/Adapters/DatabaseChannelAdapter.php similarity index 74% rename from src/Messaging/Adapters/DatabaseChannelAdapter.php rename to src/Notifier/Adapters/DatabaseChannelAdapter.php index e0c17c18..eb4aceda 100644 --- a/src/Messaging/Adapters/DatabaseChannelAdapter.php +++ b/src/Notifier/Adapters/DatabaseChannelAdapter.php @@ -1,11 +1,11 @@ toDatabase($context); + $database = $notifier->toDatabase($context); if ($database === null) { throw new \RuntimeException( diff --git a/src/Messaging/Adapters/MailChannelAdapter.php b/src/Notifier/Adapters/MailChannelAdapter.php similarity index 61% rename from src/Messaging/Adapters/MailChannelAdapter.php rename to src/Notifier/Adapters/MailChannelAdapter.php index fe986533..bf7f52b0 100644 --- a/src/Messaging/Adapters/MailChannelAdapter.php +++ b/src/Notifier/Adapters/MailChannelAdapter.php @@ -1,11 +1,11 @@ toMail($context); + $envelop = $notifier->toMail($context); if ($envelop === null) { throw new \RuntimeException( diff --git a/src/Messaging/Adapters/SlackChannelAdapter.php b/src/Notifier/Adapters/SlackChannelAdapter.php similarity index 67% rename from src/Messaging/Adapters/SlackChannelAdapter.php rename to src/Notifier/Adapters/SlackChannelAdapter.php index 0fdcfb54..1e87981c 100644 --- a/src/Messaging/Adapters/SlackChannelAdapter.php +++ b/src/Notifier/Adapters/SlackChannelAdapter.php @@ -1,30 +1,30 @@ toSlack($context); + $data = $notifier->toSlack($context); if (!isset($data['content'])) { throw new \InvalidArgumentException('The content are required for Slack'); @@ -39,17 +39,14 @@ public function send(Model $context, Messaging $message): void $client = new Client(); try { - $client->post( - $webhook_url, - [ + $client->post($webhook_url, [ 'json' => $data['content'], 'headers' => [ 'Content-Type' => 'application/json' ] - ] - ); + ]); } catch (\Exception $e) { - throw new \RuntimeException('Error while sending Slack message: ' . $e->getMessage()); + throw new \RuntimeException('Error while sending Slack notifier: ' . $e->getMessage()); } } } diff --git a/src/Messaging/Adapters/SmsChannelAdapter.php b/src/Notifier/Adapters/SmsChannelAdapter.php similarity index 64% rename from src/Messaging/Adapters/SmsChannelAdapter.php rename to src/Notifier/Adapters/SmsChannelAdapter.php index bf5915e9..b32ca278 100644 --- a/src/Messaging/Adapters/SmsChannelAdapter.php +++ b/src/Notifier/Adapters/SmsChannelAdapter.php @@ -1,10 +1,10 @@ sendWithTwilio($context, $message); + $this->sendWithTwilio($context, $notifier); } /** - * Send the message via SMS using Twilio + * Send the notifier via SMS using Twilio * * @param Model $context - * @param Messaging $message + * @param Notifier $notifier * @return void */ - private function sendWithTwilio(Model $context, Messaging $message): void + private function sendWithTwilio(Model $context, Notifier $notifier): void { - $data = $message->toSms($context); + $data = $notifier->toSms($context); - $account_sid = config('messaging.twilio.account_sid'); - $auth_token = config('messaging.twilio.auth_token'); - $this->from_number = config('messaging.twilio.from'); + $account_sid = config('notifier.twilio.account_sid'); + $auth_token = config('notifier.twilio.auth_token'); + $this->from_number = config('notifier.twilio.from'); if (!$account_sid || !$auth_token || !$this->from_number) { throw new InvalidArgumentException('Twilio credentials are required'); @@ -77,17 +77,14 @@ private function sendWithTwilio(Model $context, Messaging $message): void $this->client = new Client($account_sid, $auth_token); if (!isset($data['to']) || !isset($data['message'])) { - throw new InvalidArgumentException('The phone number and message are required'); + throw new InvalidArgumentException('The phone number and notifier are required'); } try { - $this->client->messages->create( - $data['to'], - [ + $this->client->notifiers->create($data['to'], [ 'from' => $this->from_number, - 'body' => $data['message'] - ] - ); + 'body' => $data['notifier'] + ]); } catch (\Exception $e) { throw new \RuntimeException('Error while sending SMS: ' . $e->getMessage()); } diff --git a/src/Messaging/Adapters/TelegramChannelAdapter.php b/src/Notifier/Adapters/TelegramChannelAdapter.php similarity index 78% rename from src/Messaging/Adapters/TelegramChannelAdapter.php rename to src/Notifier/Adapters/TelegramChannelAdapter.php index 0becaf30..186ab70a 100644 --- a/src/Messaging/Adapters/TelegramChannelAdapter.php +++ b/src/Notifier/Adapters/TelegramChannelAdapter.php @@ -1,10 +1,10 @@ toTelegram($context); + $data = $notifier->toTelegram($context); if (!isset($data['chat_id']) || !isset($data['message'])) { throw new InvalidArgumentException('The chat ID and message are required for Telegram'); @@ -56,16 +56,13 @@ public function send(Model $context, Messaging $message): void $endpoint = "https://api.telegram.org/bot{$this->botToken}/sendMessage"; try { - $client->post( - $endpoint, - [ + $client->post($endpoint, [ 'json' => [ 'chat_id' => $data['chat_id'], 'text' => $data['message'], 'parse_mode' => $data['parse_mode'] ?? 'HTML' ] - ] - ); + ]); } catch (Exception $e) { throw new RuntimeException('Error while sending Telegram message: ' . $e->getMessage()); } diff --git a/src/Messaging/Contracts/ChannelAdapterInterface.php b/src/Notifier/Contracts/ChannelAdapterInterface.php similarity index 55% rename from src/Messaging/Contracts/ChannelAdapterInterface.php rename to src/Notifier/Contracts/ChannelAdapterInterface.php index 7fb1c431..32fcc480 100644 --- a/src/Messaging/Contracts/ChannelAdapterInterface.php +++ b/src/Notifier/Contracts/ChannelAdapterInterface.php @@ -1,9 +1,9 @@ bags = [ - "message" => $message, + "notifier" => $notifier, "context" => $context, ]; } @@ -40,8 +40,8 @@ public function __construct( */ public function process(): void { - $message = $this->bags['message']; - $message->process($this->bags['context']); + $notifier = $this->bags['notifier']; + $notifier->process($this->bags['context']); } /** diff --git a/src/Messaging/README.md b/src/Notifier/README.md similarity index 81% rename from src/Messaging/README.md rename to src/Notifier/README.md index 2635e882..e4096a8f 100644 --- a/src/Messaging/README.md +++ b/src/Notifier/README.md @@ -1,4 +1,4 @@ -# Bow Framework - Messaging System +# Bow Framework - Notifier System Le système de messaging de Bow Framework permet d'envoyer des notifications à travers différents canaux (email, base de données, etc.) de manière simple et flexible. @@ -14,9 +14,9 @@ namespace App\Messages; use Bow\Database\Barry\Model; use Bow\Mail\Envelop; -use Bow\Messaging\Messaging; +use Bow\Notifier\Notifier; -class WelcomeMessage extends Messaging +class WelcomeNotifier extends Notifier { /** * Définir les canaux de diffusion du message @@ -59,29 +59,29 @@ class WelcomeMessage extends Messaging ```php // Envoi synchrone -$user->sendMessage(new WelcomeMessage()); +$user->sendMessage(new WelcomeNotifier()); // Envoi asynchrone (file d'attente) -$user->setMessageQueue(new WelcomeMessage()); +$user->setMessageQueue(new WelcomeNotifier()); // Envoi différé -$user->sendMessageLater(3600, new WelcomeMessage()); // Délai en secondes +$user->sendMessageLater(3600, new WelcomeNotifier()); // Délai en secondes // Envoi sur une file d'attente spécifique -$user->sendMessageQueueOn('high-priority', new WelcomeMessage()); +$user->sendMessageQueueOn('high-priority', new WelcomeNotifier()); ``` ## Configuration -Pour utiliser le système de messaging, assurez-vous que votre modèle implémente le trait `SendMessaging` : +Pour utiliser le système de messaging, assurez-vous que votre modèle implémente le trait `SendNotifier` : ```php -use Bow\Messaging\Message; +use Bow\Notifier\Message; use Bow\Database\Barry\Model; class User extends Model { - use SendMessaging; + use SendNotifier; // ... } @@ -101,22 +101,22 @@ class User extends Model 1. Créez un message par type de notification 2. Utilisez les files d'attente pour les notifications non urgentes 3. Personnalisez les canaux en fonction du contexte -4. Utilisez les vues pour les templates d'emails +4. Utilisez les vues pour les templates d'emails ## Exemple de configuration -```mermaid +```mermaid sequenceDiagram participant User as Utilisateur participant Model as Modèle (User) - participant Message as WelcomeMessage + participant Message as WelcomeNotifier participant Mail as Canal Email participant DB as Canal Database participant Services as Services (SMTP/BDD) Note over User,Services: Envoi d'une notification de bienvenue - User->>Model: sendMessage(new WelcomeMessage("Bienvenue!")) + User->>Model: sendMessage(new WelcomeNotifier("Bienvenue!")) Model->>Message: process(context) Message->>Message: channels(context) @@ -131,7 +131,7 @@ sequenceDiagram end Note over User,Services: Envoi asynchrone - User->>Model: setMessageQueue(new WelcomeMessage()) + User->>Model: setMessageQueue(new WelcomeNotifier()) Model->>Services: Ajout à la file d'attente Services-->>Model: Confirmation ``` diff --git a/src/Messaging/SendMessaging.php b/src/Notifier/WithNotifier.php similarity index 53% rename from src/Messaging/SendMessaging.php rename to src/Notifier/WithNotifier.php index 16d866f7..4ae1df89 100644 --- a/src/Messaging/SendMessaging.php +++ b/src/Notifier/WithNotifier.php @@ -1,29 +1,29 @@ process($this); + $notifier->process($this); } /** * Send message on queue * - * @param Messaging $message + * @param Notifier $notifier * @return void */ - public function setMessageQueue(Messaging $message): void + public function setMessageQueue(Notifier $notifier): void { - $queue_job = new MessagingQueueJob($this, $message); + $queue_job = new NotifierQueueJob($this, $notifier); queue($queue_job); } @@ -32,12 +32,12 @@ public function setMessageQueue(Messaging $message): void * Send message on specific queue * * @param string $queue - * @param Messaging $message + * @param Notifier $notifier * @return void */ - public function sendMessageQueueOn(string $queue, Messaging $message): void + public function sendMessageQueueOn(string $queue, Notifier $notifier): void { - $queue_job = new MessagingQueueJob($this, $message); + $queue_job = new NotifierQueueJob($this, $notifier); $queue_job->setQueue($queue); @@ -48,12 +48,12 @@ public function sendMessageQueueOn(string $queue, Messaging $message): void * Send mail later * * @param integer $delay - * @param Messaging $message + * @param Notifier $notifier * @return void */ - public function sendMessageLater(int $delay, Messaging $message): void + public function sendMessageLater(int $delay, Notifier $notifier): void { - $queue_job = new MessagingQueueJob($this, $message); + $queue_job = new NotifierQueueJob($this, $notifier); $queue_job->setDelay($delay); @@ -65,12 +65,12 @@ public function sendMessageLater(int $delay, Messaging $message): void * * @param integer $delay * @param string $queue - * @param Messaging $message + * @param Notifier $notifier * @return void */ - public function sendMessageLaterOn(int $delay, string $queue, Messaging $message): void + public function sendMessageLaterOn(int $delay, string $queue, Notifier $notifier): void { - $queue_job = new MessagingQueueJob($this, $message); + $queue_job = new NotifierQueueJob($this, $notifier); $queue_job->setQueue($queue); $queue_job->setDelay($delay); diff --git a/src/Queue/Adapters/BeanstalkdAdapter.php b/src/Queue/Adapters/BeanstalkdAdapter.php index 8a6d9643..6fb159e7 100644 --- a/src/Queue/Adapters/BeanstalkdAdapter.php +++ b/src/Queue/Adapters/BeanstalkdAdapter.php @@ -5,13 +5,13 @@ namespace Bow\Queue\Adapters; use Bow\Queue\QueueJob; -use ErrorException; use Pheanstalk\Contract\PheanstalkPublisherInterface; use Pheanstalk\Pheanstalk; use Pheanstalk\Values\Timeout; use Pheanstalk\Values\TubeName; use RuntimeException; use Throwable; +use ErrorException; class BeanstalkdAdapter extends QueueAdapter { diff --git a/src/Queue/Adapters/DatabaseAdapter.php b/src/Queue/Adapters/DatabaseAdapter.php index bf55d505..2b71c00f 100644 --- a/src/Queue/Adapters/DatabaseAdapter.php +++ b/src/Queue/Adapters/DatabaseAdapter.php @@ -5,7 +5,6 @@ use Bow\Database\Database; use Bow\Database\Exception\QueryBuilderException; use Bow\Database\QueryBuilder; -use Bow\Queue\ProducerService; use Bow\Queue\QueueJob; use ErrorException; use Exception; diff --git a/tests/Console/GeneratorDeepTest.php b/tests/Console/GeneratorDeepTest.php index aae12324..7440775d 100644 --- a/tests/Console/GeneratorDeepTest.php +++ b/tests/Console/GeneratorDeepTest.php @@ -271,7 +271,7 @@ public function test_generate_controller_stubs() $this->assertNotNull($content); $this->assertMatchesSnapshot($content); - $this->assertMatchesRegularExpression("@\nclass\sExampleController\sextends\sController\n@", $content); + $this->assertMatchesRegularExpression("@\nclass\sExampleController\n@", $content); } public function test_generate_controller_no_plain_stubs() @@ -285,7 +285,7 @@ public function test_generate_controller_no_plain_stubs() $this->assertNotNull($content); $this->assertMatchesSnapshot($content); - $this->assertMatchesRegularExpression('@\nclass\sExampleController\sextends\sController\n@', $content); + $this->assertMatchesRegularExpression('@\nclass\sExampleController\n@', $content); $this->assertMatchesRegularExpression('@public\sfunction\sindex()@', $content); $this->assertMatchesRegularExpression('@public\sfunction\screate()@', $content); $this->assertMatchesRegularExpression('@public\sfunction\supdate\(Request\s\$request,\smixed\s\$id\)@', $content); @@ -306,7 +306,7 @@ public function test_generate_controller_rest_stubs() $this->assertNotNull($content); $this->assertMatchesSnapshot($content); - $this->assertMatchesRegularExpression('@\nclass\sExampleController\sextends\sController\n@', $content); + $this->assertMatchesRegularExpression('@\nclass\sExampleController\n@', $content); $this->assertMatchesRegularExpression('@public\sfunction\sindex()@', $content); $this->assertMatchesRegularExpression('@public\sfunction\supdate\(Request\s\$request,\smixed\s\$id\)@', $content); $this->assertMatchesRegularExpression('@public\sfunction\sshow\(Request\s\$request,\smixed\s\$id\)@', $content); @@ -314,18 +314,18 @@ public function test_generate_controller_rest_stubs() $this->assertMatchesRegularExpression('@public\sfunction\sdestroy\(Request\s\$request,\smixed\s\$id\)@', $content); } - public function test_generate_messaging_stubs() + public function test_generate_notifier_stubs() { - $generator = new Generator(TESTING_RESOURCE_BASE_DIRECTORY, 'WelcomeMessage'); - $content = $generator->makeStubContent('messaging', [ - "className" => "WelcomeMessage", + $generator = new Generator(TESTING_RESOURCE_BASE_DIRECTORY, 'WelcomeNotifier'); + $content = $generator->makeStubContent('notifier', [ + "className" => "WelcomeNotifier", "baseNamespace" => "App\\", - "namespace" => "Messages" + "namespace" => "Notifiers" ]); $this->assertNotNull($content); $this->assertMatchesSnapshot($content); - $this->assertMatchesRegularExpression('@\nclass\sWelcomeMessage\sextends\sMessaging\n@', $content); + $this->assertMatchesRegularExpression('@\nclass\sWelcomeNotifier\sextends\sNotifier\n@', $content); $this->assertMatchesRegularExpression('@public\sfunction\schannels\(Model\s\$notifiable\)@', $content); $this->assertMatchesRegularExpression('@public\sfunction\stoMail\(Model\s\$notifiable\)@', $content); $this->assertMatchesRegularExpression('@public\sfunction\stoDatabase\(Model\s\$notifiable\)@', $content); diff --git a/tests/Console/__snapshots__/GeneratorDeepTest__test_generate_controller_no_plain_stubs__1.txt b/tests/Console/__snapshots__/GeneratorDeepTest__test_generate_controller_no_plain_stubs__1.txt index 8dc1dc87..c2e9e59f 100644 --- a/tests/Console/__snapshots__/GeneratorDeepTest__test_generate_controller_no_plain_stubs__1.txt +++ b/tests/Console/__snapshots__/GeneratorDeepTest__test_generate_controller_no_plain_stubs__1.txt @@ -5,7 +5,7 @@ namespace App\Controllers; use App\\Controller; use Bow\Http\Request; -class ExampleController extends Controller +class ExampleController { /** * Application entry point diff --git a/tests/Console/__snapshots__/GeneratorDeepTest__test_generate_controller_rest_stubs__1.txt b/tests/Console/__snapshots__/GeneratorDeepTest__test_generate_controller_rest_stubs__1.txt index 0a72e1aa..9dda9928 100644 --- a/tests/Console/__snapshots__/GeneratorDeepTest__test_generate_controller_rest_stubs__1.txt +++ b/tests/Console/__snapshots__/GeneratorDeepTest__test_generate_controller_rest_stubs__1.txt @@ -5,7 +5,7 @@ namespace App\Controllers; {modelNamespace}use App\\Controller; use Bow\Http\Request; -class ExampleController extends Controller +class ExampleController { /** * Start point diff --git a/tests/Console/__snapshots__/GeneratorDeepTest__test_generate_controller_stubs__1.txt b/tests/Console/__snapshots__/GeneratorDeepTest__test_generate_controller_stubs__1.txt index b5fc17f2..d3cd0ede 100644 --- a/tests/Console/__snapshots__/GeneratorDeepTest__test_generate_controller_stubs__1.txt +++ b/tests/Console/__snapshots__/GeneratorDeepTest__test_generate_controller_stubs__1.txt @@ -5,7 +5,7 @@ namespace App\Controllers; use App\\Controller; use Bow\Http\Request; -class ExampleController extends Controller +class ExampleController { // } diff --git a/tests/Console/__snapshots__/GeneratorDeepTest__test_generate_messaging_stubs__1.txt b/tests/Console/__snapshots__/GeneratorDeepTest__test_generate_messaging_stubs__1.txt index 1f8fdcb8..6022ac21 100644 --- a/tests/Console/__snapshots__/GeneratorDeepTest__test_generate_messaging_stubs__1.txt +++ b/tests/Console/__snapshots__/GeneratorDeepTest__test_generate_messaging_stubs__1.txt @@ -4,9 +4,9 @@ namespace App\Messages; use Bow\Database\Barry\Model; use Bow\Mail\Envelop; -use Bow\Messaging\Messaging; +use Bow\Notifier\Notifier; -class WelcomeMessage extends Messaging +class WelcomeMessage extends Notifier { /** * Returns the available channels to be used diff --git a/tests/Console/__snapshots__/GeneratorDeepTest__test_generate_notifier_stubs__1.txt b/tests/Console/__snapshots__/GeneratorDeepTest__test_generate_notifier_stubs__1.txt new file mode 100644 index 00000000..295cb619 --- /dev/null +++ b/tests/Console/__snapshots__/GeneratorDeepTest__test_generate_notifier_stubs__1.txt @@ -0,0 +1,43 @@ +context = new TestNotifiableModel(); - $this->message = $this->createMock(TestMessage::class); + $this->notifier = $this->createMock(TestNotifier::class); } public function test_can_send_message_synchronously(): void { - $this->message->expects($this->once()) + $this->notifier->expects($this->once()) ->method('process') ->with($this->context); - $this->context->sendMessage($this->message); + $this->context->sendMessage($this->notifier); } public function test_message_sends_to_correct_channels(): void { - $message = new TestMessage(); - $channels = $message->channels($this->context); + $notifier = new TestNotifier(); + $channels = $notifier->channels($this->context); $this->assertIsArray($channels); $this->assertCount(5, $channels); @@ -71,7 +70,7 @@ public function test_message_sends_to_correct_channels(): void public function test_message_can_send_to_mail(): void { - $message = new TestMessage(); + $message = new TestNotifier(); $mailMessage = $message->toMail($this->context); $this->assertInstanceOf(Envelop::class, $mailMessage); @@ -83,7 +82,7 @@ public function test_message_can_send_to_mail(): void public function test_message_can_send_to_database(): void { - $message = new TestMessage(); + $message = new TestNotifier(); $data = $message->toDatabase($this->context); $this->assertIsArray($data); @@ -97,7 +96,7 @@ public function test_message_can_send_to_database(): void public function test_message_can_send_to_slack(): void { - $message = new TestMessage(); + $message = new TestNotifier(); $data = $message->toSlack($this->context); $this->assertIsArray($data); @@ -111,7 +110,7 @@ public function test_message_can_send_to_slack(): void public function test_message_can_send_to_sms(): void { - $message = new TestMessage(); + $message = new TestNotifier(); $data = $message->toSms($this->context); $this->assertIsArray($data); @@ -123,7 +122,7 @@ public function test_message_can_send_to_sms(): void public function test_message_can_send_to_telegram(): void { - $message = new TestMessage(); + $message = new TestNotifier(); $data = $message->toTelegram($this->context); $this->assertIsArray($data); @@ -137,7 +136,7 @@ public function test_message_can_send_to_telegram(): void public function test_process_calls_all_channels(): void { - $message = $this->getMockBuilder(TestMessage::class) + $message = $this->getMockBuilder(TestNotifier::class) ->onlyMethods(['channels', 'toMail', 'toDatabase']) ->getMock(); @@ -163,7 +162,7 @@ public function test_process_calls_all_channels(): void public function test_message_returns_empty_array_for_unconfigured_channels(): void { - $messaging = new class extends Messaging { + $messaging = new class extends Notifier { public function channels(Model $context): array { return []; @@ -183,7 +182,7 @@ public function test_can_push_custom_channels(): void 'custom' => \stdClass::class, ]; - $result = Messaging::pushChannels($customChannels); + $result = Notifier::pushChannels($customChannels); $this->assertIsArray($result); $this->assertArrayHasKey('custom', $result); @@ -193,7 +192,7 @@ public function test_can_push_custom_channels(): void public function test_message_process_skips_invalid_channels(): void { - $message = $this->getMockBuilder(TestMessage::class) + $message = $this->getMockBuilder(TestNotifier::class) ->onlyMethods(['channels', 'toMail']) ->getMock(); @@ -213,7 +212,7 @@ public function test_message_process_skips_invalid_channels(): void public function test_mail_message_returns_correct_envelop_instance(): void { - $message = new TestMessage(); + $message = new TestNotifier(); $mailMessage = $message->toMail($this->context); $this->assertInstanceOf(Envelop::class, $mailMessage); @@ -223,7 +222,7 @@ public function test_mail_message_returns_correct_envelop_instance(): void public function test_database_message_has_required_structure(): void { - $message = new TestMessage(); + $message = new TestNotifier(); $data = $message->toDatabase($this->context); // Verify required structure @@ -235,7 +234,7 @@ public function test_database_message_has_required_structure(): void public function test_slack_message_has_valid_webhook_url(): void { - $message = new TestMessage(); + $message = new TestNotifier(); $data = $message->toSlack($this->context); $this->assertArrayHasKey('webhook_url', $data); @@ -245,7 +244,7 @@ public function test_slack_message_has_valid_webhook_url(): void public function test_sms_message_has_valid_phone_number(): void { - $message = new TestMessage(); + $message = new TestNotifier(); $data = $message->toSms($this->context); $this->assertArrayHasKey('to', $data); @@ -255,7 +254,7 @@ public function test_sms_message_has_valid_phone_number(): void public function test_telegram_message_has_valid_parse_mode(): void { - $message = new TestMessage(); + $message = new TestNotifier(); $data = $message->toTelegram($this->context); $this->assertArrayHasKey('parse_mode', $data); @@ -266,23 +265,23 @@ public function test_context_has_send_message_trait(): void { $this->assertTrue( method_exists($this->context, 'sendMessage'), - 'Context should have sendMessage method from SendMessaging trait' + 'Context should have sendMessage method from SendNotifier trait' ); $this->assertTrue( method_exists($this->context, 'setMessageQueue'), - 'Context should have setMessageQueue method from SendMessaging trait' + 'Context should have setMessageQueue method from SendNotifier trait' ); $this->assertTrue( method_exists($this->context, 'sendMessageQueueOn'), - 'Context should have sendMessageQueueOn method from SendMessaging trait' + 'Context should have sendMessageQueueOn method from SendNotifier trait' ); } public function test_channels_method_is_abstract_and_must_be_implemented(): void { - $message = new TestMessage(); + $message = new TestNotifier(); $this->assertTrue( method_exists($message, 'channels'), diff --git a/tests/Notifier/Stubs/TestNotifiableModel.php b/tests/Notifier/Stubs/TestNotifiableModel.php new file mode 100644 index 00000000..fd68c8da --- /dev/null +++ b/tests/Notifier/Stubs/TestNotifiableModel.php @@ -0,0 +1,11 @@ +to("bow@bow.org"); $envelop->subject("hello from bow"); - $producer = new MailQueueProducer("email", [], $envelop); + $producer = new MailQueueJob("email", [], $envelop); - $this->assertInstanceOf(MailQueueProducer::class, $producer); + $this->assertInstanceOf(MailQueueJob::class, $producer); $adapter = static::$connection->setConnection("beanstalkd")->getAdapter(); @@ -67,9 +67,9 @@ public function it_should_create_mail_producer_with_correct_parameters(): void $envelop->from("sender@example.com"); $envelop->subject("Test Subject"); - $producer = new MailQueueProducer("test-template", ["name" => "John"], $envelop); + $producer = new MailQueueJob("test-template", ["name" => "John"], $envelop); - $this->assertInstanceOf(MailQueueProducer::class, $producer); + $this->assertInstanceOf(MailQueueJob::class, $producer); } /** @@ -80,7 +80,7 @@ public function it_should_push_mail_to_specific_queue(): void $envelop = new Envelop(); $envelop->to("priority@example.com"); $envelop->subject("Priority Mail"); - $producer = new MailQueueProducer("email", [], $envelop); + $producer = new MailQueueJob("email", [], $envelop); $adapter = static::$connection->setConnection("beanstalkd")->getAdapter(); $adapter->setQueue("priority-mail"); @@ -98,7 +98,7 @@ public function it_should_set_mail_retry_attempts(): void $envelop->to("retry@example.com"); $envelop->subject("Retry Test"); - $producer = new MailQueueProducer("email", [], $envelop); + $producer = new MailQueueJob("email", [], $envelop); $producer->setRetry(3); $this->assertEquals(3, $producer->getRetry()); diff --git a/tests/Queue/MessagingQueueTest.php b/tests/Queue/NotifierQueueTest.php similarity index 77% rename from tests/Queue/MessagingQueueTest.php rename to tests/Queue/NotifierQueueTest.php index 2a2faa73..281fa7ab 100644 --- a/tests/Queue/MessagingQueueTest.php +++ b/tests/Queue/NotifierQueueTest.php @@ -5,19 +5,18 @@ use Bow\Cache\CacheConfiguration; use Bow\Configuration\EnvConfiguration; use Bow\Configuration\LoggerConfiguration; -use Bow\Database\Barry\Model; use Bow\Database\DatabaseConfiguration; use Bow\Mail\MailConfiguration; -use Bow\Messaging\MessagingQueueJob; +use Bow\Notifier\NotifierQueueJob; use Bow\Queue\Connection as QueueConnection; use Bow\Queue\QueueConfiguration; use Bow\Tests\Config\TestingConfiguration; -use Bow\Tests\Messaging\Stubs\TestMessage; -use Bow\Tests\Messaging\Stubs\TestNotifiableModel; +use Bow\Tests\Notifier\Stubs\TestNotifier; +use Bow\Tests\Notifier\Stubs\TestNotifiableModel; use Bow\View\ViewConfiguration; use PHPUnit\Framework\TestCase; -class MessagingQueueTest extends TestCase +class NotifierQueueTest extends TestCase { private static QueueConnection $connection; @@ -42,7 +41,7 @@ public static function setUpBeforeClass(): void public function test_can_send_message_synchronously(): void { $context = new TestNotifiableModel(); - $message = $this->getMockBuilder(TestMessage::class) + $message = $this->getMockBuilder(TestNotifier::class) ->onlyMethods(['process']) ->getMock(); @@ -57,12 +56,12 @@ public function test_can_send_message_to_queue(): void { // Use real objects for queue tests (mock objects don't serialize) $context = new TestNotifiableModel(); - $message = new TestMessage(); + $message = new TestNotifier(); - $producer = new MessagingQueueJob($context, $message); + $producer = new NotifierQueueJob($context, $message); // Verify that the producer is created with correct parameters - $this->assertInstanceOf(MessagingQueueJob::class, $producer); + $this->assertInstanceOf(NotifierQueueJob::class, $producer); // Push to queue and verify $result = static::$connection->setConnection("beanstalkd")->getAdapter()->push($producer); @@ -73,12 +72,12 @@ public function test_can_send_message_to_specific_queue(): void { $queue = 'high-priority'; $context = new TestNotifiableModel(); - $message = new TestMessage(); + $message = new TestNotifier(); - $producer = new MessagingQueueJob($context, $message); + $producer = new NotifierQueueJob($context, $message); // Verify that the producer is created with correct parameters - $this->assertInstanceOf(MessagingQueueJob::class, $producer); + $this->assertInstanceOf(NotifierQueueJob::class, $producer); // Push to specific queue and verify $adapter = static::$connection->setConnection("beanstalkd")->getAdapter(); @@ -92,12 +91,12 @@ public function test_can_send_message_with_delay(): void { $delay = 3600; $context = new TestNotifiableModel(); - $message = new TestMessage(); + $message = new TestNotifier(); - $producer = new MessagingQueueJob($context, $message); + $producer = new NotifierQueueJob($context, $message); // Verify that the producer is created with correct parameters - $this->assertInstanceOf(MessagingQueueJob::class, $producer); + $this->assertInstanceOf(NotifierQueueJob::class, $producer); // Push to queue with delay and verify $adapter = static::$connection->setConnection("beanstalkd")->getAdapter(); @@ -112,12 +111,12 @@ public function test_can_send_message_with_delay_on_specific_queue(): void $delay = 3600; $queue = 'delayed-notifications'; $context = new TestNotifiableModel(); - $message = new TestMessage(); + $message = new TestNotifier(); - $producer = new MessagingQueueJob($context, $message); + $producer = new NotifierQueueJob($context, $message); // Verify that the producer is created with correct parameters - $this->assertInstanceOf(MessagingQueueJob::class, $producer); + $this->assertInstanceOf(NotifierQueueJob::class, $producer); // Push to specific queue with delay and verify $adapter = static::$connection->setConnection("beanstalkd")->getAdapter();