Skip to content

Commit

Permalink
[PageFlow] Replaced the IPageFlow::setAttribute(), getAttribute(), an…
Browse files Browse the repository at this point in the history
…d hasAttribute() methods with getAttributes() method using the Symfony\Component\HttpFoundation\ParameterBag class. (Issue piece#4)
  • Loading branch information
iteman committed Sep 18, 2012
1 parent 5b20144 commit e7ac4c7
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 94 deletions.
11 changes: 1 addition & 10 deletions src/Piece/Flow/Continuation/NullPageFlow.php
Expand Up @@ -67,20 +67,11 @@ public function getID()
return $this->id;
}

public function setAttribute($name, $value)
{
}

public function getAttribute($name)
public function getAttributes()
{
return null;
}

public function hasAttribute($name)
{
return false;
}

public function validateReceivedEvent()
{
return false;
Expand Down
14 changes: 2 additions & 12 deletions src/Piece/Flow/Continuation/PageFlowInstance.php
Expand Up @@ -107,19 +107,9 @@ public function activate($eventID)
}
}

public function setAttribute($name, $value)
public function getAttributes()
{
$this->pageFlow->setAttribute($name, $value);
}

public function getAttribute($name)
{
return $this->pageFlow->getAttribute($name);
}

public function hasAttribute($name)
{
return $this->pageFlow->hasAttribute($name);
return $this->pageFlow->getAttributes();
}

public function validateReceivedEvent()
Expand Down
23 changes: 2 additions & 21 deletions src/Piece/Flow/PageFlow/IPageFlow.php
Expand Up @@ -54,28 +54,9 @@ interface IPageFlow
public function getID();

/**
* Sets an attribute with the specified name.
*
* @param string $name
* @param mixed $value
*/
public function setAttribute($name, $value);

/**
* Gets the attribute bound with the specified name.
*
* @param string $name
* @return mixed|null
*/
public function getAttribute($name);

/**
* Checks whether this page flow has the attribute bound with the specified name.
*
* @param string $name
* @return boolean
* @return \Symfony\Component\HttpFoundation\ParameterBag
*/
public function hasAttribute($name);
public function getAttributes();

/**
* Validates whether the last event given by a user is valid or not.
Expand Down
22 changes: 9 additions & 13 deletions src/Piece/Flow/PageFlow/PageFlow.php
Expand Up @@ -44,6 +44,7 @@
use Stagehand\FSM\Event;
use Stagehand\FSM\FSM;
use Stagehand\FSM\State;
use Symfony\Component\HttpFoundation\ParameterBag;

/**
* A web flow engine for handling page flows of web applications.
Expand Down Expand Up @@ -75,7 +76,11 @@ class PageFlow implements IPageFlow
protected $fsm;
protected $id;
protected $views = array();
protected $attributes = array();

/**
* @var \Symfony\Component\HttpFoundation\ParameterBag
*/
protected $attributes;

