Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix state accessor array loader #155

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function getConfigTreeBuilder()
$rootProto
->scalarNode('class')->isRequired()->end()
->scalarNode('graph')->defaultValue('default')->end()
->scalarNode('property_path')->defaultValue('finiteState')->end();
->scalarNode('property_path')->end();

$this->addStateSection($rootProto);
$this->addTransitionSection($rootProto);
Expand Down
7 changes: 5 additions & 2 deletions src/Finite/Loader/ArrayLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public function __construct(array $config, CallbackHandler $handler = null, Call
array(
'class' => '',
'graph' => 'default',
'property_path' => 'finiteState',
'states' => array(),
'transitions' => array(),
),
Expand All @@ -69,7 +68,11 @@ public function load(StateMachineInterface $stateMachine)
$this->callbackBuilderFactory = new CallbackBuilderFactory();
}

if (!$stateMachine->hasStateAccessor()) {
if (
isset($this->config['property_path'])
&& is_string($this->config['property_path'])
&& !empty($this->config['property_path'])
) {
$stateMachine->setStateAccessor(new PropertyPathStateAccessor($this->config['property_path']));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ private function getExpectedConfig()
return array(
'class' => 'Stateful1',
'graph' => 'default',
'property_path' => 'finiteState',
'states' => array(
'state1' => array(
'type' => 'initial',
Expand Down
62 changes: 60 additions & 2 deletions tests/Finite/Test/Loader/ArrayLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ protected function setUp()
public function testLoad()
{
$sm = $this->getMock('Finite\StateMachine\StateMachine');
$sm->expects($this->once())->method('setStateAccessor');
$sm->expects($this->once())->method('setGraph');
$sm->expects($this->exactly(3))->method('addState');
$sm->expects($this->exactly(2))->method('addTransition');
Expand Down Expand Up @@ -155,7 +154,7 @@ public function testLoadCallbacks()

public function testLoadWithProperties()
{
$sm = new StateMachine();
$sm = new StateMachine($this->getMock('Finite\StatefulInterface'));

$this->object = new ArrayLoader(
array(
Expand Down Expand Up @@ -205,4 +204,63 @@ public function testSupports()
$this->assertTrue($alternativeLoader->supports($object, 'foobar'));
$this->assertFalse($alternativeLoader->supports($object));
}

public function testDefaultPropertyPath()
{
$arrayLoader = new ArrayLoader(
array(
'class' => 'Finite\StatefulInterface',
'states' => array(
'begin' => array('type' => 'initial', 'properties' => array()),
'end' => array('type' => 'final', 'properties' => array()),
),
'transitions' => array(
'finish' => array(
'from' => array('begin'),
'to' => 'end'
)
)
),
$this->callbackHandler
);

$stateMachine = new StateMachine($this->getMock('Finite\StatefulInterface'));

$arrayLoader->load($stateMachine);

$this->assertAttributeInstanceOf('Finite\State\Accessor\PropertyPathStateAccessor', 'stateAccessor', $stateMachine);
$stateAccessor = $this->readAttribute($stateMachine, 'stateAccessor');

$this->assertAttributeEquals('finiteState', 'propertyPath', $stateAccessor);
}

public function testCustomPropertyPath()
{
$arrayLoader = new ArrayLoader(
array(
'class' => 'Finite\StatefulInterface',
'property_path' => 'customField',
'states' => array(
'begin' => array('type' => 'initial', 'properties' => array()),
'end' => array('type' => 'final', 'properties' => array()),
),
'transitions' => array(
'finish' => array(
'from' => array('begin'),
'to' => 'end'
)
)
),
$this->callbackHandler
);

$stateMachine = new StateMachine($this->getMock('Finite\StatefulInterface'));

$arrayLoader->load($stateMachine);

$this->assertAttributeInstanceOf('Finite\State\Accessor\PropertyPathStateAccessor', 'stateAccessor', $stateMachine);
$stateAccessor = $this->readAttribute($stateMachine, 'stateAccessor');

$this->assertAttributeEquals('customField', 'propertyPath', $stateAccessor);
}
}