Skip to content

Commit

Permalink
Merge pull request #23 from softius/custom-exceptions
Browse files Browse the repository at this point in the history
Custom exceptions
  • Loading branch information
softius authored Jul 11, 2018
2 parents 1527c0f + 002bac1 commit f02d3db
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 22 deletions.
13 changes: 13 additions & 0 deletions src/Engine/InvalidStateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Phlow\Engine;

/**
* Class InvalidStateException
* Indicates that the execution has reached an invalid state and can not advance further.
* For instance an EndEvent has been already reached or the next Workflow Node is undefined.
* @package Phlow\Engine
*/
class InvalidStateException extends \RuntimeException
{
}
12 changes: 12 additions & 0 deletions src/Engine/UndefinedHandlerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Phlow\Engine;

/**
* Class UndefinedHandlerException
* Indicates that no handler was found and execution can not advance further
* @package Phlow\Engine
*/
class UndefinedHandlerException extends \Exception
{
}
11 changes: 6 additions & 5 deletions src/Engine/WorkflowInstance.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function advance($howMany = 1)
{
$this->initNodes();
if ($this->isCompleted()) {
throw new \RuntimeException("Workflow has been already completed.");
throw new InvalidStateException('Workflow execution has reached an End event and can not advance further.');
}

// Retrieve and execute the next node
Expand Down Expand Up @@ -112,6 +112,7 @@ private function handleCurrentNode(): void
* Handles a raised exception by moving the flow to an error event
* If no error handling was configured, another Exception will be thrown halting the execution
* @param \Exception $exception
* @throws UndefinedHandlerException
*/
private function handleException(\Exception $exception): void
{
Expand All @@ -128,7 +129,7 @@ private function handleException(\Exception $exception): void
$exceptionClass = get_parent_class($exceptionClass);
}

throw new \RuntimeException(
throw new UndefinedHandlerException(
sprintf("The exception %s was thrown but no Error Event was found", get_class($exception))
);
}
Expand All @@ -144,7 +145,7 @@ private function initNodes(): void

$startEvents = $this->workflow->getAllByClass(StartEvent::class);
if (empty($startEvents)) {
throw new \RuntimeException('Start event is missing');
throw new InvalidStateException('Start event is missing.');
}

$this->currentNode = $startEvents[0];
Expand All @@ -157,7 +158,7 @@ private function getErrorEvents(): array
{
$errorEvents = $this->workflow->getAllByClass(ErrorEvent::class);
if (empty($errorEvents)) {
throw new \RuntimeException('Error events are missing');
throw new InvalidStateException('Error events are missing');
}

$errorEventsMap = [];
Expand Down Expand Up @@ -197,6 +198,6 @@ public function current(): WorkflowNode
return $this->currentNode;
}

throw new \RuntimeException("Execution has not been initiated for this Workflow.");
throw new InvalidStateException('Execution has not been initiated for this Workflow.');
}
}
2 changes: 1 addition & 1 deletion src/Handler/ConditionalConnectionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function handle(WorkflowNode $workflowNode, Exchange $exchange): Workflow
}
}

throw new \RuntimeException("No condition was matched. Unable to calculate the next node.");
throw new UnmatchedConditionException('No condition was matched');
}


Expand Down
2 changes: 1 addition & 1 deletion src/Handler/SingleConnectionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function handle(WorkflowNode $workflowNode, Exchange $exchange): Workflow
{
$connections = $workflowNode->getOutgoingConnections();
if (empty($connections)) {
throw new \RuntimeException("No connections found.");
throw new UnmatchedConditionException('No condition was matched');
}

/** @var WorkflowConnection $connection */
Expand Down
7 changes: 7 additions & 0 deletions src/Handler/UnmatchedConditionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Phlow\Handler;

class UnmatchedConditionException extends \RuntimeException
{
}
2 changes: 1 addition & 1 deletion src/Model/ExceptionHandlerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function getExceptionClass(): string
return $this->exceptionClass;
}

throw new \RuntimeException("No Exception class was never provided for this task.");
throw new \UnexpectedValueException("No Exception class was provided for this Node");
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Model/ExecutableNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface ExecutableNode
/**
* Returns the callback associated with this Task
* If no callback was provided, an Exception is thrown
* @throws \RuntimeException
* @throws \UnexpectedValueException
* @return callable
*/
public function getCallback(): callable;
Expand Down
4 changes: 2 additions & 2 deletions src/Model/ExecutableNodeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait ExecutableNodeTrait
/**
* Returns the callback associated with this Task
* If no callback was provided, an Exception is thrown
* @throws \RuntimeException
* @throws \UnexpectedValueException
* @return callable
*/
public function getCallback(): callable
Expand All @@ -22,7 +22,7 @@ public function getCallback(): callable
return $this->callback;
}

