Zeta Workflow 10: Execution State Interpreter

The inter­preter returns a text string. Here is the logic:

  1. If the ‘can­cel’ vari­able exists, return “cancel”.
  2. If the ‘swap’ vari­able exists, it names the new work­flow. Return “swap “ fol­lowed by the new work­flow name. See the below discussion.
  3. If the ‘slide’ vari­able exists, return “slide “ fol­lowed by the slide variable’s value.
  4. Oth­er­wise, 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() {
        $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) {
        } else {

    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