Skip to content

Commit

Permalink
fixes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
railken committed Apr 4, 2024
1 parent c938a63 commit 64a4592
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 6 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"amethyst/file": "0.3.*",
"amethyst/notification": "0.3.*",
"box/spout": "^2.7",
"guzzlehttp/guzzle": "^7.8"
"guzzlehttp/guzzle": "^7.8",
"nicoswd/php-rule-parser": "^0.7.1"
},
"require-dev": {
"amethyst/foo": "0.3.*",
Expand Down
2 changes: 1 addition & 1 deletion src/Actions/Switcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Amethyst\Models\WorkflowNodeState;
use Amethyst\Services\Bag;
use Illuminate\Support\Facades\Log;
use nicoSWD\Rules\Rule;
use nicoSWD\Rule\Rule;

class Switcher extends Action
{
Expand Down
1 change: 1 addition & 0 deletions src/Providers/ActionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function boot()
app('amethyst.action')->addType('notification', Actions\Notification::class);

if (app('amethyst')->hasAllTables('amethyst.action.data')) {
app('amethyst.action')->boot();
app('amethyst.action')->starter();
}

Expand Down
13 changes: 12 additions & 1 deletion src/Services/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public function starter()
{
Log::debug('Workflow - Checking');

$this->boot();
$this->dispatchByRelations();
$this->dispatchByWorkflowNodeState();
}
Expand Down Expand Up @@ -79,6 +78,7 @@ public function boot()
$this->events = Collection::make();

Event::listen(['*'], function ($eventName, $events) {

if (count($events) === 0) {
return true;
}
Expand Down Expand Up @@ -191,6 +191,8 @@ public function dispatch($workflowNode, $workflowNodeState = null, $parameterDat
$workflowState = $workflowNodeState->workflow_state;
}



// Data is filtered based on output workflowNode
$output = new Bag((array) Yaml::parse((string) $workflowNode->output));

Expand All @@ -199,6 +201,11 @@ public function dispatch($workflowNode, $workflowNodeState = null, $parameterDat

if (count($output) > 0) {
foreach ($output as $key => $value) {

if (array_is_list($output->toArray())) {
$key = $value;
}

$output->set($key, \Illuminate\Support\Arr::get($data->toArray(), $value));
}

Expand All @@ -207,6 +214,7 @@ public function dispatch($workflowNode, $workflowNodeState = null, $parameterDat
$data = new Bag($output);
}


// Define a new state for the node as done
// First time executed, already done
if (!$workflowNodeState) {
Expand Down Expand Up @@ -294,6 +302,9 @@ public function dispatch($workflowNode, $workflowNodeState = null, $parameterDat
$rendered = $generator->generateAndRender($workflowNode->data, $data->toArray());
$data = $data->merge(Yaml::parse($rendered));


Log::debug('Rendering data pre-handle: '.$workflowNode->data);

$actioner->setData($data);

$actioner->handle($data, $workflowNode, $workflowNodeState ? $workflowNodeState : null);
Expand Down
5 changes: 5 additions & 0 deletions tests/DummyEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ public function getData()
'message' => $this->message,
];
}

public function __toString()
{
return "DummyEvent: ".$this->message;
}
}
9 changes: 9 additions & 0 deletions tests/NotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,14 @@ public function testNotification()
]);

app('amethyst.action')->dispatchByWorkflow($workflow);

$this->assertEquals(
"foo",
app('amethyst')->get('notification')->getRepository()->newQuery()->where('notifiable_id', 1)->first()->data->message
);
$this->assertEquals(
1,
app('amethyst')->get('notification')->getRepository()->newQuery()->count()
);
}
}
76 changes: 73 additions & 3 deletions tests/StateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function testLogEvent()

$workflow = $workflowManager->createOrFail([
'name' => 'Log events',
'autostart' => 1
])->getResource();

$node1 = $workflowNodeManager->createOrFail([
Expand All @@ -66,7 +67,7 @@ public function testLogEvent()
'name' => 'foo',
'action' => 'create',
'parameters' => [
'name' => '{{ event.message }}',
'name' => 'Nome: {{ event }}',
],
]),
])->getResource();
Expand All @@ -87,6 +88,19 @@ public function testLogEvent()

