Skip to content

Instantly share code, notes, and snippets.

@wq9578
Last active April 18, 2023 12:53
Show Gist options
  • Save wq9578/5e52bfc9e0e5178aa4eb99aca0ebcd71 to your computer and use it in GitHub Desktop.
Save wq9578/5e52bfc9e0e5178aa4eb99aca0ebcd71 to your computer and use it in GitHub Desktop.
Workflow Test
<?php
namespace Tests\Feature;
use App\Workflows\Tests\VerifyEmailAddress\VerifyEmailAddressWorkflow;
use Tests\TestCase;
use Workflow\WorkflowStub;
class EmailVerificationWorkflowPHPUnitTest extends TestCase
{
public function test_workflow()
{
$workflow = WorkflowStub::make(VerifyEmailAddressWorkflow::class);
$workflow->start('test@example.com');
}
}
<?php
namespace Tests\Feature;
use App\Workflows\Tests\VerifyEmailAddress\VerifyEmailAddressWorkflow;
use Composer\InstalledVersions;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Workflow\WorkflowStub;
it('logs the PHP, database engine and composer version', closure: function () {
Log::info('PHP ' . phpversion());
Log::info(env('DB_CONNECTION') . ' ' . DB::select('select version()')[0]->{'version()'});
Log::info(trim(`composer --version`));
expect(true)->toBeTrue();
});
it('logs the composer package versions', closure: function (string $package) {
Log::info($package . ': ' . InstalledVersions::getVersion($package));
expect(true)->toBeTrue();
})->with([
'laravel/framework',
'doctrine/dbal',
'pestphp/pest',
'pestphp/pest-plugin-laravel',
'laravel-workflow/laravel-workflow',
]);
it('test the email address verification workflow', closure: function () {
$workflow = WorkflowStub::make(VerifyEmailAddressWorkflow::class);
$workflow->start('test@example.com');
expect($workflow->id())->toBeNumeric();
});
[2023-04-18 04:30:11] testing.INFO: App\Workflows\Tests\VerifyEmailAddress\VerifyEmailAddressWorkflow::execute(test@example.com)
[2023-04-18 04:39:06] testing.INFO: PHP 8.2.4
[2023-04-18 04:39:06] testing.INFO: mysql 8.0.32
[2023-04-18 04:39:06] testing.INFO: Composer version 2.5.5 2023-03-21 11:50:05
[2023-04-18 04:39:06] testing.INFO: laravel/framework: 10.7.1.0
[2023-04-18 04:39:06] testing.INFO: doctrine/dbal: 3.6.2.0
[2023-04-18 04:39:06] testing.INFO: pestphp/pest: 2.5.0.0
[2023-04-18 04:39:06] testing.INFO: pestphp/pest-plugin-laravel: 2.0.0.0
[2023-04-18 04:39:06] testing.INFO: laravel-workflow/laravel-workflow: 0.0.31.0
[2023-04-18 04:39:06] testing.INFO: App\Workflows\Tests\VerifyEmailAddress\VerifyEmailAddressWorkflow::execute(test@example.com)
<?php
namespace App\Workflows\Tests\VerifyEmailAddress;
use App\Notifications\Tests\VerifyEmailAddress;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Notification;
use Workflow\Activity;
class SendEmailAddressVerificationEmailActivity extends Activity
{
public function execute($email_address)
{
$log_string = __CLASS__ . '::' . __FUNCTION__ . '(' . $email_address . ')';
dump($log_string);
Log::info($log_string);
Notification::route('mail', $email_address)
->notify(new VerifyEmailAddress($this->workflowId()));
}
}
<?php
namespace App\Notifications\Tests;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\URL;
class VerifyEmailAddress extends Notification
{
use Queueable;
private $workflowId;
/**
* Create a new notification instance.
*/
public function __construct($workflowId)
{
$this->workflowId = $workflowId;
}
/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{
$url = URL::temporarySignedRoute(
'verify-email',
now()->addMinutes(30),
['workflow_id' => $this->workflowId],
);
return (new MailMessage)
->subject('Email Verification')
->line('Please verify your email address by clicking the following link:')
->action('Verify', url($url))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
//
];
}
}
<?php
namespace App\Workflows\Tests\VerifyEmailAddress;
use Illuminate\Support\Facades\Log;
use Workflow\ActivityStub;
use Workflow\SignalMethod;
use Workflow\Workflow;
use Workflow\WorkflowStub;
class VerifyEmailAddressWorkflow extends Workflow
{
private bool $verified = false;
#[SignalMethod]
public function verify()
{
$this->verified = true;
}
public function execute($email_address = '')
{
$log_string = __CLASS__ . '::' . __FUNCTION__ . '(' . $email_address . ')';
dump($log_string);
Log::info($log_string);
yield ActivityStub::make(SendEmailAddressVerificationEmailActivity::class, $email_address);
dump('DONE');
yield WorkflowStub::await(fn() => $this->verified);
yield ActivityStub::make(SendEmailAddressVerificationEmailActivity::class, $email_address);
}
}
<?php
namespace App\Console\Commands;
use App\Workflows\Tests\VerifyEmailAddress\VerifyEmailAddressWorkflow;
use Illuminate\Console\Command;
use Workflow\WorkflowStub;
class Workflow extends Command
{
protected $signature = 'workflow:run';
protected $description = 'Runs a workflow';
public function handle()
{
/*
$workflow = WorkflowStub::make(SimpleWorkflow::class);
$workflow->start();
*/
$workflow = WorkflowStub::make(VerifyEmailAddressWorkflow::class);
$workflow->start('test@example.com');
while ($workflow->running());
dump($workflow->output());
}
}
<?php
namespace App\Console\Commands;
use App\Workflows\Tests\VerifyEmailAddress\VerifyEmailAddressWorkflow;
use Illuminate\Console\Command;
use Workflow\WorkflowStub;
class WorkflowTest extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'workflow:test';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Test the workflow mechanism';
/**
* Execute the console command.
*/
public function handle(): int
{
$workflow = WorkflowStub::make(VerifyEmailAddressWorkflow::class);
$workflow->start('test@example.com');
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment