Skip to content

Commit

Permalink
Merge pull request #13 from chijioke-ibekwe/add-vonage-sms-provider
Browse files Browse the repository at this point in the history
make migration classes anonymous
  • Loading branch information
chijioke-ibekwe authored Oct 20, 2024
2 parents d3bd944 + 479443b commit ec0274f
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateNotificationContextsTable extends Migration
return new class extends Migration
{
public function up()
{
Expand Down
156 changes: 156 additions & 0 deletions tests/Feature/DatabaseNotificationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

namespace ChijiokeIbekwe\Raven\Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use ChijiokeIbekwe\Raven\Data\Scroll;
use ChijiokeIbekwe\Raven\Events\Raven;
use ChijiokeIbekwe\Raven\Exceptions\RavenInvalidDataException;
use ChijiokeIbekwe\Raven\Listeners\RavenListener;
use ChijiokeIbekwe\Raven\Models\NotificationContext;
use ChijiokeIbekwe\Raven\Notifications\DatabaseNotificationSender;
use ChijiokeIbekwe\Raven\Tests\TestCase;
use ChijiokeIbekwe\Raven\Tests\Utilities\User;

class DatabaseNotificationTest extends TestCase
{
use RefreshDatabase;

public function getEnvironmentSetUp($app): void
{
config()->set('raven.customizations.templates_directory', resource_path('templates'));

$migrations = require __DIR__.'/../../database/migrations/create_notification_contexts_table.php.stub';

$migrations->up();
}

/**
* @throws \Throwable
*/
public function test_that_database_notifications_are_sent_when_the_raven_listener_receives_a_database_context()
{
$data = [
'title' => 'Verification',
'body' => 'User with id {{user_id}} has been verified on the platform on {{date_time}}',
];

$json = json_encode($data, JSON_PRETTY_PRINT);

if (!is_dir(resource_path('templates/in_app'))) {
mkdir(resource_path('templates/in_app'), 0777, true);
}

file_put_contents(resource_path('templates/in_app/user-verified.json'), $json);

Notification::fake();

$user = User::factory()->make([
'name' => 'John Doe',
'email' => 'john.doe@raven.com'
]);

$context = NotificationContext::factory()->create([
'name' => 'user-verified',
'in_app_template_filename' => 'user-verified.json',
'type' => 'user',
'channels' => ['DATABASE']
]);

$scroll = new Scroll();
$scroll->setContextName('user-verified');
$scroll->setRecipients($user);
$scroll->setParams([
'user_id' => '345',
'date_time' => '11-12-2023 10:51'
]);

(new RavenListener())->handle(
new Raven($scroll)
);

Notification::assertSentTo(
$user,
DatabaseNotificationSender::class,
function (DatabaseNotificationSender $notification) use ($user, $scroll, $context) {
$content = $notification->toDatabase($user);
$via = $notification->via($user);

return $notification->scroll === $scroll &&
$notification->notificationContext->name === $context->name &&
data_get($content, 'title') === 'Verification' &&
data_get($content, 'body') === 'User with id 345 has been verified on the platform on 11-12-2023 10:51' &&
$via === ['database'];
}
);
}

/**
* @throws \Throwable
*/
public function test_that_exception_is_thrown_when_database_notification_context_has_no_filename()
{
$this->expectException(RavenInvalidDataException::class);
$this->expectExceptionMessage('Database notification context with name user-updated has no template filename');
$this->expectExceptionCode(422);

Notification::fake();

$user = User::factory()->make([
'name' => 'John Doe',
'email' => 'john.doe@raven.com'
]);

NotificationContext::factory()->create([
'name' => 'user-updated',
'channels' => ['DATABASE']
]);

$scroll = new Scroll();
$scroll->setContextName('user-updated');
$scroll->setRecipients($user);
$scroll->setParams([
'user_id' => '345',
'date_time' => '11-12-2023 10:51'
]);

(new RavenListener())->handle(
new Raven($scroll)
);
}

/**
* @throws \Throwable
*/
public function test_that_exception_is_thrown_when_database_notification_context_template_file_does_not_exist()
{
$this->expectException(RavenInvalidDataException::class);
$this->expectExceptionCode(422);

Notification::fake();

$user = User::factory()->make([
'name' => 'John Doe',
'email' => 'john.doe@raven.com'
]);

NotificationContext::factory()->create([
'name' => 'user-updated',
'in_app_template_filename' => 'user-updated.json',
'channels' => ['DATABASE']
]);

$scroll = new Scroll();
$scroll->setContextName('user-updated');
$scroll->setRecipients($user);
$scroll->setParams([
'user_id' => '345',
'date_time' => '11-12-2023 10:51'
]);

(new RavenListener())->handle(
new Raven($scroll)
);
}
}
5 changes: 3 additions & 2 deletions tests/Feature/NotificationContextRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ class NotificationContextRouteTest extends TestCase

public function getEnvironmentSetUp($app): void
{
// run the up() method (perform the migration)
(new \CreateNotificationContextsTable)->up();
$migrations = require __DIR__.'/../../database/migrations/create_notification_contexts_table.php.stub';

$migrations->up();
}

public function test_that_authorized_users_can_successfully_fetch_all_notification_contexts ()
Expand Down
142 changes: 6 additions & 136 deletions tests/Feature/SendGridNotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@
use ChijiokeIbekwe\Raven\Exceptions\RavenInvalidDataException;
use ChijiokeIbekwe\Raven\Listeners\RavenListener;
use ChijiokeIbekwe\Raven\Models\NotificationContext;
use ChijiokeIbekwe\Raven\Notifications\DatabaseNotificationSender;
use ChijiokeIbekwe\Raven\Notifications\EmailNotificationSender;
use ChijiokeIbekwe\Raven\Tests\TestCase;
use ChijiokeIbekwe\Raven\Tests\Utilities\User;

class SendGridNotificationTest extends TestCase
class SendGridNotificationTest extends TestCase
{
use RefreshDatabase;

public function getEnvironmentSetUp($app): void
{
$app['config']->set('raven.default.email', 'sendgrid');
$app['config']->set('raven.customizations.templates_directory', resource_path('templates'));
config()->set('raven.default.email', 'sendgrid');
config()->set('raven.customizations.templates_directory', resource_path('templates'));

// run the up() method (perform the migration)
(new \CreateNotificationContextsTable)->up();
$migrations = require __DIR__.'/../../database/migrations/create_notification_contexts_table.php.stub';

$migrations->up();
}

/**
Expand Down Expand Up @@ -127,66 +127,6 @@ function (EmailNotificationSender $notification) use ($user, $scroll, $context)

}

/**
* @throws \Throwable
*/
public function test_that_database_notifications_are_sent_when_the_raven_listener_receives_a_database_context()
{
$data = [
'title' => 'Verification',
'body' => 'User with id {{user_id}} has been verified on the platform on {{date_time}}',
];

$json = json_encode($data, JSON_PRETTY_PRINT);

if (!is_dir(resource_path('templates/in_app'))) {
mkdir(resource_path('templates/in_app'), 0777, true);
}

file_put_contents(resource_path('templates/in_app/user-verified.json'), $json);

Notification::fake();

$user = User::factory()->make([
'name' => 'John Doe',
'email' => 'john.doe@raven.com'
]);

$context = NotificationContext::factory()->create([
'name' => 'user-verified',
'in_app_template_filename' => 'user-verified.json',
'type' => 'user',
'channels' => ['DATABASE']
]);

$scroll = new Scroll();
$scroll->setContextName('user-verified');
$scroll->setRecipients($user);
$scroll->setParams([
'user_id' => '345',
'date_time' => '11-12-2023 10:51'
]);

(new RavenListener())->handle(
new Raven($scroll)
);

Notification::assertSentTo(
$user,
DatabaseNotificationSender::class,
function (DatabaseNotificationSender $notification) use ($user, $scroll, $context) {
$content = $notification->toDatabase($user);
$via = $notification->via($user);

return $notification->scroll === $scroll &&
$notification->notificationContext->name === $context->name &&
data_get($content, 'title') === 'Verification' &&
data_get($content, 'body') === 'User with id 345 has been verified on the platform on 11-12-2023 10:51' &&
$via === ['database'];
}
);
}

/**
* @throws \Throwable
*/
Expand Down Expand Up @@ -278,76 +218,6 @@ public function test_that_exception_is_thrown_when_email_notification_context_ha
);
}

/**
* @throws \Throwable
*/
public function test_that_exception_is_thrown_when_database_notification_context_has_no_filename()
{
$this->expectException(RavenInvalidDataException::class);
$this->expectExceptionMessage('Database notification context with name user-updated has no template filename');
$this->expectExceptionCode(422);

Notification::fake();

$user = User::factory()->make([
'name' => 'John Doe',
'email' => 'john.doe@raven.com'
]);

NotificationContext::factory()->create([
'name' => 'user-updated',
'channels' => ['DATABASE']
]);

$scroll = new Scroll();
$scroll->setContextName('user-updated');
$scroll->setRecipients($user);
$scroll->setParams([
'user_id' => '345',
'date_time' => '11-12-2023 10:51'
]);

(new RavenListener())->handle(
new Raven($scroll)
);
}

/**
* @throws \Throwable
*/
public function test_that_exception_is_thrown_when_database_notification_context_template_file_does_not_exist()
{
$this->expectException(RavenInvalidDataException::class);
$this->expectExceptionMessage('Database notification context with name user-updated has no template file ' .
'in /home/chijioke-ibekwe/projects/raven/vendor/orchestra/testbench-core/laravel/resources/templates/in_app');
$this->expectExceptionCode(422);

Notification::fake();

$user = User::factory()->make([
'name' => 'John Doe',
'email' => 'john.doe@raven.com'
]);

NotificationContext::factory()->create([
'name' => 'user-updated',
'in_app_template_filename' => 'user-updated.json',
'channels' => ['DATABASE']
]);

$scroll = new Scroll();
$scroll->setContextName('user-updated');
$scroll->setRecipients($user);
$scroll->setParams([
'user_id' => '345',
'date_time' => '11-12-2023 10:51'
]);

(new RavenListener())->handle(
new Raven($scroll)
);
}

/**
* @throws \Throwable
*/
Expand Down

0 comments on commit ec0274f

Please sign in to comment.