Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 90696ad

Browse files
committed
Added state cloning if it is object
1 parent 314fc18 commit 90696ad

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

Memento/Memento.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44

55
class Memento
66
{
7-
/* @var string */
7+
/* @var mixed */
88
private $state;
99

1010
/**
11-
* @param string $stateToSave
11+
* @param mixed $stateToSave
1212
*/
1313
public function __construct($stateToSave)
1414
{
1515
$this->state = $stateToSave;
1616
}
1717

1818
/**
19-
* @return string
19+
* @return mixed
2020
*/
2121
public function getState()
2222
{

Memento/Originator.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
class Originator
66
{
7-
/* @var string */
7+
/* @var mixed */
88
private $state;
99

1010
// The class could also contain additional data that is not part of the
1111
// state saved in the memento..
1212

1313
/**
14-
* @param string $state
14+
* @param mixed $state
1515
*/
1616
public function setState($state)
1717
{
@@ -23,7 +23,9 @@ public function setState($state)
2323
*/
2424
public function saveToMemento()
2525
{
26-
return new Memento($this->state);
26+
$state = is_object($this->state) ? clone $this->state : $this->state;
27+
28+
return new Memento($state);
2729
}
2830

2931
public function restoreFromMemento(Memento $memento)

Tests/Memento/MementoTest.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
use DesignPatterns\Memento;
66

77
/**
8-
* MementoTest tests memento design pattern
8+
* MementoTest tests the memento pattern
99
*/
1010
class MementoTest extends \PHPUnit_Framework_TestCase
1111
{
1212

13-
public function testOriginator()
13+
public function testStringState()
1414
{
1515
$originator = new Memento\Originator();
1616
$originator->setState("State1");
@@ -33,4 +33,37 @@ public function testOriginator()
3333

3434
$this->assertAttributeEquals("State2", "state", $originator);
3535
}
36+
37+
public function testObjectState()
38+
{
39+
$originator = new Memento\Originator();
40+
41+
$foo = new \stdClass();
42+
$foo->data = "foo";
43+
44+
$originator->setState($foo);
45+
46+
$this->assertAttributeEquals($foo, "state", $originator);
47+
48+
$savedState = $originator->saveToMemento();
49+
50+
$this->assertAttributeEquals($foo, "state", $savedState);
51+
52+
$bar = new \stdClass();
53+
$bar->data = "bar";
54+
55+
$originator->setState($bar);
56+
57+
$this->assertAttributeEquals($bar, "state", $originator);
58+
59+
$originator->restoreFromMemento($savedState);
60+
61+
$this->assertAttributeEquals($foo, "state", $originator);
62+
63+
$foo->data = null;
64+
65+
$this->assertAttributeNotEquals($foo, "state", $savedState);
66+
67+
$this->assertAttributeNotEquals($foo, "state", $originator);
68+
}
3669
}

0 commit comments

Comments
 (0)