Zeta Workflow 10: Execution State Interpreter
The interpreter returns a text string. Here is the logic:
- If the ‘cancel’ variable exists, return “cancel”.
- If the ‘swap’ variable exists, it names the new workflow. Return “swap “ followed by the new workflow name. See the below discussion.
- If the ‘slide’ variable exists, return “slide “ followed by the slide variable’s value.
- Otherwise, return an empty string.
The above steps are in priority order.
The class has only a single method, interpretExecutionState(ExecutionState $state). The object does not need to cache anything; it simply interprets the passed-in execution state object, returning a string as the interpretation.
Given that our unit tests already interpret the slide variable, we can adapt one of those tests to exercise the interpreter. Once interpreter development is complete (with unit tests), make a copy of the subWorkflowTest.php. Change the copy’s verifySlide method to use the interpreter. All sub workflow tests should continue to pass while using the Execution State Interpreter.
Oops, I got ahead of myself. We’ll adapt a copy of the subWorkflowTest once the entire sniffer is complete and is getting attached to the workflow.
Test Outcomes
Given the above simplistic interpretation rules, we have eight possible outcomes depending on whether the cancel, swap, slide variables do or do not exist. We can set up a single test with a data provider driving the eight scenarios.
In the data provider, we’ll use null to indicate the variable does not exist, and its value otherwise. We can condition a mock ExecutionState object.
After the several iterations fleshing out the tests and production code, here is the result.
The test:
namespace StarTribune\Workflow\Tests\Sniffer; use Mockery as m; class executionStateInterpreterTest extends \TestCase { public $state; public $fixture; public function setUp() { parent::setUp(); $this->state = m::mock('\StarTribune\Workflow\Sniffer\ExecutionState'); $this->fixture = new \StarTribune\Workflow\Sniffer\ExecutionStateInterpreter; } public function conditionState($cancel, $swap, $slide) { $this->conditionStateVariable('cancel', $cancel); $this->conditionStateVariable('swap', $swap); $this->conditionStateVariable('slide', $slide); } public function conditionStateVariable($key, $value) { if(null === $value) { $this->state->shouldReceive('hasVariable') ->with($key) ->andReturn(false); } else { $this->state->shouldReceive('hasVariable') ->with($key) ->andReturn(true); $this->state->shouldReceive('getVariable') ->with($key) ->andReturn($value); } } public function testMockCorrectClass() { $this->assertTrue($this->state instanceof \StarTribune\Workflow\Sniffer\ExecutionState); } public function testFixtureCorrectClass() { $this->assertTrue($this->fixture instanceof \StarTribune\Workflow\Sniffer\ExecutionStateInterpreter); } /** * @dataProvider dataInterpretation */ public function testInterpretation($cancel, $swap, $slide, $result) { $this->conditionState($cancel, $swap, $slide); $actual = $this->fixture->interpretExecutionState($this->state); $this->assertSame($result, $actual); } public function dataInterpretation() { $data = array(); $data[] = array(null, null, null, ''); $data[] = array(1, null, null, 'cancel'); $data[] = array(0, null, null, 'cancel'); $data[] = array('', null, null, 'cancel'); $data[] = array('cancel', null, null, 'cancel'); $data[] = array(1, 1, null, 'cancel'); $data[] = array(1, null, 1, 'cancel'); $data[] = array(1, 1, 1, 'cancel'); $data[] = array(null, 1, null, 'swap 1'); $data[] = array(null, 0, null, 'swap 0'); $data[] = array(null, '', null, 'swap '); $data[] = array(null, 'swap', null, 'swap swap'); $data[] = array(null, 1, 1234, 'swap 1'); $data[] = array(null, 0, 1234, 'swap 0'); $data[] = array(null, '', 1234, 'swap '); $data[] = array(null, 'swap', 1234, 'swap swap'); $data[] = array(null, null, 1, 'slide 1'); $data[] = array(null, null, 0, 'slide 0'); $data[] = array(null, null, '', 'slide '); $data[] = array(null, null, 'slide', 'slide slide'); return $data; } }
The production class:
namespace StarTribune\Workflow\Sniffer; use \StarTribune\Workflow\Sniffer\ExecutionState; class ExecutionStateInterpreter { public function interpretExecutionState(\StarTribune\Workflow\Sniffer\ExecutionState $state) { $result = ''; if($state->hasVariable('cancel')) { $result = 'cancel'; } elseif($state->hasVariable('swap')) { $result = 'swap '.$state->getVariable('swap'); } elseif($state->hasVariable('slide')) { $result = 'slide '.$state->getVariable('slide'); } return $result; } }
Next up: The Sniffer Plugin