/**
* @var array
Expand All @@ -97,6 +102,7 @@ class PageFlow implements IPageFlow
public function __construct($id)
{
$this->id = $id;
$this->attributes = new ParameterBag();
}

/**
Expand Down Expand Up @@ -216,19 +222,9 @@ public function getCurrentState()
return $this->fsm->getCurrentState();
}

public function setAttribute($name, $value)
{
$this->attributes[$name] = $value;
}

public function hasAttribute($name)
{
return array_key_exists($name, $this->attributes);
}

public function getAttribute($name)
public function getAttributes()
{
return $this->attributes[$name];
return $this->attributes;
}

public function setPayload($payload)
Expand Down
48 changes: 24 additions & 24 deletions test/Piece/Flow/Continuation/ContinuationServerTest.php
Expand Up @@ -126,7 +126,7 @@ public function testInvocationInMultipleFlowModeAndFlowInNonExclusiveMode()

$this->assertThat(strlen($pageFlowInstance1->getID()), $this->greaterThan(0));
$this->assertEquals('Counter', $pageFlowInstance2->getView());
$this->assertEquals(1, $pageFlowInstance2->getAttribute('counter'));
$this->assertEquals(1, $pageFlowInstance2->getAttributes()->get('counter'));
$this->assertEquals($pageFlowInstance1->getID(), $pageFlowInstance2->getID());
}

Expand All @@ -148,7 +148,7 @@ public function testMultipleInvocationInMultipleFlowModeAndFlowInNonExclusiveMod
$server->activate(new \stdClass());
$pageFlowInstance1 = $server->getPageFlowInstance();

$this->assertEquals(0, $pageFlowInstance1->getAttribute('counter'));
$this->assertEquals(0, $pageFlowInstance1->getAttributes()->get('counter'));

$server->shutdown();

Expand All @@ -161,7 +161,7 @@ public function testMultipleInvocationInMultipleFlowModeAndFlowInNonExclusiveMod
$server->activate(new \stdClass());
$pageFlowInstance2 = $server->getPageFlowInstance();

$this->assertEquals(0, $pageFlowInstance2->getAttribute('counter'));
$this->assertEquals(0, $pageFlowInstance2->getAttributes()->get('counter'));
$this->assertThat(strlen($pageFlowInstance1->getID()), $this->greaterThan(0));
$this->assertThat(strlen($pageFlowInstance2->getID()), $this->greaterThan(0));
$this->assertEquals('SecondCounter', $pageFlowInstance2->getView());
Expand All @@ -178,7 +178,7 @@ public function testMultipleInvocationInMultipleFlowModeAndFlowInNonExclusiveMod
$server->activate(new \stdClass());
$pageFlowInstance3 = $server->getPageFlowInstance();

$this->assertEquals(1, $pageFlowInstance3->getAttribute('counter'));
$this->assertEquals(1, $pageFlowInstance3->getAttributes()->get('counter'));

$this->assertEquals('Counter', $pageFlowInstance3->getView());
$this->assertEquals($pageFlowInstance1->getID(), $pageFlowInstance3->getID());
Expand All @@ -195,7 +195,7 @@ public function testMultipleInvocationInMultipleFlowModeAndFlowInNonExclusiveMod
$pageFlowInstance4 = $server->getPageFlowInstance();

$this->assertEquals('SecondCounter', $pageFlowInstance4->getView());
$this->assertEquals(1, $pageFlowInstance4->getAttribute('counter'));
$this->assertEquals(1, $pageFlowInstance4->getAttributes()->get('counter'));
$this->assertEquals($pageFlowInstance2->getID(), $pageFlowInstance4->getID());

$server->shutdown();
Expand All @@ -210,7 +210,7 @@ public function testMultipleInvocationInMultipleFlowModeAndFlowInNonExclusiveMod
$pageFlowInstance5 = $server->getPageFlowInstance();

$this->assertEquals('SecondCounter', $pageFlowInstance5->getView());
$this->assertEquals(0, $pageFlowInstance5->getAttribute('counter'));
$this->assertEquals(0, $pageFlowInstance5->getAttributes()->get('counter'));
$this->assertTrue($pageFlowInstance2->getID() != $pageFlowInstance5->getID());
}

Expand Down Expand Up @@ -253,7 +253,7 @@ public function testInvocationInMultipleFlowModeAndFlowInExclusiveMode()
$server->activate(new \stdClass());
$pageFlowInstance1 = $server->getPageFlowInstance();

$this->assertEquals(0, $pageFlowInstance1->getAttribute('counter'));
$this->assertEquals(0, $pageFlowInstance1->getAttributes()->get('counter'));

$server->shutdown();

Expand All @@ -263,7 +263,7 @@ public function testInvocationInMultipleFlowModeAndFlowInExclusiveMode()
$server->activate(new \stdClass());
$pageFlowInstance3 = $server->getPageFlowInstance();

$this->assertEquals(0, $pageFlowInstance3->getAttribute('counter'));
$this->assertEquals(0, $pageFlowInstance3->getAttributes()->get('counter'));

$server->shutdown();

Expand All @@ -273,7 +273,7 @@ public function testInvocationInMultipleFlowModeAndFlowInExclusiveMode()
$server->activate(new \stdClass());
$pageFlowInstance2 = $server->getPageFlowInstance();

$this->assertEquals(1, $pageFlowInstance2->getAttribute('counter'));
$this->assertEquals(1, $pageFlowInstance2->getAttributes()->get('counter'));

$this->assertThat(strlen($pageFlowInstance1->getID()), $this->greaterThan(0));
$this->assertThat(strlen($pageFlowInstance3->getID()), $this->greaterThan(0));
Expand All @@ -295,17 +295,17 @@ public function testSettingAttribute()
$this->flowExecutionTicket = null;
$server->activate(new \stdClass());
$pageFlowInstance = $server->getPageFlowInstance();
$pageFlowInstance->setAttribute('foo', 'bar');
$pageFlowInstance->getAttributes()->set('foo', 'bar');
$server->shutdown();

$this->flowID = 'Counter';
$this->eventName = 'increase';
$this->flowExecutionTicket = $pageFlowInstance->getID();
$server->activate(new \stdClass());
$pageFlowInstance = $server->getPageFlowInstance();
$pageFlowInstance->setAttribute('bar', 'baz');
$pageFlowInstance->getAttributes()->set('bar', 'baz');
$baz1 = new \stdClass();
$pageFlowInstance->setAttribute('baz', $baz1);
$pageFlowInstance->getAttributes()->set('baz', $baz1);
$server->shutdown();

$this->flowID = 'Counter';
Expand All @@ -314,17 +314,17 @@ public function testSettingAttribute()
$server->activate(new \stdClass());
$pageFlowInstance = $server->getPageFlowInstance();

$this->assertTrue($pageFlowInstance->hasAttribute('foo'));
$this->assertEquals('bar', $pageFlowInstance->getAttribute('foo'));
$this->assertTrue($pageFlowInstance->hasAttribute('bar'));
$this->assertEquals('baz', $pageFlowInstance->getAttribute('bar'));
$this->assertTrue($pageFlowInstance->getAttributes()->has('foo'));
$this->assertEquals('bar', $pageFlowInstance->getAttributes()->get('foo'));
$this->assertTrue($pageFlowInstance->getAttributes()->has('bar'));
$this->assertEquals('baz', $pageFlowInstance->getAttributes()->get('bar'));

$baz1->foo = 'bar';

$this->assertTrue(property_exists($baz1, 'foo'));
$this->assertEquals('bar', $baz1->foo);

$baz2 = $pageFlowInstance->getAttribute('baz');
$baz2 = $pageFlowInstance->getAttributes()->get('baz');

$this->assertEquals(strtolower('stdClass'), strtolower(get_class($baz2)));

Expand Down Expand Up @@ -446,7 +446,7 @@ public function testShouldBeRequiredFlowExecutionTicketWheneverContinuingFlowExe
$server->activate(new \stdClass());
$pageFlowInstance1 = $server->getPageFlowInstance();

$this->assertEquals(0, $pageFlowInstance1->getAttribute('counter'));
$this->assertEquals(0, $pageFlowInstance1->getAttributes()->get('counter'));

$server->shutdown();

Expand All @@ -457,7 +457,7 @@ public function testShouldBeRequiredFlowExecutionTicketWheneverContinuingFlowExe
$pageFlowInstance2 = $server->getPageFlowInstance();

\Phake::verify($pageFlowInstanceRepository)->remove($pageFlowInstance1);
$this->assertEquals(0, $pageFlowInstance2->getAttribute('counter'));
$this->assertEquals(0, $pageFlowInstance2->getAttributes()->get('counter'));
$this->assertTrue($pageFlowInstance1->getID() != $pageFlowInstance2->getID());
}

Expand All @@ -484,15 +484,15 @@ public function testGettingFlowExecutionTicketByFlowName()
$this->flowExecutionTicket = $pageFlowInstance1->getID();
$server->activate(new \stdClass());

$this->assertEquals(1, $pageFlowInstance1->getAttribute('counter'));
$this->assertEquals(1, $pageFlowInstance1->getAttributes()->get('counter'));

$this->flowID = 'SecondCounter';
$this->eventName = null;
$this->flowExecutionTicket = null;
$server->activate(new \stdClass());
$pageFlowInstance2 = $server->getPageFlowInstance();

$this->assertEquals(0, $pageFlowInstance2->getAttribute('counter'));
$this->assertEquals(0, $pageFlowInstance2->getAttributes()->get('counter'));
$this->assertFalse($pageFlowInstance1->getID() == $pageFlowInstance2->getID());
$this->assertThat($server->getPageFlowInstanceRepository()->findByPageFlowID('Counter'), $this->logicalNot($this->equalTo(null)));
$this->assertEquals($pageFlowInstance1->getID(), $server->getPageFlowInstanceRepository()->findByPageFlowID('Counter')->getID());
Expand Down Expand Up @@ -786,12 +786,12 @@ protected function createCounterActionInvoker()
{
$actionInvoker = \Phake::mock('Piece\Flow\PageFlow\ActionInvoker');
\Phake::when($actionInvoker)->invoke('setup', $this->anything())->thenGetReturnByLambda(function ($actionID, EventContext $eventContext) {
if (!$eventContext->getPageFlow()->hasAttribute('counter')) {
$eventContext->getPageFlow()->setAttribute('counter', 0);
if (!$eventContext->getPageFlow()->getAttributes()->has('counter')) {
$eventContext->getPageFlow()->getAttributes()->set('counter', 0);
}
});
\Phake::when($actionInvoker)->invoke('increase', $this->anything())->thenGetReturnByLambda(function ($actionID, EventContext $eventContext) {
$eventContext->getPageFlow()->setAttribute('counter', $eventContext->getPageFlow()->getAttribute('counter') + 1);
$eventContext->getPageFlow()->getAttributes()->set('counter', $eventContext->getPageFlow()->getAttributes()->get('counter') + 1);

return 'succeed';
});
Expand Down
28 changes: 14 additions & 14 deletions test/Piece/Flow/PageFlow/PageFlowTest.php
Expand Up @@ -172,10 +172,10 @@ public function testSettingAttribute()
$flow = $this->pageFlowFactory->create($this->source);
$flow->setActionInvoker(\Phake::mock('Piece\Flow\PageFlow\ActionInvoker'));
$flow->start();
$flow->setAttribute('foo', 'bar');
$flow->getAttributes()->set('foo', 'bar');

$this->assertTrue($flow->hasAttribute('foo'));
$this->assertEquals('bar', $flow->getAttribute('foo'));
$this->assertTrue($flow->getAttributes()->has('foo'));
$this->assertEquals('bar', $flow->getAttributes()->get('foo'));
}

public function testOptionalElements()
Expand Down Expand Up @@ -228,14 +228,14 @@ public function testToPreventTriggeringProtectedEvents()
$actionInvoker = \Phake::mock('Piece\Flow\PageFlow\ActionInvoker');
\Phake::when($actionInvoker)->invoke($this->anything(), $this->anything())
->thenGetReturnByLambda(function ($actionID, EventContext $eventContext) {
if ($eventContext->getPageFlow()->hasAttribute('numberOfUpdate')) {
$numberOfUpdate = $eventContext->getPageFlow()->getAttribute('numberOfUpdate');
if ($eventContext->getPageFlow()->getAttributes()->has('numberOfUpdate')) {
$numberOfUpdate = $eventContext->getPageFlow()->getAttributes()->get('numberOfUpdate');
} else {
$numberOfUpdate = 0;
}

++$numberOfUpdate;
$eventContext->getPageFlow()->setAttribute('numberOfUpdate', $numberOfUpdate);
$eventContext->getPageFlow()->getAttributes()->set('numberOfUpdate', $numberOfUpdate);
});

$flow = $this->pageFlowFactory->create('CDPlayer');
Expand All @@ -244,42 +244,42 @@ public function testToPreventTriggeringProtectedEvents()
$flow->start();

$this->assertEquals('Stop', $flow->getCurrentState()->getID());
$this->assertEquals(1, $flow->getAttribute('numberOfUpdate'));
$this->assertEquals(1, $flow->getAttributes()->get('numberOfUpdate'));

$flow->triggerEvent('foo');

$this->assertEquals('Stop', $flow->getCurrentState()->getID());
$this->assertEquals(2, $flow->getAttribute('numberOfUpdate'));
$this->assertEquals(2, $flow->getAttributes()->get('numberOfUpdate'));

$flow->triggerEvent(Event::EVENT_ENTRY);

$this->assertEquals('Stop', $flow->getCurrentState()->getID());
$this->assertEquals(3, $flow->getAttribute('numberOfUpdate'));
$this->assertEquals(3, $flow->getAttributes()->get('numberOfUpdate'));

$flow->triggerEvent(Event::EVENT_EXIT);

$this->assertEquals('Stop', $flow->getCurrentState()->getID());
$this->assertEquals(4, $flow->getAttribute('numberOfUpdate'));
$this->assertEquals(4, $flow->getAttributes()->get('numberOfUpdate'));

$flow->triggerEvent(Event::EVENT_START);

$this->assertEquals('Stop', $flow->getCurrentState()->getID());
$this->assertEquals(5, $flow->getAttribute('numberOfUpdate'));
$this->assertEquals(5, $flow->getAttributes()->get('numberOfUpdate'));

$flow->triggerEvent(Event::EVENT_END);

$this->assertEquals('Stop', $flow->getCurrentState()->getID());
$this->assertEquals(6, $flow->getAttribute('numberOfUpdate'));
$this->assertEquals(6, $flow->getAttributes()->get('numberOfUpdate'));

$flow->triggerEvent(Event::EVENT_DO);

$this->assertEquals('Stop', $flow->getCurrentState()->getID());
$this->assertEquals(7, $flow->getAttribute('numberOfUpdate'));
$this->assertEquals(7, $flow->getAttributes()->get('numberOfUpdate'));

$flow->triggerEvent('play');

$this->assertEquals('Playing', $flow->getCurrentState()->getID());
$this->assertEquals(7, $flow->getAttribute('numberOfUpdate'));
$this->assertEquals(7, $flow->getAttributes()->get('numberOfUpdate'));
}

/**
Expand Down

0 comments on commit e7ac4c7

Please sign in to comment.