@@ -18,29 +18,45 @@ public function testUsageExample()
18
18
$ caretaker = new Caretaker ();
19
19
20
20
$ character = new \stdClass ();
21
- $ character ->name = "Gandalf " ; // new object
22
- $ originator ->setState ($ character ); // connect Originator to character object
23
-
24
- $ character ->name = "Gandalf the Grey " ; // work on the object
25
- $ character ->race = "Maia " ; // still change something
26
- $ snapshot = $ originator ->getStateAsMemento (); // time to save state
27
- $ caretaker ->saveToHistory ($ snapshot ); // put state to log
21
+ // new object
22
+ $ character ->name = "Gandalf " ;
23
+ // connect Originator to character object
24
+ $ originator ->setState ($ character );
25
+
26
+ // work on the object
27
+ $ character ->name = "Gandalf the Grey " ;
28
+ // still change something
29
+ $ character ->race = "Maia " ;
30
+ // time to save state
31
+ $ snapshot = $ originator ->getStateAsMemento ();
32
+ // put state to log
33
+ $ caretaker ->saveToHistory ($ snapshot );
28
34
29
- $ character ->name = "Sauron " ; // change something
30
- $ character ->race = "Ainur " ; // and again
31
- $ this ->assertAttributeEquals ($ character , "state " , $ originator ); // state inside the Originator was equally changed
35
+ // change something
36
+ $ character ->name = "Sauron " ;
37
+ // and again
38
+ $ character ->race = "Ainur " ;
39
+ // state inside the Originator was equally changed
40
+ $ this ->assertAttributeEquals ($ character , "state " , $ originator );
32
41
33
- $ snapshot = $ originator ->getStateAsMemento (); // time to save another state
34
- $ caretaker ->saveToHistory ($ snapshot ); // put state to log
42
+ // time to save another state
43
+ $ snapshot = $ originator ->getStateAsMemento ();
44
+ // put state to log
45
+ $ caretaker ->saveToHistory ($ snapshot );
35
46
36
47
$ rollback = $ caretaker ->getFromHistory (0 );
37
- $ originator ->restoreFromMemento ($ rollback ); // return to first state
38
- $ character = $ rollback ->getState (); // use character from old state
39
-
40
- $ this ->assertEquals ("Gandalf the Grey " , $ character ->name ); // yes, that what we need
41
- $ character ->name = "Gandalf the White " ; // make new changes
42
-
43
- $ this ->assertAttributeEquals ($ character , "state " , $ originator ); // and Originator linked to actual object again
48
+ // return to first state
49
+ $ originator ->restoreFromMemento ($ rollback );
50
+ // use character from old state
51
+ $ character = $ rollback ->getState ();
52
+
53
+ // yes, that what we need
54
+ $ this ->assertEquals ("Gandalf the Grey " , $ character ->name );
55
+ // make new changes
56
+ $ character ->name = "Gandalf the White " ;
57
+
58
+ // and Originator linked to actual object again
59
+ $ this ->assertAttributeEquals ($ character , "state " , $ originator );
44
60
}
45
61
46
62
public function testStringState ()
@@ -88,14 +104,18 @@ public function testCanChangeActualState()
88
104
$ snapshot = $ originator ->getStateAsMemento ();
89
105
$ second_state = $ snapshot ->getState ();
90
106
91
- $ first_state ->first_property = 1 ; // still actual
92
- $ second_state ->second_property = 2 ; // just history
107
+ // still actual
108
+ $ first_state ->first_property = 1 ;
109
+ // just history
110
+ $ second_state ->second_property = 2 ;
93
111
$ this ->assertAttributeEquals ($ first_state , "state " , $ originator );
94
112
$ this ->assertAttributeNotEquals ($ second_state , "state " , $ originator );
95
113
96
114
$ originator ->restoreFromMemento ($ snapshot );
97
- $ first_state ->first_property = 11 ; // now it lost state
98
- $ second_state ->second_property = 22 ; // must be actual
115
+ // now it lost state
116
+ $ first_state ->first_property = 11 ;
117
+ // must be actual
118
+ $ second_state ->second_property = 22 ;
99
119
$ this ->assertAttributeEquals ($ second_state , "state " , $ originator );
100
120
$ this ->assertAttributeNotEquals ($ first_state , "state " , $ originator );
101
121
}
0 commit comments