diff --git a/composer.json b/composer.json index 54ecd44..bb20726 100644 --- a/composer.json +++ b/composer.json @@ -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.*", diff --git a/src/Actions/Switcher.php b/src/Actions/Switcher.php index 6a2d4bc..35bafd3 100644 --- a/src/Actions/Switcher.php +++ b/src/Actions/Switcher.php @@ -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 { diff --git a/src/Providers/ActionServiceProvider.php b/src/Providers/ActionServiceProvider.php index 0d80f08..3b702cf 100644 --- a/src/Providers/ActionServiceProvider.php +++ b/src/Providers/ActionServiceProvider.php @@ -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(); } diff --git a/src/Services/Action.php b/src/Services/Action.php index 97cf357..d7ed017 100644 --- a/src/Services/Action.php +++ b/src/Services/Action.php @@ -51,7 +51,6 @@ public function starter() { Log::debug('Workflow - Checking'); - $this->boot(); $this->dispatchByRelations(); $this->dispatchByWorkflowNodeState(); } @@ -79,6 +78,7 @@ public function boot() $this->events = Collection::make(); Event::listen(['*'], function ($eventName, $events) { + if (count($events) === 0) { return true; } @@ -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)); @@ -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)); } @@ -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) { @@ -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); diff --git a/tests/DummyEvent.php b/tests/DummyEvent.php index e30f3f4..44485ef 100644 --- a/tests/DummyEvent.php +++ b/tests/DummyEvent.php @@ -17,4 +17,9 @@ public function getData() 'message' => $this->message, ]; } + + public function __toString() + { + return "DummyEvent: ".$this->message; + } } diff --git a/tests/NotificationTest.php b/tests/NotificationTest.php index f39040a..278980e 100644 --- a/tests/NotificationTest.php +++ b/tests/NotificationTest.php @@ -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() + ); } } diff --git a/tests/StateTest.php b/tests/StateTest.php index 8a0dd6f..dd0fa7e 100644 --- a/tests/StateTest.php +++ b/tests/StateTest.php @@ -46,6 +46,7 @@ public function testLogEvent() $workflow = $workflowManager->createOrFail([ 'name' => 'Log events', + 'autostart' => 1 ])->getResource(); $node1 = $workflowNodeManager->createOrFail([ @@ -66,7 +67,7 @@ public function testLogEvent() 'name' => 'foo', 'action' => 'create', 'parameters' => [ - 'name' => '{{ event.message }}', + 'name' => 'Nome: {{ event }}', ], ]), ])->getResource(); @@ -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() @@ -122,6 +136,7 @@ public function testSwitcher() $workflow = $workflowManager->createOrFail([ 'name' => 'Should work with 2 workflow', + 'autostart' => 1 ])->getResource(); $node1 = $workflowNodeManager->createOrFail([ @@ -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() + ); } /** @@ -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', @@ -259,6 +296,7 @@ public function testHttp() $workflow = $workflowManager->createOrFail([ 'name' => 'Send http call', + 'autostart' => 1 ])->getResource(); $node1 = $workflowNodeManager->createOrFail([ @@ -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', @@ -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, @@ -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'); @@ -349,4 +418,5 @@ public function testNodes() ], ]); } + */ }