Perforce Chronicle 2012.2/486814
API Documentation

Workflow_Test_StateTest Class Reference

Test the workflow state model. More...

List of all members.

Public Member Functions

 testAccessors ()
 Test accessors.
 testHasTransition ()
 Tets hasTransition() method.

Detailed Description

Test the workflow state model.

Copyright:
2011-2012 Perforce Software. All rights reserved
License:
Please see LICENSE.txt in top-level folder of this distribution.
Version:
2012.2/486814

Member Function Documentation

Workflow_Test_StateTest::testAccessors ( )

Test accessors.

    {
        $workflowData = array(
            'id'            => 'workflow_test',
            'label'         => 'test Workflow',
            'states'        => array(
                'test'          => array(
                    'label'         => 'test label',
                    'transitions'   => array(
                        'foo'       => array('label' => 'promote to foo'),
                        'bar'       => array('label' => 'promote to bar'),
                        'baz'       => array('label' => 'promote to baz')
                    )
                ),
                'foo'           => array(
                    'label'         => 'foo state',
                ),
                'bar'           => array(
                    'label'         => 'bar state',
                ),
                'baz'           => array(
                    'label'         => 'baz state',
                )
            )
        );
        $workflow = Workflow_Model_Workflow::store($workflowData);

        // get state object
        $state = $workflow->getStateModel('test');

        $this->assertSame(
            'test',
            $state->getId(),
            "Expected state id."
        );
        $this->assertSame(
            'test label',
            $state->getLabel(),
            "Expected state label name."
        );
        $this->assertSame(
            $workflowData['states']['test']['transitions'],
            $state->getTransitions(),
            "Expected state transitions."
        );
        $this->assertTrue(
            $state->getWorkflow() instanceof Workflow_Model_Workflow,
            "Expected state workflow class type."
        );

        // getTransitionModels() should return iterator with transition objects
        $transitions = $state->getTransitionModels();
        $this->assertTrue(
            $transitions instanceof P4Cms_Model_Iterator,
            "Expected class type returned by getTransitions() method."
        );
        $this->assertEquals(
            3,
            $transitions->count(),
            "Expected number of state transitions."
        );

        foreach ($transitions as $transition) {
            $this->assertTrue(
                $transition instanceof Workflow_Model_Transition,
                "Expected class type of state transition item."
            );
        }

        // fetch transition
        $this->assertSame(
            array('label' => 'promote to bar'),
            $state->getTransition('bar'),
            "Expected empty array when fetch bar transition."
        );
        $this->assertSame(
            array('label' => 'promote to baz'),
            $state->getTransition('baz'),
            "Expected empty array when fetch baz transition."
        );

        // fetch transition model
        $this->assertSame(
            'promote to bar',
            $state->getTransitionModel('bar')->getlabel(),
            "Expected value when fetch bar transition model."
        );
        $this->assertSame(
            'promote to baz',
            $state->getTransitionModel('baz')->getlabel(),
            "Expected value when fetch baz transition model."
        );

        // verify getTransition() throws exception if transition doesn't exist
        try {
            $state->getTransition('noexist');
            $this->fail("Unexpected getTransition() method behavior - should throw an exception.");
        } catch (Workflow_Exception $e) {
            // expected exception
        }

        // verify that getWorkflow() throws an exception if workflow is not set
        $state = Workflow_Model_State::create(
            array(
                'label'         => 'test state',
                'transitions'   => array(
                    'foo'       => array('label' => 'promote to foo'),
                    'bar'       => array('label' => 'promote to bar')
                )
            )
        );

        try {
            $state->getWorkflow();
            $this->fail("Unexpected getWorkflow() method behavior - should throw an exception.");
        } catch (Workflow_Exception $e) {
            // expected exception
        }

        // verify that getTransitions() return only transitions that have valid target state
        $workflow = Workflow_Model_Workflow::create(
            array(
                'states' => array('bar' => array())
            )
        );

        $state->setWorkflow($workflow);
        $this->assertSame(
            array('bar'),
            array_keys($state->getTransitions()),
            "Expected no transiitons as foo is not a valid state of the associated workflow."
        );
    }
Workflow_Test_StateTest::testHasTransition ( )

Tets hasTransition() method.

    {
        $workflow = new Workflow_Model_Workflow;
        $state    = Workflow_Model_State::create(
            array(
                'label'         => 'foo',
                'transitions'   => array(
                    'foo'       => array('label' => 'bar'),
                    'bar'       => array('label' => 'foo')
                ),
                'gg'            => array(
                    'ff'
                ),
                'workflow'      => $workflow
            )
        );

        // hasTransition considers only valid transitions
        $this->assertFalse(
            $state->hasTransition('foo'),
            "Expected state has no transition 'foo' as 'foo' is not defined by the governing workflow."
        );

        $workflow->setStates(array('foo' => array(), 'bar' => array()));
        $this->assertTrue(
            $state->hasTransition('foo'),
            "Expected state has transition 'foo'."
        );
        $this->assertTrue(
            $state->hasTransition('bar'),
            "Expected state has transition 'bar'."
        );
        $this->assertFalse(
            $state->hasTransition('foobar'),
            "Expected state has no transition 'foobar'."
        );
    }

The documentation for this class was generated from the following file: