-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathDataManagerTest.php
More file actions
203 lines (166 loc) · 6.52 KB
/
Copy pathDataManagerTest.php
File metadata and controls
203 lines (166 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
namespace Craue\FormFlowBundle\Tests\Storage;
use Craue\FormFlowBundle\Form\FormFlow;
use Craue\FormFlowBundle\Storage\DataManager;
use Craue\FormFlowBundle\Storage\DataManagerInterface;
use Craue\FormFlowBundle\Storage\ExtendedDataManagerInterface;
use Craue\FormFlowBundle\Storage\SessionStorage;
use Craue\FormFlowBundle\Storage\StorageInterface;
use Craue\FormFlowBundle\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
/**
* @group unit
*
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2011-2015 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class DataManagerTest extends UnitTestCase {
/**
* @var StorageInterface
*/
protected $storage;
/**
* @var ExtendedDataManagerInterface
*/
protected $dataManager;
protected $createLocationFlowName = 'createLocation';
protected $createLocationFlowInstanceId = '26xz98wx38';
protected $createLocationFlowData = array(
// step 1
array(
'country' => 'US',
'_token' => '81jaZU6lQD_oCV-BmTZSxAN3rORRQgAVEu4iZbsACRE',
),
// step 2
array(
'region' => 'US-TX',
'_token' => 'ytCzDJAGrpo7ToVKqU89IOIQ0sDW2LDeve_5X0x6Sy0',
),
);
protected $createVehicleFlowName = 'createVehicle';
protected $createVehicleFlowInstanceId = '3a03y1o9at';
protected $createVehicleFlowData = array(
// step 1
array(
'vehicle' => array(
'numberOfWheels' => '2',
),
'_token' => 'sGcKUDmMmeaLedFQhoDt2ULi_39hErh4ZqZN1qZDCjc',
),
);
/**
* {@inheritDoc}
*/
protected function setUp() {
$this->storage = new SessionStorage(new Session(new MockArraySessionStorage()));
$this->dataManager = new DataManager($this->storage);
}
public function testGetStorage() {
$this->assertSame($this->storage, $this->dataManager->getStorage());
}
public function testSaveLoadDrop() {
// create two flows
$createLocationFlow = $this->getFlow($this->createLocationFlowName, $this->createLocationFlowInstanceId);
$createVehicleFlow = $this->getFlow($this->createVehicleFlowName, $this->createVehicleFlowInstanceId);
// save their data
$this->dataManager->save($createLocationFlow, $this->createLocationFlowData);
$this->dataManager->save($createVehicleFlow, $this->createVehicleFlowData);
// ensure their data has been saved correctly
$this->assertEquals($this->createLocationFlowData, $this->dataManager->load($createLocationFlow));
$this->assertEquals($this->createVehicleFlowData, $this->dataManager->load($createVehicleFlow));
// drop data for one of them
$this->dataManager->drop($createLocationFlow);
// ensure only data of the correct flow has been dropped
$this->assertEquals(array(), $this->dataManager->load($createLocationFlow));
$this->assertEquals($this->createVehicleFlowData, $this->dataManager->load($createVehicleFlow));
}
public function testSave_overwriteOldDate() {
// create a flow
$createLocationFlow = $this->getFlow($this->createLocationFlowName, $this->createLocationFlowInstanceId);
// save its data
$this->dataManager->save($createLocationFlow, $this->createLocationFlowData);
$newData = array('blah' => 'blah');
// save changed data
$this->dataManager->save($createLocationFlow, $newData);
// ensure its data has been overwritten correctly
$this->assertEquals($newData, $this->dataManager->load($createLocationFlow));
}
/**
* Ensure that even without any data an array is returned.
*/
public function testLoad_emptyStorage() {
$createLocationFlow = $this->getFlow($this->createLocationFlowName, $this->createLocationFlowInstanceId);
$this->assertEquals(array(), $this->dataManager->load($createLocationFlow));
}
public function testListFlows() {
// create three flows
$createLocationFlow1 = $this->getFlow($this->createLocationFlowName, $this->createLocationFlowInstanceId);
$createLocationFlow2 = $this->getFlow($this->createLocationFlowName, 'other-instance');
$createVehicleFlow = $this->getFlow($this->createVehicleFlowName, $this->createVehicleFlowInstanceId);
// save their data
$this->dataManager->save($createLocationFlow1, $this->createLocationFlowData);
$this->dataManager->save($createLocationFlow2, $this->createLocationFlowData);
$this->dataManager->save($createVehicleFlow, $this->createVehicleFlowData);
// get names of all flows
$expectedResult = array(
$this->createLocationFlowName,
$this->createVehicleFlowName,
);
$this->assertEquals($expectedResult, $this->dataManager->listFlows());
}
/**
* Ensure that even without any data an array is returned.
*/
public function testListFlows_emptyStorage() {
$this->assertEquals(array(), $this->dataManager->listFlows());
}
public function testListInstances() {
// create three flows
$createLocationFlow1 = $this->getFlow($this->createLocationFlowName, $this->createLocationFlowInstanceId);
$createLocationFlow2 = $this->getFlow($this->createLocationFlowName, 'other-instance');
$createVehicleFlow = $this->getFlow($this->createVehicleFlowName, $this->createVehicleFlowInstanceId);
// save their data
$this->dataManager->save($createLocationFlow1, $this->createLocationFlowData);
$this->dataManager->save($createLocationFlow2, $this->createLocationFlowData);
$this->dataManager->save($createVehicleFlow, $this->createVehicleFlowData);
// get instances of one flow
$expectedResult = array(
$this->createLocationFlowInstanceId,
'other-instance',
);
$this->assertEquals($expectedResult, $this->dataManager->listInstances($this->createLocationFlowName));
}
/**
* Ensure that even without any data an array is returned.
*/
public function testListInstances_emptyStorage() {
$this->assertEquals(array(), $this->dataManager->listInstances('whatever'));
}
public function testDropAll() {
// create a flow
$createLocationFlow = $this->getFlow($this->createLocationFlowName, $this->createLocationFlowInstanceId);
// save its data
$this->dataManager->save($createLocationFlow, $this->createLocationFlowData);
// drop all data
$this->dataManager->dropAll();
// ensure all data has been dropped
$this->assertFalse($this->storage->has(DataManagerInterface::STORAGE_ROOT));
}
/**
* @param string $name
* @param string $instanceId
* return PHPUnit_Framework_MockObject_MockObject|FormFlow
*/
protected function getFlow($name, $instanceId) {
$flow = $this->getFlowWithMockedMethods(array('getName'));
$flow
->expects($this->any())
->method('getName')
->will($this->returnValue($name))
;
$flow->setInstanceId($instanceId);
return $flow;
}
}