This repository has been archived by the owner on Jun 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
308 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace CultuurNet\UDB3\UiTPASService\Broadway\Saga\State; | ||
|
||
use Broadway\Saga\State; | ||
use Broadway\UuidGenerator\UuidGeneratorInterface; | ||
|
||
class StateCopier implements StateCopierInterface | ||
{ | ||
/** | ||
* @var UuidGeneratorInterface | ||
*/ | ||
private $generator; | ||
|
||
/** | ||
* StateCopier constructor. | ||
* @param UuidGeneratorInterface $generator | ||
*/ | ||
public function __construct(UuidGeneratorInterface $generator) | ||
{ | ||
$this->generator = $generator; | ||
} | ||
|
||
/** | ||
* Create a copy with new id. | ||
* | ||
* @param State $state | ||
* @return State | ||
*/ | ||
public function copy(State $state) | ||
{ | ||
$stateAsArray = $state->serialize(); | ||
$stateAsArray['id'] = $this->generator->generate(); | ||
|
||
return State::deserialize($stateAsArray); | ||
} | ||
|
||
/** | ||
* Create a copy with new id and applied values. | ||
* | ||
* @param State $state | ||
* @param array $values | ||
* @return State | ||
*/ | ||
public function copyWithValues(State $state, array $values) | ||
{ | ||
$copiedState = $this->copy($state); | ||
|
||
foreach ($values as $key => $value) { | ||
$copiedState->set($key, $value); | ||
} | ||
|
||
return $copiedState; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace CultuurNet\UDB3\UiTPASService\Broadway\Saga\State; | ||
|
||
use Broadway\Saga\State; | ||
|
||
interface StateCopierInterface | ||
{ | ||
/** | ||
* Create a copy with new id. | ||
* | ||
* @param State $state | ||
* @return State | ||
*/ | ||
public function copy(State $state); | ||
|
||
/** | ||
* Create a copy with new id and applied values. | ||
* | ||
* @param State $state | ||
* @param array $values | ||
* @return State | ||
*/ | ||
public function copyWithValues(State $state, array $values); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
namespace CultuurNet\UDB3\UiTPASService\Broadway\Saga\State; | ||
|
||
use Broadway\Saga\State; | ||
use Broadway\UuidGenerator\UuidGeneratorInterface; | ||
|
||
class StateCopierTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $id; | ||
|
||
/** | ||
* @var State | ||
*/ | ||
private $state; | ||
|
||
/** | ||
* @var UuidGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $generator; | ||
|
||
/** | ||
* @var StateCopier | ||
*/ | ||
private $stateCopier; | ||
|
||
protected function setUp() | ||
{ | ||
$this->id = '6cd53be3-7512-4a64-98b9-b51b2ed6988d'; | ||
|
||
$this->state = $this->createStateWithValues( | ||
'e9d11f11-9579-48c3-b531-33deaebc5947' | ||
); | ||
|
||
$this->generator = $this->createMock(UuidGeneratorInterface::class); | ||
|
||
$this->generator->expects($this->once()) | ||
->method('generate') | ||
->willReturn($this->id); | ||
|
||
$this->stateCopier = new StateCopier($this->generator); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_can_copy_a_state() | ||
{ | ||
$this->state->setDone(); | ||
|
||
$copiedState = $this->stateCopier->copy($this->state); | ||
|
||
$expectedState = $this->createStateWithValues($this->id); | ||
$expectedState->setDone(); | ||
|
||
$this->assertEquals($expectedState, $copiedState); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_can_copy_a_state_with_values() | ||
{ | ||
$extraValues = [ | ||
'key3' => 'value3', | ||
'key4' => 'value4', | ||
]; | ||
|
||
$copiedState = $this->stateCopier->copyWithValues( | ||
$this->state, | ||
$extraValues | ||
); | ||
|
||
$expectedState = $this->createStateWithValues( | ||
$this->id, | ||
$extraValues | ||
); | ||
|
||
$this->assertEquals($expectedState, $copiedState); | ||
} | ||
|
||
/** | ||
* @param string $id | ||
* @param array $extraValues | ||
* @return State | ||
*/ | ||
private function createStateWithValues($id, array $extraValues = []) | ||
{ | ||
$state = new State($id); | ||
|
||
$state->set('key1', 'value1'); | ||
$state->set('key2', 'value2'); | ||
|
||
foreach ($extraValues as $key => $extraValue) { | ||
$state->set($key, $extraValue); | ||
} | ||
|
||
return $state; | ||
} | ||
} |
Oops, something went wrong.