diff --git a/src/Piece/Flow/Continuation/ContinuationServer.php b/src/Piece/Flow/Continuation/ContinuationServer.php index 6304de8..9e989fb 100644 --- a/src/Piece/Flow/Continuation/ContinuationServer.php +++ b/src/Piece/Flow/Continuation/ContinuationServer.php @@ -92,15 +92,12 @@ public function __construct(PageFlowRepository $pageFlowRepository, GC $gc = nul * Adds a flow definition to the Continuation object. * * @param string $flowID - * @param string $source * @param boolean $isExclusive */ - public function addFlow($flowID, $source, $isExclusive = false) + public function addFlow($flowID, $isExclusive = false) { - $this->flowDefinitions[$flowID] = array('source' => $source, - 'isExclusive' => $isExclusive - ); - $this->pageFlowRepository->add($source); + $this->flowDefinitions[$flowID] = array('isExclusive' => $isExclusive); + $this->pageFlowRepository->add($flowID); } /** @@ -339,9 +336,9 @@ protected function startFlowExecution($payload) throw new FlowNotFoundException("The flow ID [ {$this->activeFlowID} ] not found in the flow definitions."); } - $flow = $this->pageFlowRepository->findByDefinitionFile($this->flowDefinitions[$this->activeFlowID]['source']); + $flow = $this->pageFlowRepository->findByID($this->activeFlowID); if (is_null($flow)) { - throw new FlowNotFoundException(sprintf('The page flow for the definition file [ %s ] is not found in the repository.', $this->flowDefinitions[$this->activeFlowID]['source'])); + throw new FlowNotFoundException(sprintf('The page flow for ID [ %s ] is not found in the repository.', $this->activeFlowID)); } $flow->setActionInvoker($this->actionInvoker); diff --git a/src/Piece/Flow/PageFlow/Definition17Configuration.php b/src/Piece/Flow/PageFlow/Definition17Configuration.php index 5b0fc22..139aebc 100644 --- a/src/Piece/Flow/PageFlow/Definition17Configuration.php +++ b/src/Piece/Flow/PageFlow/Definition17Configuration.php @@ -54,7 +54,6 @@ public function getConfigTreeBuilder() $treeBuilder = new TreeBuilder(); $treeBuilder->root('definition17') ->children() - ->scalarNode('name')->defaultNull()->cannotBeEmpty()->end() ->scalarNode('firstState')->isRequired()->cannotBeEmpty()->end() ->arrayNode('viewState') ->isRequired() diff --git a/src/Piece/Flow/PageFlow/PageFlow.php b/src/Piece/Flow/PageFlow/PageFlow.php index a99caef..732c743 100644 --- a/src/Piece/Flow/PageFlow/PageFlow.php +++ b/src/Piece/Flow/PageFlow/PageFlow.php @@ -76,6 +76,7 @@ class PageFlow const EVENT_PROTECTED = '__protected'; protected $fsm; + protected $id; protected $views; protected $attributes = array(); protected $lastState; @@ -87,6 +88,15 @@ class PageFlow */ protected $actionInvoker; + /** + * @param string $id + * @since Method available since Release 2.0.0 + */ + public function __construct($id) + { + $this->id = $id; + } + /** * @return array * @since Method available since Release 2.0.0 @@ -159,7 +169,7 @@ public function getView() } if (!array_key_exists($viewIndex, $this->views)) { - throw new InvalidTransitionException("A invalid transition detected. The state [ $viewIndex ] does not have a view. Maybe The state [ $viewIndex ] is an action state. Check the definition of the flow [ {$this->getID()} ]."); + throw new InvalidTransitionException("A invalid transition detected. The state [ $viewIndex ] does not have a view. Maybe The state [ $viewIndex ] is an action state. Check the definition of the flow [ {$this->id} ]."); } return $this->views[$viewIndex]; @@ -172,7 +182,7 @@ public function getView() */ public function getID() { - return $this->fsm->getID(); + return $this->id; } /** diff --git a/src/Piece/Flow/PageFlow/PageFlowFactory.php b/src/Piece/Flow/PageFlow/PageFlowFactory.php index 631be9a..aafe342 100644 --- a/src/Piece/Flow/PageFlow/PageFlowFactory.php +++ b/src/Piece/Flow/PageFlow/PageFlowFactory.php @@ -47,12 +47,25 @@ class PageFlowFactory { /** - * @param string $definitionFile + * @var \Piece\Flow\PageFlow\PageFlowRegistry + */ + protected $pageFlowRegistry; + + /** + * @param \Piece\Flow\PageFlow\PageFlowRegistry $pageFlowRegistry + */ + public function __construct(PageFlowRegistry $pageFlowRegistry) + { + $this->pageFlowRegistry = $pageFlowRegistry; + } + + /** + * @param string $id * @return \Piece\Flow\PageFlow\PageFlow */ - public function create($definitionFile) + public function create($id) { - $pageFlowGenerator = new PageFlowGenerator($definitionFile); + $pageFlowGenerator = new PageFlowGenerator($id, $this->pageFlowRegistry); return $pageFlowGenerator->generate(); } } diff --git a/src/Piece/Flow/PageFlow/PageFlowGenerator.php b/src/Piece/Flow/PageFlow/PageFlowGenerator.php index a911c9e..6096b43 100644 --- a/src/Piece/Flow/PageFlow/PageFlowGenerator.php +++ b/src/Piece/Flow/PageFlow/PageFlowGenerator.php @@ -60,30 +60,26 @@ class PageFlowGenerator protected $pageFlow; /** - * @var \Stagehand\FSM\FSMBuilder + * @var \Piece\Flow\PageFlow\PageFlowRegistry * @since Property available since Release 2.0.0 */ - protected $fsmBuilder; + protected $pageFlowRegistry; /** - * @param string $definitionFile + * @var \Stagehand\FSM\FSMBuilder * @since Property available since Release 2.0.0 */ - protected $definitionFile; + protected $fsmBuilder; /** - * @param string $definitionFile - * @throws \Piece\Flow\PageFlow\FileNotFoundException + * @param string $id + * @param \Piece\Flow\PageFlow\PageFlowRegistry $pageFlowRegistry */ - public function __construct($definitionFile) + public function __construct($id, PageFlowRegistry $pageFlowRegistry) { - if (!file_exists($definitionFile)) { - throw new FileNotFoundException(sprintf('The flow definition file [ %s ] is not found.', $definitionFile)); - } - - $this->definitionFile = $definitionFile; - $this->pageFlow = new PageFlow(); - $this->fsmBuilder = new FSMBuilder(); + $this->pageFlow = new PageFlow($id); + $this->pageFlowRegistry = $pageFlowRegistry; + $this->fsmBuilder = new FSMBuilder($this->pageFlow->getID()); } /** @@ -98,12 +94,6 @@ public function generate() throw new ProtectedStateException("The state [ {$definition['firstState']} ] cannot be used in flow definitions."); } - if (is_null($definition['name'])) { - $this->fsmBuilder->setID(realpath($this->definitionFile)); - } else { - $this->fsmBuilder->setID($definition['name']); - } - $this->fsmBuilder->setFirstState($definition['firstState']); if (!empty($definition['lastState'])) { @@ -277,7 +267,7 @@ protected function readDefinition() $processor = new Processor(); return $processor->processConfiguration( new Definition17Configuration(), - array('definition17' => Yaml::parse($this->definitionFile)) + array('definition17' => Yaml::parse($this->pageFlowRegistry->getFileName($this->pageFlow->getID()))) ); } } diff --git a/src/Piece/Flow/PageFlow/PageFlowRegistry.php b/src/Piece/Flow/PageFlow/PageFlowRegistry.php new file mode 100644 index 0000000..0eec6a4 --- /dev/null +++ b/src/Piece/Flow/PageFlow/PageFlowRegistry.php @@ -0,0 +1,88 @@ +, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * @package Piece_Flow + * @copyright 2012 KUBO Atsuhiro + * @license http://www.opensource.org/licenses/bsd-license.php New BSD License + * @version Release: @package_version@ + * @since File available since Release 2.0.0 + */ + +namespace Piece\Flow\PageFlow; + +/** + * @package Piece_Flow + * @copyright 2012 KUBO Atsuhiro + * @license http://www.opensource.org/licenses/bsd-license.php New BSD License + * @version Release: @package_version@ + * @since Class available since Release 2.0.0 + */ +class PageFlowRegistry +{ + /** + * @var string + */ + protected $baseDir; + + /** + * @var string + */ + protected $extension; + + /** + * @param string $baseDir + * @param string $extension + */ + public function __construct($baseDir, $extension) + { + $this->baseDir = $baseDir; + $this->extension = $extension; + } + + /** + * @param string $id + * @return string + */ + public function getFileName($id) + { + return $this->baseDir . '/' . $id . $this->extension; + } +} + +/* + * Local Variables: + * mode: php + * coding: iso-8859-1 + * tab-width: 4 + * c-basic-offset: 4 + * c-hanging-comment-ender-p: nil + * indent-tabs-mode: nil + * End: + */ diff --git a/src/Piece/Flow/PageFlow/PageFlowRepository.php b/src/Piece/Flow/PageFlow/PageFlowRepository.php index dba5fd7..01a3b69 100644 --- a/src/Piece/Flow/PageFlow/PageFlowRepository.php +++ b/src/Piece/Flow/PageFlow/PageFlowRepository.php @@ -57,52 +57,52 @@ class PageFlowRepository protected $pageFlowFactory; /** - * @var array + * @var \Piece\Flow\PageFlow\PageFlowRegistry + */ + protected $pageFlowRegistry; + + /** + * @var arrayn */ protected $pageFlows = array(); /** + * @param \Piece\Flow\PageFlow\PageFlowRegistry $pageFlowRegistry * @param \Piece\Flow\PageFlow\PageFlowCacheFactory $pageFlowCacheFactory */ - public function __construct(PageFlowCacheFactory $pageFlowCacheFactory) + public function __construct(PageFlowRegistry $pageFlowRegistry, PageFlowCacheFactory $pageFlowCacheFactory) { + $this->pageFlowRegistry = $pageFlowRegistry; $this->pageFlowCacheFactory = $pageFlowCacheFactory; - $this->pageFlowFactory = new PageFlowFactory(); + $this->pageFlowFactory = new PageFlowFactory($this->pageFlowRegistry); } /** - * @param string $definitionFile + * @param string $id * @throws \Piece\Flow\PageFlow\FileNotFoundException */ - public function add($definitionFile) + public function add($id) { - $absoluteDefinitionFile = realpath($definitionFile); - if (!$absoluteDefinitionFile) { - throw new FileNotFoundException(sprintf('The page flow definition file [ %s ] is not found or not readable.', $definitionFile)); + if (!file_exists($this->pageFlowRegistry->getFileName($id))) { + throw new FileNotFoundException(sprintf('The page flow definition file [ %s ] is not found.', $this->pageFlowRegistry->getFileName($id))); } - $pageFlowCache = $this->pageFlowCacheFactory->create($absoluteDefinitionFile); + $pageFlowCache = $this->pageFlowCacheFactory->create($this->pageFlowRegistry->getFileName($id)); if (!$pageFlowCache->isFresh()) { - $pageFlowCache->write($this->pageFlowFactory->create($absoluteDefinitionFile)); + $pageFlowCache->write($this->pageFlowFactory->create($id)); } - $this->pageFlows[$absoluteDefinitionFile] = $pageFlowCache; + $this->pageFlows[$id] = $pageFlowCache; } /** - * @param string $definitionFile + * @param string $id * @return \Piece\Flow\PageFlow\PageFlow - * @throws \Piece\Flow\PageFlow\FileNotFoundException */ - public function findByDefinitionFile($definitionFile) + public function findByID($id) { - $absoluteDefinitionFile = realpath($definitionFile); - if (!$absoluteDefinitionFile) { - throw new FileNotFoundException(sprintf('The page flow definition file [ %s ] is not found or not readable.', $definitionFile)); - } - - if (array_key_exists($absoluteDefinitionFile, $this->pageFlows)) { - return $this->pageFlows[$absoluteDefinitionFile]->read(); + if (array_key_exists($id, $this->pageFlows)) { + return $this->pageFlows[$id]->read(); } else { return null; } diff --git a/test/Piece/Flow/Continuation/ContinuationServerTest.php b/test/Piece/Flow/Continuation/ContinuationServerTest.php index 67a9352..f78d21a 100644 --- a/test/Piece/Flow/Continuation/ContinuationServerTest.php +++ b/test/Piece/Flow/Continuation/ContinuationServerTest.php @@ -39,6 +39,7 @@ use Piece\Flow\PageFlow\EventContext; use Piece\Flow\PageFlow\PageFlowCacheFactory; +use Piece\Flow\PageFlow\PageFlowRegistry; use Piece\Flow\PageFlow\PageFlowRepository; /** @@ -92,8 +93,8 @@ protected function setUp() public function testInvocationInMultipleFlowModeAndFlowInNonExclusiveMode() { - $server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true))); - $server->addFlow('Counter', "{$this->cacheDirectory}/Counter.yaml"); + $server = new ContinuationServer(new PageFlowRepository(new PageFlowRegistry($this->cacheDirectory, '.yaml'), new PageFlowCacheFactory($this->cacheDirectory, true))); + $server->addFlow('Counter'); $server->setEventNameCallback(array($this, 'getEventName')); $server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket')); $server->setFlowIDCallback(array($this, 'getFlowID')); @@ -120,9 +121,9 @@ public function testInvocationInMultipleFlowModeAndFlowInNonExclusiveMode() public function testMultipleInvocationInMultipleFlowModeAndFlowInNonExclusiveMode() { - $server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true))); - $server->addFlow('Counter', "{$this->cacheDirectory}/Counter.yaml"); - $server->addFlow('SecondCounter', "{$this->cacheDirectory}/SecondCounter.yaml"); + $server = new ContinuationServer(new PageFlowRepository(new PageFlowRegistry($this->cacheDirectory, '.yaml'), new PageFlowCacheFactory($this->cacheDirectory, true))); + $server->addFlow('Counter'); + $server->addFlow('SecondCounter'); $server->setEventNameCallback(array($this, 'getEventName')); $server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket')); $server->setFlowIDCallback(array($this, 'getFlowID')); @@ -208,8 +209,8 @@ public function testMultipleInvocationInMultipleFlowModeAndFlowInNonExclusiveMod */ public function testFailureOfContinuationByInvalidFlowNameInMultipleFlowMode() { - $server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true))); - $server->addFlow('Counter', "{$this->cacheDirectory}/Counter.yaml"); + $server = new ContinuationServer(new PageFlowRepository(new PageFlowRegistry($this->cacheDirectory, '.yaml'), new PageFlowCacheFactory($this->cacheDirectory, true))); + $server->addFlow('Counter'); $server->setEventNameCallback(array($this, 'getEventName')); $server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket')); $server->setFlowIDCallback(array($this, 'getFlowID')); @@ -232,8 +233,8 @@ public function testFailureOfContinuationByInvalidFlowNameInMultipleFlowMode() */ public function testFailureToInvokeByNonExistingFlowConfiguration() { - $server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true))); - $server->addFlow('NonExistingFile', "{$this->cacheDirectory}/NonExistingFile.yaml"); + $server = new ContinuationServer(new PageFlowRepository(new PageFlowRegistry($this->cacheDirectory, '.yaml'), new PageFlowCacheFactory($this->cacheDirectory, true))); + $server->addFlow('NonExistingFile'); $server->setEventNameCallback(array($this, 'getEventName')); $server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket')); $server->setFlowIDCallback(array($this, 'getFlowID')); @@ -247,9 +248,9 @@ public function testFailureToInvokeByNonExistingFlowConfiguration() public function testInvocationInMultipleFlowModeAndFlowInExclusiveMode() { - $server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true))); - $server->addFlow('Counter', "{$this->cacheDirectory}/Counter.yaml", true); - $server->addFlow('SecondCounter', "{$this->cacheDirectory}/SecondCounter.yaml"); + $server = new ContinuationServer(new PageFlowRepository(new PageFlowRegistry($this->cacheDirectory, '.yaml'), new PageFlowCacheFactory($this->cacheDirectory, true))); + $server->addFlow('Counter'); + $server->addFlow('SecondCounter'); $server->setEventNameCallback(array($this, 'getEventName')); $server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket')); $server->setFlowIDCallback(array($this, 'getFlowID')); @@ -292,8 +293,8 @@ public function testInvocationInMultipleFlowModeAndFlowInExclusiveMode() public function testSettingAttribute() { - $server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true))); - $server->addFlow('Counter', "{$this->cacheDirectory}/Counter.yaml", true); + $server = new ContinuationServer(new PageFlowRepository(new PageFlowRegistry($this->cacheDirectory, '.yaml'), new PageFlowCacheFactory($this->cacheDirectory, true))); + $server->addFlow('Counter'); $server->setEventNameCallback(array($this, 'getEventName')); $server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket')); $server->setFlowIDCallback(array($this, 'getFlowID')); @@ -346,8 +347,8 @@ public function testSettingAttribute() */ public function testFailureToSetAttributeBeforeStartingContinuation() { - $server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true))); - $server->addFlow('Counter', "{$this->cacheDirectory}/Counter.yaml", true); + $server = new ContinuationServer(new PageFlowRepository(new PageFlowRegistry($this->cacheDirectory, '.yaml'), new PageFlowCacheFactory($this->cacheDirectory, true))); + $server->addFlow('Counter'); $server->setEventNameCallback(array($this, 'getEventName')); $server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket')); $server->setFlowIDCallback(array($this, 'getFlowID')); @@ -360,8 +361,8 @@ public function testFailureToSetAttributeBeforeStartingContinuation() */ public function testFailureToGetAttributeBeforeStartingContinuation() { - $server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true))); - $server->addFlow('Counter', "{$this->cacheDirectory}/Counter.yaml", true); + $server = new ContinuationServer(new PageFlowRepository(new PageFlowRegistry($this->cacheDirectory, '.yaml'), new PageFlowCacheFactory($this->cacheDirectory, true))); + $server->addFlow('Counter'); $server->setEventNameCallback(array($this, 'getEventName')); $server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket')); $server->setFlowIDCallback(array($this, 'getFlowID')); @@ -376,8 +377,8 @@ public function testStartingNewFlowExecutionAfterShuttingDownContinuationInNonEx \Phake::when($actionInvoker)->invoke('finalize', $this->anything())->thenGetReturnByLambda(function ($actionID, EventContext $eventContext) use (&$shutdownCount) { ++$shutdownCount; }); - $server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true))); - $server->addFlow('Shutdown', "{$this->cacheDirectory}/Shutdown.yaml"); + $server = new ContinuationServer(new PageFlowRepository(new PageFlowRegistry($this->cacheDirectory, '.yaml'), new PageFlowCacheFactory($this->cacheDirectory, true))); + $server->addFlow('Shutdown'); $server->setEventNameCallback(array($this, 'getEventName')); $server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket')); $server->setFlowIDCallback(array($this, 'getFlowID')); @@ -424,8 +425,8 @@ public function testStartingNewFlowExecutionAfterShuttingDownContinuationInExclu \Phake::when($actionInvoker)->invoke('finalize', $this->anything())->thenGetReturnByLambda(function ($actionID, EventContext $eventContext) use (&$shutdownCount) { ++$shutdownCount; }); - $server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true))); - $server->addFlow('Shutdown', "{$this->cacheDirectory}/Shutdown.yaml", true); + $server = new ContinuationServer(new PageFlowRepository(new PageFlowRegistry($this->cacheDirectory, '.yaml'), new PageFlowCacheFactory($this->cacheDirectory, true))); + $server->addFlow('Shutdown'); $server->setEventNameCallback(array($this, 'getEventName')); $server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket')); $server->setFlowIDCallback(array($this, 'getFlowID')); @@ -468,8 +469,8 @@ public function testStartingNewFlowExecutionAfterShuttingDownContinuationInExclu */ public function testShouldBeRequiredFlowExecutionTicketWheneverContinuingFlowExecution() { - $server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true))); - $server->addFlow('Counter', "{$this->cacheDirectory}/Counter.yaml", true); + $server = new ContinuationServer(new PageFlowRepository(new PageFlowRegistry($this->cacheDirectory, '.yaml'), new PageFlowCacheFactory($this->cacheDirectory, true))); + $server->addFlow('Counter', true); $server->setEventNameCallback(array($this, 'getEventName')); $server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket')); $server->setFlowIDCallback(array($this, 'getFlowID')); @@ -508,9 +509,9 @@ public function testShouldBeRequiredFlowExecutionTicketWheneverContinuingFlowExe */ public function testGettingFlowExecutionTicketByFlowName() { - $server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true))); - $server->addFlow('Counter', "{$this->cacheDirectory}/Counter.yaml", true); - $server->addFlow('SecondCounter', "{$this->cacheDirectory}/SecondCounter.yaml"); + $server = new ContinuationServer(new PageFlowRepository(new PageFlowRegistry($this->cacheDirectory, '.yaml'), new PageFlowCacheFactory($this->cacheDirectory, true))); + $server->addFlow('Counter', true); + $server->addFlow('SecondCounter'); $server->setEventNameCallback(array($this, 'getEventName')); $server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket')); $server->setFlowIDCallback(array($this, 'getFlowID')); @@ -548,8 +549,8 @@ public function testGettingFlowExecutionTicketByFlowName() public function testFlowExecutionExpiredExceptionShouldBeRaisedWhenFlowExecutionHasExpired() { $flowName = 'FlowExecutionExpired'; - $server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true)), new GC(1)); - $server->addFlow($flowName, "{$this->cacheDirectory}/$flowName.yaml"); + $server = new ContinuationServer(new PageFlowRepository(new PageFlowRegistry($this->cacheDirectory, '.yaml'), new PageFlowCacheFactory($this->cacheDirectory, true)), new GC(1)); + $server->addFlow($flowName); $server->setEventNameCallback(array($this, 'getEventName')); $server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket')); $server->setFlowIDCallback(array($this, 'getFlowID')); @@ -575,8 +576,8 @@ public function testFlowExecutionExpiredExceptionShouldBeRaisedWhenFlowExecution public function testFlowExecutionExpiredExceptionShouldNotBeRaisedWhenFlowExecutionHasNotExpired() { $flowName = 'FlowExecutionExpired'; - $server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true)), new GC(2)); - $server->addFlow($flowName, "{$this->cacheDirectory}/$flowName.yaml"); + $server = new ContinuationServer(new PageFlowRepository(new PageFlowRegistry($this->cacheDirectory, '.yaml'), new PageFlowCacheFactory($this->cacheDirectory, true)), new GC(2)); + $server->addFlow($flowName); $server->setEventNameCallback(array($this, 'getEventName')); $server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket')); $server->setFlowIDCallback(array($this, 'getFlowID')); @@ -617,8 +618,8 @@ public function testNewFlowExecutionShouldBeAbleToStartWithSameRequestAfterFlowE { $flowName = 'FlowExecutionExpired'; $this->flowID = $flowName; - $server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true)), new GC(1)); - $server->addFlow($flowName, "{$this->cacheDirectory}/$flowName.yaml"); + $server = new ContinuationServer(new PageFlowRepository(new PageFlowRegistry($this->cacheDirectory, '.yaml'), new PageFlowCacheFactory($this->cacheDirectory, true)), new GC(1)); + $server->addFlow($flowName); $server->setEventNameCallback(array($this, 'getEventName')); $server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket')); $server->setFlowIDCallback(array($this, 'getFlowID')); @@ -658,8 +659,8 @@ public function testNewFlowExecutionShouldBeAbleToStartWithSameRequestAfterFlowE public function testCheckLastEventShouldReturnTrueIfContinuationHasJustStarted() { $flowName = 'CheckLastEvent'; - $server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true))); - $server->addFlow($flowName, "{$this->cacheDirectory}/$flowName.yaml"); + $server = new ContinuationServer(new PageFlowRepository(new PageFlowRegistry($this->cacheDirectory, '.yaml'), new PageFlowCacheFactory($this->cacheDirectory, true))); + $server->addFlow($flowName); $server->setEventNameCallback(array($this, 'getEventName')); $server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket')); $server->setFlowIDCallback(array($this, 'getFlowID')); @@ -680,8 +681,8 @@ public function testCheckLastEventShouldReturnTrueIfContinuationHasJustStarted() public function testCheckLastEventShouldReturnTrueWhenValidEventIsGivenByUser() { $flowName = 'CheckLastEvent'; - $server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true))); - $server->addFlow($flowName, "{$this->cacheDirectory}/$flowName.yaml"); + $server = new ContinuationServer(new PageFlowRepository(new PageFlowRegistry($this->cacheDirectory, '.yaml'), new PageFlowCacheFactory($this->cacheDirectory, true))); + $server->addFlow($flowName); $server->setEventNameCallback(array($this, 'getEventName')); $server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket')); $server->setFlowIDCallback(array($this, 'getFlowID')); @@ -718,8 +719,8 @@ public function testCheckLastEventShouldReturnTrueWhenValidEventIsGivenByUser() public function testCheckLastEventShouldReturnFalseWhenInvalidEventIsGivenByUser() { $flowName = 'CheckLastEvent'; - $server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true))); - $server->addFlow($flowName, "{$this->cacheDirectory}/$flowName.yaml"); + $server = new ContinuationServer(new PageFlowRepository(new PageFlowRegistry($this->cacheDirectory, '.yaml'), new PageFlowCacheFactory($this->cacheDirectory, true))); + $server->addFlow($flowName); $server->setEventNameCallback(array($this, 'getEventName')); $server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket')); $server->setFlowIDCallback(array($this, 'getFlowID')); @@ -746,8 +747,8 @@ public function testCheckLastEventShouldReturnFalseWhenInvalidEventIsGivenByUser public function testCheckLastEventShouldReturnTrueIfContinuationHasNotActivatedYet() { $flowName = 'CheckLastEvent'; - $server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true))); - $server->addFlow($flowName, "{$this->cacheDirectory}/$flowName.yaml"); + $server = new ContinuationServer(new PageFlowRepository(new PageFlowRegistry($this->cacheDirectory, '.yaml'), new PageFlowCacheFactory($this->cacheDirectory, true))); + $server->addFlow($flowName); $server->setEventNameCallback(array($this, 'getEventName')); $server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket')); $server->setFlowIDCallback(array($this, 'getFlowID')); @@ -766,8 +767,8 @@ public function testCheckLastEventShouldReturnTrueIfContinuationHasNotActivatedY public function testCurrentStateNameShouldBeAbleToGetIfContinuationHasActivated() { $flowName = 'CheckLastEvent'; - $server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true))); - $server->addFlow($flowName, "{$this->cacheDirectory}/$flowName.yaml"); + $server = new ContinuationServer(new PageFlowRepository(new PageFlowRegistry($this->cacheDirectory, '.yaml'), new PageFlowCacheFactory($this->cacheDirectory, true))); + $server->addFlow($flowName); $server->setEventNameCallback(array($this, 'getEventName')); $server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket')); $server->setFlowIDCallback(array($this, 'getFlowID')); @@ -809,8 +810,8 @@ public function testCurrentStateNameShouldBeAbleToGetIfContinuationHasActivated( public function testGetCurrentStateNameShouldRaiseExceptionIfContinuationHasNotActivated() { $flowName = 'CheckLastEvent'; - $server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true))); - $server->addFlow($flowName, "{$this->cacheDirectory}/$flowName.yaml"); + $server = new ContinuationServer(new PageFlowRepository(new PageFlowRegistry($this->cacheDirectory, '.yaml'), new PageFlowCacheFactory($this->cacheDirectory, true))); + $server->addFlow($flowName); $server->setEventNameCallback(array($this, 'getEventName')); $server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket')); $server->setFlowIDCallback(array($this, 'getFlowID')); @@ -822,102 +823,14 @@ public function testGetCurrentStateNameShouldRaiseExceptionIfContinuationHasNotA $service->getCurrentStateName(); } - /** - * @since Method available since Release 1.15.0 - */ - public function testFlowExecutionShouldWorkWithConfigDirectory() - { - $server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true))); - $server->addFlow('/counter/one.php', $this->cacheDirectory . '/Counter/One.flow'); - $server->addFlow('/counter/two.php', $this->cacheDirectory . '/Counter/Two.flow'); - $server->setEventNameCallback(array($this, 'getEventName')); - $server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket')); - $server->setFlowIDCallback(array($this, 'getFlowID')); - $server->setActionInvoker($this->createCounterActionInvoker()); - - /* - * Starting a new '/counter/one.php'. - */ - $this->flowID = '/counter/one.php'; - $this->eventName = null; - $this->flowExecutionTicket = null; - $flowExecutionTicket1 = $server->invoke(new \stdClass()); - $service = $server->createService(); - - $this->assertEquals(0, $service->getAttribute('counter')); - - $server->shutdown(); - - /* - * Starting a new '/counter/two.php'. - */ - $this->flowID = '/counter/two.php'; - $this->eventName = null; - $this->flowExecutionTicket = null; - $flowExecutionTicket2 = $server->invoke(new \stdClass()); - $service = $server->createService(); - - $this->assertEquals(0, $service->getAttribute('counter')); - $this->assertRegexp('/[0-9a-f]{40}/', $flowExecutionTicket1); - $this->assertRegexp('/[0-9a-f]{40}/', $flowExecutionTicket2); - $this->assertEquals('SecondCounter', $server->getView()); - $this->assertTrue($flowExecutionTicket1 != $flowExecutionTicket2); - - $server->shutdown(); - - /* - * Continuing the first '/counter/one.php'. - */ - $this->flowID = '/counter/one.php'; - $this->eventName = 'increase'; - $this->flowExecutionTicket = $flowExecutionTicket1; - $flowExecutionTicket3 = $server->invoke(new \stdClass()); - $service = $server->createService(); - - $this->assertEquals(1, $service->getAttribute('counter')); - - $this->assertEquals('Counter', $server->getView()); - $this->assertEquals($flowExecutionTicket1, $flowExecutionTicket3); - - $server->shutdown(); - - /* - * Continuing the first '/counter/two.php'. - */ - $this->flowID = '/counter/two.php'; - $this->eventName = 'increase'; - $this->flowExecutionTicket = $flowExecutionTicket2; - $flowExecutionTicket4 = $server->invoke(new \stdClass()); - $service = $server->createService(); - - $this->assertEquals('SecondCounter', $server->getView()); - $this->assertEquals(1, $service->getAttribute('counter')); - $this->assertEquals($flowExecutionTicket2, $flowExecutionTicket4); - - $server->shutdown(); - - /* - * Starting a new '/counter/two.php'. - */ - $this->flowID = '/counter/two.php'; - $this->eventName = null; - $this->flowExecutionTicket = null; - $flowExecutionTicket5 = $server->invoke(new \stdClass()); - $service = $server->createService(); - - $this->assertEquals('SecondCounter', $server->getView()); - $this->assertEquals(0, $service->getAttribute('counter')); - $this->assertTrue($flowExecutionTicket2 != $flowExecutionTicket5); - } - /** * @since Method available since Release 1.15.1 */ public function testFlowExecutionExpiredExceptionShouldRaiseAfterSweepingIt() { $flowName = 'FlowExecutionExpired'; - $server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true)), new GC(1)); - $server->addFlow($flowName, "{$this->cacheDirectory}/$flowName.yaml"); + $server = new ContinuationServer(new PageFlowRepository(new PageFlowRegistry($this->cacheDirectory, '.yaml'), new PageFlowCacheFactory($this->cacheDirectory, true)), new GC(1)); + $server->addFlow($flowName); $server->setEventNameCallback(array($this, 'getEventName')); $server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket')); $server->setFlowIDCallback(array($this, 'getFlowID')); diff --git a/test/Piece/Flow/Continuation/ContinuationServerTest/Counter.yaml b/test/Piece/Flow/Continuation/ContinuationServerTest/Counter.yaml index 44947d2..95e20ff 100644 --- a/test/Piece/Flow/Continuation/ContinuationServerTest/Counter.yaml +++ b/test/Piece/Flow/Continuation/ContinuationServerTest/Counter.yaml @@ -1,5 +1,3 @@ -# $Id$ -name: Counter firstState: DisplayForm lastState: { name: Finish, view: Finish } viewState: diff --git a/test/Piece/Flow/Continuation/ContinuationServerTest/SecondCounter.yaml b/test/Piece/Flow/Continuation/ContinuationServerTest/SecondCounter.yaml index 5255e34..361a35b 100644 --- a/test/Piece/Flow/Continuation/ContinuationServerTest/SecondCounter.yaml +++ b/test/Piece/Flow/Continuation/ContinuationServerTest/SecondCounter.yaml @@ -1,5 +1,3 @@ -# $Id$ -name: SecondCounter firstState: DisplayForm lastState: { name: Finish, view: Finish } viewState: diff --git a/test/Piece/Flow/Continuation/ContinuationServerTest/Shutdown.yaml b/test/Piece/Flow/Continuation/ContinuationServerTest/Shutdown.yaml index c3d5553..5102efb 100644 --- a/test/Piece/Flow/Continuation/ContinuationServerTest/Shutdown.yaml +++ b/test/Piece/Flow/Continuation/ContinuationServerTest/Shutdown.yaml @@ -1,5 +1,3 @@ -# $Id: Counter.yaml 95 2006-06-09 17:03:08Z iteman $ -name: Shutdown firstState: starting final: method: finalize diff --git a/test/Piece/Flow/PageFlow/PageFlowTest.php b/test/Piece/Flow/PageFlow/PageFlowTest.php index 26b96c9..f86bb56 100644 --- a/test/Piece/Flow/PageFlow/PageFlowTest.php +++ b/test/Piece/Flow/PageFlow/PageFlowTest.php @@ -62,8 +62,8 @@ class PageFlowTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->cacheDirectory = dirname(__FILE__) . '/' . basename(__FILE__, '.php'); - $this->source = "{$this->cacheDirectory}/Registration.yaml"; - $this->pageFlowFactory = new PageFlowFactory(); + $this->source = 'Registration'; + $this->pageFlowFactory = new PageFlowFactory(new PageFlowRegistry($this->cacheDirectory, '.yaml')); } public function testGettingView() @@ -193,13 +193,13 @@ public function testFailureToSetAttributeBeforeStartingFlow() */ public function testFailureToSetPayloadBeforeConfiguringFlow() { - $flow = new PageFlow(); + $flow = new PageFlow('foo'); $flow->setPayload(new \stdClass()); } public function testOptionalElements() { - $flow = $this->pageFlowFactory->create("{$this->cacheDirectory}/optional.yaml"); + $flow = $this->pageFlowFactory->create('optional'); $flow->setActionInvoker(\Phake::mock('Piece\Flow\PageFlow\ActionInvoker')); $flow->setPayload(new \stdClass()); $flow->start(); @@ -209,7 +209,7 @@ public function testOptionalElements() public function testInitialAndFinalActionsWithYAML() { - $this->assertInitialAndFinalActions('/initial.yaml'); + $this->assertInitialAndFinalActions('initial'); } /** @@ -227,7 +227,7 @@ public function testFailureToGetViewBeforeStartingFlow() */ public function testInvalidTransition() { - $flow = $this->pageFlowFactory->create("{$this->cacheDirectory}/invalid.yaml"); + $flow = $this->pageFlowFactory->create('invalid'); $flow->setActionInvoker(\Phake::mock('Piece\Flow\PageFlow\ActionInvoker')); $flow->setPayload(new \stdClass()); $flow->start(); @@ -237,7 +237,7 @@ public function testInvalidTransition() public function testCheckingWhetherCurrentStateIsFinalState() { - $flow = $this->pageFlowFactory->create("{$this->cacheDirectory}/initial.yaml"); + $flow = $this->pageFlowFactory->create('initial'); $flow->setActionInvoker(\Phake::mock('Piece\Flow\PageFlow\ActionInvoker')); $flow->setPayload(new \stdClass()); $flow->start(); @@ -298,7 +298,7 @@ public function testToPreventTriggeringProtectedEvents() $eventContext->getPageFlow()->setAttribute('numberOfUpdate', $numberOfUpdate); }); - $flow = $this->pageFlowFactory->create("{$this->cacheDirectory}/CDPlayer.yaml"); + $flow = $this->pageFlowFactory->create('CDPlayer'); $flow->setActionInvoker($actionInvoker); $flow->setPayload(new \stdClass()); $flow->start(); @@ -348,7 +348,7 @@ public function testToPreventTriggeringProtectedEvents() */ public function testProtectedEvents() { - $this->pageFlowFactory->create("{$this->cacheDirectory}/ProtectedEvents.yaml", \Phake::mock('Piece\Flow\PageFlow\ActionInvoker')); + $this->pageFlowFactory->create('ProtectedEvents', \Phake::mock('Piece\Flow\PageFlow\ActionInvoker')); } /** @@ -357,7 +357,7 @@ public function testProtectedEvents() */ public function testProtectedStates() { - $this->pageFlowFactory->create("{$this->cacheDirectory}/ProtectedStates.yaml", \Phake::mock('Piece\Flow\PageFlow\ActionInvoker')); + $this->pageFlowFactory->create('ProtectedStates', \Phake::mock('Piece\Flow\PageFlow\ActionInvoker')); } /** @@ -367,7 +367,7 @@ public function testInvalidEventFromATransitionActionsOrActivities() { $actionInvoker1 = \Phake::mock('Piece\Flow\PageFlow\ActionInvoker'); \Phake::when($actionInvoker1)->invoke('register', $this->anything())->thenReturn('invalidEventFromRegister'); - $flow1 = $this->pageFlowFactory->create("{$this->cacheDirectory}/InvalidEventFromTransitionActionsOrActivities.yaml"); + $flow1 = $this->pageFlowFactory->create('InvalidEventFromTransitionActionsOrActivities'); $flow1->setActionInvoker($actionInvoker1); $flow1->setPayload(new \stdClass()); $flow1->start(); @@ -389,7 +389,7 @@ public function testInvalidEventFromATransitionActionsOrActivities() $actionInvoker2 = \Phake::mock('Piece\Flow\PageFlow\ActionInvoker'); \Phake::when($actionInvoker2)->invoke('register', $this->anything())->thenReturn('goDisplayFinish'); \Phake::when($actionInvoker2)->invoke('setupFinish', $this->anything())->thenReturn('invalidEventFromSetupFinish'); - $flow2 = $this->pageFlowFactory->create("{$this->cacheDirectory}/InvalidEventFromTransitionActionsOrActivities.yaml"); + $flow2 = $this->pageFlowFactory->create('InvalidEventFromTransitionActionsOrActivities'); $flow2->setActionInvoker($actionInvoker2); $flow2->setPayload(new \stdClass()); $flow2->start(); @@ -416,7 +416,7 @@ public function testProblemThatActivityIsInvokedTwiceUnexpectedly() { $actionInvoker = \Phake::mock('Piece\Flow\PageFlow\ActionInvoker'); \Phake::when($actionInvoker)->invoke('validate', $this->anything())->thenReturn('goDisplayConfirmation'); - $flow = $this->pageFlowFactory->create("{$this->cacheDirectory}/ProblemThatActivityIsInvokedTwiceUnexpectedly.yaml"); + $flow = $this->pageFlowFactory->create('ProblemThatActivityIsInvokedTwiceUnexpectedly'); $flow->setActionInvoker($actionInvoker); $flow->setPayload(new \stdClass()); $flow->start(); @@ -432,7 +432,7 @@ public function testProblemThatActivityIsInvokedTwiceUnexpectedly() protected function assertInitialAndFinalActions($source) { $actionInvoker = \Phake::mock('Piece\Flow\PageFlow\ActionInvoker'); - $flow = $this->pageFlowFactory->create("{$this->cacheDirectory}/$source"); + $flow = $this->pageFlowFactory->create($source); $flow->setActionInvoker($actionInvoker); $flow->setPayload(new \stdClass()); $flow->start();