From b805fde3ce5528a0bdf843a592263d76c18d0605 Mon Sep 17 00:00:00 2001 From: Julien Pottier Date: Wed, 1 Jul 2020 15:24:38 +0200 Subject: [PATCH 1/3] Missing an object for StateMachine construct in ArrayLoaderTest --- tests/Finite/Test/Loader/ArrayLoaderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Finite/Test/Loader/ArrayLoaderTest.php b/tests/Finite/Test/Loader/ArrayLoaderTest.php index 4217845..3816797 100644 --- a/tests/Finite/Test/Loader/ArrayLoaderTest.php +++ b/tests/Finite/Test/Loader/ArrayLoaderTest.php @@ -155,7 +155,7 @@ public function testLoadCallbacks() public function testLoadWithProperties() { - $sm = new StateMachine(); + $sm = new StateMachine($this->getMock('Finite\StatefulInterface')); $this->object = new ArrayLoader( array( From 027c0b2930cf857c3c0a7aadab3a4daecaf0edae Mon Sep 17 00:00:00 2001 From: Julien Pottier Date: Wed, 1 Jul 2020 16:47:36 +0200 Subject: [PATCH 2/3] Fixed property path accessor in state machine with array loader and custom property path --- .../DependencyInjection/Configuration.php | 2 +- src/Finite/Loader/ArrayLoader.php | 7 ++- tests/Finite/Test/Loader/ArrayLoaderTest.php | 60 ++++++++++++++++++- 3 files changed, 65 insertions(+), 4 deletions(-) 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/Loader/ArrayLoaderTest.php b/tests/Finite/Test/Loader/ArrayLoaderTest.php index 3816797..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'); @@ -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); + } } From 9f0c39c30e08bb495a9eb817c70b466e1a1bfb99 Mon Sep 17 00:00:00 2001 From: Julien Pottier Date: Wed, 1 Jul 2020 16:57:28 +0200 Subject: [PATCH 3/3] Fixed test --- .../DependencyInjection/FiniteFiniteExtensionTest.php | 1 - 1 file changed, 1 deletion(-) 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',