Skip to content

Add tests #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions tests/Unit/Config/WorkflowsConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Tests\Unit\Config;

use Tests\TestCase;

final class WorkflowsConfigTest extends TestCase
{
public function testConfigIsLoaded(): void
{
$config = require dirname(__DIR__, 3) . '/src/config/workflows.php';

$this->assertNotEmpty($config, 'The workflows config file is not loaded.');

$expectedConfig = [
'workflows_folder' => 'Workflows',
'base_model' => \Illuminate\Database\Eloquent\Model::class,
'stored_workflow_model' => \Workflow\Models\StoredWorkflow::class,
'stored_workflow_exception_model' => \Workflow\Models\StoredWorkflowException::class,
'stored_workflow_log_model' => \Workflow\Models\StoredWorkflowLog::class,
'stored_workflow_signal_model' => \Workflow\Models\StoredWorkflowSignal::class,
'stored_workflow_timer_model' => \Workflow\Models\StoredWorkflowTimer::class,
'workflow_relationships_table' => 'workflow_relationships',
'serializer' => \Workflow\Serializers\Y::class,
'prune_age' => '1 month',
'webhooks_route' => env('WORKFLOW_WEBHOOKS_ROUTE', 'webhooks'),
'monitor' => env('WORKFLOW_MONITOR', false),
'monitor_url' => env('WORKFLOW_MONITOR_URL'),
'monitor_api_key' => env('WORKFLOW_MONITOR_API_KEY'),
'monitor_connection' => env('WORKFLOW_MONITOR_CONNECTION', config('queue.default')),
'monitor_queue' => env(
'WORKFLOW_MONITOR_QUEUE',
config('queue.connections.' . config('queue.default') . '.queue', 'default')
),
];

foreach ($expectedConfig as $key => $expectedValue) {
$this->assertTrue(array_key_exists($key, $config), "The config key [workflows.{$key}] is missing.");

$this->assertEquals(
$expectedValue,
$config[$key],
"The config key [workflows.{$key}] does not match the expected value."
);
}
}
}
21 changes: 21 additions & 0 deletions tests/Unit/Exceptions/NonRetryableExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Tests\Unit;

use Exception;
use Tests\TestCase;
use Workflow\Exceptions\NonRetryableException;

final class NonRetryableExceptionTest extends TestCase
{
public function testException(): void
{
$exception = new NonRetryableException('test', 1, new Exception('test'));

$this->assertSame('test', $exception->getMessage());
$this->assertSame(1, $exception->getCode());
$this->assertInstanceOf(Exception::class, $exception->getPrevious());
}
}
106 changes: 106 additions & 0 deletions tests/Unit/Providers/WorkflowServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

declare(strict_types=1);

namespace Tests\Unit;

use Illuminate\Support\Facades\Artisan;
use Tests\TestCase;
use Workflow\Events\ActivityCompleted;
use Workflow\Events\ActivityFailed;
use Workflow\Events\ActivityStarted;
use Workflow\Events\WorkflowCompleted;
use Workflow\Events\WorkflowFailed;
use Workflow\Events\WorkflowStarted;
use Workflow\Providers\WorkflowServiceProvider;

final class WorkflowServiceProviderTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$this->app->register(WorkflowServiceProvider::class);
}

public function testProviderLoads(): void
{
$this->assertTrue(
$this->app->getProvider(WorkflowServiceProvider::class) instanceof WorkflowServiceProvider
);
}

public function testEventListenersAreRegistered(): void
{
config([
'workflows.monitor' => true,
]);

(new WorkflowServiceProvider($this->app))->boot();

$dispatcher = app('events');

$expectedListeners = [
WorkflowStarted::class => \Workflow\Listeners\MonitorWorkflowStarted::class,
WorkflowCompleted::class => \Workflow\Listeners\MonitorWorkflowCompleted::class,
WorkflowFailed::class => \Workflow\Listeners\MonitorWorkflowFailed::class,
ActivityStarted::class => \Workflow\Listeners\MonitorActivityStarted::class,
ActivityCompleted::class => \Workflow\Listeners\MonitorActivityCompleted::class,
ActivityFailed::class => \Workflow\Listeners\MonitorActivityFailed::class,
];

foreach ($expectedListeners as $event => $listener) {
$registeredListeners = $dispatcher->getListeners($event);

$attached = false;
foreach ($registeredListeners as $registeredListener) {
if ($registeredListener instanceof \Closure) {
$closureReflection = new \ReflectionFunction($registeredListener);
$useVariables = $closureReflection->getStaticVariables();

if (isset($useVariables['listener']) && is_array($useVariables['listener'])) {
if ($useVariables['listener'][0] === $listener) {
$attached = true;
break;
}
}
}
}

$this->assertTrue($attached, "Event [{$event}] does not have the [{$listener}] listener attached.");
}
}

public function testConfigIsPublished(): void
{
Artisan::call('vendor:publish', [
'--tag' => 'config',
]);

$this->assertFileExists(config_path('workflows.php'));
}

public function testMigrationsArePublished(): void
{
Artisan::call('vendor:publish', [
'--tag' => 'migrations',
]);

$migrationFiles = glob(database_path('migrations/*.php'));
$this->assertNotEmpty($migrationFiles, 'Migrations should be published');
}

public function testCommandsAreRegistered(): void
{
$registeredCommands = array_keys(Artisan::all());

$expectedCommands = ['make:activity', 'make:workflow'];

foreach ($expectedCommands as $command) {
$this->assertContains(
$command,
$registeredCommands,
"Command [{$command}] is not registered in Artisan."
);
}
}
}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy