diff --git a/src/Finite/Bundle/FiniteBundle/DependencyInjection/Configuration.php b/src/Finite/Bundle/FiniteBundle/DependencyInjection/Configuration.php index 5a2b8d0..32fb60b 100644 --- a/src/Finite/Bundle/FiniteBundle/DependencyInjection/Configuration.php +++ b/src/Finite/Bundle/FiniteBundle/DependencyInjection/Configuration.php @@ -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); diff --git a/src/Finite/Loader/ArrayLoader.php b/src/Finite/Loader/ArrayLoader.php index 12755b6..2dab6d9 100644 --- a/src/Finite/Loader/ArrayLoader.php +++ b/src/Finite/Loader/ArrayLoader.php @@ -48,7 +48,6 @@ public function __construct(array $config, CallbackHandler $handler = null, Call array( 'class' => '', 'graph' => 'default', - 'property_path' => 'finiteState', 'states' => array(), 'transitions' => array(), ), @@ -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'])); } diff --git a/tests/Finite/Test/Bundle/FiniteBundle/DependencyInjection/FiniteFiniteExtensionTest.php b/tests/Finite/Test/Bundle/FiniteBundle/DependencyInjection/FiniteFiniteExtensionTest.php index 6f47922..35e2f74 100644 --- a/tests/Finite/Test/Bundle/FiniteBundle/DependencyInjection/FiniteFiniteExtensionTest.php +++ b/tests/Finite/Test/Bundle/FiniteBundle/DependencyInjection/FiniteFiniteExtensionTest.php @@ -54,7 +54,6 @@ private function getExpectedConfig() return array( 'class' => 'Stateful1', 'graph' => 'default', - 'property_path' => 'finiteState', 'states' => array( 'state1' => array( 'type' => 'initial', diff --git a/tests/Finite/Test/Loader/ArrayLoaderTest.php b/tests/Finite/Test/Loader/ArrayLoaderTest.php index 4217845..927b67c 100644 --- a/tests/Finite/Test/Loader/ArrayLoaderTest.php +++ b/tests/Finite/Test/Loader/ArrayLoaderTest.php @@ -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'); @@ -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( @@ -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); + } }