throw new \RuntimeException("Callback was never provided for this task");
throw new \UnexpectedValueException("No callback was provided for this Node");
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Model/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class Workflow
* Maintains reference for all the nodes, that composite this workflow.
* @param WorkflowNode $node
* @return WorkflowNode
* @throws \RuntimeException
*/
public function add(WorkflowNode $node): WorkflowNode
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Activity/TaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function testWithoutCallbacks()
{
$task = new Task();
$this->assertFalse($task->hasCallback());
$this->expectException(\RuntimeException::class);
$this->expectException(\UnexpectedValueException::class);
$task->getCallback();
}
}
3 changes: 2 additions & 1 deletion tests/Engine/SingleConnectionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Phlow\Activity\Task;
use Phlow\Engine\Exchange;
use Phlow\Handler\SingleConnectionHandler;
use Phlow\Handler\UnmatchedConditionException;
use Phlow\Model\WorkflowConnection;
use PHPUnit\Framework\TestCase;

Expand All @@ -26,7 +27,7 @@ public function testConnection()
public function testNoConnection()
{
$handler = new SingleConnectionHandler();
$this->expectException(\RuntimeException::class);
$this->expectException(UnmatchedConditionException::class);
$handler->handle(new Task(), new Exchange());
}
}
3 changes: 2 additions & 1 deletion tests/Event/EndEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Phlow\Engine\Exchange;
use Phlow\Handler\SingleConnectionHandler;
use Phlow\Handler\UnmatchedConditionException;
use Phlow\Event\EndEvent;
use PHPUnit\Framework\TestCase;

Expand All @@ -14,7 +15,7 @@ public function testNext()
$task = new EndEvent();
$handler = new SingleConnectionHandler();

$this->expectException(\RuntimeException::class);
$this->expectException(UnmatchedConditionException::class);
$handler->handle($task, new Exchange());
}
}
3 changes: 2 additions & 1 deletion tests/Gateway/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Phlow\Engine\Exchange;
use Phlow\Engine\ExpressionEngine;
use Phlow\Handler\ConditionalConnectionHandler;
use Phlow\Handler\UnmatchedConditionException;
use Phlow\Gateway\ExclusiveGateway;
use Phlow\Model\WorkflowConnection;
use Phlow\Tests\Engine\DummyExpressionEngine;
Expand All @@ -27,7 +28,7 @@ public function testFlows()
$this->assertEquals($nextTask, $handler->handle($gateway, $exchange));

$exchange = new Exchange((object) ['num' => 50]);
$this->expectException(\RuntimeException::class);
$this->expectException(UnmatchedConditionException::class);
$this->assertEquals($nextTask, $handler->handle($gateway, $exchange));

$exchange = new Exchange((object) ['num' => 500]);
Expand Down
14 changes: 8 additions & 6 deletions tests/Workflow/WorkflowInstanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace Phlow\Tests\Workflow;

use Phlow\Activity\Task;
use Phlow\Engine\UndefinedHandlerException;
use Phlow\Event\EndEvent;
use Phlow\Model\Workflow;
use Phlow\Model\WorkflowBuilder;
use Phlow\Engine\WorkflowInstance;
use Phlow\Engine\InvalidStateException;
use PHPUnit\Framework\TestCase;

class WorkflowInstanceTest extends TestCase
Expand Down Expand Up @@ -45,7 +47,7 @@ public function testNoStartEvent()
{
$flow = new Workflow();
$instance = new WorkflowInstance($flow, []);
$this->expectException(\RuntimeException::class);
$this->expectException(InvalidStateException::class);
$instance->advance();
}

Expand All @@ -56,7 +58,7 @@ public function testAlreadyCompleted()
->start()
->end();
$instance = new WorkflowInstance($builder->getWorkflow(), []);
$this->expectException(\RuntimeException::class);
$this->expectException(InvalidStateException::class);
$instance->advance(3);

$this->assertTrue($instance->isCompleted());
Expand All @@ -67,7 +69,7 @@ public function testAlreadyCompleted()
public function testCurrentBeforeExecution()
{
$workflow = $this->getPipeline();
$this->expectException(\RuntimeException::class);
$this->expectException(InvalidStateException::class);
$workflow->current();
}

Expand Down Expand Up @@ -108,11 +110,11 @@ public function testMissingErrorHandling()
->end();
$instance = new WorkflowInstance($builder->getWorkflow(), ['num' => 10]);

$this->expectException(\RuntimeException::class);
$this->expectException(InvalidStateException::class);
$instance->advance(2);
}

public function testUnmatchedErrorHandling()
public function testUndefinedErrorHandling()
{
$builder = new WorkflowBuilder();
$builder
Expand All @@ -132,7 +134,7 @@ public function testUnmatchedErrorHandling()
->end();
$instance = new WorkflowInstance($builder->getWorkflow(), ['num' => 10]);

$this->expectException(\RuntimeException::class);
$this->expectException(UndefinedHandlerException::class);
$instance->advance(2);
}

Expand Down

0 comments on commit f02d3db

Please sign in to comment.