event(new DummyEvent('Yeah!'));
event(new DummyEvent('Not Anymore!'));

$this->assertEquals(
"Nome: DummyEvent: Yeah!",
app('amethyst')->get('foo')->getRepository()->newQuery()->where('id', 1)->first()->name
);
$this->assertEquals(
"Nome: DummyEvent: Not Anymore!",
app('amethyst')->get('foo')->getRepository()->newQuery()->where('id', 2)->first()->name
);
$this->assertEquals(
2,
app('amethyst')->get('foo')->getRepository()->newQuery()->count()
);
}

public function testSwitcher()
Expand Down Expand Up @@ -122,6 +136,7 @@ public function testSwitcher()

$workflow = $workflowManager->createOrFail([
'name' => 'Should work with 2 workflow',
'autostart' => 1
])->getResource();

$node1 = $workflowNodeManager->createOrFail([
Expand Down Expand Up @@ -229,6 +244,20 @@ public function testSwitcher()
]);

event(new DummyEvent('1'));

// Only Street 1 should be called since of the switcher
$this->assertEquals(
"Street 1: Event 1",
app('amethyst')->get('foo')->getRepository()->newQuery()->where('id', 1)->first()->name
);
$this->assertEquals(
"The end",
app('amethyst')->get('foo')->getRepository()->newQuery()->where('id', 2)->first()->name
);
$this->assertEquals(
2,
app('amethyst')->get('foo')->getRepository()->newQuery()->count()
);
}

/**
Expand All @@ -248,6 +277,14 @@ public function testHttp()
'arguments' => [],
]),
])->getResource();

$dataAction = $actionManager->createOrFail([
'name' => 'Data Manipulation',
'payload' => Yaml::dump([
'class' => 'data',
'arguments' => [],
]),
])->getResource();

$eventListenerAction = $actionManager->createOrFail([
'name' => 'Event Listener',
Expand All @@ -259,6 +296,7 @@ public function testHttp()

$workflow = $workflowManager->createOrFail([
'name' => 'Send http call',
'autostart' => 1
])->getResource();

$node1 = $workflowNodeManager->createOrFail([
Expand All @@ -277,7 +315,7 @@ public function testHttp()
'target_id' => $httpAction->id,
'output' => Yaml::dump(['response']),
'data' => Yaml::dump([
'url' => 'https://api.github.com/orgs/octokit/repos',
'url' => 'https://api.github.com/random',
'method' => 'GET',
'headers' => [
'test' => 'Hello',
Expand All @@ -289,6 +327,20 @@ public function testHttp()
]),
])->getResource();

$node3 = $workflowNodeManager->createOrFail([
'workflow_id' => $workflow->id,
'target_type' => 'action',
'target_id' => $dataAction->id,
'data' => Yaml::dump([
'name' => 'foo',
'action' => 'create',
'parameters' => [
'name' => 'Request Log',
'description' => 'Status: {{ response.status }}',
],
]),
])->getResource();

$relationManager->createOrFail([
'source_type' => 'workflow',
'source_id' => $workflow->id,
Expand All @@ -303,9 +355,26 @@ public function testHttp()
'target_id' => $node2->id,
]);

$relationManager->createOrFail([
'source_type' => 'workflow-node',
'source_id' => $node2->id,
'target_type' => 'workflow-node',
'target_id' => $node3->id,
]);


event(new DummyEvent("It's time for a new request!"));
}

$this->assertEquals(
"Status: 404",
app('amethyst')->get('foo')->getRepository()->newQuery()->where('id', 1)->first()->description
);
$this->assertEquals(
1,
app('amethyst')->get('foo')->getRepository()->newQuery()->count()
);
}
/**
public function testNodes()
{
$node = \Amethyst\Services\Node::workflow('Hello darkness my old friend');
Expand Down Expand Up @@ -349,4 +418,5 @@ public function testNodes()
],
]);
}
*/
}

0 comments on commit 64a4592

Please sign in to comment.